-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebthing.js
More file actions
52 lines (43 loc) · 1.1 KB
/
webthing.js
File metadata and controls
52 lines (43 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import express from 'express';
const app = express();
/**
* Web Thing.
*
* Represents a W3C WoT Web Thing.
*/
class WebThing {
propertyReadHandlers = {};
constructor(partialTD) {
// TODO: Parse and validate TD.
this.partialTD = partialTD;
}
getThingDescription() {
// TODO: Add forms etc.
return this.partialTD;
}
setPropertyReadHandler(name, handler) {
this.propertyReadHandlers[name] = handler;
}
readProperty(name) {
if(!this.propertyReadHandlers[name]) {
console.error('No property read handler for the property ' + name);
throw new Error();
} else {
return this.propertyReadHandlers[name]();
}
}
expose(port) {
app.get('/', (request, response) => {
response.json(this.getThingDescription());
});
app.get('/properties/:name', async (request, response) => {
const name = request.params.name;
const value = await this.readProperty(name);
response.status(200).json(value);
});
app.listen(port, () => {
console.log(`Web Thing being served on port ${port}`)
});
}
}
export default WebThing;