forked from Controlhaus/lutron-integration-protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
231 lines (186 loc) · 4.71 KB
/
main.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
const argv = require('yargs').argv;
const repl = require('repl');
const Telnet = require('telnet-client')
var serviceName = argv.serviceName || 'Lutron';
var ipAddress = argv.ipAddress;
var port = argv.port || 23;
var login = argv.login || 'lutron';
var password = argv.password || 'integration';
var heartbeatInterval = argv.heartbeatInterval || 10000;
var client;
var heartbeat;
var reconnectTimeout;
var doNotReconnect;
var retrying;
/* Startup */
// connect();
/* Command Line Interface */
// repl.start({ prompt: '> ', eval: evaulateCliCommands });
function evaulateCliCommands(command, context, filename, callback) {
processCommand(command);
callback(null, 'OK');
}
function log(message) {
console.log(serviceName + ': ' + message);
}
/* Catch Connect Client Messages */
process.on("message", (data) => {
processCommand(data);
});
function sendResponse(response) {
log(response);
//process.send() only exists if the app is started as a child process
if (typeof process.send === 'function') {
process.send(response);
}
}
/* Create Device Commands */
function processCommand(command) {
switch (command) {
case 'connect\n':
connect();
break;
case 'close\n':
close();
break;
default:
sendToSocket(command);
break;
}
}
/* Parse Device Responses */
function parseResponse(response) {
log('Parsing response ' + response);
responseArray = response.toString().split('\r\n');
for (var i = 0; i < responseArray.length; i++) {
if (responseArray[i] != 'QNET> ' && responseArray[i].length > 0 && isAlive === true) {
sendResponse(responseArray[i]);
}
}
isAlive = true;
}
/* Socket Functions */
async function sendToSocket(message) {
if (client) {
log('Sending to socket: ' + message);
client.send(message + '\r', (error, data) => {
// if (error) {
// log("sendToSocket result error: ", error)
// }
// if (data) {
// log("sendToSocket result data: ", data)
// }
});
} else {
log('Cannot send to undefined socket.');
}
}
async function connect(params) {
client = new Telnet();
if (params.port && params.host) {
log('Connecting with ip address: ' + params.host + ' and port: ' + params.port + ' and login: ' + login);
var options = {
debug: true,
host: params.host,
port: params.port,
negotiationMandatory: true,
timeout: 0,
loginPrompt: 'login: ',
passwordPrompt: 'password: ',
username: login + '\r',
password: password + '\r',
shellPrompt: 'QNET>',
};
client.connect(options).catch((err) => {
errorEventHandler(err);
});
client.on('failedlogin', function () {
failedLoginEventHandler();
});
client.on('connect', function () {
log('logging in');
});
client.on('ready', function () {
log('logged in');
connectEventHandler();
});
client.on('data', data => {
parseResponse(data);
});
client.on('timeout', function () {
// timeoutEventHandler();
});
client.on('close', function () {
// closeEventHandler();
});
} else {
log('Cannot connect with ip address: ' + params.host + ' and port: ' + params.port);
}
}
function close() {
if (client) {
if (reconnectTimeout) {
clearTimeout(reconnectTimeout);
}
doNotReconnect = true;
client.end();
} else {
log('Cannot close. Socket undefined. ');
}
}
/* Socket Event Handlers */
function connectEventHandler() {
log('Socket connected.');
sendResponse('catch-service-connected');
retrying = false;
clearInterval(reconnectTimeout);
startHearbeat();
}
function startHearbeat() {
isAlive = true;
heartbeat = setInterval(checkHeartbeat, heartbeatInterval);
}
function checkHeartbeat() {
if (isAlive === true) {
isAlive = false;
sendToSocket('gettime');
return;
}
log('Heartbeat timed out.');
doNotReconnect = false;
client.destroy();
}
function failedLoginEventHandler() {
sendResponse('catch-service-login-failed');
log('Failed Login.');
doNotReconnect = true;
client.destroy();
}
function timeoutEventHandler() {
log('Socket timeout event.');
doNotReconnect = false;
client.destroy();
}
function errorEventHandler(err) {
log('Socket error: ' + err);
doNotReconnect = false;
client.destroy();
}
function closeEventHandler() {
if (heartbeat) {
clearInterval(heartbeat);
}
if (reconnectTimeout) {
clearInterval(reconnectTimeout);
}
sendResponse('catch-service-disconnected');
log('Socket closed.');
if (!retrying && !doNotReconnect) {
retrying = true;
log('Reconnecting... ');
}
if (!doNotReconnect) {
reconnectTimeout = setTimeout(connect.bind(this), 10000);
}
}
exports.connect = connect