This repository has been archived by the owner on Oct 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
45 lines (41 loc) · 1.47 KB
/
util.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
var request = require('request');
module.exports.createAccessory = function (device, UUIDGen, Accessory) {
uuid = UUIDGen.generate(device.name);
var newAccessory = new Accessory(device.name, uuid);
newAccessory.context.idFromConrad = device.id
newAccessory.context.type = device.metadata.types[0];
newAccessory.context.characteristics = device.metadata.properties;
return newAccessory;
}
module.exports.getOrAddCharacteristic = function(service, characteristic) {
var result = service.getCharacteristic(characteristic);
if (result == null) {
result = service.addCharacteristic(characteristic);
}
return result;
}
module.exports.getStatus = function (callback) {
var platform = this.platform;
var accessory = this.accessory;
var property = this.property;
platform.log(`getting value for device ${accessory.context.idFromConrad}`);
request.post(platform.config.postUrl, {
json: {
action: "status",
device: accessory.context.idFromConrad,
property: property
},
headers: {
'Authorization': `Bearer ${platform.config.bearerToken}`
}
}, (error, res, body) => {
if (error || !body.result || !body.result.value) {
platform.log(error)
platform.log(`statusCode: ${res.statusCode}`)
platform.log(body)
callback(error);
return;
}
callback(null, body.result.value);
});
}