summaryrefslogtreecommitdiff
path: root/slixmpp/plugins/xep_0323/sensordata.py
diff options
context:
space:
mode:
Diffstat (limited to 'slixmpp/plugins/xep_0323/sensordata.py')
-rw-r--r--slixmpp/plugins/xep_0323/sensordata.py128
1 files changed, 64 insertions, 64 deletions
diff --git a/slixmpp/plugins/xep_0323/sensordata.py b/slixmpp/plugins/xep_0323/sensordata.py
index afb694d0..41a3b58d 100644
--- a/slixmpp/plugins/xep_0323/sensordata.py
+++ b/slixmpp/plugins/xep_0323/sensordata.py
@@ -29,12 +29,12 @@ log = logging.getLogger(__name__)
class XEP_0323(BasePlugin):
"""
- XEP-0323: IoT Sensor Data
+ XEP-0323: IoT Sensor Data
This XEP provides the underlying architecture, basic operations and data
structures for sensor data communication over XMPP networks. It includes
- a hardware abstraction model, removing any technical detail implemented
+ a hardware abstraction model, removing any technical detail implemented
in underlying technologies.
Also see <http://xmpp.org/extensions/xep-0323.html>
@@ -55,10 +55,10 @@ class XEP_0323(BasePlugin):
Sensordata Event:Rejected -- Received a reject from sensor for a request
Sensordata Event:Cancelled -- Received a cancel confirm from sensor
Sensordata Event:Fields -- Received fields from sensor for a request
- This may be triggered multiple times since
+ This may be triggered multiple times since
the sensor can split up its response in
multiple messages.
- Sensordata Event:Failure -- Received a failure indication from sensor
+ Sensordata Event:Failure -- Received a failure indication from sensor
for a request. Typically a comm timeout.
Attributes:
@@ -69,7 +69,7 @@ class XEP_0323(BasePlugin):
relevant to a request's session. This dictionary is used
both by the client and sensor side. On client side, seqnr
is used as key, while on sensor side, a session_id is used
- as key. This ensures that the two will not collide, so
+ as key. This ensures that the two will not collide, so
one instance can be both client and sensor.
Sensor side
-----------
@@ -89,12 +89,12 @@ class XEP_0323(BasePlugin):
Sensor side
-----------
- register_node -- Register a sensor as available from this XMPP
+ register_node -- Register a sensor as available from this XMPP
instance.
Client side
-----------
- request_data -- Initiates a request for data from one or more
+ request_data -- Initiates a request for data from one or more
sensors. Non-blocking, a callback function will
be called when data is available.
@@ -102,7 +102,7 @@ class XEP_0323(BasePlugin):
name = 'xep_0323'
description = 'XEP-0323 Internet of Things - Sensor Data'
- dependencies = set(['xep_0030'])
+ dependencies = set(['xep_0030'])
stanza = stanza
@@ -198,9 +198,9 @@ class XEP_0323(BasePlugin):
def register_node(self, nodeId, device, commTimeout, sourceId=None, cacheType=None):
"""
Register a sensor/device as available for serving of data through this XMPP
- instance.
+ instance.
- The device object may by any custom implementation to support
+ The device object may by any custom implementation to support
specific devices, but it must implement the functions:
has_field
request_fields
@@ -212,11 +212,11 @@ class XEP_0323(BasePlugin):
commTimeout -- Time in seconds to wait between each callback from device during
a data readout. Float.
sourceId -- [optional] identifying the data source controlling the device
- cacheType -- [optional] narrowing down the search to a specific kind of node
+ cacheType -- [optional] narrowing down the search to a specific kind of node
"""
- self.nodes[nodeId] = {"device": device,
+ self.nodes[nodeId] = {"device": device,
"commTimeout": commTimeout,
- "sourceId": sourceId,
+ "sourceId": sourceId,
"cacheType": cacheType};
def _set_authenticated(self, auth=''):
@@ -228,9 +228,9 @@ class XEP_0323(BasePlugin):
"""
Event handler for reception of an Iq with req - this is a request.
- Verifies that
+ Verifies that
- all the requested nodes are available
- - at least one of the requested fields is available from at least
+ - at least one of the requested fields is available from at least
one of the nodes
If the request passes verification, an accept response is sent, and
@@ -331,12 +331,12 @@ class XEP_0323(BasePlugin):
iq['type'] = 'error';
iq['rejected']['seqnr'] = seqnr;
iq['rejected']['error'] = error_msg;
- iq.send(block=False);
+ iq.send(block=False);
def _threaded_node_request(self, session, process_fields, flags):
- """
+ """
Helper function to handle the device readouts in a separate thread.
-
+
Arguments:
session -- The request session id
process_fields -- The fields to request from the devices
@@ -344,7 +344,7 @@ class XEP_0323(BasePlugin):
Formatted as a dictionary like { "flag name": "flag value" ... }
"""
for node in self.sessions[session]["node_list"]:
- self.sessions[session]["nodeDone"][node] = False;
+ self.sessions[session]["nodeDone"][node] = False;
for node in self.sessions[session]["node_list"]:
timer = TimerReset(self.nodes[node]['commTimeout'], self._event_comm_timeout, args=(session, node));
@@ -354,11 +354,11 @@ class XEP_0323(BasePlugin):
self.nodes[node]['device'].request_fields(process_fields, flags=flags, session=session, callback=self._device_field_request_callback);
def _event_comm_timeout(self, session, nodeId):
- """
+ """
Triggered if any of the readout operations timeout.
Sends a failure message back to the client, stops communicating
with the failing device.
-
+
Arguments:
session -- The request session id
nodeId -- The id of the device which timed out
@@ -366,7 +366,7 @@ class XEP_0323(BasePlugin):
msg = self.xmpp.Message();
msg['from'] = self.sessions[session]['to'];
msg['to'] = self.sessions[session]['from'];
- msg['failure']['seqnr'] = self.sessions[session]['seqnr'];
+ msg['failure']['seqnr'] = self.sessions[session]['seqnr'];
msg['failure']['error']['text'] = "Timeout";
msg['failure']['error']['nodeId'] = nodeId;
msg['failure']['error']['timestamp'] = datetime.datetime.now().replace(microsecond=0).isoformat();
@@ -403,9 +403,9 @@ class XEP_0323(BasePlugin):
self._threaded_node_request(session, process_fields, req_flags);
def _all_nodes_done(self, session):
- """
+ """
Checks wheter all devices are done replying to the readout.
-
+
Arguments:
session -- The request session id
"""
@@ -415,22 +415,22 @@ class XEP_0323(BasePlugin):
return True;
def _device_field_request_callback(self, session, nodeId, result, timestamp_block, error_msg=None):
- """
+ """
Callback function called by the devices when they have any additional data.
- Composes a message with the data and sends it back to the client, and resets
+ Composes a message with the data and sends it back to the client, and resets
the timeout timer for the device.
-
+
Arguments:
session -- The request session id
nodeId -- The device id which initiated the callback
result -- The current result status of the readout. Valid values are:
"error" - Readout failed.
"fields" - Contains readout data.
- "done" - Indicates that the readout is complete. May contain
+ "done" - Indicates that the readout is complete. May contain
readout data.
- timestamp_block -- [optional] Only applies when result != "error"
+ timestamp_block -- [optional] Only applies when result != "error"
The readout data. Structured as a dictionary:
- {
+ {
timestamp: timestamp for this datablock,
fields: list of field dictionary (one per readout field).
readout field dictionary format:
@@ -442,7 +442,7 @@ class XEP_0323(BasePlugin):
dataType: The datatype of the field. Only applies to type enum.
flags: [optional] data classifier flags for the field, e.g. momentary
Formatted as a dictionary like { "flag name": "flag value" ... }
- }
+ }
}
error_msg -- [optional] Only applies when result == "error".
Error details when a request failed.
@@ -463,7 +463,7 @@ class XEP_0323(BasePlugin):
msg['failure']['error']['timestamp'] = datetime.datetime.now().replace(microsecond=0).isoformat();
# Drop communication with this device and check if we are done
- self.sessions[session]["nodeDone"][nodeId] = True;
+ self.sessions[session]["nodeDone"][nodeId] = True;
if (self._all_nodes_done(session)):
msg['failure']['done'] = 'true';
# The session is complete, delete it
@@ -481,11 +481,11 @@ class XEP_0323(BasePlugin):
ts = node.add_timestamp(timestamp_block["timestamp"]);
for f in timestamp_block["fields"]:
- data = ts.add_data( typename=f['type'],
- name=f['name'],
- value=f['value'],
- unit=f['unit'],
- dataType=f['dataType'],
+ data = ts.add_data( typename=f['type'],
+ name=f['name'],
+ value=f['value'],
+ unit=f['unit'],
+ dataType=f['dataType'],
flags=f['flags']);
if result == "done":
@@ -503,7 +503,7 @@ class XEP_0323(BasePlugin):
msg.send();
def _handle_event_cancel(self, iq):
- """ Received Iq with cancel - this is a cancel request.
+ """ Received Iq with cancel - this is a cancel request.
Delete the session and confirm. """
seqnr = iq['cancel']['seqnr'];
@@ -518,8 +518,8 @@ class XEP_0323(BasePlugin):
iq.reply();
iq['type'] = 'result';
iq['cancelled']['seqnr'] = seqnr;
- iq.send(block=False);
-
+ iq.send(block=False);
+
# Delete session
del self.sessions[s]
return
@@ -529,22 +529,22 @@ class XEP_0323(BasePlugin):
iq['type'] = 'error';
iq['rejected']['seqnr'] = seqnr;
iq['rejected']['error'] = "Cancel request received, no matching request is active.";
- iq.send(block=False);
+ iq.send(block=False);
# =================================================================
# Client side (data retriever) API
def request_data(self, from_jid, to_jid, callback, nodeIds=None, fields=None, flags=None):
- """
+ """
Called on the client side to initiade a data readout.
Composes a message with the request and sends it to the device(s).
Does not block, the callback will be called when data is available.
-
+
Arguments:
from_jid -- The jid of the requester
to_jid -- The jid of the device(s)
- callback -- The callback function to call when data is availble.
-
+ callback -- The callback function to call when data is availble.
+
The callback function must support the following arguments:
from_jid -- The jid of the responding device(s)
@@ -565,7 +565,7 @@ class XEP_0323(BasePlugin):
The timestamp of data in this callback. One callback will only
contain data from one timestamp.
fields -- [optional] Mandatory when result == "fields".
- List of field dictionaries representing the readout data.
+ List of field dictionaries representing the readout data.
Dictionary format:
{
typename: The field type (numeric, boolean, dateTime, timeSpan, string, enum)
@@ -575,11 +575,11 @@ class XEP_0323(BasePlugin):
dataType: The datatype of the field. Only applies to type enum.
flags: [optional] data classifier flags for the field, e.g. momentary.
Formatted as a dictionary like { "flag name": "flag value" ... }
- }
+ }
error_msg -- [optional] Mandatory when result == "rejected" or "failure".
- Details about why the request is rejected or failed.
- "rejected" means that the request is stopped, but note that the
+ Details about why the request is rejected or failed.
+ "rejected" means that the request is stopped, but note that the
request will continue even after a "failure". "failure" only means
that communication was stopped to that specific device, other
device(s) (if any) will continue their readout.
@@ -610,17 +610,17 @@ class XEP_0323(BasePlugin):
iq['req']._set_flags(flags);
self.sessions[seqnr] = {"from": iq['from'], "to": iq['to'], "seqnr": seqnr, "callback": callback};
- iq.send(block=False);
+ iq.send(block=False);
return seqnr;
def cancel_request(self, session):
- """
+ """
Called on the client side to cancel a request for data readout.
Composes a message with the cancellation and sends it to the device(s).
- Does not block, the callback will be called when cancellation is
+ Does not block, the callback will be called when cancellation is
confirmed.
-
+
Arguments:
session -- The session id of the request to cancel
"""
@@ -651,7 +651,7 @@ class XEP_0323(BasePlugin):
callback(from_jid=iq['from'], result=result);
def _handle_event_rejected(self, iq):
- """ Received Iq with rejected - this is a reject.
+ """ Received Iq with rejected - this is a reject.
Delete the session. """
seqnr = iq['rejected']['seqnr'];
callback = self.sessions[seqnr]["callback"];
@@ -660,9 +660,9 @@ class XEP_0323(BasePlugin):
del self.sessions[seqnr];
def _handle_event_cancelled(self, iq):
- """
- Received Iq with cancelled - this is a cancel confirm.
- Delete the session.
+ """
+ Received Iq with cancelled - this is a cancel confirm.
+ Delete the session.
"""
#print("Got cancelled")
seqnr = iq['cancelled']['seqnr'];
@@ -672,7 +672,7 @@ class XEP_0323(BasePlugin):
del self.sessions[seqnr];
def _handle_event_fields(self, msg):
- """
+ """
Received Msg with fields - this is a data reponse to a request.
If this is the last data block, issue a "done" callback.
"""
@@ -694,16 +694,16 @@ class XEP_0323(BasePlugin):
fields.append(field_block);
callback(from_jid=msg['from'], result="fields", nodeId=node['nodeId'], timestamp=ts['value'], fields=fields);
-
+
if msg['fields']['done'] == "true":
callback(from_jid=msg['from'], result="done");
# Session done
del self.sessions[seqnr];
def _handle_event_failure(self, msg):
- """
+ """
Received Msg with failure - our request failed
- Delete the session.
+ Delete the session.
"""
seqnr = msg['failure']['seqnr'];
callback = self.sessions[seqnr]["callback"];
@@ -713,11 +713,11 @@ class XEP_0323(BasePlugin):
del self.sessions[seqnr];
def _handle_event_started(self, msg):
- """
- Received Msg with started - our request was queued and is now started.
+ """
+ Received Msg with started - our request was queued and is now started.
"""
seqnr = msg['started']['seqnr'];
callback = self.sessions[seqnr]["callback"];
callback(from_jid=msg['from'], result="started");
-
+