forked from estbeetoo/node-red-contrib-eiscp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eiscp.js
233 lines (207 loc) · 8.7 KB
/
eiscp.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/**
* Created by aborovsky on 27.08.2015.
*/
var util = require('util'),
eiscp = require('eiscp');
module.exports = function (RED) {
/**
* ====== Globalcache-controller ================
* Holds configuration for eiscpjs host+port,
* initializes new eiscpjs connections
* =======================================
*/
function EISCPControllerNode(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
this.host = config.host;
this.port = config.port;
this.model = config.model;
this.eiscpjsconn = null;
var node = this;
//node.log("new EISCPControllerNode, config: %j", config);
/**
* Initialize an eiscpjs socket, calling the handler function
* when successfully connected, passing it the eiscpjs connection
*/
this.initializeEISCPConnection = function (handler) {
try {
if (node.eiscpjsconn) {
node.log('already configured to EISCP device at ' + config.host + ':' + config.port + ' model[' + config.model + ']');
if (handler && (typeof handler === 'function'))
handler(node.eiscpjsconn);
return node.eiscpjsconn;
}
node.log('configuring to EISCP device at ' + config.host + ':' + config.port + ' model[' + config.model + ']');
node.eiscpjsconn = eiscp;
node.eiscpjsconn.on('error', function (err) {
node.error('Error: ' + err.toString());
});
node.eiscpjsconn.connect({
host: config.host,
model: config.model,
port: config.port,
verify_commands: false
});
node.log('EISCP: successfully connected to ' + config.host + ':' + config.port + ' model[' + config.model + ']');
if (handler && (typeof handler === 'function'))
handler(node.eiscpjsconn);
return node.eiscpjsconn;
} catch (err) {
node.error('Error while connecting, cause: ' + err.toString());
}
};
this.on("close", function () {
node.log('disconnecting from eiscpjs server at ' + config.host + ':' + config.port + ' model[' + config.model + ']');
node.eiscpjsconn && node.eiscpjsconn.disconnect && node.eiscpjsconn.disconnect();
});
}
RED.nodes.registerType("eiscp-controller", EISCPControllerNode);
/**
* ====== 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.on("input", function (msg) {
node.log('eiscpout.onInput msg[' + util.inspect(msg) + ']');
if (!(msg && msg.hasOwnProperty('payload'))) return;
var payload = msg.payload;
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, 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() {
node.status({fill: "red", shape: "dot", text: "disconnected"});
}
function nodeStatusConnecting() {
node.status({fill: "green", shape: "ring", text: "connecting"});
}
this.send = function (data, 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() {
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);
try {
node.log("send: " + JSON.stringify(data));
if (connection.is_connected)
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);
/**
* ====== EISCP-IN ========================
* Handles incoming EISCP, injecting
* json into node-red flows
* =======================================
*/
function EISCPIn(config) {
RED.nodes.createNode(this, config);
this.name = config.name;
this.connection = null;
var node = this;
//node.log('new EISCPIn, config: %j', config);
var eiscpjsController = RED.nodes.getNode(config.controller);
/* ===== Node-Red events ===== */
this.on("input", function (msg) {
if (msg != null) {
}
});
this.on("close", function () {
if (node.receiveEvent && node.connection)
node.connection.removeListener('event', node.receiveEvent);
if (node.receiveStatus && node.connection)
node.connection.removeListener('status', node.receiveStatus);
});
function nodeStatusConnecting() {
node.status({fill: "green", shape: "ring", text: "connecting"});
}
function nodeStatusConnected() {
node.status({fill: "green", shape: "dot", text: "connected"});
}
function nodeStatusDisconnected() {
node.status({fill: "red", shape: "dot", text: "disconnected"});
}
node.receiveData = function (data) {
node.log('eiscp event data[' + data.toString('hex') + ']');
node.send({
topic: 'eiscp',
payload: data
});
};
// this.on("error", function(msg) {});
/* ===== eiscpjs events ===== */
eiscpjsController.initializeEISCPConnection(function (connection) {
node.connection = connection;
node.connection.removeListener('data', node.receiveData);
node.connection.on('data', node.receiveData);
if (node.connection.connected)
nodeStatusConnected();
else
nodeStatusDisconnected();
node.connection.removeListener('connecting', nodeStatusConnecting);
node.connection.on('connecting', nodeStatusConnecting);
node.connection.removeListener('connect', nodeStatusConnected);
node.connection.on('connect', nodeStatusConnected);
node.connection.removeListener('close', nodeStatusDisconnected);
node.connection.on('close', nodeStatusDisconnected);
});
}
RED.nodes.registerType("eiscp-in", EISCPIn);
}