Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
weblate committed Nov 29, 2024
2 parents a074733 + b52d08a commit 5b22a4c
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 45 deletions.
2 changes: 1 addition & 1 deletion tariff/awattar.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (t *Awattar) run(done chan error) {
ar := api.Rate{
Start: r.StartTimestamp.Local(),
End: r.EndTimestamp.Local(),
Price: t.totalPrice(r.Marketprice / 1e3),
Price: t.totalPrice(r.Marketprice/1e3, r.StartTimestamp),
}
data = append(data, ar)
}
Expand Down
2 changes: 1 addition & 1 deletion tariff/edf-tempo.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (t *EdfTempo) run(done chan error) {
ar := api.Rate{
Start: ts,
End: ts.Add(time.Hour),
Price: t.totalPrice(t.prices[strings.ToLower(r.Value)]),
Price: t.totalPrice(t.prices[strings.ToLower(r.Value)], ts),
}
data = append(data, ar)
}
Expand Down
8 changes: 4 additions & 4 deletions tariff/elering.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ func (t *Elering) run(done chan error) {

data := make(api.Rates, 0, len(res.Data[t.region]))
for _, r := range res.Data[t.region] {
ts := time.Unix(r.Timestamp, 0)
ts := time.Unix(r.Timestamp, 0).Local()

ar := api.Rate{
Start: ts.Local(),
End: ts.Add(time.Hour).Local(),
Price: t.totalPrice(r.Price / 1e3),
Start: ts,
End: ts.Add(time.Hour),
Price: t.totalPrice(r.Price/1e3, ts),
}
data = append(data, ar)
}
Expand Down
49 changes: 29 additions & 20 deletions tariff/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tariff
import (
"errors"
"fmt"
"time"

"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
Expand All @@ -13,10 +14,16 @@ type embed struct {
Tax float64 `mapstructure:"tax"`
Formula string `mapstructure:"formula"`

calc func(float64) (float64, error)
calc func(float64, time.Time) (float64, error)
}

func (t *embed) init() error {
func (t *embed) init() (err error) {
defer func() {
if r := recover(); r != nil && err == nil {
err = fmt.Errorf("panic: %v", r)
}
}()

if t.Formula == "" {
return nil
}
Expand All @@ -26,19 +33,18 @@ func (t *embed) init() error {
return err
}

if _, err := vm.Eval(`import "math"`); err != nil {
return err
}

if _, err := vm.Eval("var price, charges, tax float64"); err != nil {
return err
}

if _, err := vm.Eval(fmt.Sprintf("charges = %f", t.Charges)); err != nil {
return err
}

if _, err := vm.Eval(fmt.Sprintf("tax = %f", t.Tax)); err != nil {
if _, err := vm.Eval(fmt.Sprintf(`
import (
"math"
"time"
)
var (
price float64
charges float64 = %f
tax float64 = %f
ts time.Time
)`, t.Charges, t.Tax)); err != nil {
return err
}

Expand All @@ -47,8 +53,11 @@ func (t *embed) init() error {
return err
}

t.calc = func(price float64) (float64, error) {
if _, err := vm.Eval(fmt.Sprintf("price = %f", price)); err != nil {
t.calc = func(price float64, ts time.Time) (float64, error) {
if _, err := vm.Eval(fmt.Sprintf(`
price = %f
ts = time.Unix(%d, 0).Local()
`, price, ts.Unix())); err != nil {
return 0, err
}

Expand All @@ -65,14 +74,14 @@ func (t *embed) init() error {
}

// test the formula
_, err = t.calc(0)
_, err = t.calc(0, time.Now())

return err
}

func (t *embed) totalPrice(price float64) float64 {
func (t *embed) totalPrice(price float64, ts time.Time) float64 {
if t.calc != nil {
res, _ := t.calc(price)
res, _ := t.calc(price, ts)
return res
}
return (price + t.Charges) * (1 + t.Tax)
Expand Down
8 changes: 4 additions & 4 deletions tariff/energinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ func (t *Energinet) run(done chan error) {

data := make(api.Rates, 0, len(res.Records))
for _, r := range res.Records {
date, _ := time.Parse("2006-01-02T15:04:05", r.HourUTC)
ts, _ := time.Parse("2006-01-02T15:04:05", r.HourUTC)
ar := api.Rate{
Start: date.Local(),
End: date.Add(time.Hour).Local(),
Price: t.totalPrice(r.SpotPriceDKK / 1e3),
Start: ts.Local(),
End: ts.Add(time.Hour).Local(),
Price: t.totalPrice(r.SpotPriceDKK/1e3, ts),
}
data = append(data, ar)
}
Expand Down
6 changes: 3 additions & 3 deletions tariff/entsoe.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ func (t *Entsoe) run(done chan error) {
data := make(api.Rates, 0, len(res))
for _, r := range res {
ar := api.Rate{
Start: r.Start,
End: r.End,
Price: t.totalPrice(r.Value),
Start: r.Start.Local(),
End: r.End.Local(),
Price: t.totalPrice(r.Value, r.Start),
}
data = append(data, ar)
}
Expand Down
13 changes: 5 additions & 8 deletions tariff/pun.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,16 @@ func (t *Pun) getData(day time.Time) (api.Rates, error) {
return nil, fmt.Errorf("load location: %w", err)
}

start := time.Date(date.Year(), date.Month(), date.Day(), hour-1, 0, 0, 0, location)
end := start.Add(time.Hour)

priceStr := strings.Replace(p.PUN, ",", ".", -1) // Ersetzen Sie Komma durch Punkt
price, err := strconv.ParseFloat(priceStr, 64)
price, err := strconv.ParseFloat(strings.ReplaceAll(p.PUN, ",", "."), 64)
if err != nil {
return nil, fmt.Errorf("parse price: %w", err)
}

ts := time.Date(date.Year(), date.Month(), date.Day(), hour-1, 0, 0, 0, location)
ar := api.Rate{
Start: start,
End: end,
Price: t.totalPrice(price / 1e3),
Start: ts,
End: ts.Add(time.Hour),
Price: t.totalPrice(price/1e3, ts),
}
data = append(data, ar)
}
Expand Down
2 changes: 1 addition & 1 deletion tariff/smartenergy.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (t *SmartEnergy) run(done chan error) {
ar := api.Rate{
Start: r.Date.Local(),
End: r.Date.Add(15 * time.Minute).Local(),
Price: t.totalPrice(r.Value / 100),
Price: t.totalPrice(r.Value/100, r.Date),
}
data = append(data, ar)
}
Expand Down
4 changes: 2 additions & 2 deletions tariff/tariff.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (t *Tariff) run(forecastG func() (string, error), done chan error) {
return backoff.Permanent(err)
}
for i, r := range data {
data[i].Price = t.totalPrice(r.Price)
data[i].Price = t.totalPrice(r.Price, r.Start)
}
return nil
}, bo()); err != nil {
Expand Down Expand Up @@ -140,7 +140,7 @@ func (t *Tariff) priceRates() (api.Rates, error) {
res[i] = api.Rate{
Start: slot,
End: slot.Add(time.Hour),
Price: t.totalPrice(price),
Price: t.totalPrice(price, slot),
}
}

Expand Down
2 changes: 1 addition & 1 deletion tariff/tibber.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (t *Tibber) rates(pi []tibber.Price) api.Rates {
for _, r := range pi {
price := r.Total
if t.Charges != 0 || t.Tax != 0 {
price = t.totalPrice(r.Energy)
price = t.totalPrice(r.Energy, r.StartsAt)
}
ar := api.Rate{
Start: r.StartsAt.Local(),
Expand Down

0 comments on commit 5b22a4c

Please sign in to comment.