forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtariff.go
155 lines (128 loc) Β· 2.98 KB
/
tariff.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
package tariff
import (
"encoding/json"
"fmt"
"slices"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/provider"
"github.com/evcc-io/evcc/util"
"github.com/jinzhu/now"
)
type Tariff struct {
*embed
log *util.Logger
data *util.Monitor[api.Rates]
priceG func() (float64, error)
}
var _ api.Tariff = (*Tariff)(nil)
func init() {
registry.Add(api.Custom, NewConfigurableFromConfig)
}
func NewConfigurableFromConfig(other map[string]interface{}) (api.Tariff, error) {
var cc struct {
embed `mapstructure:",squash"`
Price *provider.Config
Forecast *provider.Config
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if (cc.Price != nil) == (cc.Forecast != nil) {
return nil, fmt.Errorf("must have either price or forecast")
}
var (
err error
priceG func() (float64, error)
forecastG func() (string, error)
)
if cc.Price != nil {
priceG, err = provider.NewFloatGetterFromConfig(*cc.Price)
if err != nil {
return nil, fmt.Errorf("price: %w", err)
}
}
if cc.Forecast != nil {
forecastG, err = provider.NewStringGetterFromConfig(*cc.Forecast)
if err != nil {
return nil, fmt.Errorf("forecast: %w", err)
}
}
t := &Tariff{
log: util.NewLogger("tariff"),
embed: &cc.embed,
priceG: priceG,
data: util.NewMonitor[api.Rates](2 * time.Hour),
}
if forecastG != nil {
done := make(chan error)
go t.run(forecastG, done)
err = <-done
}
return t, err
}
func (t *Tariff) run(forecastG func() (string, error), done chan error) {
var once sync.Once
bo := newBackoff()
tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
var data api.Rates
if err := backoff.Retry(func() error {
s, err := forecastG()
if err != nil {
return backoffPermanentError(err)
}
if err := json.Unmarshal([]byte(s), &data); err != nil {
return backoff.Permanent(err)
}
return nil
}, bo); err != nil {
once.Do(func() { done <- err })
t.log.ERROR.Println(err)
continue
}
data.Sort()
t.data.Set(data)
once.Do(func() { close(done) })
}
}
func (t *Tariff) forecastRates() (api.Rates, error) {
var res api.Rates
err := t.data.GetFunc(func(val api.Rates) {
res = slices.Clone(val)
})
return res, err
}
func (t *Tariff) priceRates() (api.Rates, error) {
price, err := t.priceG()
if err != nil {
return nil, err
}
res := make(api.Rates, 48)
start := now.BeginningOfHour()
for i := 0; i < len(res); i++ {
slot := start.Add(time.Duration(i) * time.Hour)
res[i] = api.Rate{
Start: slot,
End: slot.Add(time.Hour),
Price: t.totalPrice(price),
}
}
return res, nil
}
// Rates implements the api.Tariff interface
func (t *Tariff) Rates() (api.Rates, error) {
if t.priceG != nil {
return t.priceRates()
}
return t.forecastRates()
}
// Type implements the api.Tariff interface
func (t *Tariff) Type() api.TariffType {
if t.priceG != nil {
return api.TariffTypePriceDynamic
}
return api.TariffTypePriceForecast
}