You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 7, 2021. It is now read-only.
Implementation of an HTTP `Web Thing <https://iot.mozilla.org/wot/>`_. This library is compatible with Python 2.7 and 3.5+.
11
+
Implementation of an HTTP `Web Thing <https://iot.mozilla.org/wot/>`_. This library is compatible with Python 3.5+.
12
12
13
13
Installation
14
14
============
@@ -63,15 +63,15 @@ First we create a new Thing:
63
63
64
64
Now we can add the required properties.
65
65
66
-
The ``on`` property reports and sets the on/off state of the light. For this, we need to have a ``Value`` object which holds the actual state and also a method to turn the light on/off. For our purposes, we just want to log the new state if the light is switched on/off.
66
+
The on property reports and sets the on/off state of the light. For this, we need to have a Value object which holds the actual state and also a method to turn the light on/off. For our purposes, we just want to log the new state if the light is switched on/off.
67
67
68
68
.. code:: python
69
69
70
70
light.add_property(
71
71
Property(
72
72
light,
73
73
'on',
74
-
Value(True, lambdav: print('On-State is now', v)),
74
+
Value(True, None, lambdax: print(x)),
75
75
metadata={
76
76
'@type': 'OnOffProperty',
77
77
'title': 'On/Off',
@@ -87,7 +87,7 @@ The ``brightness`` property reports the brightness level of the light and sets t
87
87
Property(
88
88
light,
89
89
'brightness',
90
-
Value(50, lambdav: print('Brightness is now', v)),
90
+
Value(50, None, lambdax: print(x)),
91
91
metadata={
92
92
'@type': 'BrightnessProperty',
93
93
'title': 'Brightness',
@@ -102,9 +102,7 @@ Now we can add our newly created thing to the server and start it:
102
102
103
103
.. code:: python
104
104
105
-
# If adding more than one thing, use MultipleThings() with a name.
106
-
# In the single thing case, the thing's name will be broadcast.
107
-
server = WebThingServer(SingleThing(light), port=8888)
105
+
server = WebThingServer(light, port=8888)
108
106
109
107
try:
110
108
server.start()
@@ -134,33 +132,51 @@ First we create a new Thing:
134
132
'A web connected humidity sensor'
135
133
)
136
134
137
-
Then we create and add the appropriate property:
135
+
Then we create and add the appropriate property.
138
136
139
-
* ``level``: tells us what the sensor is actually reading
137
+
Contrary to the light, the value cannot be set via an API call, as it wouldn't make much sense, to SET what a sensor is reading. Therefore, we are creating a **readOnly** property.
140
138
141
-
- Contrary to the light, the value cannot be set via an API call, as it wouldn't make much sense, to SET what a sensor is reading. Therefore, we are creating a **readOnly** property.
139
+
.. code:: python
140
+
141
+
sensor.add_property(
142
+
Property(
143
+
sensor,
144
+
'level',
145
+
Value(None, self.read_from_gpio, None),
146
+
metadata={
147
+
'@type': 'LevelProperty',
148
+
'title': 'Humidity',
149
+
'type': 'number',
150
+
'description': 'The current humidity in %',
151
+
'minimum': 0,
152
+
'maximum': 100,
153
+
'unit': 'percent',
154
+
'readOnly': True,
155
+
}))
142
156
143
-
.. code:: python
157
+
In this example, we pass a `readproperty` method that will read and return the sensor value every time it is requested.
144
158
145
-
level = Value(0.0);
159
+
Alternatively, we can create a thread that queries the physical sensor every few seconds. We first remove the `readproperty` argument from our Property.
146
160
147
-
sensor.add_property(
148
-
Property(
149
-
sensor,
150
-
'level',
151
-
level,
152
-
metadata={
153
-
'@type': 'LevelProperty',
154
-
'title': 'Humidity',
155
-
'type': 'number',
156
-
'description': 'The current humidity in %',
157
-
'minimum': 0,
158
-
'maximum': 100,
159
-
'unit': 'percent',
160
-
'readOnly': True,
161
-
}))
161
+
.. code:: python
162
+
163
+
sensor.add_property(
164
+
Property(
165
+
sensor,
166
+
'level',
167
+
Value(0.0),
168
+
metadata={
169
+
'@type': 'LevelProperty',
170
+
'title': 'Humidity',
171
+
'type': 'number',
172
+
'description': 'The current humidity in %',
173
+
'minimum': 0,
174
+
'maximum': 100,
175
+
'unit': 'percent',
176
+
'readOnly': True,
177
+
}))
162
178
163
-
Now we have a sensor that constantly reports 0%. To make it usable, we need a thread or some kind of input when the sensor has a new reading available. For this purpose we start a thread that queries the physical sensor every few seconds. For our purposes, it just calls a fake method.
179
+
We then create our looping function to periodically query the sensor and set the property value.
164
180
165
181
.. code:: python
166
182
@@ -171,13 +187,12 @@ Now we have a sensor that constantly reports 0%. To make it usable, we need a th
logging.debug('setting new humidity level: %s', new_level)
176
-
self.level.notify_of_external_update(new_level)
177
192
except CancelledError:
178
193
pass
179
194
180
-
This will update our ``Value`` object with the sensor readings via the ``self.level.notify_of_external_update(read_from_gpio())`` call. The ``Value`` object now notifies the property and the thing that the value has changed, which in turn notifies all websocket listeners.
195
+
This will update our ``Value`` object with the sensor readings via the ``sensor.properties["level"].value.set(self.read_from_gpio())`` call. The ``Value`` object now notifies the property and the thing that the value has changed, which in turn notifies all websocket listeners.
0 commit comments