forked from aklambeth/bg-hive-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (116 loc) · 4.32 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
var request = require('request');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Hub = require('./hubs');
var connection = require('./Connection.js');
var config = {
credentials : {
"username": '', "password": ''
},
api : {
"Hive": "https://api.bgchlivehome.co.uk/v5",
"AlertMe" : "https://api.alertme.com/v5"
}
}
function Hive(username, password, api) {
config.credentials.username = username;
config.credentials.password = password;
if (!api)
api = 'Hive';
if (api == "AlertMe")
connection.context.domain = config.api.AlertMe;
else
connection.context.domain = config.api.Hive;
}
util.inherits(Hive, EventEmitter);
logout = function(self){
var logoutTask = {
logout:{
POST:{}
}
}
connection.command.push(logoutTask, function (error, response, body) {
if (!error && response.statusCode == 204) {
connection.context.authToken = undefined;
connection.context.username = undefined;
self.emit('logout');
}
else {
console.log(response.statusCode);
}
// stop the queue from processing and clear down all tasks
connection.command.pause();
connection.command.kill();
});
}
Hive.prototype.Login = function() {
var loginTask = {
login:{
POST:{
"username": config.credentials.username,
"password": config.credentials.password,
"caller": "HiveHome"
}
}
}
var self = this;
// if we're already connected then re-use the existing connection
if (connection.context.authToken && connection.context.username) {
var hub = new Hub(JSON.parse('{\"users\":{\"' + connection.context.username + '\":{}}}'));
hub.FindController(function(deviceId){
// remove the drain callback which may log us out again
connection.command.drain = undefined;
self.emit('login', deviceId);
});
} else {
// re-authenticate our credentials with the server
connection.command.unshift(loginTask, function(error, response, body){
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
var j = request.jar();
j.setCookie(request.cookie('ApiSession=' + data.ApiSession), config.api.Hive);
connection.context.authToken = j;
connection.context.username = data.username;
var userObject = JSON.parse('{\"users\":{\"' + data.username + '\":{}}}');
if (data.hubIds && data.hubIds.length > 0) {
connection.context.hubs[0].id = data.hubIds[0];
var hub = new Hub(userObject);
hub.FindController(function(deviceId){
// remove the drain callback which may log us out again
connection.command.drain = undefined;
self.emit('login', deviceId);
});
}
} else {
var errorReason = JSON.parse(response.body);
if (response.statusCode == 400) {
if (errorReason.error.reason == 'USERNAME_PASSWORD_ERROR') {
self.emit('not_authorised', errorReason);
} else if (errorReason.error.reason == 'ACCOUNT_LOCKED') {
self.emit('locked', errorReason);
} else if (errorReason.error.reason == 'TOKENS_ARE_NOT_COMPATIBLE_WITH_LOGIN') {
self.emit('invalid', errorReason);
}
} else if (response.statusCode == 500) {
self.emit('unavailable', errorReason);
} else if (response.statusCode == 503) {
self.emit('rate_limit', errorReason);
}
}
});
}
// resume processing the queue
if (connection.command.paused)
connection.command.resume();
}
Hive.prototype.Logout = function() {
var self = this;
if (!connection.command.idle()) {
connection.command.drain = function(){
logout(self);
};
} else {
logout(self);
}
};
module.exports = Hive;