-
Notifications
You must be signed in to change notification settings - Fork 7
/
eiscp-in.js
93 lines (81 loc) · 3.27 KB
/
eiscp-in.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
/**
* Created by aborovsky on 27.08.2015.
*/
var util = require('util'),
eiscp = require('eiscp');
module.exports = function (RED) {
/**
* ====== 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;
this.currentError = null;
//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.currentError = null;
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});
}
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);
node.connection.removeListener('error', nodeStatusDisconnected);
node.connection.on('error', function (err) {
node.currentError = err;
nodeStatusDisconnectedWithError(err);
});
});
}
RED.nodes.registerType("eiscp-in", EISCPIn);
}