| 57 | } |
| 58 | |
| 59 | class WebThings { |
| 60 | constructor(mdns, dictionary = {}) { |
| 61 | if (mdns) |
| 62 | this.mdns = mdns; |
| 63 | this.things = []; |
| 64 | this.server = new Server(dictionary.server); |
| 65 | this.server.callback = this.http; |
| 66 | this.server.webThings = this; |
| 67 | } |
| 68 | close () { |
| 69 | if (this.server) |
| 70 | this.server.close(); |
| 71 | delete this.server; |
| 72 | delete this.things; |
| 73 | } |
| 74 | add(WebThing, ...args) { |
| 75 | const description = WebThing.description; |
| 76 | description.properties.controller = { |
| 77 | type: "object", |
| 78 | description: "property inputs", |
| 79 | }; |
| 80 | for (let property in description.properties) |
| 81 | description.properties[property].href = `/thng/prop/${description.name}/${property}`; |
| 82 | const thing = { |
| 83 | description, |
| 84 | instance: new WebThing(this, ...args), |
| 85 | service: { |
| 86 | name: "webthing", |
| 87 | protocol: "tcp", |
| 88 | port: 80, |
| 89 | txt: { |
| 90 | path: `/thng/desc/${description.name}`, |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | this.things.push(thing); |
| 95 | this.updateTXT(thing); |
| 96 | if (this.mdns) { |
| 97 | this.monitor(); |
| 98 | this.mdns.add(thing.service); |
| 99 | } |
| 100 | |
| 101 | return thing.instance; |
| 102 | } |
| 103 | updateTXT(thing) { |
| 104 | let changed; |
| 105 | for (let name in thing.description.properties) { |
| 106 | const property = thing.description.properties[name]; |
| 107 | if (!property.txt) continue; |
| 108 | const value = thing.instance[name]; |
| 109 | let prev = thing.service.txt[property.txt]; |
| 110 | if ("boolean" === property.type) { |
| 111 | if (undefined === prev) |
| 112 | prev = false; |
| 113 | else if ("" === prev) |
| 114 | prev = true; |
| 115 | if (value !== prev) { |
| 116 | if (value) |
nothing calls this directly
no outgoing calls
no test coverage detected