UNPKG

node-red-contrib-jinhub

Version:

jinhub allows your device exchange data in the cloud throught websocket

141 lines (133 loc) 5.24 kB
/** * * This node red node write the data to jinpage throught websocket interface. * * Copyright 2020 jinyistudio / bill lee * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * **/ module.exports = function(RED) { "use strict"; /** Jinhub in Node **/ function JinhubInNode(config) { RED.nodes.createNode(this,config); this.config = config; this.txrx = false; var node = this; // p2p // type:'event',event:'things_rawdata',params:{topic:'',payload:{'test':1}}; function inputlistener(msg) { // jinhub // "prop":"jinhub","etype":"runFunction","value":[{"topic":"","payload":{"led":27}}], var b1 = Boolean(msg.hasOwnProperty("etype")); var b2 = Boolean(msg.hasOwnProperty("event")); if(!b1 && !b2) { node.status({fill:"red",shape:"ring",text:"*etype,event"}); return; } if(b1) { // etype if(msg.etype != 'runFunction') { node.status({fill:"red",shape:"ring",text:"*runFunction"}); return; } } else { // event if(msg.event != 'things_rawdata') { node.status({fill:"red",shape:"ring",text:"*things_rawdata"}); return; } } if(!msg.hasOwnProperty("value")) { if(!msg.hasOwnProperty("params")) { node.status({fill:"red",shape:"ring",text:"*value,params"}); return; } // p2p var hub = msg.params; } else { var hub = msg.value[0]; } var topic1=''; if(hub.hasOwnProperty('topic')) { topic1 = hub.topic; } if(hub.hasOwnProperty('payload')) { var data = hub.payload; if(data.hasOwnProperty(node.config.prop)) { node.send({topic:topic1, payload:data[node.config.prop]}); this.status({fill:"green", shape:node.txrx?"dot":"ring", text:"ok"}); node.txrx = !node.txrx; } } else { node.status({fill:"red",shape:"ring",text:"*payload"}); } } node.on("input", inputlistener); this.status({fill:"green",shape:"ring",text:"ready"}); node.on("close", function(done) { if (node.retry) { clearTimeout(node.retry); } node.status({fill:"grey",shape:"ring",text:"closed"}); done(); }); } RED.nodes.registerType("jinhub-in",JinhubInNode); /**********************/ /** Jinhub out Node **/ /**********************/ function JinhubOutNode(config) { RED.nodes.createNode(this,config); this.config = config; this.txrx = false; var node = this; // p2p, jinhub // type:'event',event:'things_rawdata',params:{topic:'B12345',payload:{'test':1,'abc':1}}; function inputlistener(msg) { if(!msg.hasOwnProperty("payload")) { node.status({fill:"red",shape:"ring",text:"*payload"}); return; } var topic1 = node.config.topic; var data1={}; data1[node.config.prop] = msg.payload; node.send({type:"event",event:"things_rawdata",params:{slot:node.config.slot, topic:topic1, payload:data1}}); this.status({fill:"green",shape:node.txrx?"dot":"ring", text:"ok"}) node.txrx = !node.txrx; } node.on("input", inputlistener); this.status({fill:"green",shape:"ring",text:"ready"}); node.on("close", function(done) { if (node.retry) { clearTimeout(node.retry); } node.status({fill:"grey",shape:"ring",text:"closed"}); done(); }); } RED.nodes.registerType("jinhub-out",JinhubOutNode); }