A Python wrapper of the Conductor API - conductor.py
The Conductor Python library is now on pypi, the Python package repo:
This library wraps the Conductor APIs with Python classes. It is useful for scripting interactions with Conductor. For Python users, using conductor.py is probably easier than using raw HTTP endpoints.
Installing the Conductor Python library
It is easy to install the Conductor library to Python 2.7.x using a package manager. For example, use pip to install the library from the command terminal by executing the following command:
pip install linklabs-conductor
The following sections describe implementation of conductor.py.
All four classes (accounts, app tokens, modules, and gateways) support querying. So you can do:
messages = gateway.get_recent_messages(mins_back=24*60)
to get all of the messages received in the last 24 hours. There is also the get_messages_time_range method to get messages for a more particular time range.
Retrieving Uplink Data
You start everything with a ConductorAccount object. From there you can get other types of objects (modules, gateways, app tokens) either by requesting an object in particular or requesting all objects of a certain type.
For example, this code snippet will store all of the gateways you own in a variable called gateways:
import conductor account = conductor.ConductorAccount('myusername', 'mypassword') gateways = account.get_gateways()
Alternatively, you could request a particular gateway:
gateway = account.get_gateway('$101$0-0-0-123456789')
The same can be done for modules and app tokens.
Getting uplink messages from a particular time range from a particular node
As an example, you could use the following code to pull a list of all uplink messages from a particular node in the time interval between start_time and stop_time:
import conductor account = conductor.ConductorAccount('myusername', 'mypassword') node = account.get_module('$301$0-0-0-123456789') messages = node.get_messages_time_range(start_time, stop_time)
where start_time and stop_time are Python 'datetime.datetime' objects.
Getting most recent uplink messages from a particular node
Similarly, you could get a list of all messages received from that node in the last 'mins_back' minutes:
import conductor account = conductor.ConductorAccount('myusername', 'mypassword') node = account.get_module('$301$0-0-0-123456789') messages = node.get_recent_messages(mins_back)
Getting uplink messages from a particular time range from an Application Token
Quite often it is appropriate to query the uplink messages from all nodes assigned to the same Application Token. For example, let's say your Conductor Account includes the application token 0123456789abcdef0123. You could get a list of all messages received during the interval start_time to stop_time from all nodes registered to that application token by doing:
import conductor account = conductor.ConductorAccount('myusername', 'mypassword') application = account.get_application_token('0123456789abcdef0123') messages = application.get_messages_time_range(start_time, stop_time)
Setting up an uplink subscription
conductor.py includes functionality to get uplink traffic using subscriptions (rather than Conductor's REST API). This can be useful if you want to stream uplink data in real time.
Uplink subscription for a particular node
The following example shows how to subscribe to the uplink coming from a particular node with the MAC address $301$0-0-0-012345678:
import conductor account = conductor.ConductorAccount('myusername', 'mypassword') subject = account.get_module('$301$0-0-0-012345678') for message in subject.subscribe_iter(): ## do whatever you want with message, for example write it to a log LOG.info(message)
The module class is special in that it supports downlink. Once you have a handle to a specific module, you can send downlink:
mod = account.get_module('$301$0-0-0-123456789') message = mod.send_message('hello')
After you send the message, you can poll Conductor for the message's status:
status = message.get_status()
This will tell you if the message has been sent successfully, if it's pending, or if it expired (i.e., was never able to be sent or ACK'd).