2024-04-18 21:46:14 发布
网友
我正在尝试用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
与样品最相关的部分是:
creation of the callback handler-收到事件时,将调用此函数,允许您对该事件执行操作:
def myEventCallback(event): print("%-33s%-30s%s" % (event.timestamp.isoformat(), event.device, event.event + ": " + json.dumps(event.data)))
客户端中的registration of the callback handler,它指示客户端为所有传入事件调用您的方法:
client.deviceEventCallback = myEventCallback
在subscription to events中,您可以限定订阅的范围以避免处理不必要的事件,或者使用默认值来订阅来自所有设备的所有事件:
eventsMid = client.subscribeToDeviceEvents(deviceType, deviceId, event)
请参阅Python客户端库:https://github.com/ibm-watson-iot/iot-python
这个特定的示例应该非常有用,您可以运行它而无需修改,并且可以看到响应事件和命令调用的函数:https://github.com/ibm-watson-iot/iot-python/tree/master/samples/simpleApp
与样品最相关的部分是:
creation of the callback handler-收到事件时,将调用此函数,允许您对该事件执行操作:
客户端中的registration of the callback handler,它指示客户端为所有传入事件调用您的方法:
在subscription to events中,您可以限定订阅的范围以避免处理不必要的事件,或者使用默认值来订阅来自所有设备的所有事件:
相关问题 更多 >
编程相关推荐