forked from kevinohara80/elkington
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
311 lines (254 loc) · 9.91 KB
/
index.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
var net = require('net');
var tls = require('tls');
var protocol = require('./lib/protocol');
var parser = require('./lib/parser');
var messaging = require('./lib/messaging');
var safereturn = require('safereturn');
var EventEmitter = require('events').EventEmitter;
// safereturn overrides
safereturn.defaultTimeout = 3000;
safereturn.onTimeout = function (wrappedCallback, oldError) {
var err = new Error('The Elk M1XEP failed to respond');
wrappedCallback(err);
}
/*********************************/
/* ElkConnection definition */
/*********************************/
var ElkConnection = function (opts) {
if (!opts) opts = {};
this.port = opts.port || 2101;
this.host = opts.host || '192.168.1.2';
this.responseTimeout = opts.responseTimeout || 3000;
this.useSecure = (opts.useSecure === true) ? true : false;
this.username = opts.username || null;
this.password = opts.password || null;
this._authorized = false;
this.defaultArmMode = (opts.defaultArmMode)
? opts.defaultArmMode.toLowerCase()
: 'away';
var that = this;
function afterConnect() {
that._connection.setEncoding('ascii');
// data event handler
that._connection.on('data', function (data) {
data = data.trim().replace('\r', '').replace('\n', '');
// check for elk auth requests
if (data == 'Username:') {
return that._connection.write(that.username + '\r\n');
} else if (data.indexOf('Password:') != -1) {
return that._connection.write(that.password + '\r\n');
} else if (data.indexOf('Elk-M1XEP: Login successful.') !== -1) {
that._authorized = true;
that.emit('connect');
}
// we are getting a weird message during auth process
if (that.useSecure && (!that._authorized || data.substring(0, 2) === '**')) {
return;
}
// if we are not using un/pw, we are connected and ready
if (!that.useSecure) that.emit('connect');
// assuming the above passes, we parse the elk message and emit
var msg = parser.parseMessage(data);
msg.time = new Date();
msg.host = that._connection.address().address;
msg.port = that.port;
msg.remotePort = that._connection.address().port;
that.emit('any', msg);
that.emit(msg.commandCode, msg);
});
// error event handler
that._connection.on('error', function (err) {
if (err.code == 'ECONNREFUSED') {
that.emit('error', 'Connection to M1XEP failed!');
} else {
that.emit('error', err.code);
}
});
// close event handler
that._connection.on('close', function () {
that.emit('end', 'The connection to the Elk M1 has been lost');
});
}
if (this.useSecure) {
var ciphers = ['TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384',
'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256',
'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384',
'TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA',
'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256',
'TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA',
'TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384',
'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256',
'TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384',
'TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256',
'TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA'].join(':');
var options = {
secureProtocol: 'TLSv1_method',
rejectUnauthorized: false,
//requestCert: true,
//cipher: 'AES256-SHA'
};
this._connection = new tls.connect(this.port, this.host, options, afterConnect);
} else {
this._connection = new net.connect(this.port, this.host, afterConnect);
}
}
// inherit from EventEmitter
ElkConnection.prototype = Object.create(EventEmitter.prototype);
ElkConnection.prototype.disconnect = function () {
if (this._connection) this._connection.destroy();
}
ElkConnection.prototype.disarm = function (opts) {
this._connection.write(messaging.writeArmingMessage('a0', opts));
}
ElkConnection.prototype.armAway = function (opts) {
this._connection.write(messaging.writeArmingMessage('a1', opts));
}
ElkConnection.prototype.armStay = function (opts) {
this._connection.write(messaging.writeArmingMessage('a2', opts));
}
ElkConnection.prototype.armStayInstant = function (opts) {
this._connection.write(messaging.writeArmingMessage('a3', opts));
}
ElkConnection.prototype.armNight = function (opts) {
this._connection.write(messaging.writeArmingMessage('a4', opts));
}
ElkConnection.prototype.armNightInstant = function (opts) {
this._connection.write(messaging.writeArmingMessage('a5', opts));
}
ElkConnection.prototype.armVacation = function (opts) {
this._connection.write(messaging.writeArmingMessage('a6', opts));
}
ElkConnection.prototype.armStepAway = function (opts) {
this._connection.write(messaging.writeArmingMessage('a7', opts));
}
ElkConnection.prototype.armStepStay = function (opts) {
this._connection.write(messaging.writeArmingMessage('a8', opts));
}
ElkConnection.prototype.armingStatusRequest = function (callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('AS', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('as'));
}
ElkConnection.prototype.alarmByZoneRequest = function (callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('AZ', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('az'));
}
ElkConnection.prototype.speak = function (message) {
var commands = messaging.getWordCommands(message);
for (var i = 0; i < commands.length; i++) {
this._connection.write(commands[i]);
}
}
ElkConnection.prototype.zoneDefinitionRequest = function (callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('ZD', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('zd'));
}
ElkConnection.prototype.zoneStatusRequest = function (callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('ZS', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('zs'));
}
ElkConnection.prototype.lightingDeviceStatusRequest = function (device, callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('DS', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('ds' + ('000' + device).substr(-3)));
}
ElkConnection.prototype.insteonLightingDeviceStatusRequest = function (startDevice, numDevices, callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('IR', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('ir' + ('000' + startDevice.toString()).substr(-3) + numDevices.toString()));
}
ElkConnection.prototype.powerLineCarrierStatusRequest = function (bank, callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('PS', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('ps' + bank.toString()));
}
ElkConnection.prototype.powerLineCarrierChangeRequest = function (houseCode, unitCode, functionCode, level, duration) {
this._connection.write(messaging.writePowerLineCarrierChangeMessage('pc', houseCode, unitCode, functionCode, level, duration));
}
ElkConnection.prototype.controlOutputOnRequest = function (controlOutput, duration) {
this._connection.write(messaging.writeControlOutputOnMessage('cn', controlOutput, duration));
}
ElkConnection.prototype.controlOutputOffRequest = function (controlOutput) {
this._connection.write(messaging.writeControlOutputOffMessage('cf', controlOutput));
}
ElkConnection.prototype.controlOutputToggleRequest = function (controlOutput) {
this._connection.write(messaging.writeControlOutputToggleMessage('ct', controlOutput));
}
ElkConnection.prototype.controlOutputStatusRequest = function (callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('CS', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeAscii('cs'));
}
ElkConnection.prototype.taskActivationRequest = function (task) {
this._connection.write(messaging.writeTaskActivationMessage('tn', task));
//console.log(messaging.writeTaskActivationMessage('tn', task));
}
ElkConnection.prototype.validUserCodeRequest = function (userCode, callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('UA', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeValidUserCodeMessage('ua', userCode));
}
ElkConnection.prototype.textDescriptionRequest = function (type, address, callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('SD', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeTextDescriptionsMessage('sd', type, address));
}
ElkConnection.prototype.systemLogRequest = function (logEntry, callback) {
if (callback && typeof callback === 'function') {
callback = safereturn(callback, this.responseTimeout);
this.once('LD', function (data) {
callback(null, data);
});
}
this._connection.write(messaging.writeSystemLogMessage('ld', logEntry));
}
/*********************************/
/* exports */
/*********************************/
module.exports.createConnection = function (opts) {
return new ElkConnection(opts);
}
module.exports.version = '0.0.2';