forked from jortgies/homebridge-rademacher-blinds
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathRademacherAccessory.js
58 lines (51 loc) · 1.92 KB
/
RademacherAccessory.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
function RademacherAccessory(log, debug, accessory, data, session) {
var info = accessory.getService(global.Service.AccessoryInformation);
accessory.context.manufacturer = "Rademacher";
info.setCharacteristic(global.Characteristic.Manufacturer, accessory.context.manufacturer.toString());
if (data.deviceNumber)
{
accessory.context.model = data.deviceNumber;
info.setCharacteristic(global.Characteristic.Model, accessory.context.model.toString());
}
if (data.uid)
{
accessory.context.serial = data.uid;
}
else if (data.sid)
{
accessory.context.serial = data.sid;
}
info.setCharacteristic(global.Characteristic.SerialNumber, accessory.context.serial.toString());
accessory.context.revision = 1; //data.version;
info.setCharacteristic(global.Characteristic.FirmwareRevision, accessory.context.revision.toString());
this.accessory = accessory;
this.log = log;
this.debug = debug
this.session = session;
this.did = data.did;
this.lastUpdate = 0;
this.device = null;
}
RademacherAccessory.prototype.getDevice = function(callback) {
if (this.lastUpdate < Date.now()) {
var self = this;
this.session.get("/v4/devices/" + this.did, 30000, function(e, body) {
if(e) return callback(new Error("Request failed: "+e), null);
if (body && (body.hasOwnProperty("device") || body.hasOwnProperty("meter")))
{
var device = body.hasOwnProperty("device")?body.device:body.meter;
self.device = device.data;
self.lastUpdate = Date.now();
callback(null, device)
}
else
{
self.log('no device, no meter');
callback(null, self.device);
}
});
} else {
callback(null, this.device);
}
};
module.exports = RademacherAccessory;