UNPKG

node-red-contrib-mysensors

Version:

Mysensors related nodes, for decoding / encoding mysensors serial protocol and MQTT topic, and wrapping arbitrary messages into mysensors compatible messages

92 lines (91 loc) 3.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.NoderedStorage = void 0; class NoderedStorage { constructor(context, storageKey, store = 'default') { this.context = context; this.storageKey = storageKey; this.store = store; } async getNodes() { const data = await this.context?.get(this.storageKey, this.store); return (data || {}); } async setNode(nodeId, data) { const nodes = await this.getNodes(); const node = nodes?.[nodeId] || { batteryLevel: -1, nodeId: nodeId, label: '', lastHeard: new Date(), lastRestart: new Date(), parentId: -1, sensors: [], sketchName: '', sketchVersion: '', used: 1, }; nodes[nodeId] = { ...node, ...data }; return this.context?.set(this.storageKey, nodes, this.store); } async setChild(nodeId, childId, data) { const children = (await this.getNodes())?.[nodeId]?.sensors; if (!children) { return; } const child = children.find((item) => item.childId === childId) || { childId, description: '', lastHeard: new Date(), nodeId: nodeId, sType: 0, }; const newSensors = children.filter((item) => item.childId !== childId).concat([{ ...child, ...data, }]); this.setNode(nodeId, { sensors: newSensors }); } async nodeHeard(nodeId) { await this.setNode(nodeId, { lastHeard: new Date() }); } async sketchName(nodeId, name) { await this.setNode(nodeId, { sketchName: name }); } async sketchVersion(nodeId, version) { await this.setNode(nodeId, { sketchVersion: version }); } async getNodeList() { return Object.values(await this.getNodes()).filter((item) => item.used); } async getFreeNodeId() { const freeNode = Object.values(await this.getNodes()).find((item) => !item.used); return freeNode?.nodeId; } async setParent(nodeId, last) { await this.setNode(nodeId, { parentId: last }); } async setBatteryLevel(nodeId, batterylevel) { await this.setNode(nodeId, { batteryLevel: batterylevel }); } async getChild(nodeId, childId) { const nodes = await this.getNodes(); return nodes[nodeId]?.sensors?.find((item) => item.childId === childId) || { childId, nodeId, description: '', lastHeard: new Date(), sType: 0 }; } async childHeard(nodeId, childId) { return this.setChild(nodeId, childId, { lastHeard: new Date() }); } async child(_nodeId, _childId, _type, _description) { await this.setChild(_nodeId, _childId, { sType: _type, description: _description }); } } exports.NoderedStorage = NoderedStorage;