Wrapping an onoff deivce with a webthing (rust)

I’ve managed to develop a webthing (in rust) for my amplifier (controlled via the serial port).

The webthing have four properties:
volume: -80 … 30
mode: “Stereo”, “Mono”, “Cinema” …
input: “TV”, “Audio”
on: true/false (OnOffProperty)

  1. Is it possible to have the webthing react to a change of “on” to true by something like:
    a) turn on the power (by using an X10 (CM11) “OnOffDevice”)
    b) wait a short while
    c) send the relevant serial commands to the receiver

  2. Does the “iot gateway” GUI listen to events from the web thing so that I can for example change the mode to “Cinema” when the user changes input to “TV”? If so, what kind of events?

Thanks for an awesome application!

The power switch device is just semantics for the API consumer, so they know this is the property to toggle the power state. Other functionality should either be done inside your webthing (and you can do anything there) or may be a rule (like triggering a pulse when turning on power that then executes the ecations to send those serial commands).

Afaik it uses the WebSocket part of the WoT protocol. If you’re using the webthing crate that should be implemented for you. The relevant messages are then sent to the gateway whenever you update a property, for example.

After that the gateway has a rules system that would let you set up a rule that does the action you describe (if prop 1 has state A, set prop 2 to state I).

Tried to clarify my question by updating my first question.

Regarding the first question. Is there an API that can be called from my webthing that can be used to control other devices?
If that’s possible then I could control the outlet power of my amplifier from my webthing. It would be awesome if there was some way to “hide” that device so that the power of the amplifier could not be changed manually without going through my webthing.

Thanks for the input regarding the second question. Will try to implement the logic in my webthing (I’m using the webthing crate) and see what happens :slight_smile:

Sure, you can be a consumer of the Webthing API yourself.

You can’t hide webthings from another webthing, only the webthing can hide itself… And likely the solution here would be to not add it to the gateway, however:

It’s probably better to leave logic like this to the layer above, i.e. the gateway. That way neither of the two things have to know about each other. (Which was also my suggestion for your second question.)

Thanks for the tips regarding alternative thinking.
Gonna check out what’s possible with the rule engine!

In order for a webthing to also control other things, you’d need to interact with the gateway’s REST API, which is definitely possible.

You could also use rules (and perhaps the pulse adapter to allow for your delay), if your webthing exposed a propert, which when triggered sent the serial commands to the receiver.

It really depends on how transparent you want things to be.

You can do some rather interesting things by creating webthings. See http://www.twobraids.com/ for some interesting examples that can be done with some more interesting custom web things.

A webthing can create arbitrary events. For example, push buttons might create a “pressed” event. The event by itself doesn’t do anything, but you can create a rule which uses the event as a trigger to perform some other action.

1 Like