-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (98 loc) · 4.61 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
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
/*jshint sub:true*/
var request = require("request");
var Service, Characteristic;
module.exports = function(homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory("homebridge-heater", "HomeHeater", HomeHeaterAccessory);
};
function HomeHeaterAccessory(log, config) {
this.log = log;
this.name = config["name"];
this.url = config["url"];
this.type = config["type"] || "page";
this.json_url = config["json_url"] || null;
this.temp_url = config["temp_url"];
this.humi_url = config["humi_url"];
this.light_url = config["light_url"] || null;
this.freq = config["freq"] || 1000;
this.temperature = 0;
this.humidity = 0;
this.light = 0;
this.services = [];
this.temperatureService = new Service.TemperatureSensor ("Temperature Sensor");
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getValue.bind(this, 'temperature'));
this.services.push(this.temperatureService);
this.humidityService = new Service.HumiditySensor ("Humidity Sensor");
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getValue.bind(this, 'humidity'));
this.services.push(this.humidityService);
if(this.light_url !== null){
this.lightService = new Service.LightSensor ("Light Sensor");
this.lightService
.getCharacteristic(Characteristic.CurrentAmbientLightLevel)
.on('get', this.getValue.bind(this, 'light'));
this.services.push(this.lightService);
}
setInterval(() => {
this.getValue(null, (err, { humidity, temperature, light}) => {
console.log("humidity " + humidity + ", temperature: " + temperature + ", light: " + light);
this.temperatureService
.setCharacteristic(Characteristic.CurrentTemperature, temperature);
this.humidityService
.setCharacteristic(Characteristic.CurrentRelativeHumidity, humidity);
if(this.light_url !== null){
this.lightService
.setCharacteristic(Characteristic.CurrentAmbientLightLevel, light);
}
});}, this.freq);
}
HomeHeaterAccessory.prototype.getValue = function(name, callback) {
console.log("getValue: " + name);
// if(this.type == "page"){
// request(this.url + this.temp_url, (error, response, body) => {
// if (!error && response.statusCode == 200) {
var temperature = 39;
if(name == "temperature"){
return callback(null, temperature);
}
else{
// request(this.url + this.temp_url, (error, response, body) => {
// if (!error && response.statusCode == 200) {
var humidity = 99;
if(name == "humidity"){
return callback(null, humidity);
}
else{
// request(this.url + this.temp_url, (error, response, body) => {
// if (!error && response.statusCode == 200) {
var light = 109;
if(name == "light"){
return callback(null, light);
}
else{
return callback(null, { humidity: humidity, temperature: temperature, light: light });
} //End: else, name != "humidity"
// } //End: if OK respone
// }); //End: request light
} //End: else, name != "humidity"
// } //End: if OK respone
// }); //End: request humidity
} //End: else, name != "temperature"
// } //End: if OK respone
// }); //End: request temperature
// } else {
// request(this.url + this.json_url, (error, response, body) => {
// if (!error && response.statusCode == 200) {
// var obj = JSON.parse(body);
// return callback(null, { humidity: obj.humidity, temperature: obj.temperature, light: obj.light });
// }
// });
// }
};
HomeHeaterAccessory.prototype.getServices = function() {
return this.services;
};