-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoon.js
128 lines (99 loc) · 2.98 KB
/
toon.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
"use strict";
var request = require('sync-request');
var uuid = require('uuid-js');
function Toon(credentials) {
this.endpoint = 'https://toonopafstand.eneco.nl/toonMobileBackendWeb/client';
this.credentials = credentials;
this.session = null;
this.state = null;
/**
* Login on initialization for testing purposes
*/
this.login();
}
Toon.prototype.login = function() {
var response = request('GET', this.endpoint + '/login', {qs: this.credentials});
this.session = JSON.parse(response.body.toString());
var params = {
clientId: this.session.clientId,
clientIdChecksum: this.session.clientIdChecksum,
agreementId: this.session.agreements[0].agreementId,
agreementIdChecksum: this.session.agreements[0].agreementIdChecksum,
random: uuid.create(1)
};
request('GET', this.endpoint + '/auth/start', {qs: params});
};
Toon.prototype.logout = function() {
var response = request('GET', this.endpoint + '/login', {qs: this.credentials});
this.session = JSON.parse(response.body.toString());
var params = {
clientId: this.session.clientId,
clientIdChecksum: this.session.clientIdChecksum,
random: uuid.create(1)
};
request('GET', this.endpoint + '/auth/logout', {qs: params});
this.session = null;
this.state = null;
};
Toon.prototype.retrieve_state = function() {
if (this.state) {
return false;
}
var params = {
clientId: this.session.clientId,
clientIdChecksum: this.session.clientIdChecksum,
random: uuid.create(1)
};
var response = request('GET', this.endpoint + '/auth/retrieveToonState', {qs: params});
this.state = JSON.parse(response.body.toString());
};
/**
* Toon methods
*/
Toon.prototype.refresh_state = function() {
this.state = null;
this.retrieve_state();
};
Toon.prototype.get_gas_usage = function() {
this.retrieve_state();
return this.state.gasUsage;
};
Toon.prototype.get_power_usage = function() {
this.retrieve_state();
return this.state.powerUsage;
};
Toon.prototype.get_thermostat_info = function() {
this.retrieve_state();
return this.state.thermostatInfo;
};
Toon.prototype.get_thermostat_states = function() {
this.retrieve_state();
return this.state.thermostatStates;
};
Toon.prototype.get_program_state = function() {
this.retrieve_state();
return this.state.thermostatInfo.activeState;
};
Toon.prototype.set_thermostat = function(temperature) {
temperature = temperature * 100;
var params = {
clientId: this.session.clientId,
clientIdChecksum: this.session.clientIdChecksum,
value: temperature,
random: uuid.create(1)
};
var response = request('GET', this.endpoint + '/auth/setPoint', {qs: params});
return JSON.parse(response.body.toString());
};
Toon.prototype.set_program_state = function(state) {
var params = {
clientId: this.session.clientId,
clientIdChecksum: this.session.clientIdChecksum,
state: 2,
temperatureState: state,
random: uuid.create(1)
};
var response = request('GET', this.endpoint + '/auth/schemeState', {qs: params});
return JSON.parse(response.body.toString());
};
module.exports = Toon;