-
Notifications
You must be signed in to change notification settings - Fork 1
/
enelogic.go
408 lines (330 loc) · 10.7 KB
/
enelogic.go
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package enelogic
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"text/template"
"time"
"github.com/energietransitie/twomes-backoffice-api/twomes"
"github.com/energietransitie/twomes-backoffice-api/twomes/measurement"
"github.com/energietransitie/twomes-backoffice-api/twomes/property"
"github.com/sirupsen/logrus"
)
// Client is the http client used to make requests to enelogic.
var client = &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
MaxIdleConns: 10,
MaxIdleConnsPerHost: 10,
MaxConnsPerHost: 10,
IdleConnTimeout: time.Second * 30,
},
}
const (
endpointMeasuringPoints = "/measuringpoints"
endpointDatapointsMonths = "/measuringpoints/{{.MeasuringPointID}}/datapoint/months/{{.From}}/{{.To}}"
endpointDatapointsDays = "/measuringpoints/{{.MeasuringPointID}}/datapoint/days/{{.From}}/{{.To}}"
endpointDatapointsInterval = "/measuringpoints/{{.MeasuringPointID}}/datapoints/{{.From}}/{{.To}}"
enelogicTimeFormat = `"2006-01-02 15:04:05"`
enelogicDefaultTimeLocation = "Europe/Amsterdam"
Day = time.Hour * 24
)
var (
baseURL = "https://enelogic.com/api"
ErrNoData = errors.New("no data from enelogic")
ErrInvalidPeriod = errors.New("invalid period")
)
// EnelogicTime is a custom time type for enelogic.
// It is used to parse the time format used by enelogic.
type EnelogicTime struct {
time.Time
LocationName *string
}
func (t *EnelogicTime) UnmarshalJSON(b []byte) error {
if t.LocationName == nil {
location := enelogicDefaultTimeLocation
t.LocationName = &location
}
loc, err := time.LoadLocation(*t.LocationName)
if err != nil {
return err
}
parsed, err := time.ParseInLocation(enelogicTimeFormat, string(b), loc)
if err != nil {
return err
}
t.Time = parsed
return nil
}
func (t EnelogicTime) MarshalJSON() ([]byte, error) {
enelogicFormattedTime := t.Time.Format(enelogicTimeFormat)
return []byte(enelogicFormattedTime), nil
}
func (t EnelogicTime) String() string {
return t.Time.Format(time.RFC3339)
}
// UnitType is the type of the unit.
// 0 = electricity.
// 1 = gas.
type UnitType int
const (
UnitTypeElectricity UnitType = 0
UnitTypeGas UnitType = 1
)
func (u UnitType) String() string {
switch u {
case UnitTypeElectricity:
return "electricity"
case UnitTypeGas:
return "gas"
}
return "unknown"
}
// Rate is the type of the Rate, as defined by enelogic.
type Rate int
// Rate constants as defined by enelogic.
// See https://enelogic.com/nl/blog/slimme-meter-data-exporteren for more information.
const (
RateUsageTotal Rate = 180
RateUsageLow Rate = 181
RateUsageHigh Rate = 182
RateReturnTotal Rate = 280
RateReturnLow Rate = 281
RateReturnHigh Rate = 282
)
func (r Rate) Parse(unit UnitType) string {
propertyNames := map[Rate]string{
RateUsageTotal: "use_cum",
RateUsageLow: "use_lo_cum",
RateUsageHigh: "use_hi_cum",
RateReturnTotal: "ret_cum",
RateReturnLow: "ret_lo_cum",
RateReturnHigh: "ret_hi_cum",
}
switch unit {
case UnitTypeElectricity:
return "e_" + propertyNames[r] + "__kWh"
case UnitTypeGas:
return "g_" + propertyNames[r] + "__m3"
}
return propertyNames[r]
}
type Quantity float64
func (q *Quantity) UnmarshalJSON(b []byte) error {
var quantity float64
err := json.Unmarshal(b, &quantity)
if err != nil {
// Try to unmarshal as string.
var quantity string
err := json.Unmarshal(b, &quantity)
if err != nil {
return err
}
q64, err := strconv.ParseFloat(quantity, 64)
if err != nil {
return err
}
*q = Quantity(q64)
return nil
}
*q = Quantity(quantity)
return nil
}
type MeasuringsPointsResponse []MeasuringPoint
type MeasuringPoint struct {
Timezone string `json:"timezone"`
ID int `json:"id"`
UnitType UnitType `json:"unitType"`
}
type DatapointsResponse []DataPoint
type DataPoint struct {
Quantity Quantity `json:"quantity"`
Rate Rate `json:"rate"`
Date EnelogicTime `json:"date"`
Datetime EnelogicTime `json:"datetime"`
}
func (d DataPoint) Parse(unit UnitType) measurement.Measurement {
t := twomes.Time(d.Date.Time)
if time.Time(t).IsZero() {
t = twomes.Time(d.Datetime.Time)
}
return measurement.Measurement{
Property: property.Property{
Name: d.Rate.Parse(unit),
},
Time: t,
Value: fmt.Sprintf("%.3f", d.Quantity),
}
}
type RequestTime struct {
time.Time
}
func (t RequestTime) String() string {
return t.Format("2006-01-02")
}
type RequestArgs struct {
MeasuringPointID int
From RequestTime
To RequestTime
}
func newRequestArgs(measuringPointID int, from, to time.Time) RequestArgs {
return RequestArgs{
MeasuringPointID: measuringPointID,
From: RequestTime{from},
To: RequestTime{to},
}
}
// Download downloads the data from enelogic.
// A slice of measurements is returned, which can be saved to the database.
//
// StartPeriod is the start of the period from which data should be downloaded.
// EndPeriod is the end of the period from which data should be downloaded.
// If EndPeriod is the zero value, the current time is used.
func Download(ctx context.Context, token string, startPeriod time.Time, endPeriod time.Time) ([]measurement.Measurement, error) {
var measurements []measurement.Measurement
isFirstDownload := startPeriod.IsZero()
if isFirstDownload {
startPeriod = endPeriod.AddDate(-1, -2, 0)
}
if endPeriod.Before(startPeriod) {
return nil, fmt.Errorf("%w: end period is before start period", ErrInvalidPeriod)
}
if dateEqual(startPeriod, endPeriod) {
return nil, ErrNoData
}
measuringPoints, err := getMeasuringPoints(ctx, token)
if err != nil {
return nil, fmt.Errorf("error getting measuring points: %w", err)
}
if len(measuringPoints) == 0 {
return nil, ErrNoData
}
for _, measuringPoint := range measuringPoints {
// Get month datapoints.
// Only get month datapoints on the first download or the first day of the month.
if endPeriod.Day() == 1 || isFirstDownload {
logrus.Infoln("downloading", measuringPoint.UnitType.String(), "month datapoints from", RequestTime{startPeriod}, "to", RequestTime{endPeriod})
args := newRequestArgs(measuringPoint.ID, startPeriod, endPeriod)
datapoints, err := getDatapoints(ctx, token, endpointDatapointsMonths, args)
if err != nil {
return nil, fmt.Errorf("error getting month datapoints: %w", err)
}
measurements = append(measurements, parseDatapoints(datapoints, measuringPoint.UnitType)...)
}
// Get day datapoints.
dayStartPeriod := startPeriod
if time.Since(dayStartPeriod) > Day*40 {
// Set dayStartPeriod to 40 days ago, if the real dayStartPeriod is more than 40 days ago.
dayStartPeriod = endPeriod.Add(-Day * 40)
}
logrus.Infoln("downloading", measuringPoint.UnitType.String(), "day datapoints from", RequestTime{dayStartPeriod}, "to", RequestTime{endPeriod})
args := newRequestArgs(measuringPoint.ID, dayStartPeriod, endPeriod)
datapoints, err := getDatapoints(ctx, token, endpointDatapointsDays, args)
if err != nil {
return nil, fmt.Errorf("error getting day datapoints: %w", err)
}
measurements = append(measurements, parseDatapoints(datapoints, measuringPoint.UnitType)...)
// Get interval datapoints.
intervalStartPeriod := startPeriod
if time.Since(intervalStartPeriod) > Day*10 {
// Set intervalStartPeriod to 10 days ago, if the real intervalStartPeriod is more than 10 days ago.
intervalStartPeriod = endPeriod.Add(-Day * 10)
}
logrus.Infoln("downloading", measuringPoint.UnitType.String(), "interval datapoints from", RequestTime{intervalStartPeriod}, "to", RequestTime{endPeriod})
for _, day := range splitDays(intervalStartPeriod, endPeriod) {
args := newRequestArgs(measuringPoint.ID, day.Start, day.End)
datapoints, err := getDatapoints(ctx, token, endpointDatapointsInterval, args)
if err != nil {
return nil, fmt.Errorf("error getting interval datapoints: %w", err)
}
measurements = append(measurements, parseDatapoints(datapoints, measuringPoint.UnitType)...)
}
}
return measurements, nil
}
func parseDatapoints(datapoints DatapointsResponse, unit UnitType) []measurement.Measurement {
var measurements []measurement.Measurement
for _, datapoint := range datapoints {
measurements = append(measurements, datapoint.Parse(unit))
}
return measurements
}
// GetMeasuringPoints returns the measuring points for the account with the given token.
func getMeasuringPoints(ctx context.Context, token string) (MeasuringsPointsResponse, error) {
requestURL := baseURL + endpointMeasuringPoints
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestURL, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Add("Authorization", "Bearer "+token)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error executing request to Enelogic: %w", err)
}
var response MeasuringsPointsResponse
err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("error decoding response to json: %w", err)
}
defer resp.Body.Close()
return response, nil
}
// GetDatapoints returns the data points for the account with the given token.
func getDatapoints(ctx context.Context, token string, endpoint string, args RequestArgs) (DatapointsResponse, error) {
requestUrl, err := getRequestURL(endpoint, args)
if err != nil {
return nil, fmt.Errorf("error getting request url: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error executing request to Enelogic: %w", err)
}
var response DatapointsResponse
json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("error decoding response to json: %w", err)
}
return response, nil
}
func getRequestURL(endpoint string, args RequestArgs) (string, error) {
requestURL := strings.Builder{}
t, err := template.New("url").Parse(baseURL + endpoint)
if err != nil {
return "", fmt.Errorf("error parsing template: %w", err)
}
err = t.Execute(&requestURL, args)
if err != nil {
return "", fmt.Errorf("error executing template: %w", err)
}
return requestURL.String(), nil
}
func dateEqual(a, b time.Time) bool {
y1, m1, d1 := a.Date()
y2, m2, d2 := b.Date()
return y1 == y2 && m1 == m2 && d1 == d2
}
type day struct {
Start time.Time
End time.Time
}
func splitDays(from, to time.Time) []day {
var days []day
for from.Before(to) && !dateEqual(from, to) {
to := from.AddDate(0, 0, 1)
days = append(days, day{
Start: from,
End: to,
})
from = to
}
return days
}