webthing
Implementation of an HTTP Web Thing.
Using
If you're using Cargo
, just add webthing
to your Cargo.toml
:
[]
= "0.5"
Example
In this example we will set up a dimmable light and a humidity sensor (both using fake data, of course). Both working examples can be found in here.
Dimmable Light
Imagine you have a dimmable light that you want to expose via the web of things API. The light can be turned on/off and the brightness can be set from 0% to 100%. Besides the name, description, and type, a Light
is required to expose two properties:
on
: the state of the light, whether it is turned on or off- Setting this property via a
PUT {"on": true/false}
call to the REST API toggles the light.
- Setting this property via a
brightness
: the brightness level of the light from 0-100%- Setting this property via a PUT call to the REST API sets the brightness level of this light.
First we create a new Thing:
let mut light = new;
Now we can add the required properties.
The on
property reports and sets the on/off state of the light. For our purposes, we just want to log the new state if the light is switched on/off.
;
let on_description = json!;
let on_description = on_description.as_object.unwrap.clone;
thing.add_property;
The brightness
property reports the brightness level of the light and sets the level. Like before, instead of actually setting the level of a light, we just log the level.
;
let brightness_description = json!;
let brightness_description = brightness_description.as_object.unwrap.clone;
thing.add_property;
Now we can add our newly created thing to the server and start it:
let mut things: = Vec new;
things.push
This will start the server, making the light available via the WoT REST API and announcing it as a discoverable resource on your local network via mDNS.
Sensor
Let's now also connect a humidity sensor to the server we set up for our light.
A MultiLevelSensor
(a sensor that returns a level instead of just on/off) has one required property (besides the name, type, and optional description): level
. We want to monitor this property and get notified if the value changes.
First we create a new Thing:
let mut thing = new;
Then we create and add the appropriate property:
-
level
: tells us what the sensor is actually reading- 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 utilizing a readOnly value.
let level_description = json!; let level_description = level_description.as_object.unwrap.clone; thing.add_property;
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.
let sensor = new );
let cloned = sensor.clone;
spawn;
This will update our property with random sensor readings. The new property value is then sent to all websocket listeners.