-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
358 lines (331 loc) · 10.5 KB
/
index.js
File metadata and controls
358 lines (331 loc) · 10.5 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
MIT License
Copyright (c) 2017 Graham White
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
Library to exercise the British Gas Hive version 6 API
Based on the work of James Saunders at
http://www.smartofthehome.com/2016/05/hive-rest-api-v6/
*/
'use strict';
var request = require('request');
var hive = module.exports = {};
/**
* Config object to change the behaviour of the module
*/
hive.config = {
username:'',
password:'',
endpoint:'https://api-prod.bgchprod.info:443/omnia',
idleMinutes: 20,
accessTime: 0,
headers: {
'Content-Type': 'application/vnd.alertme.zoo-6.1+json',
'Accept': 'application/vnd.alertme.zoo-6.1+json',
'X-Omnia-Client': 'Hive Web Dashboard'
}
}
/**
* Initialises the config object
* Allows any part of the config to be overridden
* Requires at least config.username and config.password are set
*/
hive.init = function(config) {
Object.assign(hive.config,config);
}
/**
* Determines if a new login attempt is required
* Works out if an API call has been made recently (defaults to 20 minutes)
* Returns a boolean
*/
hive.needToLogin = function() {
var epoch = new Date().getTime();
if ((epoch - hive.config.accessTime) / 60000 < hive.config.idleMinutes)
return false;
else
return true;
}
/**
* Login to Hive
* Requires a valid hive.config object has been set
* Returns a Promise which is resolved with nothing or rejected with an error
*/
hive.login = function() {
return new Promise(function(resolve,reject) {
if (!hive.needToLogin()) {
resolve();
} else {
var body = {
sessions: [{
username: hive.config.username,
password: hive.config.password,
caller: 'WEB'
}]
}
var options = {
url: hive.config.endpoint + "/auth/sessions",
headers: hive.config.headers,
method: 'POST',
body: JSON.stringify(body)
}
hive.config.accessTime = new Date().getTime();
request(options, function(error, response, body) {
try {
var responseJson = JSON.parse(body);
} catch(e) {
reject({err:e});
}
if (!error && response.statusCode == 200) {
hive.config.headers['X-Omnia-Access-Token'] = responseJson.sessions[0].sessionId;
resolve();
} else if (responseJson.error) {
reject(responseJson);
} else if (error) {
reject(error);
} else {
reject({error:"Unknown error"});
}
});
}
})
}
/**
* Wrapper function to divert to a real hive API call after a login attempt
*/
hive.loginAndThen = function (hiveCB, cb) {
hive.login().then(function(result) {
hiveCB(cb);
}, function(err) {
cb(err);
});
}
/**
* Wrapper function to API call to make sure there is a valid login
*/
hive.getDevices = function(cb) {
hive.loginAndThen(hive._getDevices, cb);
}
/**
* Get all devices
* Should not be called directly, use the wrapper function getDevices() to ensure a login first
* Returns to the callback a JSON Object of devices or an error object or array of errors
*/
hive._getDevices = function(cb) {
var options = {
url: hive.config.endpoint + "/nodes",
headers: hive.config.headers,
method: 'GET',
}
hive.config.accessTime = new Date().getTime();
request(options, function(error, response, body) {
try {
var responseJson = JSON.parse(body);
} catch(e) {
cb({err:e});
}
if (!error && response.statusCode == 200) {
cb(responseJson);
} else if (responseJson.error) {
cb(responseJson);
} else if (error) {
cb(error);
} else {
cb({error:"Unknown error"});
}
});
}
/**
* Get the current state of the heating relay
* Returns to the callback "ON", "OFF", "unknown" or an error object or array of errors
*/
hive.heatingIsOn = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var stateHeatingRelay = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHeatingRelay) {
stateHeatingRelay = result.nodes[i].attributes.stateHeatingRelay.reportedValue || "unknown";
break;
}
}
cb(stateHeatingRelay);
}
})
}
/**
* Get the current "in room" heating temperature
* Returns to the callback the temperature as a string or "unknown" or an error object or array of errors
*/
hive.getInRoomTemperature = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var temperature = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHeatingRelay) {
temperature = result.nodes[i].attributes.temperature.reportedValue || "unknown";
break;
}
}
cb(temperature);
}
})
}
/**
* Get the target heating temperature
* Returns to the callback the temperature as a string or "unknown" or an error object or array of errors
*/
hive.getTargetHeatTemperature = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var targetHeatTemperature = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHeatingRelay) {
targetHeatTemperature = result.nodes[i].attributes.targetHeatTemperature.reportedValue || "unknown";
break;
}
}
cb(targetHeatTemperature);
}
})
}
/**
* Get the frost protect temperature
* Returns to the callback the temperature as a string or "unknown" or an error object or array of errors
*/
hive.getFrostProtectTemperature = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var frostProtectTemperature = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHeatingRelay) {
frostProtectTemperature = result.nodes[i].attributes.frostProtectTemperature.reportedValue || "unknown";
break;
}
}
cb(frostProtectTemperature);
}
})
}
/**
* Get the minimum heating temperature
* Returns to the callback the temperature as a string or "unknown" or an error object or array of errors
*/
hive.getMinHeatTemperature = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var minTemperature = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHeatingRelay) {
minTemperature = result.nodes[i].attributes.minHeatTemperature.reportedValue || "unknown";
break;
}
}
cb(minTemperature);
}
})
}
/**
* Get the maximum temperature
* Returns to the callback the temperature as a string or "unknown" or an error object or array of errors
*/
hive.getMaxHeatTemperature = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var maxTemperature = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHeatingRelay) {
maxTemperature = result.nodes[i].attributes.maxHeatTemperature.reportedValue || "unknown";
break;
}
}
cb(maxTemperature);
}
})
}
/**
* Get the current heating schedule
* Returns to the callback the schedule as a object or "unknown" or an error object or array of errors
*/
hive.getHeatingSchedule = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var heatingSchedule = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHeatingRelay) {
heatingSchedule = result.nodes[i].attributes.schedule.reportedValue || "unknown";
break;
}
}
cb(heatingSchedule);
}
})
}
/**
* Get the current state of the hot water relay
* Returns to the callback "ON", "OFF", "unknown" or an error object or array of errors
*/
hive.hotWaterIsOn = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var stateHotWaterRelay = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHotWaterRelay) {
stateHotWaterRelay = result.nodes[i].attributes.stateHotWaterRelay.reportedValue || "unknown";
break;
}
}
cb(stateHotWaterRelay);
}
})
}
/**
* Get the current hot water schedule
* Returns to the callback the schedule as a object or "unknown" or an error object or array of errors
*/
hive.getHotWaterSchedule = function(cb) {
hive.getDevices(function(result) {
if (result.error || result.errors) {
cb(result);
} else {
var hotWaterSchedule = "unknown";
for (var i=0; i<result.nodes.length; i++) {
if (result.nodes[i].name === "Receiver" && result.nodes[i].attributes.stateHotWaterRelay) {
hotWaterSchedule = result.nodes[i].attributes.schedule.reportedValue || "unknown";
break;
}
}
cb(hotWaterSchedule);
}
})
}