-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
81 lines (66 loc) · 2.46 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
//DEBUG=* ./bin/homebridge -D -P ../homebridge-CurrentAmbientLightLevel/
var Service, Characteristic;
var request = require('sync-request');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-CurrentAmbientLightLevel", "Light", HttpTemperature);
}
function HttpTemperature(log, config) {
this.log = log;
// url info
this.url = config["url"];
this.http_method = config["http_method"] || "GET";
this.name = config["name"];
this.manufacturer = config["manufacturer"] || "@lagunacomputer";
this.model = config["model"] || "Model not available";
this.serial = config["serial"] || "Non-defined serial";
}
HttpTemperature.prototype = {
httpRequest: function (url, body, method, username, password, sendimmediately, callback) {
cons
request({
url: url,
body: body,
method: method,
rejectUnauthorized: false
},
function (error, response, body) {
callback(error, response, body)
})
},
getState: function (callback) {
var body;
var res = request(this.http_method, this.url, {});
if(res.statusCode > 400){
this.log('HTTP get state function failed');
callback(error);
} else {
this.log('HTTP get state function succeeded!');
var info = JSON.parse(res.body);
this.temperatureService.setCharacteristic(
Characteristic.CurrentAmbientLightLevel,
info.lightval
);
this.log(info);
this.lightval = info.lightval;
callback(null, this.lightval);
}
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function () {
this.informationService = new Service.AccessoryInformation();
this.informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
this.temperatureService = new Service.LightSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getState.bind(this));
return [this.informationService, this.temperatureService];
}
};