pythonibm-cloudlisteneriotwatson-iot

Watson-IoT event listener in python


I'm trying to create a listener in python that automatically retrieve events from devices in Watson-IoT as they occur. When an event occurs I want to call a specific function.

I have read through the documentation and the API-specifications but couldn't find anything.

Is there a way to do this?


Solution

  • See the Python client library: https://github.com/ibm-watson-iot/iot-python

    This specific sample should prove very helpful, you can run it without modification and see a function being called in response to events & commands: https://github.com/ibm-watson-iot/iot-python/tree/master/samples/simpleApp

    The most relavent parts to the sample are:

    1. The creation of the callback handler - when an event is received, this function will be called allowing you to take action for that event:

      def myEventCallback(event):
          print("%-33s%-30s%s" % (event.timestamp.isoformat(), event.device, event.event + ": " + json.dumps(event.data)))
      
    2. The registration of the callback handler in the client, which directs the client to invoke your method for all incoming events:

      client.deviceEventCallback = myEventCallback
      
    3. The subscription to events, you can scope the subscription to avoid processing unnecessary events, or use the defaults to subscribe to all events from all devices:

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