forked from jortgies/homebridge-rademacher-blinds
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRademacherDoorSensorAccessory.js
118 lines (107 loc) · 5.16 KB
/
RademacherDoorSensorAccessory.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
var tools = require("./tools.js");
var RademacherAccessory = require("./RademacherAccessory.js");
function RademacherDoorSensorAccessory(log, debug, accessory, sensor, session) {
RademacherAccessory.call(this, log, debug, accessory, sensor, session);
this.sensor = sensor;
this.services = [];
// contactsensor
this.currentState=this.sensor.readings.contact_state=="closed"?false:true
var contactsensorService = this.accessory.getService(global.Service.ContactSensor);
contactsensorService.getCharacteristic(global.Characteristic.ContactSensorState)
.setValue(this.currentState)
.on('get', this.getCurrentDoorState.bind(this));
this.services.push(contactsensorService);
// battery
this.currentBatteryLevel=this.sensor.batteryStatus;
var batteryService = this.accessory.getService(global.Service.Battery);
batteryService.getCharacteristic(global.Characteristic.BatteryLevel)
.setValue(this.currentBatteryLevel)
.on('get', this.getCurrentBatteryLevel.bind(this));
this.services.push(batteryService);
// TODO configure interval
setInterval(this.update.bind(this), 10000);
}
RademacherDoorSensorAccessory.prototype = Object.create(RademacherAccessory.prototype);
RademacherDoorSensorAccessory.prototype.getCurrentDoorState = function (callback) {
if (this.debug) this.log("%s [%s] - getCurrentDoorState()", this.accessory.displayName, this.sensor.did);
callback(null,this.currentState);
var self = this;
this.session.get("/v4/devices?devtype=Sensor", 30000, function(err, body) {
if(err)
{
self.log("%s [%s] - getCurrentDoorState(): error=%s", self.accessory.displayName, self.sensor.did,err);
return;
}
body.meters.forEach(function(data) {
if(data.did == self.sensor.did)
{
if (self.debug) self.log("%s [%s] - getCurrentDoorState(): readings=%s", self.accessory.displayName, self.sensor.did,data.readings);
var contact_state=data.readings.contact_state;
var closed=contact_state=="closed";
this.currentState=!closed;
if (self.debug) self.log("%s [%s] - getCurrentDoorState(): open=%s", self.accessory.displayName, self.sensor.did, this.currentState);
var contactsensorService = self.accessory.getService(global.Service.ContactSensor);
contactsensorService.getCharacteristic(Characteristic.ContactSensorState).updateValue(this.currentState);
}
});
});
};
RademacherDoorSensorAccessory.prototype.getCurrentBatteryLevel = function (callback) {
if (this.debug) this.log("%s [%s] - getCurrentBatteryLevel()", this.accessory.displayName, this.sensor.did);
callback(null,this.currentBatteryLevel);
var self = this;
this.session.get("/v4/devices?devtype=Sensor", 30000, function(err, body) {
if(err)
{
self.log("%s [%s] - getCurrentBatteryLevel(): error=%s", self.accessory.displayName, self.sensor.did,err);
return;
}
body.meters.forEach(function(data) {
if(data.did == self.sensor.did)
{
self.currentBatteryLevel=data.batteryStatus;
if (self.debug) self.log("%s [%s] - getCurrentBatteryLevel(): battery status=%s", self.accessory.displayName, self.sensor.did, self.currentBatteryLevel);
var batteryService = self.accessory.getService(global.Service.Battery);
batteryService.getCharacteristic(Characteristic.BatteryLevel).updateValue(self.currentBatteryLevel);
}
});
});
};
RademacherDoorSensorAccessory.prototype.getServices = function () {
return this.services;
};
RademacherDoorSensorAccessory.prototype.update = function() {
if (this.debug) this.log(`%s [%s] - update()`, this.accessory.displayName, this.sensor.did);
var self = this;
// Switch state
this.getCurrentDoorState(function(err, state) {
if (err)
{
self.log(`%s [%s] - update().getCurrentDoorState(): error=%s`, self.accessory.displayName, self.sensor.did, err);
}
else if (state===null)
{
self.log(`%s [%s] - update().getCurrentDoorState(): got null state`, self.accessory.displayName, self.sensor.did);
}
else
{
if (self.debug) self.log(`%s [%s] - update().getCurrentDoorState(): open=%s`, self.accessory.displayName, self.sensor.did, state);
}
}.bind(this));
// battery level
this.getCurrentBatteryLevel(function(err, level) {
if (err)
{
self.log(`%s [%s] - update().getCurrentBatteryLevel(): error=%s`, self.accessory.displayName, self.sensor.did, err);
}
else if (level===null)
{
self.log(`%s [%s] - update().getCurrentBatteryLevel(): got null battery level`, self.accessory.displayName, self.sensor.did);
}
else
{
if (self.debug) self.log(`%s [%s] - update().getCurrentBatteryLevel(): level=%s`, self.accessory.displayName, self.sensor.did, level);
}
}.bind(this));
};
module.exports = RademacherDoorSensorAccessory;