forked from CABrouwers/node-red-contrib-telnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet-echo.js
72 lines (51 loc) · 1.78 KB
/
telnet-echo.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
module.exports = function (RED) {
function echoNode(config) {
RED.nodes.createNode(this, config);
var node = this;
var server = RED.nodes.getNode(config.connection);
var broadcasting = null;
var statusBroadcasting = null;
var statusVal = { fill: "grey", shape: "ring", text: "idle" };
var statusResetter = null;
if (server) {
var engine = server.engine
var statusSender = server.getStatusBroadcaster();
broadcasting = engine.echoString(
(pl) => {
if (pl) {
let msg = { payload: pl };
setStatus(true);
node.send(msg);
}
})
statusBroadcasting = statusSender.thenAgain(
(st) => {
statusVal = Object.assign(statusVal, st);
this.status(statusVal);
})
}
const setStatus = (state) => {
clearTimeout(statusResetter);
if (state) {
statusVal['shape'] = "dot";
this.status(statusVal);
statusResetter = setTimeout(() => { setStatus(false) }, 500)
}
else {
statusVal['shape'] = "ring";
this.status(statusVal);
}
}
setStatus(false);
node.on('input', function (msg, send, done) {
send(msg);
done()
})
node.on('close', function () {
if (broadcasting) { broadcasting.terminate(); }
if (statusBroadcasting) { broadcstatusBroadcastingasting.resolve(); }
});
}
RED.nodes.registerType("telnet-echo", echoNode, {
})
}