-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.js
198 lines (173 loc) · 6.34 KB
/
client.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
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
const FormData = require('form-data');
class EtekCityClient {
constructor() {
const HyperRequest = require('hyper-request');
this.client = new HyperRequest({
baseUrl: 'https://server1.vesync.com:4007',
disablePipe: true,
respondWithProperty: false,
parserFunction: function (data) {
return JSON.parse(data.replace(/\\/g, '').replace('"[', '[').replace(']"', ']'));
}
});
}
static get HISTORIC_STAT_TYPES() {
return {
DAY: 'day',
MONTH: 'month',
YEAR: 'year',
EXT_DAY: 'extDay'
}
};
async login(username, password) {
let formData = new FormData();
formData.append('Account', username);
formData.append('Password', password);
formData.append('AppVersion', '1.70.2');
formData.append('AppVersionCode', '111');
formData.append('OS', 'Android');
formData.append('DevToken', 'AkuEZmg_eu5m14eQRDxqYBsUzR-I7ZjaQtmKvU5Mw5a2');
let response = await this.client.post('/login', {
headers: Object.assign({
password: password,
account: username,
'Content-Type': 'application/x-www-form-urlencoded'
}, formData.getHeaders())
});
this.token = response.tk;
this.uniqueId = response.id;
return response;
}
async getDevices() {
let response = await this.client.post('/loadMain', {
headers: {
tk: this.token,
'Content-Type': 'application/x-www-form-urlencoded'
},
});
return response.devices.map((device) => {
return {
id: device.id,
name: device.deviceName,
status: device.relay
};
});
}
async getMeter(deviceId) {
let formData = new FormData();
formData.append('cid', deviceId);
formData.append('uri', '/getRuntime');
let response = await this.client.post('/devRequest', {
headers: Object.assign({
tk: this.token,
id: this.uniqueId,
uniqueId: this.uniqueId,
cid : deviceId,
'Content-Type': 'application/x-www-form-urlencoded',
}, formData.getHeaders()),
body : {
cid : deviceId,
uri : '/getRuntime'
}
});
if(response.power !== 'NaN'){
response.power = this.parseNumeric(response.power).current;
}
if(response.voltage !== 'NaN'){
response.voltage = this.parseNumeric(response.voltage).current;
}
if(response.current !== 'NaN'){
response.current = this.parseNumeric(response.current).current;
}
return response;
}
_round(value) {
return Math.round(value / 3600 * 1000) / 1000;
}
async getStats(deviceId,
reqDate = new Date().toISOString().slice(0,10).replace(/-/g,''),
timeZoneOffset = new Date().getTimezoneOffset() / -60,
round = true,
type = EtekCityClient.HISTORIC_STAT_TYPES.EXT_DAY) {
let formData = new FormData();
formData.append('cid', deviceId);
formData.append('date', reqDate);
formData.append('Type', type);
formData.append('zoneOffset', timeZoneOffset);
let response = await this.client.post('/loadStat', {
headers: Object.assign({
tk: this.token,
id: this.uniqueId,
uniqueId: this.uniqueId,
cid : deviceId,
'Content-Type': 'application/x-www-form-urlencoded',
}, formData.getHeaders()),
body : {
cid : deviceId,
date : reqDate,
Type: type,
zoneOffset: timeZoneOffset
}
});
if(type === EtekCityClient.HISTORIC_STAT_TYPES.EXT_DAY) {
return {
//API has it spelt cuurentDay...dont ask, adding response.currentDay incase they fix it
currentDay: round ? this._round(response.currentDay || response.cuurentDay) : (response.currentDay||response.cuurentDay),
sevenDay: round ? this._round(response.sevenDay) : response.sevenDay,
thirtyDay: round ? this._round(response.thirtyDay) : response.thirtyDay
};
}
else if(Array.isArray(response)){
return response.map(e => { return { value : e }; });//future put timestamp in
}
return response;
}
// This is pulled directly(ported to javascript) from the Mobile apps Java source code
parseNumeric(input) {
let split = input.split(':');
let v_stat = parseInt(split[0], 16);
let v_imm = parseInt(split[1], 16);
let current = (v_stat >> 12) + ( (4095 & v_stat) / 1000.0 );
let instant = (v_imm >> 12) + ( (4095 & v_imm) / 1000.0 );
return { current, instant };
}
async turnOn(deviceId) {
let formData = new FormData();
formData.append('cid', deviceId);
formData.append('uri', '/relay');
formData.append('action', 'open');
return this.client.post('/devRequest', {
headers: Object.assign({
tk: this.token,
id: this.uniqueId,
uniqueId: this.uniqueId,
'Content-Type': 'application/x-www-form-urlencoded'
}, formData.getHeaders()),
body : {
cid : deviceId,
uri : '/relay',
action : 'open'
}
});
}
async turnOff(deviceId) {
let formData = new FormData();
formData.append('cid', deviceId);
formData.append('uri', '/relay');
formData.append('action', 'break');
return this.client.post('/devRequest', {
headers: Object.assign({
tk: this.token,
id: this.uniqueId,
uniqueId: this.uniqueId,
'Content-Type': 'application/x-www-form-urlencoded'
}, formData.getHeaders()),
body : {
cid : deviceId,
uri : '/relay',
action : 'break'
}
});
}
}
module.exports = EtekCityClient;