Node.js implementation of a W3C WoT web thing
This is a work-in-progress pure Node.js replacement for the webthing-node library which conforms to W3C WoT Thing Description 1.1. It will implement the HTTP Basic Profile, HTTP SSE Profile and the Web Thing Protocol (WebSocket sub-protocol).
To run an example web thing representing a lamp:
$ npm run example
Import the webthing library:
import WebThing from '../webthing.js';Create a partial Thing Description without Forms or security definitions:
const partialTD = {
"title": "My Lamp",
"description": "A web connected lamp",
"properties": {
"on": {
"type": "boolean",
"title": "On/Off",
"description": "Whether the lamp is turned on",
},
"level" : {
"type": "integer",
"title": "Brightness",
"description": "The level of light from 0-100",
"unit": "percent",
"minimum" : 0,
"maximum" : 100,
}
},
"actions": {
"fade": {
"title": "Fade",
"description": "Fade the lamp to a given level",
"synchronous": false,
"input": {
"type": "object",
"properties": {
"level": {
"title": "Brightness",
"type": "integer",
"minimum": 0,
"maximum": 100,
"unit": "percent"
},
"duration": {
"title": "Duration",
"type": "integer",
"minimum": 0,
"unit": "milliseconds"
}
}
},
}
},
"events": {
"overheated": {
"title": "Overheated",
"data": {
"type": "number",
"unit": "degree celsius"
},
"description": "The lamp has exceeded its safe operating temperature",
}
},
}Instantiate a Web Thing from the partial Thing Description:
const thing = new WebThing(partialTD);Set property handlers:
thing.setPropertyReadHandler('on', async function() {
return true;
});
thing.setPropertyReadHandler('level', async function() {
return 50;
});Expose to the Thing to the Web of Things by starting its web server, providing a port number as an argument:
thing.expose(8080);