2 Slixmpp: The Slick XMPP Library
3 Implementation of xeps for Internet of Things
4 http://wiki.xmpp.org/web/Tech_pages/IoT_systems
5 Copyright (C) 2013 Sustainable Innovation, Joachim.lindborg@sust.se, bjorn.westrom@consoden.se
6 This file is part of Slixmpp.
8 See the file LICENSE for copying permission.
15 Example implementation of a device control object.
17 The device object may by any custom implementation to support
18 specific devices, but it must implement the functions:
23 def __init__(self
, nodeId
):
25 self
.control_fields
= {};
27 def has_control_field(self
, field
, typename
):
29 Returns true if the supplied field name exists
30 and the type matches for control in this device.
33 field -- The field name
34 typename -- The expected type
36 if field
in self
.control_fields
and self
.control_fields
[field
]["type"] == typename
:
40 def set_control_fields(self
, fields
, session
, callback
):
42 Starts a control setting procedure. Verifies the fields,
43 sets the data and (if needed) and calls the callback.
46 fields -- List of control fields in tuple format:
47 (name, typename, value)
48 session -- Session id, only used in the callback as identifier
49 callback -- Callback function to call when control set is complete.
51 The callback function must support the following arguments:
53 session -- Session id, as supplied in the
55 nodeId -- Identifier for this device
56 result -- The current result status of the readout.
58 "error" - Set fields failed.
59 "ok" - All fields were set.
60 error_field -- [optional] Only applies when result == "error"
61 The field name that failed
62 (usually means it is missing)
63 error_msg -- [optional] Only applies when result == "error".
64 Error details when a request failed.
69 for name
, typename
, value
in fields
:
70 if not self
.has_control_field(name
, typename
):
71 self
._send
_control
_reject
(session
, name
, "NotFound", callback
)
74 for name
, typename
, value
in fields
:
75 self
._set
_field
_value
(name
, value
)
77 callback(session
, result
="ok", nodeId
=self
.nodeId
);
80 def _send_control_reject(self
, session
, field
, message
, callback
):
82 Sends a reject to the caller
85 session -- Session id, see definition in
86 set_control_fields function
87 callback -- Callback function, see definition in
88 set_control_fields function
90 callback(session
, result
="error", nodeId
=self
.nodeId
, error_field
=field
, error_msg
=message
);
92 def _add_control_field(self
, name
, typename
, value
):
94 Adds a control field to the device
97 name -- Name of the field
98 typename -- Type of the field, one of:
99 (boolean, color, string, date, dateTime,
100 double, duration, int, long, time)
103 self
.control_fields
[name
] = {"type": typename
, "value": value
};
105 def _set_field_value(self
, name
, value
):
107 Set the value of a control field
110 name -- Name of the field
111 value -- New value for the field
113 if name
in self
.control_fields
:
114 self
.control_fields
[name
]["value"] = value
;
116 def _get_field_value(self
, name
):
118 Get the value of a control field. Only used for unit testing.
121 name -- Name of the field
123 if name
in self
.control_fields
:
124 return self
.control_fields
[name
]["value"];