forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedf-tempo.go
163 lines (135 loc) Β· 3.82 KB
/
edf-tempo.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
package tariff
import (
"errors"
"fmt"
"net/http"
"slices"
"strings"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/oauth"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/transport"
"github.com/fatih/structs"
"github.com/jinzhu/now"
"golang.org/x/oauth2"
)
type EdfTempo struct {
*embed
*request.Helper
log *util.Logger
basic string
data *util.Monitor[api.Rates]
prices map[string]float64
}
var _ api.Tariff = (*EdfTempo)(nil)
func init() {
registry.Add("edf-tempo", NewEdfTempoFromConfig)
}
func NewEdfTempoFromConfig(other map[string]interface{}) (api.Tariff, error) {
var cc struct {
embed `mapstructure:",squash"`
ClientID string
ClientSecret string
Prices struct {
Blue, Red, White float64 `structs:",omitempty"`
}
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.ClientID == "" || cc.ClientSecret == "" {
return nil, errors.New("missing credentials")
}
basic := transport.BasicAuthHeader(cc.ClientID, cc.ClientSecret)
log := util.NewLogger("edf-tempo").Redact(basic)
t := &EdfTempo{
embed: &cc.embed,
log: log,
basic: basic,
Helper: request.NewHelper(log),
data: util.NewMonitor[api.Rates](2 * time.Hour),
}
prices := structs.Map(cc.Prices)
if len(prices) != 3 {
return nil, errors.New("missing prices for red/blue/white")
}
for k, v := range prices {
t.prices[strings.ToLower(k)] = v.(float64)
}
t.Client.Transport = &oauth2.Transport{
Base: t.Client.Transport,
Source: oauth.RefreshTokenSource(new(oauth2.Token), t),
}
done := make(chan error)
go t.run(done)
err := <-done
return t, err
}
func (t *EdfTempo) RefreshToken(_ *oauth2.Token) (*oauth2.Token, error) {
tokenURL := "https://digital.iservices.rte-france.com/token/oauth"
req, _ := request.New(http.MethodPost, tokenURL, nil, map[string]string{
"Authorization": t.basic,
"Content-Type": request.FormContent,
"Accept": request.JSONContent,
})
var res oauth2.Token
client := request.NewHelper(t.log)
err := client.DoJSON(req, &res)
return util.TokenWithExpiry(&res), err
}
func (t *EdfTempo) run(done chan error) {
var once sync.Once
tick := time.NewTicker(time.Hour)
for ; true; <-tick.C {
var res struct {
Data struct {
Values []struct {
StartDate time.Time `json:"start_date"`
EndDate time.Time `json:"end_date"`
Value string `json:"value"`
} `json:"values"`
} `json:"tempo_like_calendars"`
}
start := now.BeginningOfDay()
end := start.AddDate(0, 0, 2)
uri := fmt.Sprintf("https://digital.iservices.rte-france.com/open_api/tempo_like_supply_contract/v1/tempo_like_calendars?start_date=%s&end_date=%s&fallback_status=true",
strings.ReplaceAll(start.Format(time.RFC3339), "+", "%2B"),
strings.ReplaceAll(end.Format(time.RFC3339), "+", "%2B"))
if err := backoff.Retry(func() error {
return backoffPermanentError(t.GetJSON(uri, &res))
}, bo()); err != nil {
once.Do(func() { done <- err })
t.log.ERROR.Println(err)
continue
}
data := make(api.Rates, 0, 24*len(res.Data.Values))
for _, r := range res.Data.Values {
for ts := r.StartDate.Local(); ts.Before(r.EndDate); ts = ts.Add(time.Hour) {
ar := api.Rate{
Start: ts,
End: ts.Add(time.Hour),
Price: t.totalPrice(t.prices[strings.ToLower(r.Value)]),
}
data = append(data, ar)
}
}
mergeRates(t.data, data)
once.Do(func() { close(done) })
}
}
// Rates implements the api.Tariff interface
func (t *EdfTempo) 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 *EdfTempo) Type() api.TariffType {
return api.TariffTypePriceForecast
}