node-red-node-jy-archer
Version:
jy-archer allows you to exchange data with Archer Html Page
155 lines (129 loc) • 5.08 kB
JavaScript
/**
*
**/
module.exports = function(RED) {
"use strict";
function ArcheroutNode(config) {
RED.nodes.createNode(this,config);
this.config = config;
this.status({fill:"green",shape:"dot",text:"OK"});
var node = this;
function inputlistener(msg) {
var vval = node.config.vval || msg.payload;
var vtype = Number(node.config.vtype);
if(vtype===1) // number
{
if(isNaN(vval)) {
this.status({fill:"red",shape:"dot",text:'err.'+vval});
return;
}
}
else if(vtype===2) // text
{
vval="'"+vval+"'";
}
else // boolean
{
if (vval === "true" || vval===1 || vval===true) {
vval='true';
}
if (vval === "false" || vval===0 || vval===false) {
vval='false';
}
}
node.send({payload:{etype:'eval',
prop:"graphic.setValue('"+node.config.vname+"',"+vval+");",
value:[]}});
}
node.on("input", inputlistener);
node.on("close", function(done) {
if (node.retry) { clearTimeout(node.retry); }
node.status({fill:"red",shape:"ring",text:"closed"});
done();
});
}
RED.nodes.registerType("jy-archer out",ArcheroutNode);
// msg.payload [string] = "{"etype":"archer","prop":"on","value":1}"
function ArcherinNode(config) {
RED.nodes.createNode(this,config);
this.config = config;
this.status({fill:"green",shape:"dot",text:"OK"});
var node = this;
function inputlistener(msg) {
if(!(typeof msg.payload==='string'))
{
this.status({fill:"red",shape:"dot",text:'er.not string'});
return;
}
var argu=JSON.parse(msg.payload);
if(!argu.hasOwnProperty("etype"))
{
this.status({fill:"red",shape:"dot",text:'er.no etype'});
return;
}
if(argu.etype!='archer')
{
this.status({fill:"red",shape:"dot",text:'er.no archer'});
return;
}
if(!argu.hasOwnProperty("prop"))
{
this.status({fill:"red",shape:"dot",text:'er.no prop'});
return;
}
var prop=argu.prop;
if(prop===''||node.config.prop!=prop) return;
if(!argu.hasOwnProperty("value"))
{
this.status({fill:"red",shape:"dot",text:'er.no value'});
return;
}
node.send({topic:node.config.topic,payload:argu.value});
}
node.on("input", inputlistener);
node.on("close", function(done) {
if (node.retry) { clearTimeout(node.retry); }
node.status({fill:"red",shape:"ring",text:"closed"});
done();
});
}
RED.nodes.registerType("jy-archer in",ArcherinNode);
// send eval to archer page
function ArcherevalNode(config) {
RED.nodes.createNode(this,config);
this.config = config;
this.status({fill:"green",shape:"dot",text:"OK"});
var node = this;
function inputlistener(msg) {
var prop = node.config.prop;
var value = node.config.value||msg.payload;
node.send({payload:{etype:'eval', prop:prop, value:value}});
}
node.on("input", inputlistener);
node.on("close", function(done) {
if (node.retry) { clearTimeout(node.retry); }
node.status({fill:"red",shape:"ring",text:"closed"});
done();
});
}
RED.nodes.registerType("jy-archer eval",ArcherevalNode);
// run function of the archer page
function ArcherrunNode(config) {
RED.nodes.createNode(this,config);
this.config = config;
this.status({fill:"green",shape:"dot",text:"OK"});
var node = this;
function inputlistener(msg) {
var prop = node.config.prop;
var value = node.config.value||msg.payload;
node.send({payload:{etype:'runFunction', prop:prop, value:value}});
}
node.on("input", inputlistener);
node.on("close", function(done) {
if (node.retry) { clearTimeout(node.retry); }
node.status({fill:"red",shape:"ring",text:"closed"});
done();
});
}
RED.nodes.registerType("jy-archer run",ArcherrunNode);
}