-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
116 lines (105 loc) · 2.82 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
var Promise = require('bluebird');
var request = require('request');
request = Promise.promisify(request.defaults({ jar: true }));
var Sensi = function(options) {
this.username = options.username;
this.password = options.password;
this.baseUrl = options.baseUrl || 'https://bus-serv.sensicomfort.com';
this.defaultHeaders = {
'X-Requested-With': 'XMLHttpRequest', // needed to get cookies instead of token
'Accept': 'application/json; version=1, */*; q=0.01'
};
}
Sensi.prototype.start = function() {
return this.authorize().then(function() {
return this.negotiate();
}.bind(this));
};
Sensi.prototype.authorize = function() {
var options = {
url: this.baseUrl + '/api/authorize',
method: 'POST',
headers: this.defaultHeaders,
json: {
'UserName': this.username,
'Password': this.password
}
}
return request(options);
};
Sensi.prototype.negotiate = function() {
var options = {
url: this.baseUrl + '/realtime/negotiate',
method: 'GET',
headers: this.defaultHeaders
};
return request(options).spread(function(_, body) {
var json = JSON.parse(body);
this.connectionToken = json['ConnectionToken'];
return json;
}.bind(this));
};
// [
// {
// "ContractorId" : 0,
// "Country" : "US",
// "ICD" : "36-6f-92-ff-fe-01-8c-d7",
// "TimeZone" : "Eastern Standard Time",
// "DeviceName" : "4 South",
// "ZipCode" : "45202"
// },
// {
// "ICD" : "36-6f-92-ff-fe-01-94-d4",
// "ContractorId" : 0,
// "Country" : "US",
// "ZipCode" : "45202",
// "DeviceName" : "4 North",
// "TimeZone" : "Eastern Standard Time"
// },
// {
// "TimeZone" : "Eastern Standard Time",
// "ZipCode" : "45202",
// "DeviceName" : "5 South",
// "ICD" : "36-6f-92-ff-fe-01-98-8a",
// "ContractorId" : 0,
// "Country" : "US"
// },
// {
// "Country" : "US",
// "ContractorId" : 0,
// "ICD" : "36-6f-92-ff-fe-01-9f-cb",
// "TimeZone" : "Eastern Standard Time",
// "DeviceName" : "5 North",
// "ZipCode" : "45202"
// }
// ]
Sensi.prototype.thermostats = function() {
var options = {
url: this.baseUrl + '/api/thermostats',
method: 'GET',
headers: this.defaultHeaders
};
return request(options).spread(function(_, body) {
return JSON.parse(body);
});
};
Sensi.prototype.setHeat = function(options) {
var httpOptions = {
url: this.baseUrl + '/realtime/send',
method: 'POST',
qs: {
transport: 'longPolling',
connectionToken: this.connectionToken,
},
form: {
data: JSON.stringify({
'H': 'thermostat-v1',
'M': 'SetHeat',
'A': [options.icd, options.temperature, options.scale],
'I': 1
})
}
};
return request(httpOptions);
};
module.exports = Sensi;