-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmeter_points.go
139 lines (115 loc) · 3.29 KB
/
meter_points.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
package octopus
import (
"fmt"
"time"
"github.com/mitchellh/mapstructure"
)
type GSP struct {
Gsp, Mpan string
ProfileClass int `mapstructure:"profile_class"`
}
// Consumption represents a half hour consumption of gas or electricity.
type Consumption struct {
// Consumption in kWh
Consumption float32
IntervalStart time.Time `mapstructure:"interval_start"`
IntervalEnd time.Time `mapstructure:"interval_end"`
}
type ConsumptionRequest struct {
PeriodFrom time.Time `url:"period_from,omitempty"`
PeriodTo time.Time `url:"period_to,omitempty"`
PageSize int `url:"page_size,omitempty"`
OrderBy string `url:"order_by,omitempty"`
GroupBy string `url:"group_by,omitempty"`
}
// ElectricityMeterPoint returns the GSP for an electricity meter.
func (c *Client) ElectricityMeterPoint(mpan string) (*GSP, error) {
uri := fmt.Sprintf("/electricity-meter-points/%s", mpan)
resp, err := c.request("GET", uri, nil, nil)
if err != nil {
return nil, err
}
var gsp GSP
decoderConfig := mapstructure.DecoderConfig{
ErrorUnused: true,
Result: &gsp,
}
decoder, err := mapstructure.NewDecoder(&decoderConfig)
if err != nil {
panic(err)
}
err = decoder.Decode(resp)
if err != nil {
return nil, fmt.Errorf(
"failed to unmarshal response into GSP struct: %w",
err,
)
}
return &gsp, nil
}
// consumption collates the common logic between the gas and electricity consumption
// functions.
// meterPointNumber is the MPRN or MPAN of the meter.
// electricity determines if we query the electricity meter endpoint or the gas one.
func (c *Client) consumption(
meterPointNumber, serial string,
options ConsumptionRequest,
electricity bool,
) ([]Consumption, error) {
var baseURL string
if electricity {
baseURL += "/electricity-meter-points"
} else {
baseURL += "/gas-meter-points"
}
uri := fmt.Sprintf(
"%s/%s/meters/%s/consumption/",
baseURL,
meterPointNumber,
serial,
)
resp, err := c.request("GET", uri, nil, options)
if err != nil {
return nil, err
}
var consumption []Consumption
decoderConfig := mapstructure.DecoderConfig{
ErrorUnused: true,
Result: &consumption,
DecodeHook: mapstructure.StringToTimeHookFunc(
"2006-01-02T15:04:05Z07:00",
),
}
decoder, err := mapstructure.NewDecoder(&decoderConfig)
if err != nil {
panic(err)
}
err = decoder.Decode(resp.(listResponse).Results)
if err != nil {
return nil, fmt.Errorf(
"failed to unmarshal response into slice of Consumption struct: %w",
err,
)
}
return consumption, nil
}
// ElectricityConsumption returns the consumption of an electricity meter.
// mpan and serial are the MPAN and serial of the electricity meter.
// options are the parameters to use when querying for consumption.
func (c *Client) ElectricityConsumption(
mpan, serial string,
options ConsumptionRequest,
) ([]Consumption, error) {
request, err := c.consumption(mpan, serial, options, true)
return request, err
}
// GasConsumption returns the consumption of a gas meter.
// mprn and serial are the MPRN and serial of the gas meter.
// options are the parameters to use when querying for consumption.
func (c *Client) GasConsumption(
mprn, serial string,
options ConsumptionRequest,
) ([]Consumption, error) {
request, err := c.consumption(mprn, serial, options, false)
return request, err
}