-
Notifications
You must be signed in to change notification settings - Fork 7
/
eiscp-out.js
146 lines (130 loc) · 5.33 KB
/
eiscp-out.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
/**
* Created by aborovsky on 27.08.2015.
*/
var util = require('util'),
eiscp = require('eiscp');
module.exports = function (RED) {
/**
* ====== Globalcache-out =======================
* Sends outgoing EISCP device from
* messages received via node-red flows
* =======================================
*/
function EISCPOut(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
this.ctrl = RED.nodes.getNode(config.controller);
var node = this;
this.currentError = null;
this.on("input", function (msg) {
node.log('eiscpout.onInput msg[' + util.inspect(msg) + ']');
if (!(msg && (msg.hasOwnProperty('payload') || msg.hasOwnProperty('raw')))) return;
var payload = msg.payload;
var sendRaw = false;
if (msg.hasOwnProperty('raw') && msg.raw) {
sendRaw = true;
payload = msg.raw;
}
if (typeof(msg.payload) === "object") {
payload = msg.payload;
} else if (typeof(msg.payload) === "string") {
try {
payload = JSON.parse(msg.payload);
} catch (e) {
payload = msg.payload.toString();
}
}
if (payload == null) {
node.log('eiscpout.onInput: illegal msg.payload!');
return;
}
node.send(payload, sendRaw, function (err) {
if (err) {
node.error('send error: ' + err);
}
if (typeof(msg.cb) === 'function')
msg.cb(err);
});
});
this.on("close", function () {
node.log('eiscpOut.close');
});
node.status({fill: "yellow", shape: "dot", text: "inactive"});
function nodeStatusConnected() {
node.status({fill: "green", shape: "dot", text: "connected"});
}
function nodeStatusDisconnected() {
if(node.currentError == null){
node.status({fill: "red", shape: "dot", text: "disconnected"});
} else {
nodeStatusDisconnectedWithError(node.currentError);
}
}
function nodeStatusDisconnectedWithError(error) {
var statusText = "disconnected (" + error + ")";
node.status({fill: "red", shape: "dot", text: statusText});
}
function nodeStatusConnecting() {
node.status({fill: "green", shape: "ring", text: "connecting"});
}
function nodeStatusDisconnectedWithError(error) {
var statusText = "disconnected (" + error.code + ")";
node.status({fill: "red", shape: "dot", text: statusText});
}
this.send = function (data, sendRaw, callback) {
node.log('send data[' + data + ']');
// init a new one-off connection from the effectively singleton EISCPController
// there seems to be no way to reuse the outgoing conn in adreek/node-eiscpjs
this.ctrl.initializeEISCPConnection(function (connection) {
function onConnect() {
if (sendRaw) {
connection.raw(data.toString(), function (err) {
callback && callback(err);
});
} else {
connection.command(data.toString(), function (err) {
callback && callback(err);
});
}
}
if (connection.is_connected)
nodeStatusConnected();
else
nodeStatusDisconnected();
connection.removeListener('connecting', nodeStatusConnecting);
connection.on('connecting', nodeStatusConnecting);
connection.removeListener('connect', nodeStatusConnected);
connection.on('connect', nodeStatusConnected);
connection.removeListener('close', nodeStatusDisconnected);
connection.on('close', nodeStatusDisconnected);
connection.on('error', function (err) {
node.currentError = err;
nodeStatusDisconnectedWithError(err);
});
try {
node.log("send: " + JSON.stringify(data));
if (connection.is_connected)
if (sendRaw) {
connection.raw(data.toString(), function (err) {
callback && callback(err);
});
} else {
connection.command(data.toString(), function (err) {
callback && callback(err);
});
}
else {
connection.removeListener('connect', onConnect);
connection.once('connect', onConnect);
}
}
catch (err) {
node.error('error calling send: ' + err);
callback(err);
}
});
}
}
//
RED.nodes.registerType("eiscp-out", EISCPOut);
}