-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstratoncmd.js
129 lines (105 loc) · 4.01 KB
/
stratoncmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
module.exports = function (RED) {
function getfullVarName(vt,v) {
if (vt === 'app' || vt === 'sys' || vt === 'oem') {
return vt + "." + v;
}
return v;
}
function StratonCmdNode(config) {
RED.nodes.createNode(this,config);
var node = this;
// Store local copies of the node configuration (as defined in the .html)
node.command = config.command;
node.variable = getfullVarName(config.variableType,config.variable);
//node.variableType = getfullVarName(config.variableType);
//node.trace("var=" + config.variable + config.variableType);
//node.trace("StratonCmdNode = " + config.command);
var flowContext = node.context().flow;
var nodeContext = this.context();
var globalContext = this.context().global;
function stratonGetStatusWS() {
var strCmd = "{\"cmd\":\"status2\"}";
msg = {
payload: JSON.parse(strCmd)
}
node.send(msg);
}
function stratonGetVarListWS() {
var strCmd = "{\"cmd\":\"list2\"}";
msg = {
payload: JSON.parse(strCmd)
}
node.send(msg);
}
function stratonReadValueWS(varName) {
var strCmd = "{\"cmd\":\"read2\",\"data\":[" + "{\"name\":\"" + varName + "\"}" + "]}";
msg = {
payload: JSON.parse(strCmd)
}
node.send(msg);
}
function stratonWriteValueWS(varName, varValue) {
var strCmd = '{\"cmd\":\"write2\",\"data\":[';
strCmd += '{\"name\":\"';
strCmd += varName;
strCmd += '\"';
strCmd += ',';
strCmd += '\"value\":\"';
strCmd += varValue;
strCmd += '\"}';
strCmd += ']}';
msg = {
payload: JSON.parse(strCmd)
}
node.send(msg);
}
// We received something from the Node input
node.on('input', function (msg) {
var strValue = "";
var value = RED.util.getMessageProperty(msg,"payload");
if (value !== undefined) {
if (typeof value === "string") { //payload is a string
strValue = value;
}
else if (typeof value === "number") { //payload is an integer
strValue = value.toString(); // we make it a string
}
}
//node.trace("var=" + this.variable);
if(this.command==="status2") {
stratonGetStatusWS();
} else if (this.command === "list2") {
stratonGetVarListWS();
} else if (this.command === "write2") {
if (this.variable !== undefined && this.variable !== '') {
stratonWriteValueWS(this.variable, strValue);
}
else {
node.status({
fill: "red",
shape: "ring",
text: "Variable not specified"
});
}
} else if (this.command === "read2") {
if (this.variable !== undefined && this.variable !== '') {
stratonReadValueWS(this.variable);
}
else {
node.status({
fill: "red",
shape: "ring",
text: "Variable not specified"
});
}
} else {
node.status({
fill: "red",
shape: "ring",
text: "Unknown command"
});
}
});
}
RED.nodes.registerType("straton command", StratonCmdNode);
}