# 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() 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():
        frame = cameraQueue.tryGet()
        if frame is not None:
            cv2.imshow("camera", frame.getCvFrame())

        currentTime = time.monotonic()
        if int(currentTime - startTime) % 2 != 0:
            if not gateOpen:
                gateControlQueue.send(dai.GateControl.openGate())
                gateOpen = True
        else: 
            if gateOpen:
                gateControlQueue.send(dai.GateControl.closeGate())
                gateOpen = False

        if cv2.waitKey(1) == ord('q'):
            pipeline.stop()
            break
```

#### 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;
    }
}
```

To pass only a set number of messages, send `dai.GateControl.openGate(numMessages)`. The gate closes again after `numMessages`
messages. To start the pipeline with a closed gate, set `gate.initialConfig.open = False` before `pipeline.start()`.

## 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

#### constexpr static const char * NAME

Kind: variable

#### 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

#### inline DeviceNodeCRTP()

Kind: function

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

Kind: function

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

Kind: function

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

Kind: function

#### inline 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.
