Package: ibmiotc
IBM Internet of Things Cloud for Python=======================================Python module for interacting with the IBM Internet of Things Cloud withPython.Platform---------  `Python 2.7 <https://www.python.org/download/releases/2.7>`__Dependencies-------------  `paho-mqtt <http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.python.git/>`__-  `iso8601 <https://bitbucket.org/micktwomey/pyiso8601/>`__Installation------------This module is not yet available in PyPi, however you can download thelatest release and use pip's ability to install from a file.::[root@localhost ~]# pip install ibmiotc-version.zipUninstall---------Uninstalling the module is simple.::[root@localhost ~]# pip uninstall ibmiotcDocumentation-------------Application Client~~~~~~~~~~~~~~~~~~Constructor^^^^^^^^^^^The Client constructor accepts an options dict containing: \* org - Yourorganization ID \* id - The unique ID of your application within yourorganization \* authMethod - Method of authentication (the only value ofauthMethod currently supported is "apikey") \* authKey - API key(required if authMethod is "apikey") \* authToken - API key token(required if authMethod is "apikey").. code:: pythonimport ibmiotc.applicationtry:options = {"org": organization, "id": appId, "auth-method": authMethod, "auth-key": authKey, "auth-token": authToken}client = ibmiotc.application.Client(options)except ibmiotc.ConnectionException  as e:...Using a Configuration File''''''''''''''''''''''''''.. code:: pythonimport ibmiotc.applicationtry:options = ibmiotc.application.ParseConfigFile(configFilePath)client = ibmiotc.application.Client(options)except ibmiotc.ConnectionException  as e:...The application configuration file must be in the following format:::org=$orgIdid=$myApplicationauth-method=apikeyauth-key=$keyauth-token=$tokenSubscribing to Device events^^^^^^^^^^^^^^^^^^^^^^^^^^^^By default, this will subscribe to all events from all connecteddevices. Use the type, id and event parameters to control the scope ofthe subscription. A single client can support multiple subscriptions.Subscribe to all events from all devices''''''''''''''''''''''''''''''''''''''''.. code:: pythonclient.subscribeToDeviceEvents()Subscribe to all events from all devices of a specific type'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''.. code:: pythonclient.subscribeToDeviceEvents(deviceType=myDeviceType)Subscribe to a specific event from all devices''''''''''''''''''''''''''''''''''''''''''''''.. code:: pythonclient.subscribeToDeviceEvents(event=myEvent)Subscribe to a specific event from two different devices''''''''''''''''''''''''''''''''''''''''''''''''''''''''.. code:: pythonclient.subscribeToDeviceEvents(deviceType=myDeviceType, deviceId=myDeviceId, event=myEvent)lient.subscribeToDeviceEvents(deviceType=myOtherDeviceType, deviceId=myOtherDeviceId, event=myEvent)Handling events from Devices^^^^^^^^^^^^^^^^^^^^^^^^^^^^To process the events received by your subscroptions you need toregister an event callback method. The messages are returned as aninstance of the Event class: \* event.device - string (uniquelyidentifies the device across all types of devices in the organization:math:`deviceType:`\ deviceId) \* event.deviceType - string \*event.deviceId - string \* event.event - string \* event.format - string\* event.data - dict \* event.timestamp - datetime.. code:: pythondef myEventCallback(event):print "%s event '%s' received from device [%s]: %s" % (event.format, event.event, event.device, json.dumps(event.data))...client.eventCallback = myEventCallbackclient.subscribeToDeviceEvents()Subscribing to Device status^^^^^^^^^^^^^^^^^^^^^^^^^^^^By default, this will subscribe to status updates for all connecteddevices. Use the type and id parameters to control the scope of thesubscription. A single client can support multiple subscriptions.Subscribe to status updates for all devices'''''''''''''''''''''''''''''''''''''''''''.. code:: pythonclient.subscribeToDeviceStatus()Subscribe to status updates for all devices of a specific type''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''.. code:: pythonclient.subscribeToDeviceStatus(deviceType=myDeviceType)Subscribe to status updates for two different devices'''''''''''''''''''''''''''''''''''''''''''''''''''''.. code:: pythonclient.subscribeToDeviceStatus(deviceType=myDeviceType, deviceId=myDeviceId)lient.subscribeToDeviceStatus(deviceType=myOtherDeviceType, deviceId=myOtherDeviceId)Handling status updates from Devices^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^To process the status updates received by your subscriptions you need toregister an event callback method. The messages are returned as aninstance of the Status class:| The following properties are set for both "Connect" and "Disconnect"status events: \* status.clientAddr - string|  \* status.protocol - string|  \* status.clientId - string|  \* status.user - string|  \* status.time - datetime|  \* status.action - string|  \* status.connectTime - datetime|  \* status.port - int| The following properties are only set when the action is "Disconnect":\* status.writeMsg - int \* status.readMsg - int \* status.reason -string|  \* status.readBytes - int|  \* status.writeBytes - int.. code:: pythondef myStatusCallback(status):if status.action == "Disconnect":print "%s - device %s - %s (%s)" % (status.time.isoformat(), status.device, status.action, status.reason)else:print "%s - %s - %s" % (status.time.isoformat(), status.device, status.action)...client.statusCallback = myStatusCallbackclient.subscribeToDeviceStstus()Publishing Events "from" Devices^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Applications can publish events as if they originated from a Device.. code:: pythonmyData={'name' : 'foo', 'cpu' : 60, 'mem' : 50}client.publishEvent(myDeviceType, myDeviceId, "status", myData)Publishing Commands to Devices^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Applications can publish commands to connected Devices.. code:: pythoncommandData={'rebootDelay' : 50}client.publishCommand(myDeviceType, myDeviceId, "reboot", myData)