Getting started¶
The Zerynth Device Manager (ZDM) Library contains classes and methods allowing the devices to connect to the ZDM. The ZDM library can be used to send data from a device and to let it receive remote commands called jobs in the ZDM lingo.
To learn how to use it and call ZDM methods, see the ZDM getting started doc.
The Credentials class¶
-
class
Credentials
¶ Creates a Credentials instance that loads the provisioning information from the appropriate location. Provisioning information is saved in the
zdevice.json
file contained in the Zerynth project folder. Based on its content, the Credentials class is capable of loading or creating all the necessary pieces of information:device_id
the unique identifier of the device in the ZDMcredential_type
the type of credentials to use (i.e. cloud token, device token, certificates, etc...)device_secret
, secret key. If empty, the secure element is used as key storageendpoint
, the mqtt broker addressca
, the ZDM root certificate
The Config class¶
-
class
Config
(keepalive=60, cycle_timeout=500, command_timeout=60000, clean_session=True, qos_publish=0, qos_subscribe=1)¶ Creates a Config instance that set parameters for the connection to the MQTT broker. In particular, the following parameters are available:
keepalive
the MQTT keep alive timeout, in secondscycle_timeout
how long to block on the socket waiting for broker input, in millisecondscommand_timeout
, how long to wait for a connect/disconnect command to finish, in millisecondsclean_session
, use MQTT clean session flagqos_publish
, the quality of service when publishing to the MQTT brokerqos_subscribe
, the quality of service when subscribing to the MQTT broker
The Device class¶
-
class
Device
(cred=None, cfg=None, jobs_dict=None, condition_tags=[], on_timestamp=None, on_open_conditions=None, fota_callback=None, time_function=default_time_func)¶ Creates a Device instance with uid
device_id
. All other parameters are optional and have default values.jobs_dict
is the dictionary that defines the device’s available jobscondition_tags
is a list of strings defining the conditions tags used by the device (default []).on_timestamp
, is a callback function called when timestamp is received after requesting current time to the ZDM (default None).on_open_conditions
, is a callback function that is called when the list of open conditions is received (default None).fota_callback
, is a function accepting one ore more arguments that will be called at different steps of the FOTA process.time_function
, is a synchronous function that returns the time. Default is retrieving the time through a ntp server.
-
connect
()¶ Connect to the ZDM with the parameters and credentials specified in the constructor. Upon successful connection, subscribe to the required ZDM topics for jobs. Upon successful subscription, request current device status from the ZDM.
Return the result code of MQTT connection (0 for ok).
-
connected
()¶ Returns
True
if the device is connected to the ZDM,False
otherwise.
-
publish
(data, tag=None)¶ Publish a message to the ZDM.
data
, is the message payload, represented by a dictionary.tag
, is a label for the data.
-
new_condition
(condition_tag)¶
- Create and return a new condition.
condition_tag
, the tag of the new condition. For the condition to be created correctly, the condition tag must be presend in thecondition_tags
parameter of the class constructor.
-
request_timestamp
()¶
Request the current timestamp. When successfully received, the on_timestamp
callback is called
-
request_open_conditions
()¶ Request all the device conditions not yet closed. When the open conditions are received, the
on_open_conditions
callback is called
The Condition class¶
-
class
Condition
(device, uuid, tag)¶ Creates a Condition for device instance
device
with unique identifieruuid
and tagtag
.A Condition is an event described by
tag
(i.e. temperature_high) with an associated opening and closing time. Conditions are useful to represent alarms or in general events with a duration in time.
-
is_open
()¶
Return True if the condition is open. False otherwise. .. method:: open(payload={}, start=None)
Open a condition.
tag
, the label associated to the conditionstart
, the opening time of the condition (RFC3339 format) (if not given, uses current timestamp)
-
close
(payload={}, finish=None)¶
Close a condition
payload
, an optional dict associated with the close operation.finish
, the closing time of the condition (RFC3339 format) (if not given, uses current timestamp)
-
reset
()¶
Reset a condition in order to reuse it without creating a new one. The method generates a new id for the condition, and reset all the others fields except the tag. .. method:: get_payload_open()
Return the payload of the condition when it was opened.
-
get_payload_close
()¶
Return the payload of the condition when it was closed. .. method:: get_id()
Return the condition unique identifier .. method:: get_tag()
Return the condition tag .. method:: get_start()
Return the opening time .. method:: get_finish()
Return the closing time