forked from tamasharasztosi/homebridge-thingspeak
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
171 lines (155 loc) · 4.92 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
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
163
164
165
166
167
168
169
/*
* Homebridge-Plugin for Thingspeak Temperature and Humidity-Sensors
* based on HttpMultisensor
*
* Sensor Request example URL:
* https://api.thingspeak.com/channels/num_of_channel/field/1/last.json
*
* Sensor returns
* {"{"created_at":"2017-12-23T16:30:53Z","entry_id":16113,"field1":"7.2"}"}
*
* License: MIT
*
* (C) tamasharasztosi, 2017
*/
var request = require('request');
module.exports = function (homebridge) {
Service = homebridge.hap.Service;
Characteristic = homebridge.hap.Characteristic;
homebridge.registerAccessory(
"homebridge-ts", // PluginName
"Httpthingspeak", // accessoryName
Httpthingspeak // constructor
);
}
function Httpthingspeak(log, config) {
this.log = log;
this.debug = config["debug"] || false;
this.debug && this.log('Httpthingspeak: reading config');
// url info
this.url = config["url"];
this.http_method = config["http_method"] || "GET";
this.name = config["name"];
this.type = config["type"];
this.manufacturer = config["manufacturer"] || "Sample Manufacturer";
this.model = config["model"] || "Sample Model";
this.serial = config["serial"] || "Sample Serial";
this.temperatureService;
this.humidityService;
this.rotationSpeedService
}
Httpthingspeak.prototype = {
httpRequest: function (url, method, callback) {
this.debug && this.log('httpRequest: '+method+' '+url);
request({
uri: url,
method: method,
rejectUnauthorized: false
},
function (error, response, body) {
callback(error, response, body)
})
},
getSensorTemperatureValue: function (callback) {
this.debug && this.log('getSensorTemperatureValue');
this.httpRequest(this.url,this.http_method,function(error, response, body) {
if (error) {
this.log('HTTP get failed: %s', error.message);
callback(error);
} else {
this.debug && this.log('HTTP success. Got result ['+body+']');
for(const k in JSON.parse(body)) {
if(k.indexOf("field") > -1)
var value = parseFloat(JSON.parse(body)[k]);
}
this.temperatureService.setCharacteristic(
Characteristic.CurrentTemperature,
value
);
callback(null, value);
}
}.bind(this));
},
getSensorHumidityValue: function (callback) {
this.debug && this.log('getSensorHumidityValue');
this.httpRequest(this.url,this.http_method,function(error, response, body) {
if (error) {
this.log('HTTP get failed: %s', error.message);
callback(error);
} else {
this.debug && this.log('HTTP success. Got result ['+body+']');
for(const k in JSON.parse(body)) {
if(k.indexOf("field") > -1)
var value = parseFloat(JSON.parse(body)[k]);
}
this.humidityService.setCharacteristic(
Characteristic.CurrentRelativeHumidity,
value
);
callback(null, value);
}
}.bind(this));
},
getSensorRotationSpeedValue: function (callback) {
this.debug && this.log('getRotationSpeedValue');
this.httpRequest(this.url,this.http_method,function(error, response, body) {
if (error) {
this.log('HTTP get failed: %s', error.message);
callback(error);
} else {
this.debug && this.log('HTTP success. Got result ['+body+']');
for(const k in JSON.parse(body)) {
if(k.indexOf("field") > -1)
var value = parseFloat(JSON.parse(body)[k]);
}
this.rotationSpeedService.setCharacteristic(
Characteristic.RotationSpeed,
value
);
callback(null, value);
}
}.bind(this));
},
identify: function (callback) {
this.log("Identify requested!");
callback(); // success
},
getServices: function () {
this.debug && this.log("getServiecs");
var services = [],
informationService = new Service.AccessoryInformation();
informationService
.setCharacteristic(Characteristic.Manufacturer, this.manufacturer)
.setCharacteristic(Characteristic.Model, this.model)
.setCharacteristic(Characteristic.SerialNumber, this.serial);
services.push(informationService);
switch (this.type) {
case "CurrentTemperature":
this.temperatureService = new Service.TemperatureSensor(this.name);
this.temperatureService
.getCharacteristic(Characteristic.CurrentTemperature)
.on('get', this.getSensorTemperatureValue.bind(this));
services.push(this.temperatureService);
break;
case "CurrentRelativeHumidity":
this.humidityService = new Service.HumiditySensor(this.name);
this.humidityService
.getCharacteristic(Characteristic.CurrentRelativeHumidity)
.on('get', this.getSensorHumidityValue.bind(this));
services.push(this.humidityService);
break;
case "RotationSpeed":
this.rotationSpeedService = new Service.Fan(this.name);
this.rotationSpeedService
.getCharacteristic(Characteristic.RotationSpeed)
.on('get', this.getSensorRotationSpeedValue.bind(this));
this.rotationSpeedService
.setCharacteristic(Characteristic.On, 1);
services.push(this.rotationSpeedService);
break;
default:
this.log('Error: unknown type: '+this.type+'. skipping...');
}
return services;
}
};