-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (99 loc) · 3.37 KB
/
index.js
File metadata and controls
107 lines (99 loc) · 3.37 KB
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
const {format: formatCSV} = require('@fast-csv/format');
const {dateRange, format, addDays, minimum} = require("./app/dates");
const axios = require('axios').default;
const fs = require('fs');
const startDate = new Date(2021, 4, 1, 9, 0, 0, 0);
const endDate = new Date();
const arguments = process.argv;
const resourceClassifiers = ['gas.consumption', 'electricity.consumption'];
const instance = axios.create({
headers: {
'Content-Type': 'application/json'
}
});
if (arguments.length >= 5) {
const username = arguments[2];
const password = arguments[3];
const applicationId = arguments[4];
run(username, password, applicationId);
} else {
console.log("--- Usage ---");
console.log("npm start <username> <password> <applicationId>");
}
function writeToCsv(data, fileName) {
const csvFile = fs.createWriteStream(fileName);
const stream = formatCSV();
stream.pipe(csvFile);
data.forEach(row => {
stream.write(row);
});
stream.end();
}
function resource(resourceId, resourceName, token, applicationId) {
Promise.all(
dateRange(startDate, endDate, 10).map(date => {
const from = format(date);
const to = format(minimum(addDays(date, 10), new Date()));
return instance.get("https://api.glowmarkt.com/api/v0-1/resource/" + resourceId +
"/readings", {
headers: {
'token': token,
'applicationId': applicationId
},
params: {
period: "PT30M",
function: "sum",
from: from,
to: to,
offset: 0
}
}).then(function (response) {
return response.data.data;
}).catch(function (error) {
console.error(error);
});
}
)
).then(function (responses) {
if (responses.length > 0) {
const concat = responses.reduce(
(concatenating, response) => concatenating.concat(response),
[])
writeToCsv(concat, resourceName + ".csv");
} else {
console.log("No resource data returned for date range")
}
}).catch(function (error) {
console.error(error);
})
}
function resources(token, applicationId) {
instance.get("https://api.glowmarkt.com/api/v0-1/resource", {
headers: {
'token': token,
'applicationId': applicationId
}
}).then(function (response) {
response.data.filter(resourceData => resourceClassifiers.includes(resourceData['classifier']))
.forEach(resourceData => {
return resource(resourceData['resourceId'], resourceData['classifier'], token, applicationId);
}
)
}).catch(function (error) {
console.error(error);
});
}
function run(username, password, applicationId) {
instance.post('https://api.glowmarkt.com/api/v0-1/auth', {
username: username,
password: password
}, {
headers: {
'applicationId': applicationId
}
}).then(function (response) {
resources(response.data.token, applicationId);
}).catch(function (error) {
console.log(error);
});
}