-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfibaroEventsDevice.js
120 lines (111 loc) · 4.13 KB
/
fibaroEventsDevice.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
const BaseNode = require('./fibaroBaseNode.js');
module.exports = function (RED) {
class EventsDevice extends BaseNode {
constructor(n) {
super(n, RED);
this.devices = [];
try {
this.devices = JSON.parse(`[${n.devices}]`);
this.devices.forEach(deviceID => {
if (deviceID != 0) {
this.fibaro.addDevice(n.id, String(deviceID));
}
});
} catch (err) {
// nothing
}
}
// respond to inputs....
onInput(msg) {
if (msg.topic === 'init') {
let payload = msg.payload;
try {
payload = JSON.parse(payload);
} catch (e) {
// Ignore malformed
}
this.fibaro.removeDevice(this.id);
if (Array.isArray(payload)) {
this.devices = payload;
} else {
try {
this.devices = JSON.parse(`[${msg.payload}]`);
} catch (err) {
// nothing
}
}
this.devices.forEach(deviceID => {
if (deviceID != 0) {
var dev = this.fibaro.devices.find(obj => {
return obj.id == deviceID
});
if (dev) {
this.fibaro.queryState(deviceID, "value", (cState) => {
dev.properties.value = cState.value;
this.fibaro.addDevice(this.id, String(deviceID));
});
}
}
});
}
if (msg.topic.endsWith("DevicePropertyUpdatedEvent")) {
if (MyMessage(msg, this.devices)) {
let payload = msg.payload;
try {
payload = JSON.parse(payload);
} catch (e) {
// Ignore malformed
}
let event = {};
event.topic = `${payload.id}`;
if (payload.property == "value") {
event.payload = payload.newValue;
this.node.send([event, null]);
} else {
event.payload = { property: payload.property, value: payload.newValue };
this.node.send([null, event]);
}
}
} else if (msg.topic.endsWith("CentralSceneEvent")) {
let payload = msg.payload;
try {
payload = JSON.parse(payload);
} catch (e) {
// Ignore malformed
}
let event = {};
event.topic = `${payload.deviceId}`;
event.payload = { property: msg.topic, value: payload };
this.node.send([null, event]);
}
}
onEvent(msg) {
this.node.status({ fill: 'yellow', shape: 'ring', text: 'event' });
setTimeout(() => {
this.node.status({});
}, 1000);
var event = {};
event.topic = String(msg.topic);
event.payload = msg.payload;
try { event.payload = JSON.parse(event.payload); } // obj
catch (e) {/* */ }
if (typeof event.payload === 'object') {
this.node.send([null, event]);
} else {
this.node.send([event, null]);
}
}
}
function MyMessage(msg, devices) {
for (var device in devices) {
var deviceID = devices[device];
if ((msg.payload.id == deviceID)) {
return true;
}
}
return false;
}
// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("fibaroXDevice", EventsDevice);
}