python 中的 Watson-IoT 事件侦听器

Watson-IoT event listener in python

我正在尝试在 python 中创建一个侦听器,它会在事件发生时自动从 Watson-IoT 中的设备检索事件。当一个事件发生时我想调用一个特定的函数。

我已通读文档和 API 规范,但找不到任何内容。

有办法吗?

查看 Python 客户端库:https://github.com/ibm-watson-iot/iot-python

这个特定示例应该证明非常有用,您可以 运行 它无需修改,并看到一个函数被调用以响应事件和命令:https://github.com/ibm-watson-iot/iot-python/tree/master/samples/simpleApp

与示例最相关的部分是:

  1. creation of the callback handler - 当接收到一个事件时,将调用此函数允许您对该事件采取行动:

    def myEventCallback(event):
        print("%-33s%-30s%s" % (event.timestamp.isoformat(), event.device, event.event + ": " + json.dumps(event.data)))
    
  2. 客户端中的registration of the callback handler,指示客户端对所有传入事件调用你的方法:

    client.deviceEventCallback = myEventCallback
    
  3. subscription to events,您可以限定订阅范围以避免处理不必要的事件,或者使用默认值订阅来自所有设备的所有事件:

    eventsMid = client.subscribeToDeviceEvents(deviceType, deviceId, event)