2013-05-17 10:18:00 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Implementation of xeps for Internet of Things
|
|
|
|
http://wiki.xmpp.org/web/Tech_pages/IoT_systems
|
2013-08-30 00:29:52 +00:00
|
|
|
Copyright (C) 2013 Sustainable Innovation, Joachim.lindborg@sust.se, bjorn.westrom@consoden.se
|
2013-05-17 10:18:00 +00:00
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
See the file LICENSE for copying permission.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
2013-08-30 00:29:52 +00:00
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
from threading import Thread, Lock, Timer
|
|
|
|
|
|
|
|
from sleekxmpp.plugins.xep_0323.timerreset import TimerReset
|
2013-05-17 10:18:00 +00:00
|
|
|
|
|
|
|
from sleekxmpp.xmlstream import JID
|
|
|
|
from sleekxmpp.xmlstream.handler import Callback
|
|
|
|
from sleekxmpp.xmlstream.matcher import StanzaPath
|
|
|
|
from sleekxmpp.plugins.base import BasePlugin
|
|
|
|
from sleekxmpp.plugins.xep_0323 import stanza
|
2013-08-30 00:29:52 +00:00
|
|
|
from sleekxmpp.plugins.xep_0323.stanza import Sensordata
|
2013-05-17 10:18:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class XEP_0323(BasePlugin):
|
|
|
|
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
XEP-0323: IoT Sensor Data
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
This XEP provides the underlying architecture, basic operations and data
|
|
|
|
structures for sensor data communication over XMPP networks. It includes
|
2014-08-17 22:52:24 +00:00
|
|
|
a hardware abstraction model, removing any technical detail implemented
|
2013-08-30 00:29:52 +00:00
|
|
|
in underlying technologies.
|
|
|
|
|
|
|
|
Also see <http://xmpp.org/extensions/xep-0323.html>
|
|
|
|
|
|
|
|
Configuration Values:
|
|
|
|
threaded -- Indicates if communication with sensors should be threaded.
|
|
|
|
Defaults to True.
|
|
|
|
|
|
|
|
Events:
|
|
|
|
Sensor side
|
|
|
|
-----------
|
|
|
|
Sensordata Event:Req -- Received a request for data
|
|
|
|
Sensordata Event:Cancel -- Received a cancellation for a request
|
|
|
|
|
|
|
|
Client side
|
|
|
|
-----------
|
|
|
|
Sensordata Event:Accepted -- Received a accept from sensor for a request
|
|
|
|
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
|
2014-08-17 22:52:24 +00:00
|
|
|
This may be triggered multiple times since
|
2013-08-30 00:29:52 +00:00
|
|
|
the sensor can split up its response in
|
|
|
|
multiple messages.
|
2014-08-17 22:52:24 +00:00
|
|
|
Sensordata Event:Failure -- Received a failure indication from sensor
|
2013-08-30 00:29:52 +00:00
|
|
|
for a request. Typically a comm timeout.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
threaded -- Indicates if command events should be threaded.
|
|
|
|
Defaults to True.
|
|
|
|
sessions -- A dictionary or equivalent backend mapping
|
|
|
|
session IDs to dictionaries containing data
|
|
|
|
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
|
2014-08-17 22:52:24 +00:00
|
|
|
as key. This ensures that the two will not collide, so
|
2013-08-30 00:29:52 +00:00
|
|
|
one instance can be both client and sensor.
|
|
|
|
Sensor side
|
|
|
|
-----------
|
|
|
|
nodes -- A dictionary mapping sensor nodes that are serviced through
|
|
|
|
this XMPP instance to their device handlers ("drivers").
|
|
|
|
Client side
|
|
|
|
-----------
|
|
|
|
last_seqnr -- The last used sequence number (integer). One sequence of
|
|
|
|
communication (e.g. -->request, <--accept, <--fields)
|
|
|
|
between client and sensor is identified by a unique
|
|
|
|
sequence number (unique between the client/sensor pair)
|
|
|
|
|
|
|
|
Methods:
|
|
|
|
plugin_init -- Overrides base_plugin.plugin_init
|
|
|
|
post_init -- Overrides base_plugin.post_init
|
|
|
|
plugin_end -- Overrides base_plugin.plugin_end
|
|
|
|
|
|
|
|
Sensor side
|
|
|
|
-----------
|
2014-08-17 22:52:24 +00:00
|
|
|
register_node -- Register a sensor as available from this XMPP
|
2013-08-30 00:29:52 +00:00
|
|
|
instance.
|
|
|
|
|
|
|
|
Client side
|
|
|
|
-----------
|
2014-08-17 22:52:24 +00:00
|
|
|
request_data -- Initiates a request for data from one or more
|
2013-08-30 00:29:52 +00:00
|
|
|
sensors. Non-blocking, a callback function will
|
|
|
|
be called when data is available.
|
|
|
|
|
2013-05-17 10:18:00 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
name = 'xep_0323'
|
|
|
|
description = 'XEP-0323 Internet of Things - Sensor Data'
|
2014-08-17 22:52:24 +00:00
|
|
|
dependencies = set(['xep_0030'])
|
2013-05-17 10:18:00 +00:00
|
|
|
stanza = stanza
|
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
default_config = {
|
|
|
|
'threaded': True
|
|
|
|
# 'session_db': None
|
|
|
|
}
|
|
|
|
|
2013-05-17 10:18:00 +00:00
|
|
|
def plugin_init(self):
|
2013-08-30 00:29:52 +00:00
|
|
|
""" Start the XEP-0323 plugin """
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Req',
|
|
|
|
StanzaPath('iq@type=get/req'),
|
|
|
|
self._handle_event_req))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Accepted',
|
|
|
|
StanzaPath('iq@type=result/accepted'),
|
|
|
|
self._handle_event_accepted))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Rejected',
|
|
|
|
StanzaPath('iq@type=error/rejected'),
|
|
|
|
self._handle_event_rejected))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Cancel',
|
|
|
|
StanzaPath('iq@type=get/cancel'),
|
|
|
|
self._handle_event_cancel))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Cancelled',
|
|
|
|
StanzaPath('iq@type=result/cancelled'),
|
|
|
|
self._handle_event_cancelled))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Fields',
|
|
|
|
StanzaPath('message/fields'),
|
|
|
|
self._handle_event_fields))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Failure',
|
|
|
|
StanzaPath('message/failure'),
|
|
|
|
self._handle_event_failure))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Sensordata Event:Started',
|
|
|
|
StanzaPath('message/started'),
|
|
|
|
self._handle_event_started))
|
|
|
|
|
|
|
|
# Server side dicts
|
2014-08-17 22:52:24 +00:00
|
|
|
self.nodes = {}
|
|
|
|
self.sessions = {}
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
self.last_seqnr = 0
|
|
|
|
self.seqnr_lock = Lock()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
## For testning only
|
|
|
|
self.test_authenticated_from = ""
|
|
|
|
|
|
|
|
def post_init(self):
|
|
|
|
""" Init complete. Register our features in Serivce discovery. """
|
|
|
|
BasePlugin.post_init(self)
|
|
|
|
self.xmpp['xep_0030'].add_feature(Sensordata.namespace)
|
|
|
|
self.xmpp['xep_0030'].set_items(node=Sensordata.namespace, items=tuple())
|
2013-05-17 10:18:00 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
def _new_session(self):
|
|
|
|
""" Return a new session ID. """
|
|
|
|
return str(time.time()) + '-' + self.xmpp.new_id()
|
2013-05-17 10:18:00 +00:00
|
|
|
|
2013-09-04 12:57:27 +00:00
|
|
|
def session_bind(self, jid):
|
|
|
|
logging.debug("setting the Disco discovery for %s" % Sensordata.namespace)
|
|
|
|
self.xmpp['xep_0030'].add_feature(Sensordata.namespace)
|
|
|
|
self.xmpp['xep_0030'].set_items(node=Sensordata.namespace, items=tuple())
|
|
|
|
|
|
|
|
|
2013-05-17 10:18:00 +00:00
|
|
|
def plugin_end(self):
|
2013-08-30 00:29:52 +00:00
|
|
|
""" Stop the XEP-0323 plugin """
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions.clear()
|
2013-08-30 00:29:52 +00:00
|
|
|
self.xmpp.remove_handler('Sensordata Event:Req')
|
|
|
|
self.xmpp.remove_handler('Sensordata Event:Accepted')
|
|
|
|
self.xmpp.remove_handler('Sensordata Event:Rejected')
|
|
|
|
self.xmpp.remove_handler('Sensordata Event:Cancel')
|
|
|
|
self.xmpp.remove_handler('Sensordata Event:Cancelled')
|
|
|
|
self.xmpp.remove_handler('Sensordata Event:Fields')
|
|
|
|
self.xmpp['xep_0030'].del_feature(feature=Sensordata.namespace)
|
|
|
|
|
|
|
|
|
|
|
|
# =================================================================
|
|
|
|
# Sensor side (data provider) API
|
|
|
|
|
|
|
|
def register_node(self, nodeId, device, commTimeout, sourceId=None, cacheType=None):
|
|
|
|
"""
|
|
|
|
Register a sensor/device as available for serving of data through this XMPP
|
2014-08-17 22:52:24 +00:00
|
|
|
instance.
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
The device object may by any custom implementation to support
|
2013-08-30 00:29:52 +00:00
|
|
|
specific devices, but it must implement the functions:
|
|
|
|
has_field
|
|
|
|
request_fields
|
|
|
|
according to the interfaces shown in the example device.py file.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
nodeId -- The identifier for the device
|
|
|
|
device -- The device object
|
|
|
|
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
|
2014-08-17 22:52:24 +00:00
|
|
|
cacheType -- [optional] narrowing down the search to a specific kind of node
|
2013-08-30 00:29:52 +00:00
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
self.nodes[nodeId] = {"device": device,
|
2013-08-30 00:29:52 +00:00
|
|
|
"commTimeout": commTimeout,
|
2014-08-17 22:52:24 +00:00
|
|
|
"sourceId": sourceId,
|
|
|
|
"cacheType": cacheType}
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _set_authenticated(self, auth=''):
|
|
|
|
""" Internal testing function """
|
2014-08-17 22:52:24 +00:00
|
|
|
self.test_authenticated_from = auth
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _handle_event_req(self, iq):
|
|
|
|
"""
|
|
|
|
Event handler for reception of an Iq with req - this is a request.
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
Verifies that
|
2013-08-30 00:29:52 +00:00
|
|
|
- all the requested nodes are available
|
2014-08-17 22:52:24 +00:00
|
|
|
- at least one of the requested fields is available from at least
|
2013-08-30 00:29:52 +00:00
|
|
|
one of the nodes
|
|
|
|
|
|
|
|
If the request passes verification, an accept response is sent, and
|
|
|
|
the readout process is started in a separate thread.
|
|
|
|
If the verification fails, a reject message is sent.
|
|
|
|
"""
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = iq['req']['seqnr']
|
|
|
|
error_msg = ''
|
|
|
|
req_ok = True
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# Authentication
|
|
|
|
if len(self.test_authenticated_from) > 0 and not iq['from'] == self.test_authenticated_from:
|
|
|
|
# Invalid authentication
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
|
|
|
error_msg = "Access denied"
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# Nodes
|
2014-08-17 22:52:24 +00:00
|
|
|
process_nodes = []
|
2013-08-30 00:29:52 +00:00
|
|
|
if len(iq['req']['nodes']) > 0:
|
|
|
|
for n in iq['req']['nodes']:
|
|
|
|
if not n['nodeId'] in self.nodes:
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
|
|
|
error_msg = "Invalid nodeId " + n['nodeId']
|
|
|
|
process_nodes = [n['nodeId'] for n in iq['req']['nodes']]
|
2013-08-30 00:29:52 +00:00
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
process_nodes = self.nodes.keys()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# Fields - if we just find one we are happy, otherwise we reject
|
2014-08-17 22:52:24 +00:00
|
|
|
process_fields = []
|
2013-08-30 00:29:52 +00:00
|
|
|
if len(iq['req']['fields']) > 0:
|
|
|
|
found = False
|
|
|
|
for f in iq['req']['fields']:
|
|
|
|
for node in self.nodes:
|
|
|
|
if self.nodes[node]["device"].has_field(f['name']):
|
2014-08-17 22:52:24 +00:00
|
|
|
found = True
|
|
|
|
break
|
2013-08-30 00:29:52 +00:00
|
|
|
if not found:
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
|
|
|
error_msg = "Invalid field " + f['name']
|
|
|
|
process_fields = [f['name'] for n in iq['req']['fields']]
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
req_flags = iq['req']._get_flags()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
request_delay_sec = None
|
|
|
|
if 'when' in req_flags:
|
|
|
|
# Timed request - requires datetime string in iso format
|
|
|
|
# ex. 2013-04-05T15:00:03
|
|
|
|
dt = None
|
|
|
|
try:
|
|
|
|
dt = datetime.datetime.strptime(req_flags['when'], "%Y-%m-%dT%H:%M:%S")
|
|
|
|
except ValueError:
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
2013-08-30 00:29:52 +00:00
|
|
|
error_msg = "Invalid datetime in 'when' flag, please use ISO format (i.e. 2013-04-05T15:00:03)."
|
|
|
|
|
|
|
|
if not dt is None:
|
|
|
|
# Datetime properly formatted
|
|
|
|
dtnow = datetime.datetime.now()
|
|
|
|
dtdiff = dt - dtnow
|
|
|
|
request_delay_sec = dtdiff.seconds + dtdiff.days * 24 * 3600
|
|
|
|
if request_delay_sec <= 0:
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
|
|
|
error_msg = "Invalid datetime in 'when' flag, cannot set a time in the past. Current time: " + dtnow.isoformat()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
if req_ok:
|
2014-08-17 22:52:24 +00:00
|
|
|
session = self._new_session()
|
|
|
|
self.sessions[session] = {"from": iq['from'], "to": iq['to'], "seqnr": seqnr}
|
|
|
|
self.sessions[session]["commTimers"] = {}
|
|
|
|
self.sessions[session]["nodeDone"] = {}
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
#print("added session: " + str(self.sessions))
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
iq.reply()
|
|
|
|
iq['accepted']['seqnr'] = seqnr
|
2013-08-30 00:29:52 +00:00
|
|
|
if not request_delay_sec is None:
|
|
|
|
iq['accepted']['queued'] = "true"
|
2014-08-17 22:52:24 +00:00
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["node_list"] = process_nodes
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
if not request_delay_sec is None:
|
|
|
|
# Delay request to requested time
|
|
|
|
timer = Timer(request_delay_sec, self._event_delayed_req, args=(session, process_fields, req_flags))
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["commTimers"]["delaytimer"] = timer
|
|
|
|
timer.start()
|
2013-08-30 00:29:52 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
if self.threaded:
|
|
|
|
#print("starting thread")
|
|
|
|
tr_req = Thread(target=self._threaded_node_request, args=(session, process_fields, req_flags))
|
|
|
|
tr_req.start()
|
|
|
|
#print("started thread")
|
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
self._threaded_node_request(session, process_fields, req_flags)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq.reply()
|
|
|
|
iq['type'] = 'error'
|
|
|
|
iq['rejected']['seqnr'] = seqnr
|
|
|
|
iq['rejected']['error'] = error_msg
|
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _threaded_node_request(self, session, process_fields, flags):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
Helper function to handle the device readouts in a separate thread.
|
2014-08-17 22:52:24 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
Arguments:
|
|
|
|
session -- The request session id
|
|
|
|
process_fields -- The fields to request from the devices
|
|
|
|
flags -- [optional] flags to pass to the devices, e.g. momentary
|
|
|
|
Formatted as a dictionary like { "flag name": "flag value" ... }
|
|
|
|
"""
|
|
|
|
for node in self.sessions[session]["node_list"]:
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["nodeDone"][node] = False
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
for node in self.sessions[session]["node_list"]:
|
2014-08-17 22:52:24 +00:00
|
|
|
timer = TimerReset(self.nodes[node]['commTimeout'], self._event_comm_timeout, args=(session, node))
|
|
|
|
self.sessions[session]["commTimers"][node] = timer
|
2013-08-30 00:29:52 +00:00
|
|
|
#print("Starting timer " + str(timer) + ", timeout: " + str(self.nodes[node]['commTimeout']))
|
2014-08-17 22:52:24 +00:00
|
|
|
timer.start()
|
|
|
|
self.nodes[node]['device'].request_fields(process_fields, flags=flags, session=session, callback=self._device_field_request_callback)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _event_comm_timeout(self, session, nodeId):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
Triggered if any of the readout operations timeout.
|
|
|
|
Sends a failure message back to the client, stops communicating
|
|
|
|
with the failing device.
|
2014-08-17 22:52:24 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
Arguments:
|
|
|
|
session -- The request session id
|
|
|
|
nodeId -- The id of the device which timed out
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
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']['error']['text'] = "Timeout"
|
|
|
|
msg['failure']['error']['nodeId'] = nodeId
|
|
|
|
msg['failure']['error']['timestamp'] = datetime.datetime.now().replace(microsecond=0).isoformat()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# Drop communication with this device and check if we are done
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["nodeDone"][nodeId] = True
|
2013-08-30 00:29:52 +00:00
|
|
|
if (self._all_nodes_done(session)):
|
2014-08-17 22:52:24 +00:00
|
|
|
msg['failure']['done'] = 'true'
|
|
|
|
msg.send()
|
2013-08-30 00:29:52 +00:00
|
|
|
# The session is complete, delete it
|
|
|
|
#print("del session " + session + " due to timeout")
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[session]
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _event_delayed_req(self, session, process_fields, req_flags):
|
|
|
|
"""
|
|
|
|
Triggered when the timer from a delayed request fires.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
session -- The request session id
|
|
|
|
process_fields -- The fields to request from the devices
|
|
|
|
flags -- [optional] flags to pass to the devices, e.g. momentary
|
|
|
|
Formatted as a dictionary like { "flag name": "flag value" ... }
|
2013-05-17 10:18:00 +00:00
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
msg = self.xmpp.Message()
|
|
|
|
msg['from'] = self.sessions[session]['to']
|
|
|
|
msg['to'] = self.sessions[session]['from']
|
|
|
|
msg['started']['seqnr'] = self.sessions[session]['seqnr']
|
|
|
|
msg.send()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
if self.threaded:
|
|
|
|
tr_req = Thread(target=self._threaded_node_request, args=(session, process_fields, req_flags))
|
|
|
|
tr_req.start()
|
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
self._threaded_node_request(session, process_fields, req_flags)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _all_nodes_done(self, session):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
Checks wheter all devices are done replying to the readout.
|
2014-08-17 22:52:24 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
Arguments:
|
|
|
|
session -- The request session id
|
|
|
|
"""
|
|
|
|
for n in self.sessions[session]["nodeDone"]:
|
|
|
|
if not self.sessions[session]["nodeDone"][n]:
|
2014-08-17 22:52:24 +00:00
|
|
|
return False
|
|
|
|
return True
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _device_field_request_callback(self, session, nodeId, result, timestamp_block, error_msg=None):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
Callback function called by the devices when they have any additional data.
|
2014-08-17 22:52:24 +00:00
|
|
|
Composes a message with the data and sends it back to the client, and resets
|
2013-08-30 00:29:52 +00:00
|
|
|
the timeout timer for the device.
|
2014-08-17 22:52:24 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
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.
|
2014-08-17 22:52:24 +00:00
|
|
|
"done" - Indicates that the readout is complete. May contain
|
2013-08-30 00:29:52 +00:00
|
|
|
readout data.
|
2014-08-17 22:52:24 +00:00
|
|
|
timestamp_block -- [optional] Only applies when result != "error"
|
2013-08-30 00:29:52 +00:00
|
|
|
The readout data. Structured as a dictionary:
|
2014-08-17 22:52:24 +00:00
|
|
|
{
|
2013-08-30 00:29:52 +00:00
|
|
|
timestamp: timestamp for this datablock,
|
|
|
|
fields: list of field dictionary (one per readout field).
|
|
|
|
readout field dictionary format:
|
|
|
|
{
|
|
|
|
type: The field type (numeric, boolean, dateTime, timeSpan, string, enum)
|
|
|
|
name: The field name
|
|
|
|
value: The field value
|
|
|
|
unit: The unit of the field. Only applies to type numeric.
|
|
|
|
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" ... }
|
2014-08-17 22:52:24 +00:00
|
|
|
}
|
2013-08-30 00:29:52 +00:00
|
|
|
}
|
|
|
|
error_msg -- [optional] Only applies when result == "error".
|
|
|
|
Error details when a request failed.
|
|
|
|
"""
|
|
|
|
if not session in self.sessions:
|
|
|
|
# This can happend if a session was deleted, like in a cancellation. Just drop the data.
|
|
|
|
return
|
|
|
|
|
|
|
|
if result == "error":
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["commTimers"][nodeId].cancel()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
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']['error']['text'] = error_msg
|
|
|
|
msg['failure']['error']['nodeId'] = nodeId
|
|
|
|
msg['failure']['error']['timestamp'] = datetime.datetime.now().replace(microsecond=0).isoformat()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# Drop communication with this device and check if we are done
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["nodeDone"][nodeId] = True
|
2013-08-30 00:29:52 +00:00
|
|
|
if (self._all_nodes_done(session)):
|
2014-08-17 22:52:24 +00:00
|
|
|
msg['failure']['done'] = 'true'
|
2013-08-30 00:29:52 +00:00
|
|
|
# The session is complete, delete it
|
|
|
|
# print("del session " + session + " due to error")
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[session]
|
|
|
|
msg.send()
|
2013-08-30 00:29:52 +00:00
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
msg = self.xmpp.Message()
|
|
|
|
msg['from'] = self.sessions[session]['to']
|
|
|
|
msg['to'] = self.sessions[session]['from']
|
|
|
|
msg['fields']['seqnr'] = self.sessions[session]['seqnr']
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
if timestamp_block is not None and len(timestamp_block) > 0:
|
2014-08-17 22:52:24 +00:00
|
|
|
node = msg['fields'].add_node(nodeId)
|
|
|
|
ts = node.add_timestamp(timestamp_block["timestamp"])
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
for f in timestamp_block["fields"]:
|
2014-08-17 22:52:24 +00:00
|
|
|
data = ts.add_data( typename=f['type'],
|
|
|
|
name=f['name'],
|
|
|
|
value=f['value'],
|
|
|
|
unit=f['unit'],
|
|
|
|
dataType=f['dataType'],
|
|
|
|
flags=f['flags'])
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
if result == "done":
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["commTimers"][nodeId].cancel()
|
|
|
|
self.sessions[session]["nodeDone"][nodeId] = True
|
|
|
|
msg['fields']['done'] = 'true'
|
2013-08-30 00:29:52 +00:00
|
|
|
if (self._all_nodes_done(session)):
|
|
|
|
# The session is complete, delete it
|
|
|
|
# print("del session " + session + " due to complete")
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[session]
|
2013-08-30 00:29:52 +00:00
|
|
|
else:
|
|
|
|
# Restart comm timer
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["commTimers"][nodeId].reset()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
msg.send()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_event_cancel(self, iq):
|
2014-08-17 22:52:24 +00:00
|
|
|
""" Received Iq with cancel - this is a cancel request.
|
2013-08-30 00:29:52 +00:00
|
|
|
Delete the session and confirm. """
|
2013-05-17 10:18:00 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = iq['cancel']['seqnr']
|
2013-08-30 00:29:52 +00:00
|
|
|
# Find the session
|
|
|
|
for s in self.sessions:
|
|
|
|
if self.sessions[s]['from'] == iq['from'] and self.sessions[s]['to'] == iq['to'] and self.sessions[s]['seqnr'] == seqnr:
|
|
|
|
# found it. Cancel all timers
|
|
|
|
for n in self.sessions[s]["commTimers"]:
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[s]["commTimers"][n].cancel()
|
2013-05-17 10:18:00 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
# Confirm
|
2014-08-17 22:52:24 +00:00
|
|
|
iq.reply()
|
|
|
|
iq['type'] = 'result'
|
|
|
|
iq['cancelled']['seqnr'] = seqnr
|
|
|
|
iq.send(block=False)
|
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
# Delete session
|
|
|
|
del self.sessions[s]
|
|
|
|
return
|
2013-05-17 10:18:00 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
# Could not find session, send reject
|
2014-08-17 22:52:24 +00:00
|
|
|
iq.reply()
|
|
|
|
iq['type'] = 'error'
|
|
|
|
iq['rejected']['seqnr'] = seqnr
|
|
|
|
iq['rejected']['error'] = "Cancel request received, no matching request is active."
|
|
|
|
iq.send(block=False)
|
2013-05-17 10:18:00 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
# =================================================================
|
2013-08-30 00:29:52 +00:00
|
|
|
# Client side (data retriever) API
|
2013-05-17 10:18:00 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
def request_data(self, from_jid, to_jid, callback, nodeIds=None, fields=None, flags=None):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
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.
|
2014-08-17 22:52:24 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
Arguments:
|
|
|
|
from_jid -- The jid of the requester
|
|
|
|
to_jid -- The jid of the device(s)
|
2014-08-17 22:52:24 +00:00
|
|
|
callback -- The callback function to call when data is availble.
|
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
The callback function must support the following arguments:
|
|
|
|
|
|
|
|
from_jid -- The jid of the responding device(s)
|
|
|
|
result -- The current result status of the readout. Valid values are:
|
|
|
|
"accepted" - Readout request accepted
|
|
|
|
"queued" - Readout request accepted and queued
|
|
|
|
"rejected" - Readout request rejected
|
|
|
|
"failure" - Readout failed.
|
|
|
|
"cancelled" - Confirmation of request cancellation.
|
|
|
|
"started" - Previously queued request is now started
|
|
|
|
"fields" - Contains readout data.
|
|
|
|
"done" - Indicates that the readout is complete.
|
|
|
|
|
|
|
|
nodeId -- [optional] Mandatory when result == "fields" or "failure".
|
|
|
|
The node Id of the responding device. One callback will only
|
|
|
|
contain data from one device.
|
|
|
|
timestamp -- [optional] Mandatory when result == "fields".
|
|
|
|
The timestamp of data in this callback. One callback will only
|
|
|
|
contain data from one timestamp.
|
|
|
|
fields -- [optional] Mandatory when result == "fields".
|
2014-08-17 22:52:24 +00:00
|
|
|
List of field dictionaries representing the readout data.
|
2013-08-30 00:29:52 +00:00
|
|
|
Dictionary format:
|
|
|
|
{
|
|
|
|
typename: The field type (numeric, boolean, dateTime, timeSpan, string, enum)
|
|
|
|
name: The field name
|
|
|
|
value: The field value
|
|
|
|
unit: The unit of the field. Only applies to type numeric.
|
|
|
|
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" ... }
|
2014-08-17 22:52:24 +00:00
|
|
|
}
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
error_msg -- [optional] Mandatory when result == "rejected" or "failure".
|
2014-08-17 22:52:24 +00:00
|
|
|
Details about why the request is rejected or failed.
|
|
|
|
"rejected" means that the request is stopped, but note that the
|
2013-08-30 00:29:52 +00:00
|
|
|
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.
|
|
|
|
|
|
|
|
nodeIds -- [optional] Limits the request to the node Ids in this list.
|
|
|
|
fields -- [optional] Limits the request to the field names in this list.
|
|
|
|
flags -- [optional] Limits the request according to the flags, or sets
|
|
|
|
readout conditions such as timing.
|
|
|
|
|
|
|
|
Return value:
|
|
|
|
session -- Session identifier. Client can use this as a reference to cancel
|
|
|
|
the request.
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = from_jid
|
|
|
|
iq['to'] = to_jid
|
|
|
|
iq['type'] = "get"
|
|
|
|
seqnr = self._get_new_seqnr()
|
|
|
|
iq['id'] = seqnr
|
|
|
|
iq['req']['seqnr'] = seqnr
|
2013-08-30 00:29:52 +00:00
|
|
|
if nodeIds is not None:
|
|
|
|
for nodeId in nodeIds:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['req'].add_node(nodeId)
|
2013-08-30 00:29:52 +00:00
|
|
|
if fields is not None:
|
|
|
|
for field in fields:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['req'].add_field(field)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['req']._set_flags(flags)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[seqnr] = {"from": iq['from'], "to": iq['to'], "seqnr": seqnr, "callback": callback}
|
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
return seqnr
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def cancel_request(self, session):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
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).
|
2014-08-17 22:52:24 +00:00
|
|
|
Does not block, the callback will be called when cancellation is
|
2013-08-30 00:29:52 +00:00
|
|
|
confirmed.
|
2014-08-17 22:52:24 +00:00
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
Arguments:
|
|
|
|
session -- The session id of the request to cancel
|
|
|
|
"""
|
|
|
|
seqnr = session
|
2014-08-17 22:52:24 +00:00
|
|
|
iq = self.xmpp.Iq()
|
2013-08-30 00:29:52 +00:00
|
|
|
iq['from'] = self.sessions[seqnr]['from']
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['to'] = self.sessions[seqnr]['to']
|
|
|
|
iq['type'] = "get"
|
|
|
|
iq['id'] = seqnr
|
|
|
|
iq['cancel']['seqnr'] = seqnr
|
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _get_new_seqnr(self):
|
|
|
|
""" Returns a unique sequence number (unique across threads) """
|
2014-08-17 22:52:24 +00:00
|
|
|
self.seqnr_lock.acquire()
|
|
|
|
self.last_seqnr += 1
|
|
|
|
self.seqnr_lock.release()
|
|
|
|
return str(self.last_seqnr)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_event_accepted(self, iq):
|
|
|
|
""" Received Iq with accepted - request was accepted """
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = iq['accepted']['seqnr']
|
2013-08-30 00:29:52 +00:00
|
|
|
result = "accepted"
|
|
|
|
if iq['accepted']['queued'] == 'true':
|
|
|
|
result = "queued"
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
callback = self.sessions[seqnr]["callback"]
|
|
|
|
callback(from_jid=iq['from'], result=result)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_event_rejected(self, iq):
|
2014-08-17 22:52:24 +00:00
|
|
|
""" Received Iq with rejected - this is a reject.
|
2013-08-30 00:29:52 +00:00
|
|
|
Delete the session. """
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = iq['rejected']['seqnr']
|
|
|
|
callback = self.sessions[seqnr]["callback"]
|
|
|
|
callback(from_jid=iq['from'], result="rejected", error_msg=iq['rejected']['error'])
|
2013-08-30 00:29:52 +00:00
|
|
|
# Session terminated
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[seqnr]
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_event_cancelled(self, iq):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
|
|
|
Received Iq with cancelled - this is a cancel confirm.
|
|
|
|
Delete the session.
|
2013-08-30 00:29:52 +00:00
|
|
|
"""
|
|
|
|
#print("Got cancelled")
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = iq['cancelled']['seqnr']
|
|
|
|
callback = self.sessions[seqnr]["callback"]
|
|
|
|
callback(from_jid=iq['from'], result="cancelled")
|
2013-08-30 00:29:52 +00:00
|
|
|
# Session cancelled
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[seqnr]
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_event_fields(self, msg):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
Received Msg with fields - this is a data reponse to a request.
|
|
|
|
If this is the last data block, issue a "done" callback.
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = msg['fields']['seqnr']
|
|
|
|
callback = self.sessions[seqnr]["callback"]
|
2013-08-30 00:29:52 +00:00
|
|
|
for node in msg['fields']['nodes']:
|
|
|
|
for ts in node['timestamps']:
|
2014-08-17 22:52:24 +00:00
|
|
|
fields = []
|
2013-08-30 00:29:52 +00:00
|
|
|
for d in ts['datas']:
|
2014-08-17 22:52:24 +00:00
|
|
|
field_block = {}
|
|
|
|
field_block["name"] = d['name']
|
|
|
|
field_block["typename"] = d._get_typename()
|
|
|
|
field_block["value"] = d['value']
|
2013-08-30 00:29:52 +00:00
|
|
|
if not d['unit'] == "": field_block["unit"] = d['unit'];
|
|
|
|
if not d['dataType'] == "": field_block["dataType"] = d['dataType'];
|
2014-08-17 22:52:24 +00:00
|
|
|
flags = d._get_flags()
|
2013-08-30 00:29:52 +00:00
|
|
|
if not len(flags) == 0:
|
2014-08-17 22:52:24 +00:00
|
|
|
field_block["flags"] = flags
|
|
|
|
fields.append(field_block)
|
|
|
|
|
|
|
|
callback(from_jid=msg['from'], result="fields", nodeId=node['nodeId'], timestamp=ts['value'], fields=fields)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
if msg['fields']['done'] == "true":
|
2014-08-17 22:52:24 +00:00
|
|
|
callback(from_jid=msg['from'], result="done")
|
2013-08-30 00:29:52 +00:00
|
|
|
# Session done
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[seqnr]
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_event_failure(self, msg):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
Received Msg with failure - our request failed
|
2014-08-17 22:52:24 +00:00
|
|
|
Delete the session.
|
2013-08-30 00:29:52 +00:00
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = msg['failure']['seqnr']
|
|
|
|
callback = self.sessions[seqnr]["callback"]
|
|
|
|
callback(from_jid=msg['from'], result="failure", nodeId=msg['failure']['error']['nodeId'], timestamp=msg['failure']['error']['timestamp'], error_msg=msg['failure']['error']['text'])
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# Session failed
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[seqnr]
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_event_started(self, msg):
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
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")
|
|
|
|
|
2013-05-17 10:18:00 +00:00
|
|
|
|