Rename to slixmpp
[slixmpp.git] / slixmpp / plugins / xep_0325 / device.py
blob26ebc89f1052541edfbfcc15d984b6d6d139f36d
1 """
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.
9 """
11 import datetime
13 class Device(object):
14 """
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:
19 has_control_field
20 set_control_fields
21 """
23 def __init__(self, nodeId):
24 self.nodeId = nodeId;
25 self.control_fields = {};
27 def has_control_field(self, field, typename):
28 """
29 Returns true if the supplied field name exists
30 and the type matches for control in this device.
32 Arguments:
33 field -- The field name
34 typename -- The expected type
35 """
36 if field in self.control_fields and self.control_fields[field]["type"] == typename:
37 return True;
38 return False;
40 def set_control_fields(self, fields, session, callback):
41 """
42 Starts a control setting procedure. Verifies the fields,
43 sets the data and (if needed) and calls the callback.
45 Arguments:
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
54 request_fields call
55 nodeId -- Identifier for this device
56 result -- The current result status of the readout.
57 Valid values are:
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.
65 """
67 if len(fields) > 0:
68 # Check availiability
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)
72 return False;
74 for name, typename, value in fields:
75 self._set_field_value(name, value)
77 callback(session, result="ok", nodeId=self.nodeId);
78 return True
80 def _send_control_reject(self, session, field, message, callback):
81 """
82 Sends a reject to the caller
84 Arguments:
85 session -- Session id, see definition in
86 set_control_fields function
87 callback -- Callback function, see definition in
88 set_control_fields function
89 """
90 callback(session, result="error", nodeId=self.nodeId, error_field=field, error_msg=message);
92 def _add_control_field(self, name, typename, value):
93 """
94 Adds a control field to the device
96 Arguments:
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)
101 value -- Field value
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
109 Arguments:
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.
120 Arguments:
121 name -- Name of the field
123 if name in self.control_fields:
124 return self.control_fields[name]["value"];
125 return None;