-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsenseway.js
136 lines (111 loc) · 3.83 KB
/
senseway.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
const mqtt = require("mqtt");
const uuid = require("uuid");
module.exports = function (RED) {
function SensewayNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const nodeContext = this.context();
if (!config.gatewayMac) {
node.error("Gateway MAC address is required");
node.status({ fill: "red", shape: "dot", text: "missing config" });
return;
}
if (!config.deviceMac) {
node.error("MQTT URI is required");
node.status({ fill: "red", shape: "dot", text: "missing config" });
return;
}
if (!config.mqttUri) {
node.error("MQTT URI is required");
node.status({ fill: "red", shape: "dot", text: "missing config" });
return;
}
node.status({ fill: "yellow", shape: "dot", text: "connecting" });
//close if already connected
let client = nodeContext.get("mqttClient");
if (client) {
client.end();
node.log("MQTT disconnected");
}
client = mqtt.connect(config.mqttUri, {
username: config.mqttUsername,
password: config.mqttPassword,
});
nodeContext.set("mqttClient", client);
node.log("MQTT connecting with URI: " + config.mqttUri + " username: " + config.mqttUsername);
client.on("connect", function () {
node.status({ fill: "green", shape: "dot", text: "connected" });
node.log("MQTT connected");
client.subscribe(
`prod/gateway/${config.gatewayMac}/device/${config.deviceMac}/measure/+/done`,
function (err) {
if (err) {
node.error("MQTT subscription error");
}
}
);
client.subscribe(
`lake/gateway/${config.gatewayMac}/device/${config.deviceMac}/measure/+/done`,
function (err) {
if (err) {
node.error("MQTT subscription error");
}
}
);
node.log("MQTT subscribed");
const intervalSeconds = config.intervalSeconds;
if (
config.accelerometerRange != 0 &&
config.samplingRate != 0 &&
config.sampleSize != 0 &&
config.intervalSeconds != 0
) {
const publishMeasurementMessage = setInterval(() => {
const measureId = uuid.v4().replace(/-/g, "").slice(0, 24);
client.publish(
`prod/gateway/${config.gatewayMac}/device/${config.deviceMac}/measure/${measureId}`,
`${config.accelerometerRange},
${config.samplingRate},
${config.sampleSize}`
);
client.publish(
`lake/gateway/${config.gatewayMac}/device/${config.deviceMac}/measure/${measureId}`,
`${config.accelerometerRange},
${config.samplingRate},
${config.sampleSize}`
);
node.log(`MQTT message published: ${measureId}`);
}, config.intervalSeconds * 1000);
if (intervalSeconds != config.intervalSeconds) {
clearInterval(publishMeasurementMessage);
}
}
});
client.on("message", function (topic, message) {
const telemetries = JSON.parse(message.toString()).TELEMETRY;
node.log(`MQTT message received: ${topic}`);
node.send({
topic: topic,
payload: telemetries,
});
});
client.on("error", function () {
node.status({ fill: "red", shape: "dot", text: "error" });
node.error("MQTT error");
});
client.on("close", function () {
node.status({ fill: "red", shape: "dot", text: "closed" });
node.error("MQTT connection closed");
});
client.on("offline", function () {
node.status({ fill: "red", shape: "dot", text: "offline" });
node.error("MQTT connection offline");
});
this.on("close", function (done) {
client.end();
node.log("MQTT disconnected");
done();
});
}
RED.nodes.registerType("senseway", SensewayNode);
};