import { Red, NodeProperties, NodeStatus } from "node-red"; import { Constants } from "../constants"; export default (RED:Red, type:string) => { class GatewayDevice { protected gateway: any; protected sid: string; constructor(props:NodeProperties) { RED.nodes.createNode( this, props); this.gateway = RED.nodes.getNode(( props).gateway); this.sid = ( props).sid; ( this).status({fill:"grey", shape:"ring", text:"battery - na"}); if (this.gateway) { ( this).on('input', (msg) => { let payload = msg.payload; // Input from gateway if (payload.sid) { if (payload.sid == this.sid) { let batteryLevel = payload.batteryLevel; var status:NodeStatus = { fill: "green", shape: "dot", text: "battery - " + batteryLevel + "%" }; if (batteryLevel < 10) { status.fill = "red"; } else if (batteryLevel < 45) { status.fill = "yellow"; } ( this).status(status); ( this).send([msg]); } } // Prepare for request else { msg.sid = this.sid; msg.gateway = this.gateway; ( this).send(msg); } }); } } } RED.nodes.registerType(`${Constants.NODES_PREFIX}-${type}`, GatewayDevice); };