-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDeviceInstanceManager.js
executable file
·242 lines (219 loc) · 7 KB
/
DeviceInstanceManager.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
const EventEmitter = require('events');
const DB = require('./Database');
const DeviceManager = require('./DeviceManager');
const DeviceState = require('./DeviceState');
let TopologyManager;
const Log = require('debug')('device');
const LogCommit = Log.extend('commit');
const RETRY_COMMIT = 3;
class DeviceInstanceManager extends EventEmitter {
constructor() {
super();
this.devices = {};
this.activeCommit = false;
this.pendingCommit = false;
this.onDeviceUpdate = this.onDeviceUpdate.bind(this);
}
async start() {
await Promise.all((await DB.getDevices()).map(async device => {
const state = await DB.getDeviceState(device._id);
device = DeviceManager.fromDB(device, state);
if (device) {
this.devices[device._id] = device;
device.on('update', this.onDeviceUpdate);
// Update every device on startup, but only keep updating if we're monitoring.
device.watch();
if (!device.monitor) {
device.unwatch();
}
}
}));
this.onDeviceUpdate({});
}
stop() {
for (let id in this.devices) {
this.devices[id].off('update', this.onDeviceUpdate);
}
}
onDeviceUpdate(evt) {
this.emit('update', evt);
let commit = false;
for (let id in this.devices) {
if (this.devices[id].needCommit()) {
commit = true;
break;
}
}
if (commit !== this.pendingCommit) {
this.pendingCommit = commit;
this.emit('commit');
}
}
getAllDevices() {
const devices = Object.values(this.devices);
const ip2int = ip => ip.split('.').reduce((ipInt, octet) => (ipInt << 8) + parseInt(octet), 0) >>> 0;
devices.sort((a, b) => ip2int(a.readKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS)) - ip2int(b.readKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS)));
return devices;
}
getWiFiDevices() {
return this.getAllDevices().filter(device => device._authenticated && device.description.properties.ap);
}
getSwitchDevices() {
return this.getAllDevices().filter(device => device._authenticated && device.description.properties.switch);
}
getAuthenticatedDevices() {
return this.getAllDevices().filter(device => device._authenticated);
}
getUnauthenticatedDevices() {
return this.getAllDevices().filter(device => !device._authenticated);
}
getDeviceById(id) {
return this.devices[id];
}
getDeviceByIP(ip) {
for (let id in this.devices) {
if (this.devices[id].readKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS) == ip) {
return this.devices[id];
}
}
return null;
}
addDevice(device) {
if (!this.devices[device._id]) {
this.devices[device._id] = device;
device.on('update', this.onDeviceUpdate);
this.emit('add');
}
}
removeDevice(device) {
if (this.devices[device._id]) {
delete this.devices[device._id];
device.off('update', this.onDeviceUpdate);
this.emit('remove');
}
}
async authenticated(device) {
if (!this.devices[device._id]) {
throw new Error('Cannot authenticate unknown device');
}
if (!device._authenticated) {
throw new Error('Not authenticated');
}
device.on('update', this.onDeviceUpdate);
await DB.updateDevice(device.toDB());
await DB.updateDeviceState(device._id, device.state.toDB());
}
commitState() {
if (this.activeCommit) {
return 'active';
}
else if (this.pendingCommit) {
return 'pending';
}
else {
return null;
}
}
async commit(config) {
if (this.activeCommit) {
LogCommit('already active');
throw new Error('Commit already active');
}
this.activeCommit = true;
this.emit('commit');
LogCommit('starting');
try {
config = Object.assign({ direction: 'near-to-far', preconnect: true, retry: 3, callback: null }, config);
if (!TopologyManager) {
TopologyManager = require('./TopologyManager');
}
// Build a commit-ordered list of devices we need to update
const devices = TopologyManager.order(Object.values(this.devices).filter(device => device.needCommit()), config.direction);
// Most commits will pre-connect to all devices before making any changes to limit potential partial-commit problems if
// a device as failed. Not full proof as a device could fail later.
LogCommit('found commit order');
let slen = 0;
if (config.preconnect) {
LogCommit('doing pre-commit');
const connect = [].concat(devices);
for (let retry = config.retry; connect.length && retry > 0; retry = (slen === connect.length ? retry - 1 : config.retry)) {
slen = connect.length;
for (let i = 0; i < connect.length; ) {
const device = connect[i];
if (config.callback) {
config.callback({ op: 'connect', ip: device.readKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS) });
}
LogCommit(`verifying ${device.name}`);
if (await device.verify()) {
LogCommit('success');
connect.splice(i, 1);
}
else {
LogCommit('failed');
i++;
}
}
}
if (connect.length) {
LogCommit(`failed to connect to ${connect.length} devices`);
throw new Error('Failed to connect');
}
}
LogCommit('doing commit');
for (let retry = config.retry; devices.length && retry > 0; retry = (slen === devices.length ? retry - 1 : config.retry)) {
slen = devices.length;
for (let i = 0; i < devices.length; ) {
const device = devices[i];
try {
LogCommit(`commit ${device.name}`);
if (!config.preconnect) {
if (config.callback) {
config.callback({ op: 'connect', ip: device.readKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS) });
}
LogCommit('verify');
if (!await device.verify()) {
LogCommit('failed');
break;
}
}
if (config.callback) {
config.callback({ op: 'commit', ip: device.readKV(DeviceState.KEY_SYSTEM_IPV4_ADDRESS) });
}
LogCommit('write');
await device.write();
LogCommit('commit');
await device.commit();
LogCommit('success');
devices.splice(i, 1);
}
catch (e) {
LogCommit('commit failed');
LogCommit(e);
if (!config.preconnect) {
break;
}
i++;
}
}
}
if (devices.length) {
LogCommit(`failed with ${devices.length} devices pending`);
throw new Error('Failed to commit');
}
}
catch (e) {
LogCommit('unexpected error', e);
}
finally {
this.activeCommit = false;
this.emit('commit');
LogCommit('commit complete');
}
}
revert() {
for (let id in this.devices) {
this.devices[id].revert();
}
}
}
module.exports = new DeviceInstanceManager();