forked from yirenWang/enedis-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
220 lines (202 loc) · 5.41 KB
/
data.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
209
210
211
212
213
214
215
216
217
218
219
220
import axios from 'axios';
import httpStatus from 'http-status';
import querystring from 'querystring';
import { getUserByEnedisId } from './db/user';
import { getDataForUserByType, createDataForUser, deleteDataForUser } from './db/data';
import { getUserAccessToken } from './user';
import _ from 'lodash';
const jwtSecret = process.env.JWT_SECRET;
// gives
// [ {metadata, graph_data}, ... ]
/**
* format elecricity data from enedis to store in database
*
* @param {object} data
*/
const formatDataFromEnedis = data => {
const graphData = data.usage_point.map(({ meter_reading }) => {
const { start, end, reading_type, usage_point_id } = meter_reading;
const d = {};
d.metadata = {
start,
end,
unit: reading_type.unit,
usagePointId: usage_point_id,
};
d.graph_data = meter_reading.interval_reading.map(point => {
const timestamp = new Date(start);
timestamp.setSeconds(
timestamp.getSeconds() + reading_type.interval_length * (point.rank - 1),
);
return { timestamp, value: point.value };
});
return d;
});
return graphData;
};
/**
* create strings from date that are 10 days apart
*/
const createDateStrings = () => {
const start = new Date();
start.setDate(start.getDate() - 10);
return { end: new Date().toISOString(), start: start.toISOString() };
};
/**
*
* @param {string} URLType can be consumption_load_curve, consumption_max_power, daily_consumption, daily_production
* @param {obj} req
* @param {obj} res
*/
const getDataFromEnedis = (URLType, req, res) => {
const url =
`https://gw.hml.api.enedis.fr/v3/metering_data/${URLType}` +
'?' +
`start=${createDateStrings().start}` +
`&end=${createDateStrings().end}` +
`&usage_point_id=${req.user.usagePointId}`;
console.log(url);
getUserAccessToken(req.user.id)
.then(accessToken => {
const options = {
method: 'get',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${accessToken || process.env.ACCESS_TOKEN}`,
},
};
return axios.get(url, options);
})
.then(r => {
if (r.status === 200) return r.data;
})
.then(data => {
const graphData = formatDataFromEnedis(data); // [ {metadata, graph_data}, ... ]
// Save data to database
graphData.forEach(d => {
createDataForUser(
req.user.id,
d.graph_data,
d.metadata.unit,
_.camelCase(URLType),
d.metadata.usagePointId,
);
});
res.send(graphData);
})
.catch(err => {
if (err.response && err.response.status === 403)
return res.send({ message: 'Le client est inconnu ou non habilité' });
console.log(err);
res.send("Un erreur s'est produit");
});
};
/**
* format the data from database to be used by the front end
*
* @param {[]object} data {timestamp, value, type, unit, usagePointId}
*/
const formatDataFromDB = data => {
// { usagePointId: [], ... }
const tmp = {};
data.forEach(e => {
tmp[e.usagePointId] = tmp[e.usagePointId] || [];
tmp[e.usagePointId].push({
timestamp: e.timestamp,
value: e.value,
});
});
// [{metadata: , data: [] }, ... ]
const graph_data = Object.keys(tmp).map(id => ({
metadata: {
start: data[0].timestamp,
end: data[data.length - 1].timestamp,
unit: data[0].unit,
usagePointId: id,
},
graph_data: tmp[id],
}));
return graph_data;
};
/**
* get consumption load curve data
*
* @param {*} req
* @param {*} res
*/
export const getConsumptionLoadCurve = (req, res) => {
// Is data in bdd
getDataForUserByType(req.user.id, 'consumptionLoadCurve').then(data => {
if (data.length === 0) {
// Data is not in bdd
getDataFromEnedis('consumption_load_curve', req, res);
} else {
res.send(formatDataFromDB(data));
}
});
};
/**
* get consumption max power data
*
* @param {*} req
* @param {*} res
*/
export const getConsumptionMaxPower = (req, res) => {
// Is data in bdd
getDataForUserByType(req.user.id, 'consumptionMaxPower').then(data => {
if (data.length === 0) {
// Data is not in bdd
getDataFromEnedis('consumption_max_power', req, res);
} else {
res.send(formatDataFromDB(data));
}
});
};
/**
* get daily consumption data
*
* @param {*} req
* @param {*} res
*/
export const getDailyConsumption = (req, res) => {
// Is data in bdd
getDataForUserByType(req.user.id, 'dailyConsumption').then(data => {
if (data.length === 0) {
// Data is not in bdd
getDataFromEnedis('daily_consumption', req, res);
} else {
res.send(formatDataFromDB(data));
}
});
};
/**
* get daily production data
*
* @param {*} req
* @param {*} res
*/
export const getDailyProduction = (req, res) => {
// Is data in bdd
getDataForUserByType(req.user.id, 'dailyProduction').then(data => {
if (data.length === 0) {
// Data is not in bdd
getDataFromEnedis('daily_production', req, res);
} else {
res.send(formatDataFromDB(data));
}
});
};
// datatype needs to be in snake_case
export const refreshData = (req, res, dataType) => {
getDataFromEnedis(dataType, req, res);
};
export const deleteMyData = (req, res) => {
return deleteDataForUser(req.user.id)
.then(affectedRows => {
res.send('ok');
})
.catch(err => {
console.log(err);
});
};