2013-08-30 00:29:52 +00:00
|
|
|
"""
|
|
|
|
SleekXMPP: The Sleek XMPP Library
|
|
|
|
Implementation of xeps for Internet of Things
|
|
|
|
http://wiki.xmpp.org/web/Tech_pages/IoT_systems
|
|
|
|
Copyright (C) 2013 Sustainable Innovation, Joachim.lindborg@sust.se, bjorn.westrom@consoden.se
|
|
|
|
This file is part of SleekXMPP.
|
|
|
|
|
|
|
|
See the file LICENSE for copying permission.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import time
|
|
|
|
from threading import Thread, Timer, Lock
|
|
|
|
|
|
|
|
from sleekxmpp.xmlstream.handler import Callback
|
|
|
|
from sleekxmpp.xmlstream.matcher import StanzaPath
|
|
|
|
from sleekxmpp.plugins.base import BasePlugin
|
|
|
|
from sleekxmpp.plugins.xep_0325 import stanza
|
|
|
|
from sleekxmpp.plugins.xep_0325.stanza import Control
|
|
|
|
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class XEP_0325(BasePlugin):
|
|
|
|
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
XEP-0325: IoT Control
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
Actuators are devices in sensor networks that can be controlled through
|
|
|
|
the network and act with the outside world. In sensor networks and
|
|
|
|
Internet of Things applications, actuators make it possible to automate
|
|
|
|
real-world processes.
|
|
|
|
This plugin implements a mechanism whereby actuators can be controlled
|
|
|
|
in XMPP-based sensor networks, making it possible to integrate sensors
|
|
|
|
and actuators of different brands, makes and models into larger
|
2013-08-30 00:29:52 +00:00
|
|
|
Internet of Things applications.
|
|
|
|
|
|
|
|
Also see <http://xmpp.org/extensions/xep-0325.html>
|
|
|
|
|
|
|
|
Configuration Values:
|
|
|
|
threaded -- Indicates if communication with sensors should be threaded.
|
|
|
|
Defaults to True.
|
|
|
|
|
|
|
|
Events:
|
|
|
|
Sensor side
|
|
|
|
-----------
|
|
|
|
Control Event:DirectSet -- Received a control message
|
|
|
|
Control Event:SetReq -- Received a control request
|
|
|
|
|
|
|
|
Client side
|
|
|
|
-----------
|
2014-08-17 22:52:24 +00:00
|
|
|
Control Event:SetResponse -- Received a response to a
|
2013-08-30 00:29:52 +00:00
|
|
|
control request, type result
|
2014-08-17 22:52:24 +00:00
|
|
|
Control Event:SetResponseError -- Received a response to a
|
2013-08-30 00:29:52 +00:00
|
|
|
control request, type error
|
|
|
|
|
|
|
|
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
|
|
|
set_request -- Initiates a control request to modify data in
|
2013-08-30 00:29:52 +00:00
|
|
|
sensor(s). Non-blocking, a callback function will
|
|
|
|
be called when the sensor has responded.
|
2014-08-17 22:52:24 +00:00
|
|
|
set_command -- Initiates a control command to modify data in
|
2013-08-30 00:29:52 +00:00
|
|
|
sensor(s). Non-blocking. The sensor(s) will not
|
|
|
|
respond regardless of the result of the command,
|
|
|
|
so no callback is made.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
name = 'xep_0325'
|
|
|
|
description = 'XEP-0325 Internet of Things - Control'
|
2014-08-17 22:52:24 +00:00
|
|
|
dependencies = set(['xep_0030'])
|
2013-08-30 00:29:52 +00:00
|
|
|
stanza = stanza
|
|
|
|
|
|
|
|
|
|
|
|
default_config = {
|
|
|
|
'threaded': True
|
|
|
|
# 'session_db': None
|
|
|
|
}
|
|
|
|
|
|
|
|
def plugin_init(self):
|
|
|
|
""" Start the XEP-0325 plugin """
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Control Event:DirectSet',
|
|
|
|
StanzaPath('message/set'),
|
|
|
|
self._handle_direct_set))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Control Event:SetReq',
|
|
|
|
StanzaPath('iq@type=set/set'),
|
|
|
|
self._handle_set_req))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Control Event:SetResponse',
|
|
|
|
StanzaPath('iq@type=result/setResponse'),
|
|
|
|
self._handle_set_response))
|
|
|
|
|
|
|
|
self.xmpp.register_handler(
|
|
|
|
Callback('Control Event:SetResponseError',
|
|
|
|
StanzaPath('iq@type=error/setResponse'),
|
|
|
|
self._handle_set_response))
|
|
|
|
|
|
|
|
# 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(Control.namespace)
|
|
|
|
self.xmpp['xep_0030'].set_items(node=Control.namespace, items=tuple())
|
|
|
|
|
|
|
|
def _new_session(self):
|
|
|
|
""" Return a new session ID. """
|
|
|
|
return str(time.time()) + '-' + self.xmpp.new_id()
|
|
|
|
|
|
|
|
def plugin_end(self):
|
|
|
|
""" Stop the XEP-0325 plugin """
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions.clear()
|
2013-08-30 00:29:52 +00:00
|
|
|
self.xmpp.remove_handler('Control Event:DirectSet')
|
|
|
|
self.xmpp.remove_handler('Control Event:SetReq')
|
|
|
|
self.xmpp.remove_handler('Control Event:SetResponse')
|
|
|
|
self.xmpp.remove_handler('Control Event:SetResponseError')
|
|
|
|
self.xmpp['xep_0030'].del_feature(feature=Control.namespace)
|
2014-08-17 22:52:24 +00:00
|
|
|
self.xmpp['xep_0030'].set_items(node=Control.namespace, items=tuple())
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# =================================================================
|
|
|
|
# Sensor side (data provider) API
|
|
|
|
|
|
|
|
def register_node(self, nodeId, device, commTimeout, sourceId=None, cacheType=None):
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
Register a sensor/device as available for control requests/commands
|
|
|
|
through this XMPP 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_control_field
|
|
|
|
set_control_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 _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_set_req(self, iq):
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
Event handler for reception of an Iq with set req - this is a
|
2013-08-30 00:29:52 +00:00
|
|
|
control 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
|
|
|
|
(if no nodes are specified in the request, assume all nodes)
|
|
|
|
- all the control fields are available from all requested nodes
|
|
|
|
(if no nodes are specified in the request, assume all nodes)
|
|
|
|
|
|
|
|
If the request passes verification, the control request is passed
|
|
|
|
to the devices (in a separate thread).
|
2014-08-17 22:52:24 +00:00
|
|
|
If the verification fails, a setResponse with error indication
|
2013-08-30 00:29:52 +00:00
|
|
|
is sent.
|
|
|
|
"""
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
error_msg = ''
|
|
|
|
req_ok = True
|
|
|
|
missing_node = None
|
|
|
|
missing_field = None
|
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
|
|
|
|
if len(iq['set']['nodes']) > 0:
|
|
|
|
for n in iq['set']['nodes']:
|
|
|
|
if not n['nodeId'] in self.nodes:
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
|
|
|
missing_node = n['nodeId']
|
|
|
|
error_msg = "Invalid nodeId " + n['nodeId']
|
|
|
|
process_nodes = [n['nodeId'] for n in iq['set']['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 - for control we need to find all in all devices, otherwise we reject
|
2014-08-17 22:52:24 +00:00
|
|
|
process_fields = []
|
2013-08-30 00:29:52 +00:00
|
|
|
if len(iq['set']['datas']) > 0:
|
|
|
|
for f in iq['set']['datas']:
|
|
|
|
for node in self.nodes:
|
|
|
|
if not self.nodes[node]["device"].has_control_field(f['name'], f._get_typename()):
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
|
|
|
missing_field = f['name']
|
|
|
|
error_msg = "Invalid field " + f['name']
|
|
|
|
break
|
|
|
|
process_fields = [(f['name'], f._get_typename(), f['value']) for f in iq['set']['datas']]
|
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": iq['id']}
|
|
|
|
self.sessions[session]["commTimers"] = {}
|
|
|
|
self.sessions[session]["nodeDone"] = {}
|
2013-08-30 00:29:52 +00:00
|
|
|
# Flag that a reply is exected when we are done
|
2014-08-17 22:52:24 +00:00
|
|
|
self.sessions[session]["reply"] = True
|
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 self.threaded:
|
|
|
|
#print("starting thread")
|
|
|
|
tr_req = Thread(target=self._threaded_node_request, args=(session, process_fields))
|
|
|
|
tr_req.start()
|
|
|
|
#print("started thread")
|
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
self._threaded_node_request(session, process_fields)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq.reply()
|
|
|
|
iq['type'] = 'error'
|
|
|
|
iq['setResponse']['responseCode'] = "NotFound"
|
2013-08-30 00:29:52 +00:00
|
|
|
if missing_node is not None:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['setResponse'].add_node(missing_node)
|
2013-08-30 00:29:52 +00:00
|
|
|
if missing_field is not None:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['setResponse'].add_data(missing_field)
|
|
|
|
iq['setResponse']['error']['var'] = "Output"
|
|
|
|
iq['setResponse']['error']['text'] = error_msg
|
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_direct_set(self, msg):
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
Event handler for reception of a Message with set command - this is a
|
2013-08-30 00:29:52 +00:00
|
|
|
direct control command.
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
Verifies that
|
2013-08-30 00:29:52 +00:00
|
|
|
- all the requested nodes are available
|
|
|
|
(if no nodes are specified in the request, assume all nodes)
|
|
|
|
- all the control fields are available from all requested nodes
|
|
|
|
(if no nodes are specified in the request, assume all nodes)
|
|
|
|
|
|
|
|
If the request passes verification, the control request is passed
|
|
|
|
to the devices (in a separate thread).
|
|
|
|
If the verification fails, do nothing.
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = True
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# Nodes
|
|
|
|
if len(msg['set']['nodes']) > 0:
|
|
|
|
for n in msg['set']['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 msg['set']['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 - for control we need to find all in all devices, otherwise we reject
|
2014-08-17 22:52:24 +00:00
|
|
|
process_fields = []
|
2013-08-30 00:29:52 +00:00
|
|
|
if len(msg['set']['datas']) > 0:
|
|
|
|
for f in msg['set']['datas']:
|
|
|
|
for node in self.nodes:
|
|
|
|
if not self.nodes[node]["device"].has_control_field(f['name'], f._get_typename()):
|
2014-08-17 22:52:24 +00:00
|
|
|
req_ok = False
|
|
|
|
missing_field = f['name']
|
|
|
|
error_msg = "Invalid field " + f['name']
|
|
|
|
break
|
|
|
|
process_fields = [(f['name'], f._get_typename(), f['value']) for f in msg['set']['datas']]
|
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": msg['from'], "to": msg['to']}
|
|
|
|
self.sessions[session]["commTimers"] = {}
|
|
|
|
self.sessions[session]["nodeDone"] = {}
|
|
|
|
self.sessions[session]["reply"] = 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 self.threaded:
|
|
|
|
#print("starting thread")
|
|
|
|
tr_req = Thread(target=self._threaded_node_request, args=(session, process_fields))
|
|
|
|
tr_req.start()
|
|
|
|
#print("started thread")
|
|
|
|
else:
|
2014-08-17 22:52:24 +00:00
|
|
|
self._threaded_node_request(session, process_fields)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _threaded_node_request(self, session, process_fields):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
2013-08-30 00:29:52 +00:00
|
|
|
Helper function to handle the device control 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 set in the devices. List of tuple format:
|
|
|
|
(name, datatype, 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 = Timer(self.nodes[node]['commTimeout'], self._event_comm_timeout, args=(session, node))
|
|
|
|
self.sessions[session]["commTimers"][node] = timer
|
|
|
|
timer.start()
|
|
|
|
self.nodes[node]['device'].set_control_fields(process_fields, session=session, callback=self._device_set_command_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 control operations timeout.
|
|
|
|
Stop communicating with the failing device.
|
2014-08-17 22:52:24 +00:00
|
|
|
If the control command was an Iq request, sends a failure
|
|
|
|
message back to the client.
|
|
|
|
|
2013-08-30 00:29:52 +00:00
|
|
|
Arguments:
|
|
|
|
session -- The request session id
|
|
|
|
nodeId -- The id of the device which timed out
|
|
|
|
"""
|
|
|
|
|
|
|
|
if self.sessions[session]["reply"]:
|
|
|
|
# Reply is exected when we are done
|
2014-08-17 22:52:24 +00:00
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = self.sessions[session]['to']
|
|
|
|
iq['to'] = self.sessions[session]['from']
|
|
|
|
iq['type'] = "error"
|
|
|
|
iq['id'] = self.sessions[session]['seqnr']
|
|
|
|
iq['setResponse']['responseCode'] = "OtherError"
|
|
|
|
iq['setResponse'].add_node(nodeId)
|
|
|
|
iq['setResponse']['error']['var'] = "Output"
|
|
|
|
iq['setResponse']['error']['text'] = "Timeout."
|
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
## TODO - should we send one timeout per node??
|
|
|
|
|
|
|
|
# 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)):
|
|
|
|
# The session is complete, delete it
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[session]
|
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 control command.
|
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_set_command_callback(self, session, nodeId, result, error_field=None, error_msg=None):
|
2014-08-17 22:52:24 +00:00
|
|
|
"""
|
|
|
|
Callback function called by the devices when the control command is
|
2013-08-30 00:29:52 +00:00
|
|
|
complete or failed.
|
2014-08-17 22:52:24 +00:00
|
|
|
If needed, composes a message with the result and sends it back to the
|
2013-08-30 00:29:52 +00:00
|
|
|
client.
|
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 control command. Valid values are:
|
|
|
|
"error" - Set fields failed.
|
|
|
|
"ok" - All fields were set.
|
2014-08-17 22:52:24 +00:00
|
|
|
error_field -- [optional] Only applies when result == "error"
|
2013-08-30 00:29:52 +00:00
|
|
|
The field name that failed (usually means it is missing)
|
|
|
|
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 timeout. 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
|
|
|
|
|
|
|
if self.sessions[session]["reply"]:
|
|
|
|
# Reply is exected when we are done
|
2014-08-17 22:52:24 +00:00
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = self.sessions[session]['to']
|
|
|
|
iq['to'] = self.sessions[session]['from']
|
|
|
|
iq['type'] = "error"
|
|
|
|
iq['id'] = self.sessions[session]['seqnr']
|
|
|
|
iq['setResponse']['responseCode'] = "OtherError"
|
|
|
|
iq['setResponse'].add_node(nodeId)
|
2013-08-30 00:29:52 +00:00
|
|
|
if error_field is not None:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['setResponse'].add_data(error_field)
|
|
|
|
iq['setResponse']['error']['var'] = error_field
|
|
|
|
iq['setResponse']['error']['text'] = error_msg
|
|
|
|
iq.send(block=False)
|
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)):
|
|
|
|
# The session is complete, delete it
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[session]
|
2013-08-30 00:29:52 +00:00
|
|
|
else:
|
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
|
|
|
self.sessions[session]["nodeDone"][nodeId] = True
|
2013-08-30 00:29:52 +00:00
|
|
|
if (self._all_nodes_done(session)):
|
|
|
|
if self.sessions[session]["reply"]:
|
|
|
|
# Reply is exected when we are done
|
2014-08-17 22:52:24 +00:00
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = self.sessions[session]['to']
|
|
|
|
iq['to'] = self.sessions[session]['from']
|
|
|
|
iq['type'] = "result"
|
|
|
|
iq['id'] = self.sessions[session]['seqnr']
|
|
|
|
iq['setResponse']['responseCode'] = "OK"
|
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# The session is complete, delete it
|
2014-08-17 22:52:24 +00:00
|
|
|
del self.sessions[session]
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
# =================================================================
|
|
|
|
# Client side (data controller) API
|
|
|
|
|
|
|
|
def set_request(self, from_jid, to_jid, callback, fields, nodeIds=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 control request.
|
|
|
|
Composes a message with the request and sends it to the device(s).
|
2014-08-17 22:52:24 +00:00
|
|
|
Does not block, the callback will be called when the device(s)
|
2013-08-30 00:29:52 +00:00
|
|
|
has responded.
|
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 result of the control request. Valid values are:
|
|
|
|
"OK" - Control request completed successfully
|
|
|
|
"NotFound" - One or more nodes or fields are missing
|
|
|
|
"InsufficientPrivileges" - Not authorized.
|
|
|
|
"Locked" - Field(s) is locked and cannot
|
|
|
|
be changed at the moment.
|
|
|
|
"NotImplemented" - Request feature not implemented.
|
2014-08-17 22:52:24 +00:00
|
|
|
"FormError" - Error while setting with
|
2013-08-30 00:29:52 +00:00
|
|
|
a form (not implemented).
|
2014-08-17 22:52:24 +00:00
|
|
|
"OtherError" - Indicates other types of
|
|
|
|
errors, such as timeout.
|
2013-08-30 00:29:52 +00:00
|
|
|
Details in the error_msg.
|
|
|
|
|
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
nodeId -- [optional] Only applicable when result == "error"
|
|
|
|
List of node Ids of failing device(s).
|
|
|
|
|
|
|
|
fields -- [optional] Only applicable when result == "error"
|
2013-08-30 00:29:52 +00:00
|
|
|
List of fields that failed.[optional] Mandatory when result == "rejected" or "failure".
|
2014-08-17 22:52:24 +00:00
|
|
|
|
|
|
|
error_msg -- Details about why the request failed.
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
fields -- Fields to set. List of tuple format: (name, typename, value).
|
|
|
|
nodeIds -- [optional] Limits the request to the node Ids in this list.
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
iq = self.xmpp.Iq()
|
|
|
|
iq['from'] = from_jid
|
|
|
|
iq['to'] = to_jid
|
|
|
|
seqnr = self._get_new_seqnr()
|
|
|
|
iq['id'] = seqnr
|
|
|
|
iq['type'] = "set"
|
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['set'].add_node(nodeId)
|
2013-08-30 00:29:52 +00:00
|
|
|
if fields is not None:
|
|
|
|
for name, typename, value in fields:
|
2014-08-17 22:52:24 +00:00
|
|
|
iq['set'].add_data(name=name, typename=typename, value=value)
|
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'], "callback": callback}
|
|
|
|
iq.send(block=False)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def set_command(self, from_jid, to_jid, fields, nodeIds=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 control command.
|
|
|
|
Composes a message with the set commandand sends it to the device(s).
|
|
|
|
Does not block. Device(s) will not respond, regardless of result.
|
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)
|
|
|
|
|
|
|
|
fields -- Fields to set. List of tuple format: (name, typename, value).
|
|
|
|
nodeIds -- [optional] Limits the request to the node Ids in this list.
|
|
|
|
"""
|
2014-08-17 22:52:24 +00:00
|
|
|
msg = self.xmpp.Message()
|
|
|
|
msg['from'] = from_jid
|
|
|
|
msg['to'] = to_jid
|
|
|
|
msg['type'] = "set"
|
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
|
|
|
msg['set'].add_node(nodeId)
|
2013-08-30 00:29:52 +00:00
|
|
|
if fields is not None:
|
|
|
|
for name, typename, value in fields:
|
2014-08-17 22:52:24 +00:00
|
|
|
msg['set'].add_data(name, typename, value)
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
# We won't get any reply, so don't create a session
|
2014-08-17 22:52:24 +00:00
|
|
|
msg.send()
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
def _handle_set_response(self, iq):
|
|
|
|
""" Received response from device(s) """
|
|
|
|
#print("ooh")
|
2014-08-17 22:52:24 +00:00
|
|
|
seqnr = iq['id']
|
|
|
|
from_jid = str(iq['from'])
|
|
|
|
result = iq['setResponse']['responseCode']
|
|
|
|
nodeIds = [n['name'] for n in iq['setResponse']['nodes']]
|
|
|
|
fields = [f['name'] for f in iq['setResponse']['datas']]
|
|
|
|
error_msg = None
|
2013-08-30 00:29:52 +00:00
|
|
|
|
|
|
|
if not iq['setResponse'].find('error') is None and not iq['setResponse']['error']['text'] == "":
|
2014-08-17 22:52:24 +00:00
|
|
|
error_msg = iq['setResponse']['error']['text']
|
2013-08-30 00:29:52 +00:00
|
|
|
|
2014-08-17 22:52:24 +00:00
|
|
|
callback = self.sessions[seqnr]["callback"]
|
|
|
|
callback(from_jid=from_jid, result=result, nodeIds=nodeIds, fields=fields, error_msg=error_msg)
|