This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
scout.js
57 lines (45 loc) · 1.76 KB
/
scout.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
var Scientist = require('zetta-scientist');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Scout = module.exports = function Scout() {
this.server = null; // set when app.use initializes it
};
Scout.prototype.createDevice = function(args) {
var machine = Scientist.create.apply(null, args);
machine._pubsub = this.server.pubsub;
machine._log = this.server._log;
machine._registry = this.server.registry;
return machine;
};
// add a new device to the registry
Scout.prototype.discover = function(constructor) {
var self = this;
var machine = this.createDevice(arguments);
machine = Scientist.init(machine);
// save device in persistant store
self.server.registry.save(machine, function(err){
self.server._jsDevices[machine.id] = machine;
self.server._log.emit('log','scout', 'Device (' + machine.type + ') ' + machine.id + ' was discovered' );
self.server.emit('deviceready', machine);
});
return machine;
};
Scout.prototype.provision = function(deviceObject, constructor) {
// if already initiated on runtime do not create a second instnace
if(this.server._jsDevices[deviceObject.id]) {
return null;
}
var args = Array.prototype.slice.call(arguments, 1);
// TODO: add new device code
var machine = this.createDevice(args);
machine.id = deviceObject.id; // must set id before machine_config runs
machine.name = deviceObject.name;
machine = Scientist.init(machine);
// add to list of initiated
this.server._jsDevices[machine.id] = machine;
this.server.registry.save(machine, function(err){
});
this.server._log.emit('log','scout', 'Device (' + machine.type + ') ' + machine.id + ' was provisioned from registry.' );
this.server.emit('deviceready', machine);
return machine;
};