forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtariffs.go
66 lines (55 loc) · 1.41 KB
/
tariffs.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
package tariff
import (
"time"
"github.com/evcc-io/evcc/api"
"golang.org/x/text/currency"
)
// Co2Equivalent is the unit for co2 emissions
const Co2Equivalent = "gCO2eq"
type Tariffs struct {
Currency currency.Unit
Grid, FeedIn, Planner api.Tariff
}
func NewTariffs(currency currency.Unit, grid, feedin, planner api.Tariff) *Tariffs {
if planner == nil {
planner = grid
}
return &Tariffs{
Currency: currency,
Grid: grid,
FeedIn: feedin,
Planner: planner,
}
}
func currentPrice(t api.Tariff) (float64, error) {
if t != nil {
if rr, err := t.Rates(); err == nil {
if r, err := rr.Current(time.Now()); err == nil {
return r.Price, nil
}
}
}
return 0, api.ErrNotAvailable
}
// CurrentGridPrice returns the current grid price.
func (t *Tariffs) CurrentGridPrice() (float64, error) {
return currentPrice(t.Grid)
}
// CurrentFeedInPrice returns the current feed-in price.
func (t *Tariffs) CurrentFeedInPrice() (float64, error) {
return currentPrice(t.FeedIn)
}
// CurrentCo2 determines the grids co2 emission.
func (t *Tariffs) CurrentCo2() (float64, error) {
if t.Planner != nil && t.Planner.Unit() == Co2Equivalent {
return currentPrice(t.Planner)
}
return 0, api.ErrNotAvailable
}
// outdatedError returns api.ErrOutdated if t is older than 2*d
func outdatedError(t time.Time, d time.Duration) error {
if time.Since(t) > 2*d {
return api.ErrOutdated
}
return nil
}