forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elering.go
112 lines (91 loc) · 2.23 KB
/
elering.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
package tariff
import (
"errors"
"fmt"
"net/url"
"slices"
"strings"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/tariff/elering"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
)
type Elering struct {
*embed
log *util.Logger
region string
data *util.Monitor[api.Rates]
}
var _ api.Tariff = (*Elering)(nil)
func init() {
registry.Add("elering", NewEleringFromConfig)
}
func NewEleringFromConfig(other map[string]interface{}) (api.Tariff, error) {
var cc struct {
embed `mapstructure:",squash"`
Region string
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.Region == "" {
return nil, errors.New("missing region")
}
t := &Elering{
embed: &cc.embed,
log: util.NewLogger("Elering"),
region: strings.ToLower(cc.Region),
data: util.NewMonitor[api.Rates](2 * time.Hour),
}
done := make(chan error)
go t.run(done)
err := <-done
return t, err
}
func (t *Elering) run(done chan error) {
var once sync.Once
client := request.NewHelper(t.log)
bo := newBackoff()
for ; true; <-time.Tick(time.Hour) {
var res elering.NpsPrice
ts := time.Now().Truncate(time.Hour)
uri := fmt.Sprintf("%s/nps/price?start=%s&end=%s", elering.URI,
url.QueryEscape(ts.Format(time.RFC3339)),
url.QueryEscape(ts.Add(48*time.Hour).Format(time.RFC3339)))
if err := backoff.Retry(func() error {
return client.GetJSON(uri, &res)
}, bo); err != nil {
once.Do(func() { done <- err })
t.log.ERROR.Println(err)
continue
}
data := make(api.Rates, 0, len(res.Data[t.region]))
for _, r := range res.Data[t.region] {
ts := time.Unix(r.Timestamp, 0)
ar := api.Rate{
Start: ts.Local(),
End: ts.Add(time.Hour).Local(),
Price: t.totalPrice(r.Price / 1e3),
}
data = append(data, ar)
}
data.Sort()
t.data.Set(data)
once.Do(func() { close(done) })
}
}
// Rates implements the api.Tariff interface
func (t *Elering) Rates() (api.Rates, error) {
var res api.Rates
err := t.data.GetFunc(func(val api.Rates) {
res = slices.Clone(val)
})
return res, err
}
// Type implements the api.Tariff interface
func (t *Elering) Type() api.TariffType {
return api.TariffTypePriceForecast
}