-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchange_event_detectors.js
47 lines (38 loc) · 1.77 KB
/
change_event_detectors.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
/**
* This script finds all UPDATE type event detectors and converts them to MULTISTATE_STATE detectors
*/
const UpdateDetectorVO = Java.type('com.serotonin.m2m2.vo.event.detector.UpdateDetectorVO');
const TimePeriods = Java.type('com.serotonin.m2m2.Common.TimePeriods');
const ModuleRegistry = Java.type('com.serotonin.m2m2.module.ModuleRegistry');
const MultistateStateEventDetectorDefinition = Java.type('com.serotonin.m2m2.module.definitions.event.detectors.MultistateStateEventDetectorDefinition');
const eventDetectorsService = services.eventDetectorsService;
const definition = ModuleRegistry.getEventDetectorDefinition(MultistateStateEventDetectorDefinition.TYPE_NAME);
let count = 0;
const upgradeDetector = function(detector) {
try {
const newDetector = definition.baseCreateEventDetectorVO(detector.getDataPoint());
newDetector.setState(1);
newDetector.setDuration(0);
newDetector.setDurationType(TimePeriods.SECONDS);
newDetector.setName(detector.getName());
newDetector.setEditPermission(detector.getEditPermission());
newDetector.setReadPermission(detector.getReadPermission());
newDetector.setAlarmLevel(detector.getAlarmLevel());
newDetector.setEventHandlerXids(detector.getEventHandlerXids());
eventDetectorsService.insert(newDetector);
eventDetectorsService.delete(detector);
count++;
} catch(e) {
print(`Failed to upgrade detector ${detector}, error ${e}`);
}
};
const eventDetectors = eventDetectorsService.buildQuery()
.equal('sourceTypeName', 'DATA_POINT')
.equal('typeName', 'UPDATE')
.query();
for (const ed of eventDetectors) {
if (ed instanceof UpdateDetectorVO) {
upgradeDetector(ed);
}
}
print(`Upgraded ${count} event detectors`);