-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
208 lines (180 loc) · 4.43 KB
/
app.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
199
200
201
202
203
204
205
206
207
208
const qs = require('querystring');
const https = require('https');
const libCookie = require('cookie');
const setCookie = require('set-cookie-parser');
const csv = require('@fast-csv/parse');
require('dotenv').config();
const devices = ['090005AC99E2']; // Add your device IDs
const total_rows = 1; // How many rows should be processed? Min. must be 1
const interval = 60; // Time between a new fetch run, default is `60`
var auth_cookie;
const login = async () => {
return new Promise((resolve, reject) => {
const postData = qs.stringify({
Username: process.env.AUTH_USERNAME,
Password: process.env.AUTH_PASSWORD,
});
const options = {
hostname: 'www.wh-observer.de',
port: 443,
path: '/Account/LogOn',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length,
},
};
const req = https.request(options, (res) => {
if (res.headers['set-cookie']) {
const cookies = setCookie.parse(res, {
decodeValues: true,
});
cookies.forEach((cookie) => {
if (cookie.name === '.ASPXAUTH' && cookie.value !== '') {
// Set the auth_cookie
auth_cookie = cookie;
}
});
if (!auth_cookie) {
console.log('login failed');
auth_cookie = '';
reject();
return;
}
} else {
console.log('login failed');
auth_cookie = '';
reject();
return;
}
resolve();
return;
});
req.on('error', (err) => {
// console.log(err);
console.log('login failed');
auth_cookie = '';
reject();
});
req.write(postData);
req.end();
});
};
const getData = async (device_id) => {
return new Promise(async (resolve, reject) => {
// Try to get a new cookie
if (!auth_cookie) {
try {
await login();
} catch (err) {
// console.log(err);
}
// Didn't worked? Just wait until the next cycle
if (!auth_cookie) {
reject();
return;
}
}
const cookie = libCookie.serialize(auth_cookie.name, auth_cookie.value, auth_cookie);
const today = new Date();
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const fromDate =
('0' + (today.getMonth() + 1)).slice(-2) +
'/' +
('0' + today.getDate()).slice(-2) +
'/' +
today.getFullYear();
const toDate =
('0' + (tomorrow.getMonth() + 1)).slice(-2) +
'/' +
('0' + tomorrow.getDate()).slice(-2) +
'/' +
tomorrow.getFullYear();
const options = {
hostname: 'www.wh-observer.de',
port: 443,
path:
'/DeviceDetails/DataCSV?deviceID=' +
device_id +
'&from=' +
fromDate +
'&to=' +
toDate +
'&detailLevel=day',
method: 'GET',
headers: {
Cookie: cookie,
},
};
const req = https.request(options, async (res) => {
var body = '';
res.setEncoding('utf8');
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', () => {
const lines = body.split(/\r\n|\r|\n/).length;
const limit = total_rows + 2; // Add 2 rows because of header and the last empty line
const skip_lines = lines - limit >= limit ? lines - limit : 0; // Get only the last row
let data = [];
csv.parseString(body, {
headers: (headers) => headers.map((h) => h?.split(' ').join('_').toLowerCase()),
delimiter: ';',
strictColumnHandling: true,
skipRows: skip_lines,
ignoreEmpty: true,
})
.on('error', (err) => {
console.log(err);
reject();
return;
})
.on('data', (row) => {
let i = 0;
for (const key in row) {
if (i++ === 0) {
row[key] = new Date(row[key]);
} else {
row[key] = Number(row[key]) ? Number(row[key]) : row[key];
}
}
data.push(row);
//
// Send the current `row` to MySQL, MongoDB, Redis, ...
// or send using websocket to your weather station
//
})
.on('end', (row_count) => {
console.log(data);
//
// Send the fetched `data` to MySQL, MongoDB, Redis, ...
// or send using websocket to your weather station
// `fetchedData` containes the full
//
resolve();
return;
});
});
});
req.on('error', (e) => {
console.log(e);
reject();
return;
});
req.end();
});
};
var loop_timeout;
const app = async () => {
clearTimeout(loop_timeout);
try {
for (const device_id of devices) {
await getData(device_id);
}
} catch (err) {
// console.log(err);
}
loop_timeout = setTimeout(app, interval * 1000);
};
app();