forked from siavashg/homebridge-glue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
125 lines (100 loc) · 4.1 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
var request = require("request");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-glue", "glue-lock", LockAccessory);
}
function LockAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.url = config["url"] || "https://api.gluehome.com/api";
this.hubID = config["hub-id"];
this.lockID = config["lock-id"];
this.username = config["username"];
this.password = config["password"];
this.lockservice = new Service.LockMechanism(this.name);
this.lockservice
.getCharacteristic(Characteristic.LockCurrentState)
.on('get', this.getState.bind(this));
this.lockservice
.getCharacteristic(Characteristic.LockTargetState)
.on('get', this.getState.bind(this))
.on('set', this.setState.bind(this));
this.battservice = new Service.BatteryService(this.name);
this.battservice
.getCharacteristic(Characteristic.BatteryLevel)
.on('get', this.getBattery.bind(this));
this.battservice
.getCharacteristic(Characteristic.StatusLowBattery)
.on('get', this.getLowBatt.bind(this));
}
LockAccessory.prototype.getState = function(callback) {
callback(null, Characteristic.LockCurrentState.UNKNOWN)
}
LockAccessory.prototype.getCharging = function(callback) {
callback(null, Characteristic.ChargingState.NOT_CHARGING);
}
LockAccessory.prototype.getBattery = function(callback) {
this.log("Getting battery level");
request.get({
url: this.url + "/Locks/" + this.lockID,
auth: { user: this.username, password: this.password }
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
var batt = json.BatteryStatusAfter / 255 * 100;
this.log("Battery level is %s", batt);
callback(null, batt); // success
}
else {
this.log("Error getting battery level (status code %s): %s", response.statusCode, err);
callback(err);
}
}.bind(this));
}
LockAccessory.prototype.getLowBatt = function(callback) {
this.log("Getting current battery...");
request.get({
url: this.url + "/Locks/" + this.lockID,
auth: { user: this.username, password: this.password }
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
var batt = json.BatteryStatusAfter / 255 * 100;
this.log("Lock battery is %s", batt);
var low = (batt > 20) ? Characteristic.StatusLowBattery.BATTERY_LEVEL_NORMAL : Characteristic.StatusLowBattery.BATTERY_LEVEL_LOW;
callback(null, low); // success
}
else {
this.log("Error getting battery (status code %s): %s", response.statusCode, err);
callback(err);
}
}.bind(this));
}
LockAccessory.prototype.setState = function(state, callback) {
this.log('state', state)
var lockState = (state == Characteristic.LockTargetState.SECURED) ? "1" : "0";
this.log("Set state to %s", lockState);
request.post({
url: this.url + "/Hubs/" + this.hubID + '/Commands',
auth: { user: this.username, password: this.password },
form: { LockId: this.lockID, HubCommand: lockState }
}, function(err, response, body) {
if (!err && response.statusCode == 200) {
var json = JSON.parse(body);
// TODO Check status...
this.log("State change complete.");
// we succeeded, so update the "current" state as well
this.lockservice.setCharacteristic(Characteristic.LockCurrentState, state);
callback(null); // success
}
else {
this.log("Error '%s' setting lock state. Response: %s", err, body);
callback(err || new Error("Error setting lock state."));
}
}.bind(this));
}
LockAccessory.prototype.getServices = function() {
return [this.lockservice, this.battservice];
}