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