-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheiscp-controller.js
61 lines (56 loc) · 2.41 KB
/
eiscp-controller.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
/**
* 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.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);
}