forked from jortgies/homebridge-rademacher-blinds
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRademacherEnvironmentSensorAccessory.js
162 lines (145 loc) · 7.33 KB
/
RademacherEnvironmentSensorAccessory.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var tools = require("./tools.js");
var RademacherAccessory = require("./RademacherAccessory.js");
function RademacherEnvironmentSensorAccessory(log, debug, accessory, sensor, session, inverted) {
RademacherAccessory.call(this, log, debug, accessory, sensor, session, inverted);
this.sensor = sensor;
this.services = [this.service];
// temperature sensor
this.currentTemperature=sensor.readings.temperature_primary;
var temperatureService = this.accessory.getService(global.Service.TemperatureSensor);
temperatureService.getCharacteristic(global.Characteristic.CurrentTemperature)
.setProps({minValue: -30.0, maxValue: 80.0})
.setValue(this.currentTemperature)
.on('get', this.getCurrentTemperature.bind(this));
this.services.push(temperatureService);
// light sensor
this.currentAmbientLightLevel=sensor.readings.sun_brightness;
var lightService = this.accessory.getService(global.Service.LightSensor);
lightService.getCharacteristic(global.Characteristic.CurrentAmbientLightLevel)
.setProps({minValue: 0, maxValue: 150000})
.setValue(this.currentAmbientLightLevel)
.on('get', this.getCurrentAmbientLightLevel.bind(this));
this.services.push(lightService);
// Rain sensor
this.currentRainState=this.sensor.readings.rain_detected;
var rainsensorService = this.accessory.getService(global.Service.ContactSensor);
rainsensorService.getCharacteristic(global.Characteristic.ContactSensorState)
.setValue(this.currentRainState)
.on('get', this.getCurrentRainState.bind(this));
this.services.push(rainsensorService);
// TODO configure interval
setInterval(this.update.bind(this), 10000);
}
RademacherEnvironmentSensorAccessory.prototype = Object.create(RademacherAccessory.prototype);
RademacherEnvironmentSensorAccessory.prototype.getCurrentTemperature = function (callback) {
if (this.debug) this.log("%s [%s] - getCurrentTemperature()", this.accessory.displayName, this.sensor.did);
callback(null, this.currentTemperature);
var self = this;
this.session.get("/v4/devices?devtype=Sensor", 30000, function(err, body) {
if(err)
{
self.log("%s [%s] - getCurrentTemperature(): error=%s", self.accessory.displayName, self.sensor.did,err);
return;
}
body.meters.forEach(function(data) {
if(data.did == self.sensor.did)
{
self.currentTemperature=data.readings.temperature_primary;
if (self.debug) self.log("%s [%s] - getCurrentTemperature(): temperature=%s", self.accessory.displayName, self.sensor.did, self.currentTemperature);
var temperatureService = self.accessory.getService(global.Service.TemperatureSensor);
temperatureService.getCharacteristic(global.Characteristic.CurrentTemperature).updateValue(self.currentTemperature)
}
});
});
};
RademacherEnvironmentSensorAccessory.prototype.getCurrentRainState = function (callback) {
if (this.debug) this.log("%s [%s] - getCurrentRainState()", this.accessory.displayName, this.sensor.did);
callback(null, this.currentRainState);
var self = this;
this.session.get("/v4/devices?devtype=Sensor", 30000, function(err, body) {
if(err)
{
self.log("%s [%s] - getCurrentRainState(): error=%s", self.accessory.displayName, self.sensor.did,err);
return;
}
body.meters.forEach(function(data) {
if(data.did == self.sensor.did)
{
self.currentRainState=data.readings.rain_detected
if (self.debug) self.log("%s [%s] - getCurrentRainState(): rain_detected=%s", self.accessory.displayName, self.sensor.did, self.currentRainState);
var rainsensorService = self.accessory.getService(global.Service.ContactSensor);
rainsensorService.getCharacteristic(global.Characteristic.ContactSensorState).updateValue(self.currentRainState);
}
});
});
};
RademacherEnvironmentSensorAccessory.prototype.getCurrentAmbientLightLevel = function (callback) {
if (this.debug) this.log("%s [%s] - getCurrentAmbientLightLevel()", this.accessory.displayName, this.sensor.did);
callback(null,this.currentAmbientLightLevel);
var self = this;
this.session.get("/v4/devices?devtype=Sensor", 30000, function(err, body) {
if(err)
{
self.log("%s [%s] - getCurrentAmbientLightLevel(): error=%s", self.accessory.displayName, self.sensor.did,err);
return;
}
body.meters.forEach(function(data) {
if(data.did == self.sensor.did)
{
self.currentAmbientLightLevel=data.readings.sun_brightness;
if (self.debug) self.log("%s [%s] - getCurrentAmbientLightLevel(): sun_brightness=%s", self.accessory.displayName, self.sensor.did, self.currentAmbientLightLevel);
var lightService = self.accessory.getService(global.Service.LightSensor);
lightService.getCharacteristic(global.Characteristic.CurrentAmbientLightLevel).updateValue(self.currentAmbientLightLevel); }
});
});
};
RademacherEnvironmentSensorAccessory.prototype.getServices = function () {
return this.services;
};
RademacherEnvironmentSensorAccessory.prototype.update = function() {
if (this.debug) this.log(`%s [%s] - update()`, this.accessory.displayName, this.sensor.did);
var self = this;
this.getCurrentTemperature(function(err, temperature) {
if (err)
{
self.log(`%s [%s] - update().getCurrentTemperature(): error=%s`, self.accessory.displayName, self.sensor.did, err);
}
else if (temperature===null)
{
self.log(`%s [%s] - update().getCurrentTemperature(): got null temperature`, self.accessory.displayName, self.sensor.did);
}
else
{
if (self.debug) self.log(`%s [%s] - update().getCurrentTemperature(): temperature=%s`, self.accessory.displayName, self.sensor.did, temperature);
}
}.bind(this));
this.getCurrentAmbientLightLevel(function(err, level) {
if (err)
{
self.log(`%s [%s] - update().getCurrentAmbientLightLevel(): error=%s`, self.accessory.displayName, self.sensor.did, err);
}
else if (level===null)
{
self.log(`%s [%s] - update().getCurrentAmbientLightLevel(): got null level`, self.accessory.displayName, self.sensor.did);
}
else
{
if (self.debug) self.log(`%s [%s] - update().getCurrentAmbientLightLevel(): level=%s`, self.accessory.displayName, self.sensor.did, level);
}
}.bind(this));
this.getCurrentRainState(function(err, rain_detected) {
if (err)
{
self.log(`%s [%s] - update().getCurrentRainState(): error=%s`, self.accessory.displayName, self.sensor.did, err);
}
else if (rain_detected===null)
{
self.log(`%s [%s] - update().getCurrentRainState(): got null state`, self.accessory.displayName, self.sensor.did);
}
else
{
if (self.debug) self.log(`%s [%s] - update().getCurrentRainState(): state=%s`, self.accessory.displayName, self.sensor.did, rain_detected);
}
}.bind(this));
};
module.exports = RademacherEnvironmentSensorAccessory;