-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (142 loc) · 5.62 KB
/
index.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
function ZwayMqttEndpoint(id, controller) {
ZwayMqttEndpoint.super_.call(this, id, controller);
}
inherits(ZwayMqttEndpoint, AutomationModule);
_module = ZwayMqttEndpoint;
ZwayMqttEndpoint.prototype.init = function (config, callback) {
ZwayMqttEndpoint.super_.prototype.init.call(this, config);
var self = this,
callback = callback || function () { };
var startsWith = function(s, searchString){
return s.substr(0, searchString.length) === searchString;
};
var endsWith = function(s, searchString) {
var start = s.length - searchString.length;
return start === s.indexOf(searchString, start);
};
var log = function(msg) {
console.log("ZwayMqttEndpoint: " + msg);
};
var deviceToTopic = function (d) {
var id = (typeof d == "string") ? d : d.get("id");
var normalizedId = id.toLowerCase().replace(/[^a-z0-9]/g, "_");
return self.config.topic_prefix + "/devices/" + normalizedId;
};
var mqttClient = new MqttClient(self.config.host, parseInt(self.config.port));
mqttClient.ondisconnect = function() {
log("Disconnected from MQTT server, attempting to reconnect");
setTimeout(function() { self.connect(); }, 5000);
};
this.connect = function () {
log("Connect");
var connectArgs = {};
if ("username" in self.config && self.config.username != null && self.config.username != "") {
connectArgs.username = self.config.username;
}
if ("password" in self.config && self.config.password != null && self.config.password != "") {
connectArgs.password = self.config.password;
}
mqttClient.connect(connectArgs, function (p) {
if ("errorMessage" in p) {
log("Cannot connect to MQTT server: " + p.errorMessage);
setTimeout(function() { self.connect(); }, 5000);
}
log("Connected");
self.setupSubscriptions();
});
};
this.parsePayload = function(payload) {
try {
return JSON.parse(String.fromCharCode.apply(null, payload));
} catch (e) {
var bytes = "";
for (var i = 0; i < payload.length; i++) {
bytes += payload[i].toString(16);
if (i < (payload.length - 1)) bytes += ", ";
}
throw Error("Cannot parse payload, string: '" + String.fromCharCode.apply(null, payload) + "', bytes: " + bytes);
}
};
this.getRetainFlag = function() {
if ("retain" in self.config) return "true" == self.config.retain;
return false;
};
var handleDeviceMessage = function (payload, topic) {
var findDeviceMatchingTopic = function (topic) {
var matches = self.controller.devices.filter(function (d) {
return deviceToTopic(d) + "/set" == topic;
});
if (matches.length == 0) {
return null;
} else {
return matches[0];
}
};
var message = self.parsePayload(payload);
var device = findDeviceMatchingTopic(topic);
if (device == null) {
log("Cannot find device matching topic: " + topic);
} else {
device.performCommand(message.command, message.args);
}
};
var handleStatusMessage = function() {
self.controller.devices.forEach(sendDeviceStatusMessage);
};
var sendDeviceStatusMessage = function (device) {
log("Device update for " + device.get("id"));
var msg = {
id: device.get("id"),
title: device.get("metrics:title"),
icon: device.get("metrics:icon"),
type: device.get("deviceType"),
level: device.get("metrics:level")
};
mqttClient.publish(deviceToTopic(device) + "/update", JSON.stringify(msg), self.getRetainFlag());
};
var queue = [];
var queueDeviceStatusMessage = function (device) {
queue.push({
id: device.get("id"),
title: device.get("metrics:title"),
icon: device.get("metrics:icon"),
type: device.get("deviceType"),
level: device.get("metrics:level")
});
};
var processQueue = function() {
var devicesSent = [];
for (var i = queue.length - 1; i >= 0; i--) {
if (queue[i].id in devicesSent) continue;
mqttClient.publish(deviceToTopic(queue[i].id) + "/update", JSON.stringify(queue[i]), self.getRetainFlag());
devicesSent[queue[i].id] = true
}
queue = [];
};
this.setupSubscriptions = function () {
this.controller.devices.on('change:metrics:level', function (device) {
queueDeviceStatusMessage(device);
if (self.config['coalesce_interval'] > 0) {
setTimeout(processQueue, self.config['coalesce_interval']);
} else {
processQueue();
}
});
mqttClient.onmessage = function(topic, payload) {
if (startsWith(topic, self.config.topic_prefix)) {
if (topic == self.config.topic_prefix + "/status") {
log("Status reques message on topic " + topic);
handleStatusMessage();
} else if (endsWith(topic, "/set")) {
log("Status change message on topic " + topic);
handleDeviceMessage(payload, topic);
}
}
};
mqttClient.subscribe(self.config.topic_prefix + "/#", callback);
};
this.end = function () {
mqttClient.close();
};
this.connect();
};