-
Notifications
You must be signed in to change notification settings - Fork 41
/
index.js
120 lines (108 loc) · 4.05 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
// MQTT Switch Accessory plugin for HomeBridge
//
// Remember to add accessory to config.json. Example:
// "accessories": [
// {
// "accessory": "mqttswitch",
// "name": "PUT THE NAME OF YOUR SWITCH HERE",
// "url": "PUT URL OF THE BROKER HERE",
// "username": "PUT USERNAME OF THE BROKER HERE",
// "password": "PUT PASSWORD OF THE BROKER HERE"
// "caption": "PUT THE LABEL OF YOUR SWITCH HERE",
// "topics": {
// "statusGet": "PUT THE MQTT TOPIC FOR THE GETTING THE STATUS OF YOUR SWITCH HERE",
// "statusSet": "PUT THE MQTT TOPIC FOR THE SETTING THE STATUS OF YOUR SWITCH HERE"
// },
// "onValue": "OPTIONALLY PUT THE VALUE THAT MEANS ON HERE (DEFAULT true)",
// "offValue": "OPTIONALLY PUT THE VALUE THAT MEANS OFF HERE (DEFAULT false)",
// "statusCmd": "OPTIONALLY PUT THE STATUS COMMAND HERE",
// "integerValue": "OPTIONALLY SET THIS TRUE TO USE 1/0 AS VALUES",
// }
// ],
//
// When you attempt to add a device, it will ask for a "PIN code".
// The default code for all HomeBridge accessories is 031-45-154.
'use strict';
var Service, Characteristic;
var mqtt = require("mqtt");
function MqttSwitchAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.url = config["url"];
this.publish_options = {
qos: ((config["qos"] !== undefined) ? config["qos"] : 0),
retain: ((config["retain"] !== undefined) ? config["retain"] : false)
};
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: ((config["retain"] !== undefined) ? config["retain"] : false)
},
username: config["username"],
password: config["password"],
rejectUnauthorized: false
};
this.caption = config["caption"];
this.topicStatusGet = config["topics"].statusGet;
this.topicStatusSet = config["topics"].statusSet;
this.onValue = (config["onValue"] !== undefined) ? config["onValue"]: "true";
this.offValue = (config["offValue"] !== undefined) ? config["offValue"]: "false";
if (config["integerValue"]) {
this.onValue = "1";
this.offValue = "0";
}
this.statusCmd = config["statusCmd"];
this.switchStatus = false;
this.service = new Service.Switch(this.name);
this.service
.getCharacteristic(Characteristic.On)
.on('get', this.getStatus.bind(this))
.on('set', this.setStatus.bind(this));
// connect to MQTT broker
this.client = mqtt.connect(this.url, this.options);
var that = this;
this.client.on('error', function () {
that.log('Error event on MQTT');
});
this.client.on('message', function (topic, message) {
if (topic == that.topicStatusGet) {
var status = message.toString();
if (status == that.onValue || status == that.offValue) {
that.switchStatus = (status == that.onValue) ? true : false;
that.service.getCharacteristic(Characteristic.On).setValue(that.switchStatus, undefined, 'fromSetValue');
}
}
});
this.client.subscribe(this.topicStatusGet);
}
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-mqttswitch", "mqttswitch", MqttSwitchAccessory);
}
MqttSwitchAccessory.prototype.getStatus = function(callback) {
if (this.statusCmd !== undefined) {
this.client.publish(this.topicStatusSet, this.statusCmd, this.publish_options);
}
callback(null, this.switchStatus);
}
MqttSwitchAccessory.prototype.setStatus = function(status, callback, context) {
if(context !== 'fromSetValue') {
this.switchStatus = status;
this.client.publish(this.topicStatusSet, status ? this.onValue : this.offValue, this.publish_options);
}
callback();
}
MqttSwitchAccessory.prototype.getServices = function() {
return [this.service];
}