# Gate

Enables opening and closing a message stream at runtime.

## How to place it

#### Python

```python
with dai.Pipeline() as pipeline:
    gate  = pipeline.create(dai.node.Gate)
```

#### C++

```cpp
dai::Pipeline pipeline;
auto gate = pipeline.create<dai::node::Gate>();
```

## Inputs and Outputs

## Usage

#### Python

```python
with dai.Pipeline(device) as pipeline:
    camera = pipeline.create(dai.node.Camera).build()
    cameraOut = camera.requestOutput((640, 400), fps=30)

    gate  = pipeline.create(dai.node.Gate)
    cameraOut.link(gate.input)
    cameraQueue = gate.output.createOutputQueue()
    gateControlQueue = gate.inputControl.createInputQueue()

    gateControl = dai.GateControl()

    pipeline.start()

    startTime = time.monotonic()
    gateOpen = True

    # Open gate every odd second and close every even second
    while pipeline.isRunning():
        currentTime = time.monotonic()
        if (startTime-currentTime) % 2 == 0: 
            if not gateOpen:
                gateControlQueue.send(dai.GateControl.openGate())
                openGate = True
        else: 
            if gateOpen:
                gateControlQueue.send(dai.GateControl.closeGate())
                gateOpen = False
```

#### C++

```cpp
while(pipeline.isRunning()) {
    auto frame = cameraQueue->tryGet<dai::ImgFrame>();
    if(frame != nullptr) {
        cv::imshow("camera", frame->getCvFrame());
    }

    auto currentTime = std::chrono::steady_clock::now();
    auto elapsedSeconds = std::chrono::duration_cast<std::chrono::seconds>(currentTime - startTime).count();
    auto oddSecond = (elapsedSeconds % 2) != 0;

    if(oddSecond) {
        if(!gateOpen) {
            gateControlQueue->send(dai::GateControl::openGate());
            gateOpen = true;
        }
    } else {
        if(gateOpen) {
            gateControlQueue->send(dai::GateControl::closeGate());
            gateOpen = false;
        }
    }

    int key = cv::waitKey(1);
    if(key == 'q') {
        pipeline.stop();
        break;
    }
}
```

## Examples of functionality

 * [Gate Example](https://docs.luxonis.com/software-v3/depthai/examples/gate/gate.md) - Open and close stream gate every n
   seconds.

## Reference

### dai::node::Gate

Kind: class

Gate Node .

This node acts as a valve for data pipelines. It controls the flow of messages from the 'input' to the 'output' based on the state
configured via 'inputControl'. It can be configured to stay open indefinitely, stay closed, or open for a specific number of
messages.

#### std::shared_ptr< GateControl > initialConfig

Kind: variable

#### Input input

Kind: variable

Main data input.

Accepts arbitrary Buffer messages (e.g., ImgFrame , NNData ). If the Gate is Open, messages received here are forwarded to
'output'. If the Gate is Closed, messages received here are discarded/dropped.; Default queue size: 1 Blocking: False

#### Output output

Kind: variable

Main data output.

Forwards messages that were allowed through the Gate . The data type matches the input message.

#### Input inputControl

Kind: variable

Control input.

Accepts ' GateControl ' messages to dynamically change the Gate 's state. Use this to Open/Close the gate or set it to pass a
specific number of frames at runtime.; Default queue size: 4

#### Gate(std::unique_ptr< Properties > props)

Kind: function

#### Gate()

Kind: function

#### void setRunOnHost(bool runOnHost)

Kind: function

Specify whether to run on host or device By default, the node will run on device.

#### bool runOnHost()

Kind: function

Check if the node is configured to run on the host.

return: true if running on host, false otherwise.

#### void run()

Kind: function

#### DeviceNodeCRTP()

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props)

Kind: function

#### DeviceNodeCRTP(std::unique_ptr< Properties > props, bool confMode)

Kind: function

#### DeviceNodeCRTP(const std::shared_ptr< Device > & device, std::unique_ptr< Properties > props, bool confMode)

Kind: function

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
