| 225 | } |
| 226 | } |
| 227 | watch(service, instance) { |
| 228 | let txt = instance.txt; |
| 229 | let name = instance.name; |
| 230 | for (let j = 0; j < this.things.length; j++) { |
| 231 | const thing = this.things[j]; |
| 232 | let properties = Object.keys(thing.description.properties); |
| 233 | let bools = properties.filter(prop => { |
| 234 | let isControlled = false; |
| 235 | for (let item of thing.instance.controllers) { |
| 236 | if ((name === item.remote) && (prop === item.property)) isControlled = true; |
| 237 | } |
| 238 | return (thing.description.properties[prop].type === "boolean") && isControlled; |
| 239 | }); |
| 240 | let item, equal, key, value; |
| 241 | for (let i=1; i<txt.length; i++) { // first item is description url |
| 242 | item = instance.txt[i]; |
| 243 | equal = item.indexOf("="); |
| 244 | key = item.substring(0, equal); |
| 245 | value = item.substring(equal+1); |
| 246 | for (let k = 0; k < thing.instance.controllers.length; k++) { |
| 247 | const item = thing.instance.controllers[k]; |
| 248 | if ((name === item.remote) && (key === item.txt)){ |
| 249 | let property = item.property; |
| 250 | let type = thing.description.properties[property]; |
| 251 | if (type) { |
| 252 | switch(type.type) { |
| 253 | case "boolean": |
| 254 | value = true; |
| 255 | bools.splice(bools.indexOf(property), 1); |
| 256 | break; |
| 257 | case "number": |
| 258 | case "integer": |
| 259 | value = ("number" === type.type) ? parseFloat(value) : parseInt(value); |
| 260 | if ((undefined !== type.minimum) && (value < type.minimum)) |
| 261 | value = type.minimum; |
| 262 | if ((undefined !== type.maximum) && (value > type.maximum)) |
| 263 | value = type.maximum; |
| 264 | break; |
| 265 | case "object": |
| 266 | case "array": |
| 267 | value = JSON.parse(value); |
| 268 | break; |
| 269 | } |
| 270 | thing.instance[property] = value; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | for (let prop of bools) |
| 276 | thing.instance[prop] = false; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | export {WebThings, WebThing}; |