-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
199 lines (158 loc) · 5.97 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
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) {
this._connection = new tls.connect(this.port, this.host, {}, 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.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));
}
/*********************************/
/* exports */
/*********************************/
module.exports.createConnection = function(opts) {
return new ElkConnection(opts);
}
module.exports.version = '0.0.2';