-
Notifications
You must be signed in to change notification settings - Fork 3
/
DeviceManager.js
executable file
·53 lines (45 loc) · 1.28 KB
/
DeviceManager.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
const FS = require('fs');
const BrowserDevice = require('./BrowserDevice');
const DeviceState = require('./DeviceState');
class DeviceManager {
constructor() {
this.devices = [];
this._loadAllDevices();
}
_loadAllDevices() {
// Put some helpers in global scope
global.OID = require('./OID');
global.Maps = require('./Maps');
this._loadDevices(`${__dirname}/device`);
}
_loadDevices(dir) {
FS.readdirSync(dir, { encoding: 'utf8', withFileTypes: true }).forEach(entry => {
if (entry.isDirectory()) {
this._loadDevices(`${dir}/${entry.name}`);
}
else if (entry.name === 'index.js') {
const description = require(dir);
switch (description.type || 'browser') {
case 'browser':
this.devices.push(new BrowserDevice(description));
break;
default:
break;
}
}
});
}
getDevices() {
return this.devices;
}
fromDB(dbDevice, dbState) {
const device = this.devices.find(device => device.description.id === dbDevice.dmId);
if (!device) {
return null;
}
const devinst = device.newInstance(dbDevice, DeviceState.fromDB(dbState));
devinst._authenticated = true;
return devinst;
}
}
module.exports = new DeviceManager();