Get the IoT plugins to pass tests on Py3
This commit is contained in:
@@ -11,36 +11,36 @@
|
||||
import datetime
|
||||
|
||||
class Device(object):
|
||||
"""
|
||||
Example implementation of a device control object.
|
||||
"""
|
||||
Example implementation of a device control object.
|
||||
|
||||
The device object may by any custom implementation to support
|
||||
specific devices, but it must implement the functions:
|
||||
has_control_field
|
||||
set_control_fields
|
||||
"""
|
||||
"""
|
||||
|
||||
def __init__(self, nodeId):
|
||||
self.nodeId = nodeId;
|
||||
self.control_fields = {};
|
||||
def __init__(self, nodeId):
|
||||
self.nodeId = nodeId;
|
||||
self.control_fields = {};
|
||||
|
||||
def has_control_field(self, field, typename):
|
||||
"""
|
||||
Returns true if the supplied field name exists
|
||||
and the type matches for control in this device.
|
||||
def has_control_field(self, field, typename):
|
||||
"""
|
||||
Returns true if the supplied field name exists
|
||||
and the type matches for control in this device.
|
||||
|
||||
Arguments:
|
||||
field -- The field name
|
||||
field -- The field name
|
||||
typename -- The expected type
|
||||
"""
|
||||
if field in self.control_fields and self.control_fields[field]["type"] == typename:
|
||||
return True;
|
||||
return False;
|
||||
"""
|
||||
if field in self.control_fields and self.control_fields[field]["type"] == typename:
|
||||
return True;
|
||||
return False;
|
||||
|
||||
def set_control_fields(self, fields, session, callback):
|
||||
"""
|
||||
Starts a control setting procedure. Verifies the fields,
|
||||
sets the data and (if needed) and calls the callback.
|
||||
def set_control_fields(self, fields, session, callback):
|
||||
"""
|
||||
Starts a control setting procedure. Verifies the fields,
|
||||
sets the data and (if needed) and calls the callback.
|
||||
|
||||
Arguments:
|
||||
fields -- List of control fields in tuple format:
|
||||
@@ -48,50 +48,50 @@ class Device(object):
|
||||
session -- Session id, only used in the callback as identifier
|
||||
callback -- Callback function to call when control set is complete.
|
||||
|
||||
The callback function must support the following arguments:
|
||||
The callback function must support the following arguments:
|
||||
|
||||
session -- Session id, as supplied in the
|
||||
request_fields call
|
||||
nodeId -- Identifier for this device
|
||||
result -- The current result status of the readout.
|
||||
Valid values are:
|
||||
session -- Session id, as supplied in the
|
||||
request_fields call
|
||||
nodeId -- Identifier for this device
|
||||
result -- The current result status of the readout.
|
||||
Valid values are:
|
||||
"error" - Set fields failed.
|
||||
"ok" - All fields were set.
|
||||
error_field -- [optional] Only applies when result == "error"
|
||||
The field name that failed
|
||||
(usually means it is missing)
|
||||
error_msg -- [optional] Only applies when result == "error".
|
||||
Error details when a request failed.
|
||||
"""
|
||||
error_field -- [optional] Only applies when result == "error"
|
||||
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 len(fields) > 0:
|
||||
# Check availiability
|
||||
for name, typename, value in fields:
|
||||
if not self.has_control_field(name, typename):
|
||||
self._send_control_reject(session, name, "NotFound", callback)
|
||||
return False;
|
||||
if len(fields) > 0:
|
||||
# Check availiability
|
||||
for name, typename, value in fields:
|
||||
if not self.has_control_field(name, typename):
|
||||
self._send_control_reject(session, name, "NotFound", callback)
|
||||
return False;
|
||||
|
||||
for name, typename, value in fields:
|
||||
self._set_field_value(name, value)
|
||||
for name, typename, value in fields:
|
||||
self._set_field_value(name, value)
|
||||
|
||||
callback(session, result="ok", nodeId=self.nodeId);
|
||||
return True
|
||||
callback(session, result="ok", nodeId=self.nodeId);
|
||||
return True
|
||||
|
||||
def _send_control_reject(self, session, field, message, callback):
|
||||
"""
|
||||
Sends a reject to the caller
|
||||
def _send_control_reject(self, session, field, message, callback):
|
||||
"""
|
||||
Sends a reject to the caller
|
||||
|
||||
Arguments:
|
||||
session -- Session id, see definition in
|
||||
set_control_fields function
|
||||
callback -- Callback function, see definition in
|
||||
set_control_fields function
|
||||
"""
|
||||
callback(session, result="error", nodeId=self.nodeId, error_field=field, error_msg=message);
|
||||
"""
|
||||
callback(session, result="error", nodeId=self.nodeId, error_field=field, error_msg=message);
|
||||
|
||||
def _add_control_field(self, name, typename, value):
|
||||
"""
|
||||
Adds a control field to the device
|
||||
def _add_control_field(self, name, typename, value):
|
||||
"""
|
||||
Adds a control field to the device
|
||||
|
||||
Arguments:
|
||||
name -- Name of the field
|
||||
@@ -99,27 +99,27 @@ class Device(object):
|
||||
(boolean, color, string, date, dateTime,
|
||||
double, duration, int, long, time)
|
||||
value -- Field value
|
||||
"""
|
||||
self.control_fields[name] = {"type": typename, "value": value};
|
||||
"""
|
||||
self.control_fields[name] = {"type": typename, "value": value};
|
||||
|
||||
def _set_field_value(self, name, value):
|
||||
"""
|
||||
Set the value of a control field
|
||||
def _set_field_value(self, name, value):
|
||||
"""
|
||||
Set the value of a control field
|
||||
|
||||
Arguments:
|
||||
name -- Name of the field
|
||||
value -- New value for the field
|
||||
"""
|
||||
if name in self.control_fields:
|
||||
self.control_fields[name]["value"] = value;
|
||||
"""
|
||||
if name in self.control_fields:
|
||||
self.control_fields[name]["value"] = value;
|
||||
|
||||
def _get_field_value(self, name):
|
||||
"""
|
||||
Get the value of a control field. Only used for unit testing.
|
||||
def _get_field_value(self, name):
|
||||
"""
|
||||
Get the value of a control field. Only used for unit testing.
|
||||
|
||||
Arguments:
|
||||
name -- Name of the field
|
||||
"""
|
||||
if name in self.control_fields:
|
||||
return self.control_fields[name]["value"];
|
||||
return None;
|
||||
"""
|
||||
if name in self.control_fields:
|
||||
return self.control_fields[name]["value"];
|
||||
return None;
|
||||
|
||||
@@ -83,10 +83,10 @@ class ControlSet(ElementBase):
|
||||
|
||||
def get_nodes(self):
|
||||
"""Return all nodes."""
|
||||
nodes = set()
|
||||
nodes = []
|
||||
for node in self['substanzas']:
|
||||
if isinstance(node, RequestNode):
|
||||
nodes.add(node)
|
||||
nodes.append(node)
|
||||
return nodes
|
||||
|
||||
def set_nodes(self, nodes):
|
||||
@@ -175,10 +175,10 @@ class ControlSet(ElementBase):
|
||||
|
||||
def get_datas(self):
|
||||
""" Return all data elements. """
|
||||
datas = set()
|
||||
datas = []
|
||||
for data in self['substanzas']:
|
||||
if isinstance(data, BaseParameter):
|
||||
datas.add(data)
|
||||
datas.append(data)
|
||||
return datas
|
||||
|
||||
def set_datas(self, datas):
|
||||
@@ -274,10 +274,10 @@ class ControlSetResponse(ElementBase):
|
||||
|
||||
def get_nodes(self):
|
||||
"""Return all nodes."""
|
||||
nodes = set()
|
||||
nodes = []
|
||||
for node in self['substanzas']:
|
||||
if isinstance(node, RequestNode):
|
||||
nodes.add(node)
|
||||
nodes.append(node)
|
||||
return nodes
|
||||
|
||||
def set_nodes(self, nodes):
|
||||
|
||||
Reference in New Issue
Block a user