-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDeviceInstance.js
76 lines (58 loc) · 1.5 KB
/
DeviceInstance.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
const EventEmitter = require('events');
const DeviceState = require('./DeviceState');
const DB = require('./Database');
const MonitorManager = require('./MonitorManager');
class DeviceInstance extends EventEmitter {
constructor(config, state) {
super();
this._id = config._id;
this.state = state;
this.state.on('update', async evt => {
this.emit('update', Object.assign(evt, { device: this }));
});
this.state.on('merge', async evt => {
this.emit('update', Object.assign(evt, { type: 'merge', device: this }));
});
}
get name() {
return this.readKV(DeviceState.KEY_SYSTEM_NAME) || this.readKV(DeviceState.KEY_SYSTEM_FIXEDNAME) || '-';
}
get properties() {
return this.state.state;
}
readKV(key, options) {
return this.state.readKV(key, options);
}
writeKV(key, value, options) {
return this.state.writeKV(key, value, options);
}
deleteKV(key) {
return this.state.deleteKV(key);
}
needCommit() {
return this.state.needCommit();
}
revert() {
this.state.revertKV();
}
async read() {
throw new Error('Abstract');
}
async write() {
throw new Error('Abstract');
}
async statistics() {
throw new Error('Abstract');
}
async commit() {
this.state.commitKV();
await DB.updateDeviceState(this._id, this.state.toDB());
}
mergeIntoState(src, trim) {
this.state.mergeIntoState(src, trim);
}
get monitor() {
return MonitorManager.isMonitored(this);
}
}
module.exports = DeviceInstance;