-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
130 lines (110 loc) · 4.08 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
/*** MQTT Z-Way module *******************************************
Version: 0.0.1
-----------------------------------------------------------------------------
Author: David Uebelacker <david@uebelacker.ch>
Buffer from: https://github.com/toots/buffer-browserify
MQTT from: https://github.com/leizongmin/MQTTClient
******************************************************************************/
function MQTT(id, controller) {
MQTT.super_.call(this, id, controller);
}
inherits(MQTT, AutomationModule);
_module = MQTT;
MQTT.prototype.init = function (config) {
const BRIDGE_URL = 'http://127.0.0.1:34254';
const DIRECT_DEVICES = [
'toggleButton',
'battery',
'sensorBinary',
'sensorMultilevel'
];
MQTT.super_.prototype.init.call(this, config);
console.log('MQTT: starting...');
var self = this;
self.config = config;
self.status = {};
this.log = function (message) {
console.log('MQTT: ' + message);
};
this.verbose = function (message) {
if (self.config.verbose) {
console.log('MQTT: ' + message);
}
};
this.findRoom = function (id) {
var locations = self.controller.locations;
if (locations) {
for (var i = 0; i < locations.length; i++) {
if (locations[i].id == id) {
return locations[i];
}
}
}
return null;
};
this.createTopic = function (device) {
var room = self.findRoom(device.get('location'));
var topic = self.config.mqtt_topic_prefix;
topic += '/';
topic += room.title;
topic += '/';
topic += device.get('metrics:title');
return topic;
};
this.deviceUpdate = function (device) {
var topic = self.createTopic(device);
var value = device.get('metrics:level');
if (!device.get('permanently_hidden')) {
if (self.config.ignore_global_room && device.get('location') == 0) {
self.verbose('self.config.ignore_global_room set to: ' + self.config.ignore_global_room+' and device.location ' +device.location+' so ignoring');
return;
}
if (self.status[topic] === undefined
|| value != self.status[topic].value
|| (DIRECT_DEVICES.indexOf(device.get('deviceType')) >= 0
&& self.status[topic].timestamp + 2000 < self.timestamp())) {
self.status[topic] = {timestamp: self.timestamp(), value: value};
self.verbose('publishing: ' + topic + ': ' + value);
self.sendUpdateToBridge(topic, value.toString().trim(), {retain: self.config.retain});
}
}
};
this.timestamp = function() {
return Date.now();
};
this.sendConfigToBridge = function () {
request = {
url: BRIDGE_URL + '/config',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify(self.config)
};
self.verbose('SENDING: ' + JSON.stringify(request));
self.verbose('RESPONSE: ' + JSON.stringify(http.request(request)));
};
this.sendUpdateToBridge = function (topic, value) {
request = {
url: BRIDGE_URL + '/update',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: JSON.stringify({topic: topic, value: value})
};
self.verbose('SENDING: ' + JSON.stringify(request));
self.verbose('RESPONSE: ' + JSON.stringify(http.request(request)));
};
if (!self.config.mqtt_host || !self.config.mqtt_port) {
this.log('Host or port not configured! will not start!')
} else {
this.sendConfigToBridge();
this.controller.devices.on('change:metrics:level', self.deviceUpdate);
}
};
MQTT.prototype.stop = function () {
MQTT.super_.prototype.stop.call(this);
this.controller.devices.off('change:metrics:level', this.deviceUpdate);
console.log('MQTT: stopped');
};