-
Notifications
You must be signed in to change notification settings - Fork 4
/
thing-client.js
305 lines (222 loc) · 9.6 KB
/
thing-client.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
// a node.js module to implement the Simple Thing Protocol
// cf., http://thethingsystem.com/dev/Simple-Thing-Protocol.html
var events = require('events')
, fs = require('fs')
, mdns = require('mdns')
, os = require('os')
, speakeasy = require('speakeasy')
, util = require('util')
, ws = require('ws')
;
var DEFAULT_LOGGER = { error : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, warning : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, notice : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, info : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
, debug : function(msg, props) { console.log(msg); if (!!props) console.log(props); }
};
var Singleton = function(options) {
var i, iface, ifaces, ifname, k;
var self = this;
if (!(self instanceof Singleton)) return new Singleton(options);
self.options = options || {};
self.logger = self.options.logger || {};
for (k in DEFAULT_LOGGER) {
if ((DEFAULT_LOGGER.hasOwnProperty(k)) && (typeof self.logger[k] === 'undefined')) self.logger[k] = DEFAULT_LOGGER[k];
}
ifaces = os.networkInterfaces();
self.ifaddrs = [];
for (ifname in ifaces) if (ifaces.hasOwnProperty(ifname)) {
iface = ifaces[ifname];
for (i = 0; i < iface.length; i++) {
if ((!iface[i].internal) && (iface[i].family === 'IPv4')) self.ifaddrs.push(iface[i].address);
}
}
self.hosts = {};
try {
self.mdns = mdns.createBrowser(mdns.tcp('wss')).on('serviceUp', function(service) {
for (i = 0; i < service.addresses.length; i++) {
if (self.ifaddrs.indexOf(service.addresses[i]) !== -1) {
service.localhost = true;
break;
}
}
self.hosts[service.host] = service;
}).on('serviceDown', function(service) {
delete(self.hosts[service.host]);
}).on('serviceChanged', function(service) {
self.hosts[service.host] = service;
}).on('error', function(err) {
self.logger.error('_wss._tcp', { event: 'mdns', diagnostic: err.message });
});
self.mdns.start();
} catch(ex) {
self.logger.error('_wss._tcp', { event: 'browse', diagnostic: ex.message });
}
};
var singleton = new Singleton();
/* options:
for logging
logger.* : function(msg, props);
for web sockets
params.url : complete 'ws:' or 'wss:' URL
to identify steward
steward.name : e.g., IP address, 127.0.0.1/localhost, place1.name
steward.uuid : e.g., 2f402f80-da50-11e1-9b23-0123456789ab
steward.crtData : steward's certificate (either as a buffer or array)
steward.crtPath : pathname to file containing steward's certificate
*/
var ThingAPI = function(options) {
var k;
var self = this;
if (!(self instanceof ThingAPI)) return new ThingAPI(options);
self.options = options || {};
self.logger = self.options.logger || {};
for (k in DEFAULT_LOGGER) {
if ((DEFAULT_LOGGER.hasOwnProperty(k)) && (typeof self.logger[k] === 'undefined')) self.logger[k] = DEFAULT_LOGGER[k];
}
if (!singleton.options.logger) {
singleton.options.logger = self.logger;
singleton.logger = self.logger;
}
self.params = self.options.params || {};
if (!!self.params.url) return self._channel(self);
if (!self.options.steward) self.options.steward = {};
self.timer = setInterval(function() {
var didP, entry, host;
if ((self.options.steward.name === '127.0.0.1') || (self.options.steward.name === 'localhost')) {
self.params.url = 'ws://localhost:8887';
clearInterval(self.timer);
return self._channel(self);
}
if ((!!self.options.steward.name) && (self.options.steward.name.length === 0)) delete(self.options.steward.name);
didP = false;
for (host in singleton.hosts) if (singleton.hosts.hasOwnProperty(host)) {
didP = true;
entry = singleton.hosts[host];
if ( ((!!self.options.steward.name)
&& (entry.host !== (self.options.steward.name + '.' + entry.replyDomain))
&& (entry.name + '.' + entry.replyDomain !== self.options.steward.name + '.')
&& (entry.txtRecord.name !== self.options.steward.name))
|| ((!!self.options.steward.uuid) && (entry.txtRecord.uuid !== self.options.steward.uuid))) continue;
if ((!self.options.steward.crtData) && (!self.options.steward.crtPath)) {
self.params.url = 'ws://' + entry.host + ':8887';
} else {
self.params.url = 'wss://' + entry.host + ':' + entry.port;
}
clearInterval(self.timer);
return self._channel(self);
}
if (!didP) return;
clearInterval(self.timer);
return self.emit('error', new Error('no matching stewards'));
}, 250);
return self;
};
util.inherits(ThingAPI, events.EventEmitter);
ThingAPI.prototype._channel = function(self) {
if (util.isArray(self.options.steward.crtData)) self.options.steward.crtData = new Buffer(self.options.steward.crtData);
self.params.ca = self.options.steward.crtData;
if ((!self.params.ca) && (!!self.options.steward.crtPath)) self.params.ca = fs.readFileSync(self.options.steward.crtPath);
if (!!self.params.ca) self.params.ca = [ self.params.ca ];
self.channel = new ws(self.params.url + '/manage', self.params).on('open', function() {
self.reqno = 1;
self.callbacks = {};
self.addCallback = function(cb, atMost) {
self.callbacks[self.reqno.toString()] = { callback: cb, times: atMost };
return self.reqno++;
};
if (!!self.options.state) {
self.thingID = self.options.state.thingID;
self.params = self.options.state.params;
return self._hello();
}
if (!!self.options.pairing) return self._pair();
return self.emit('error', new Error('no pairing information'));
}).on('message', function(data, flags) {
var callback, doneP, message, requestID;
if ((!!flags) && (flags.binary === true)) return self.emit('error', new Error('binary message'));
try { message = JSON.parse(data.toString()); } catch(ex) {return self.emit('error', new Error('error parsing message')); }
self.logger.debug('>>> recv: ' + JSON.stringify(message));
if (!!message.path) return self.emit('message', message);
requestID = message.requestID.toString();
if (!self.callbacks[requestID]) return;
callback = self.callbacks[requestID].callback;
doneP = (self.callbacks[requestID].times-- < 2) || (!!message.error);
if (doneP) delete(self.callbacks[requestID]);
if (!!callback) return callback(message, doneP);
}).on('close', function() {
self.emit('close');
}).on('error', function(err) {
self.emit('error', err);
});
return self;
};
ThingAPI.prototype._pair = function() {
var json, thingUUID;
var self = this;
if (!self.channel) throw new Error('channel not open');
thingUUID = self.options.pairing.thingUUID;
json = { path : '/api/v1/thing/pair/' + thingUUID
, name : thingUUID
};
if (self.options.pairing.code) json.pairingCode = self.options.pairing.code;
return self._send(json, function(message, doneP) {
if (!!message.error) return self.emit('error', new Error(message.error.diagnostic), message.error);
if (!doneP) return;
self.thingID = message.result.thingID;
self.params = message.result.params;
delete(message.result.success);
self.emit('paired', { thingID: self.thingID, params: self.params });
self._hello();
});
};
ThingAPI.prototype._hello = function() {
var json;
var self = this;
if (!self.channel) throw new Error('channel not open');
json = { path : '/api/v1/thing/hello/' + self.thingID
, response : speakeasy.totp({ key: self.params.base32, length: 6, encoding: 'base32', step: self.params.step })
};
return self._send(json, function(message) {
if (!!message.error) return self.emit('error', new Error(message.error.diagnostic), message.error);
self.emit('ready');
});
};
ThingAPI.prototype._send = function(json, callback, onceP) {
var self = this;
if (!self.channel) throw new Error('channel not open');
if (typeof callback === 'undefined') callback = function() {};
if (typeof callback !== 'function') throw new Error('callback must be function');
json.requestID = self.addCallback(callback, onceP ? 1 : 2);
self.logger.debug('>>> send: ' + JSON.stringify(json));
self.channel.send(JSON.stringify(json));
return self;
};
ThingAPI.prototype.prototype = function(things, cb) {
return this._send({ path : '/api/v1/thing/prototype'
, things : things
}, cb, true);
};
ThingAPI.prototype.register = function(things, cb) {
return this._send({ path : '/api/v1/thing/register'
, things : things
}, cb, true);
};
ThingAPI.prototype.update = function(things, cb) {
return this._send({ path : '/api/v1/thing/update'
, things : things
}, cb, true);
};
ThingAPI.prototype.report = function(events, cb) {
return this._send({ path : '/api/v1/thing/report'
, events : events
}, cb, true);
};
ThingAPI.prototype.reply = function(response) {
var self = this;
if (!self.channel) throw new Error('channel not open');
self.logger.debug('>>> send: ' + JSON.stringify(response));
self.channel.send(JSON.stringify(response));
return self;
};
exports.ThingAPI = ThingAPI;