-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmg2-reading.js
144 lines (111 loc) · 3.09 KB
/
mmg2-reading.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
//jshint esversion:10
const
HW_STATE_RELAY = 1, //Relay state
HW_STATE_SWITCH = 2, //Switch state
ERROR_CONDITIONS = [
"Failure to obtain data from sensor", //0
"Failure to control device", //1
"Failure to read enable pin" //2
],
{
MMG2CoreAPI
} = require("rcm-core-client");
module.exports = function(RED) {
RED.nodes.registerType(
"mmg2-reading",
function(config) {
let subscribed = null,
connectionNode = RED.nodes.getNode(config.connection);
inputs = {
locationID: parseInt(config.locationID),
beginTime: parseInt(config.beginTime)
},
updateReadingHandler = (topic, msg) => {
if (!msg) {
return;
}
let
errors = [],
payload = {
serialNumber: msg.serialNumber,
timestamp: new Date(msg.timestamp),
relay: ((msg.hwState & HW_STATE_RELAY) ? "On" : "Off"),
switch: ((msg.hwState & HW_STATE_SWITCH) ? "Enable" : "Disable"),
value: msg.value,
errors: null
};
for (let a = 0; a < ERROR_CONDITIONS.length; a += 1) {
if (msg.hwState & (1 << (a + 2))) {
errors.push(ERROR_CONDITIONS[a]);
}
}
payload.errors = errors.join("\n");
if (config.wrap) {
this.send({ payload: payload });
}
else {
this.send(payload);
}
},
exec = (msg) => {
let connection = connectionNode.context().global.connection;
msg = msg || {};
if (msg.locationID !== undefined) {
inputs.locationID = parseInt(msg.locationID);
}
if (msg.beginTime !== undefined) {
inputs.beginTime = parseInt(msg.beginTime);
}
if ((!inputs.locationID) || (!(connection instanceof MMG2CoreAPI))) {
setTimeout(exec.bind(this), 250);
return;
}
if (subscribed !== inputs.locationID) {
if (subscribed) {
connection.unregisterPushHandler("SRV/readings/" + subscribed, updateReadingHandler);
}
connection.registerPushHandler("SRV/readings/" + inputs.locationID, updateReadingHandler);
subscribed = inputs.locationID;
connection.request({
resource: "/API/monitor/getStatus",
params: {
locationID: inputs.locationID,
beginTime: inputs.beginTime
},
onSuccess: (res) => {
if (res.data.status.length) {
updateReadingHandler(null, res.data.status[0]);
}
else {
this.send({
payload: null
});
}
},
onFailure: () => {
this.send({
payload: new Error("Failure to obtain reading")
});
}
});
}
};
RED.nodes.createNode(this, config);
this.on("input", exec);
exec();
this.on("remove", () => {
console.log("remove event.")
connection.logOff(
() => {
console.log("Log off Successful.");
},
() => {
console.log("Log off Unsuccessful.");
}
);
connectionNode.context().global.connection = null;
connection = null;
});
}
);
};