# Gate

允许在运行时打开和关闭消息流。

## 如何放置

#### 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>();
```

## 输入和输出

## 用法

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

    # 每隔奇数秒打开门，偶数秒关闭门
    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;
    }
}
```

## 功能示例

 * [Gate Example](https://docs.luxonis.com/software-v3/depthai/examples/gate/gate.md) - 每n秒打开和关闭流门。

## 参考

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

### 需要帮助？

请前往 [OAKChina 官网](https://www.oakchina.cn/) 获取技术支持或解答您的任何疑问。
