Forecast: allow multiple solar tariffs (#18920)
This commit is contained in:
parent
2808a29c84
commit
70dca20f47
15 changed files with 274 additions and 51 deletions
|
|
@ -140,7 +140,7 @@ type Tariffs struct {
|
|||
FeedIn config.Typed
|
||||
Co2 config.Typed
|
||||
Planner config.Typed
|
||||
Solar config.Typed
|
||||
Solar []config.Typed
|
||||
}
|
||||
|
||||
type Network struct {
|
||||
|
|
|
|||
29
api/rates.go
29
api/rates.go
|
|
@ -2,7 +2,6 @@ package api
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"slices"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -23,25 +22,29 @@ func (r Rate) IsZero() bool {
|
|||
type Rates []Rate
|
||||
|
||||
// Sort rates by start time
|
||||
func (r Rates) Sort() {
|
||||
slices.SortStableFunc(r, func(i, j Rate) int {
|
||||
func (rr Rates) Sort() {
|
||||
slices.SortStableFunc(rr, func(i, j Rate) int {
|
||||
return i.Start.Compare(j.Start)
|
||||
})
|
||||
}
|
||||
|
||||
// Current returns the rates current rate or error
|
||||
func (r Rates) Current(now time.Time) (Rate, error) {
|
||||
for _, rr := range r {
|
||||
if !rr.Start.After(now) && rr.End.After(now) {
|
||||
return rr, nil
|
||||
// At returns the rate for given timestamp or error.
|
||||
// Rates MUST be sorted by start time.
|
||||
func (rr Rates) At(ts time.Time) (Rate, error) {
|
||||
if i, ok := slices.BinarySearchFunc(rr, ts, func(r Rate, ts time.Time) int {
|
||||
switch {
|
||||
case ts.Before(r.Start):
|
||||
return +1
|
||||
case !ts.Before(r.End):
|
||||
return -1
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}); ok {
|
||||
return rr[i], nil
|
||||
}
|
||||
|
||||
if len(r) == 0 {
|
||||
return Rate{}, fmt.Errorf("no matching rate for: %s", now.Local().Format(time.RFC3339))
|
||||
}
|
||||
return Rate{}, fmt.Errorf("no matching rate for: %s, %d rates (%s to %s)",
|
||||
now.Local().Format(time.RFC3339), len(r), r[0].Start.Local().Format(time.RFC3339), r[len(r)-1].End.Local().Format(time.RFC3339))
|
||||
return Rate{}, ErrNotAvailable
|
||||
}
|
||||
|
||||
// MarshalMQTT implements server.MQTTMarshaler
|
||||
|
|
|
|||
38
api/rates_test.go
Normal file
38
api/rates_test.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/benbjohnson/clock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRates(t *testing.T) {
|
||||
clock := clock.NewMock()
|
||||
rate := func(start int, val float64) Rate {
|
||||
return Rate{
|
||||
Start: clock.Now().Add(time.Duration(start) * time.Hour),
|
||||
End: clock.Now().Add(time.Duration(start+1) * time.Hour),
|
||||
Price: val,
|
||||
}
|
||||
}
|
||||
|
||||
rr := Rates{rate(1, 1), rate(2, 2), rate(3, 3), rate(4, 4)}
|
||||
|
||||
_, err := rr.At(clock.Now())
|
||||
assert.Error(t, err)
|
||||
|
||||
for i := 1; i <= 4; i++ {
|
||||
r, err := rr.At(clock.Now().Add(time.Duration(i) * time.Hour))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, float64(i), r.Price)
|
||||
|
||||
r, err = rr.At(clock.Now().Add(time.Duration(i)*time.Hour + 30*time.Minute))
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, float64(i), r.Price)
|
||||
}
|
||||
|
||||
_, err = rr.At(clock.Now().Add(5 * time.Hour))
|
||||
assert.Error(t, err)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue