-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMonitorManager.js
146 lines (128 loc) · 3.71 KB
/
MonitorManager.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
const EventEmitter = require('events');
const DB = require('./Database');
const Log = require('debug')('monitor');
class MonitorManager extends EventEmitter {
constructor() {
super();
this.enabled = [];
this.monitors = [];
}
async start() {
const DeviceInstanceManager = require('./DeviceInstanceManager');
const data = await DB.getMonitorList();
if (data) {
this.enabled = JSON.parse(data.enabled);
for (let i = 0; i < this.enabled.length; i++) {
const id = this.enabled[i];
const device = DeviceInstanceManager.getDeviceById(id);
if (device) {
await DB.createMonitor(`device-${id}`);
device.watch();
}
}
this.monitors = JSON.parse(data.monitors);
// XXX - Remove me June 6th, 2021
this.monitors = this.monitors.filter(mon => mon.type !== 'clients');
// XXX
}
this._monitorUpdates = evt => {
if (evt.type === 'merge' && evt.op === 'update' && this.enabled.indexOf(evt.device._id) !== -1 && evt.key.match(/^network\.physical\.port\.[0-9]+\.statistics\./)) {
DB.updateMonitor(`device-${evt.device._id}`, { key: evt.key, value: evt.value }).catch(err => Log(err));
}
}
DeviceInstanceManager.on('update', this._monitorUpdates);
}
stop() {
DeviceInstanceManager.on('update', this._monitorUpdates);
this.enabled.forEach(id => {
const device = DeviceInstanceManager.getDeviceById(id);
if (device) {
device.unwatch();
}
});
}
async monitorDevice(device, enable) {
const idx = this.enabled.indexOf(device._id);
if (idx !== -1) {
this.enabled.splice(idx, 1);
device.unwatch();
}
if (enable) {
this.enabled.push(device._id);
await DB.createMonitor(`device-${device._id}`);
device.watch();
}
await this.updateMonitors();
}
async monitorDeviceKeysType(device, id, title, keys, monitorType, unit) {
const idx = this.monitors.findIndex(mon => mon.id === id);
if (idx !== -1) {
this.monitors.splice(idx, 1);
}
if (keys[0].key !== 'none' && monitorType !== 'none') {
const mon = {
id: id,
deviceid: device._id,
title: title,
keys: keys,
type: monitorType,
name: `${device._id}-${id}`,
unit: unit,
order: -1
};
this.monitors.push(mon);
}
await this.updateMonitors();
}
async monitorCustom(id, enable, title, monitorType) {
const idx = this.monitors.findIndex(mon => mon.id === id);
if (idx !== -1) {
this.monitors.splice(idx, 1);
}
if (enable) {
const mon = {
id: id,
title: title,
type: monitorType,
order: -1
};
this.monitors.push(mon);
}
await this.updateMonitors();
}
getAllMonitors() {
return this.monitors;
}
getDevicePortMonitors(device, portnr) {
if (this.enabled.indexOf(device._id) === -1) {
return null;
}
const patt = new RegExp(`^network\.physical\.port\.${portnr}\.statistics.*$`);
const monitors = [];
this.monitors.forEach(mon => {
if (mon.deviceid === device._id && mon.keys[0].key.match(patt)) {
monitors.push(mon);
}
});
return monitors;
}
isMonitored(device) {
return this.enabled.indexOf(device._id) !== -1;
}
newMonitorId() {
return DB.newMonitorId();
}
async updateMonitors() {
this.monitors.sort((a, b) => a.order - b.order);
this.monitors.forEach((m, idx) => m.order = idx);
await DB.updateMonitorList(this.toDB());
}
toDB() {
return {
_id: 'monitors',
enabled: JSON.stringify(this.enabled),
monitors: JSON.stringify(this.monitors)
};
}
}
module.exports = new MonitorManager();