-
Notifications
You must be signed in to change notification settings - Fork 5
/
DigOut.js
193 lines (184 loc) · 7.28 KB
/
DigOut.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
module.exports = function(RED) {
const DigOutPID = '/sys/kernel/dout_drv/DOUT_DATA'
const do1 = 1;
const do2 = 2;
const do3 = 4;
const do4 = 8;
const TRUE = true;
const FALSE= false;
const waitingTime = 1; // Waiting time (in ms) for a new attempt to write a digital output (if file is blocked)
const maxTime = 5000; // Max time (in ms) for the writing process
global.monitor = false;
// Delay if several outputs are to be written simultaneously
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
// Write a Digital Output
function writeDO(node, msg, value, fs, digout, starttime) {
var currentTime = Date.now();
var DO;
// Check which digital output should be set to generate appropriate messages
switch (digout) {
case do1:
DO = "DO 1";
break;
case do2:
DO = "DO 2";
break;
case do3:
DO = "DO 3";
break;
case do4:
DO = "DO 4";
break;
default:
DO = "NOT EXISTING DO"
break;
}
if((currentTime - starttime) <= maxTime) {
if (monitor === false) {
monitor = true;
// Read the state of the Output's from file
fs.readFile(DigOutPID,function(err, data) {
if (err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
} else {
console.log("Read " + DO + " on CC100 was successful.");
console.log("Raw Data: " + data);
value = Number(data);
// Write the Digital Output (value) if needed
console.log("Writing " + DO + " on CC100.");
node.status({fill:"green",shape:"ring",text:"Writing"});
if (msg.payload === true & ((value & digout) != digout)){
value = (value + digout);
msg.payload = value;
} else if (msg.payload === false & ((value & digout) === digout)) {
value = (value - digout);
msg.payload = value;
} else {
msg.payload = value;
}
}
// Write the Digital Output to file
const fs = require('fs');
fs.writeFile(DigOutPID, String(msg.payload), function(err) {
if(err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
} else {
console.log("Write " + DO + " on CC100 was successful.");
node.status({fill:"green",shape:"ring",text:"OK"});
}
});
if (msg.payload & (value & digout)) {
msg.payload=TRUE
} else {
msg.payload=FALSE
}
monitor = false;
node.send(msg);
});
} else {
console.log("Write " + DO + " on CC100 was not possible."); // Writing blocked - waiting to write DO on CC100
node.status({fill:"yellow",shape:"ring",text:"Blocked"});
delay(waitingTime).then(() => {
writeDO(node, msg, value, fs, digout, starttime);
});
}
} else {
console.log("Writing " + DO + " on CC100 took too long.");
node.status({fill:"red",shape:"ring",text:"Took too long"});
}
}
// Set Digital Output 1
function setDO1(config) {
RED.nodes.createNode(this,config);
var node = this;
var value;
const fs = require('fs');
node.on('input', function(msg) {
var starttime = Date.now();
writeDO(node, msg, value, fs, do1, starttime);
});
}
RED.nodes.registerType("Set-DO1",setDO1);
// Set Digital Output 2
function setDO2(config) {
RED.nodes.createNode(this,config);
var node = this;
//var parts;
var value;
const fs = require('fs');
node.on('input', function(msg) {
var starttime = Date.now();
writeDO(node, msg, value, fs, do2, starttime);
});
}
RED.nodes.registerType("Set-DO2",setDO2);
// Set Digital Output 3
function setDO3(config) {
RED.nodes.createNode(this,config);
var node = this;
//var parts;
var value;
const fs = require('fs');
node.on('input', function(msg) {
var starttime = Date.now();
writeDO(node, msg, value, fs, do3, starttime);
});
}
RED.nodes.registerType("Set-DO3",setDO3);
// Set Digital Output 4
function setDO4(config) {
RED.nodes.createNode(this,config);
var node = this;
//var parts;
var value;
const fs = require('fs');
node.on('input', function(msg) {
var starttime = Date.now();
writeDO(node, msg, value, fs, do4, starttime);
});
}
RED.nodes.registerType("Set-DO4",setDO4);
// Read all Digital Outputs at once from file
function readDO(config) {
RED.nodes.createNode(this,config);
var node = this;
node.on('input', function(msg) {
const fs = require('fs');
var parts;
fs.readFile(DigOutPID,function(err, data) {
if (err) {
node.status({fill:"red",shape:"ring",text:"Failed"});
return console.log(err);
} else {
console.log("Read all DO's on CC100 was successful.");
console.log("Raw Data: " + data.toString());
node.status({fill:"green",shape:"ring",text:"OK"});
parts = data.toString().split(':');
//console.log(parts);
var value = Number(parts);
var msg1 = {};
var msg2 = {};
var msg3 = {};
var msg4 = {};
var msg5 = {};
msg1.payload = ((value & do1) === do1);
msg2.payload = ((value & do2) === do2);
msg3.payload = ((value & do3) === do3);
msg4.payload = ((value & do4) === do4);
msg5.payload = {
"Dig-OUT-1":((value & do1) === do1),
"Dig-OUT-2":((value & do2) === do2),
"Dig-OUT-3":((value & do3) === do3),
"Dig-OUT-4":((value & do4) === do4),
}
node.send([msg1,msg2,msg3,msg4,msg5]);
}
});
});
}
RED.nodes.registerType("Read-DO",readDO);
};