|
| 1 | +package tariff |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/xml" |
| 6 | + "errors" |
| 7 | + "net/http" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/cenkalti/backoff/v4" |
| 14 | + "github.com/evcc-io/evcc/api" |
| 15 | + "github.com/evcc-io/evcc/tariff/entsoe" |
| 16 | + "github.com/evcc-io/evcc/util" |
| 17 | + "github.com/evcc-io/evcc/util/request" |
| 18 | + "github.com/evcc-io/evcc/util/transport" |
| 19 | +) |
| 20 | + |
| 21 | +type Entsoe struct { |
| 22 | + *request.Helper |
| 23 | + *embed |
| 24 | + mux sync.Mutex |
| 25 | + log *util.Logger |
| 26 | + token string |
| 27 | + domain string |
| 28 | + data api.Rates |
| 29 | + updated time.Time |
| 30 | +} |
| 31 | + |
| 32 | +var _ api.Tariff = (*Entsoe)(nil) |
| 33 | + |
| 34 | +func init() { |
| 35 | + registry.Add("entsoe", NewEntsoeFromConfig) |
| 36 | +} |
| 37 | + |
| 38 | +func NewEntsoeFromConfig(other map[string]interface{}) (api.Tariff, error) { |
| 39 | + var cc struct { |
| 40 | + embed `mapstructure:",squash"` |
| 41 | + Securitytoken string |
| 42 | + Domain string |
| 43 | + } |
| 44 | + |
| 45 | + if err := util.DecodeOther(other, &cc); err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + if cc.Securitytoken == "" { |
| 50 | + return nil, errors.New("securitytoken must be defined") |
| 51 | + } |
| 52 | + |
| 53 | + domain, err := entsoe.Area(entsoe.BZN, strings.ToUpper(cc.Domain)) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + log := util.NewLogger("entsoe").Redact(cc.Securitytoken) |
| 59 | + |
| 60 | + t := &Entsoe{ |
| 61 | + log: log, |
| 62 | + Helper: request.NewHelper(log), |
| 63 | + embed: &cc.embed, |
| 64 | + token: cc.Securitytoken, |
| 65 | + domain: domain, |
| 66 | + } |
| 67 | + |
| 68 | + // Wrap the client with a decorator that adds the security token to each request. |
| 69 | + t.Client.Transport = &transport.Decorator{ |
| 70 | + Base: t.Client.Transport, |
| 71 | + Decorator: transport.DecorateQuery(map[string]string{ |
| 72 | + "securityToken": cc.Securitytoken, |
| 73 | + }), |
| 74 | + } |
| 75 | + |
| 76 | + done := make(chan error) |
| 77 | + go t.run(done) |
| 78 | + err = <-done |
| 79 | + |
| 80 | + return t, err |
| 81 | +} |
| 82 | + |
| 83 | +func (t *Entsoe) run(done chan error) { |
| 84 | + var once sync.Once |
| 85 | + |
| 86 | + bo := newBackoff() |
| 87 | + |
| 88 | + // Data updated by ESO every half hour, but we only need data every hour to stay current. |
| 89 | + for ; true; <-time.Tick(time.Hour) { |
| 90 | + var tr entsoe.PublicationMarketDocument |
| 91 | + |
| 92 | + if err := backoff.Retry(func() error { |
| 93 | + // Request the next 24 hours of data. |
| 94 | + data, err := t.DoBody(entsoe.DayAheadPricesRequest(t.domain, time.Hour*24)) |
| 95 | + |
| 96 | + // Consider whether errors.As would be more appropriate if this needs to start dealing with wrapped errors. |
| 97 | + if se, ok := err.(request.StatusError); ok { |
| 98 | + if se.HasStatus(http.StatusBadRequest) { |
| 99 | + return backoff.Permanent(se) |
| 100 | + } |
| 101 | + |
| 102 | + return se |
| 103 | + } |
| 104 | + |
| 105 | + var doc entsoe.Document |
| 106 | + if err := xml.NewDecoder(bytes.NewReader(data)).Decode(&doc); err != nil { |
| 107 | + return backoff.Permanent(err) |
| 108 | + } |
| 109 | + |
| 110 | + switch doc.XMLName.Local { |
| 111 | + case entsoe.AcknowledgementMarketDocumentName: |
| 112 | + var doc entsoe.AcknowledgementMarketDocument |
| 113 | + if err := xml.NewDecoder(bytes.NewReader(data)).Decode(&doc); err != nil { |
| 114 | + return backoff.Permanent(err) |
| 115 | + } |
| 116 | + |
| 117 | + return backoff.Permanent(errors.New(doc.Reason.Text)) |
| 118 | + |
| 119 | + case entsoe.PublicationMarketDocumentName: |
| 120 | + if err := xml.NewDecoder(bytes.NewReader(data)).Decode(&tr); err != nil { |
| 121 | + return backoff.Permanent(err) |
| 122 | + } |
| 123 | + |
| 124 | + if tr.Type != string(entsoe.ProcessTypeDayAhead) { |
| 125 | + return backoff.Permanent(errors.New("invalid document type: " + tr.Type)) |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | + |
| 130 | + default: |
| 131 | + return backoff.Permanent(errors.New("invalid document name: " + doc.XMLName.Local)) |
| 132 | + } |
| 133 | + }, bo); err != nil { |
| 134 | + once.Do(func() { done <- err }) |
| 135 | + |
| 136 | + t.log.ERROR.Println(err) |
| 137 | + continue |
| 138 | + } |
| 139 | + |
| 140 | + if len(tr.TimeSeries) == 0 { |
| 141 | + once.Do(func() { done <- entsoe.ErrInvalidData }) |
| 142 | + t.log.ERROR.Println(entsoe.ErrInvalidData) |
| 143 | + continue |
| 144 | + } |
| 145 | + |
| 146 | + // extract desired series |
| 147 | + tsdata, err := entsoe.GetTsPriceData(tr.TimeSeries, entsoe.ResolutionHour) |
| 148 | + if err != nil { |
| 149 | + once.Do(func() { done <- err }) |
| 150 | + t.log.ERROR.Println(err) |
| 151 | + continue |
| 152 | + } |
| 153 | + |
| 154 | + once.Do(func() { close(done) }) |
| 155 | + |
| 156 | + t.mux.Lock() |
| 157 | + t.updated = time.Now() |
| 158 | + |
| 159 | + t.data = make(api.Rates, 0, len(tsdata)) |
| 160 | + for _, r := range tsdata { |
| 161 | + ar := api.Rate{ |
| 162 | + Start: r.ValidityStart, |
| 163 | + End: r.ValidityEnd, |
| 164 | + Price: t.totalPrice(r.Value), |
| 165 | + } |
| 166 | + t.data = append(t.data, ar) |
| 167 | + } |
| 168 | + |
| 169 | + t.mux.Unlock() |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +// Rates implements the api.Tariff interface |
| 174 | +func (t *Entsoe) Rates() (api.Rates, error) { |
| 175 | + t.mux.Lock() |
| 176 | + defer t.mux.Unlock() |
| 177 | + return slices.Clone(t.data), outdatedError(t.updated, time.Hour) |
| 178 | +} |
| 179 | + |
| 180 | +// Type implements the api.Tariff interface |
| 181 | +func (t *Entsoe) Type() api.TariffType { |
| 182 | + return api.TariffTypePriceDynamic |
| 183 | +} |
0 commit comments