-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmg2-relay.js
91 lines (68 loc) · 1.7 KB
/
mmg2-relay.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
//jshint esversion:10
const
{
MMG2CoreAPI
} = require("rcm-core-client");
module.exports = function(RED) {
RED.nodes.registerType(
"mmg2-relay",
function(config) {
let subscribed = null,
changed = false,
called = false,
connectionNode = RED.nodes.getNode(config.connection);
inputs = {
locationID: parseInt(config.locationID),
control: parseFloat(config.control)
},
updateHandler = (topic, msg) => {
this.send({
payload: msg
});
},
exec = (msg) => {
let connection = connectionNode.context().global.connection;
msg = msg || {};
if (typeof msg.locationID === "number") {
if (inputs.locationID !== msg.locationID) {
changed = true;
inputs.locationID = msg.locationID;
}
}
if (typeof msg.control === "number") {
if (inputs.control !== msg.control) {
inputs.control = msg.control;
changed = true;
}
}
if ((!inputs.locationID) || (!inputs.control) || (!(connection instanceof MMG2CoreAPI))) {
setTimeout(exec.bind(this), 250);
return;
}
if (called || !changed) {
return;
}
called = true;
connection.request({
resource: "/API/deviceControl/setRelay",
params: {
locationID: inputs.locationID,
aso: inputs.control
},
onSuccess: (res) => {
console.log("Success :)");
changed = false;
called = false;
},
onFailure: (code, msg) => {
console.log("failed", code, msg);
called = false;
}
});
};
RED.nodes.createNode(this, config);
this.on("input", exec);
exec();
}
);
};