-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
73 lines (56 loc) · 1.88 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
var Service, Characteristic;
var mqtt = require('mqtt');
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-mqtt-motionsensor", "mqtt-motionsensor", MotionSensorAccessory);
}
function MotionSensorAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.url = config['url'];
this.topic = config['topic'];
this.sn = config['sn'] || 'Unknown';
this.client_Id = 'mqttjs_' + Math.random().toString(16).substr(2, 8);
this.options = {
keepalive: 10,
clientId: this.client_Id,
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
reconnectPeriod: 1000,
connectTimeout: 30 * 1000,
will: {
topic: 'WillMsg',
payload: 'Connection Closed abnormally..!',
qos: 0,
retain: false
},
username: config["username"],
password: config["password"],
rejectUnauthorized: false
};
this.service = new Service.MotionSensor();
this.client = mqtt.connect(this.url, this.options);
var self = this;
this.client.subscribe(this.topic);
this.client.on('message', function (topic, message) {
data = JSON.parse(message);
if (data === null) return null;
self.value = Boolean(parseInt(data,10));
self.service.getCharacteristic(Characteristic.MotionDetected).setValue(self.value);
});
}
MotionSensorAccessory.prototype.getState = function(callback) {
this.log(this.name, " - MQTT : ", this.value);
callback(null, this.value);
}
MotionSensorAccessory.prototype.getServices = function() {
var informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Name, this.name)
.setCharacteristic(Characteristic.Manufacturer, "heisice")
.setCharacteristic(Characteristic.Model, "Motion Sensor")
.setCharacteristic(Characteristic.SerialNumber, this.sn);
return [informationService, this.service];
}