Skip to content

Commit

Permalink
Persistent Savings (evcc-io#4956)
Browse files Browse the repository at this point in the history
  • Loading branch information
naltatis authored Oct 25, 2022
1 parent 2905c0a commit cef8bb1
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion assets/i18n/de.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ modalUpdateStatusFailed = "Aktualisierung nicht möglich: "
footerShort = "{percent}% Sonne"
footerLong = "{percent}% Sonnenenergie"
modalTitle = "Auswertung Ladeenergie"
sinceServerStart = "Seit Serverstart {since}."
since = "Seit {since}."
percentTitle = "Sonnenenergie"
percentSelf = "{self} kWh Sonne"
percentGrid = "{grid} kWh Netz"
Expand Down
2 changes: 1 addition & 1 deletion assets/i18n/en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ modalUpdateStatusFailed = "Update failed: "
footerShort = "{percent}% solar"
footerLong = "{percent}% solar energy"
modalTitle = "Charge Energy Overview"
sinceServerStart = "Since server start {since}."
since = "Since {since}."
percentTitle = "Solar Energy"
percentSelf = "{self} kWh solar"
percentGrid = "{grid} kWh grid"
Expand Down
2 changes: 1 addition & 1 deletion assets/i18n/lt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ modalUpdateStatusFailed = "Naujinimas nepavyko: "
footerShort = "{percent}% saulės"
footerLong = "{percent}% saulės energija"
modalTitle = "Įkrovimo energijos apžvalga"
sinceServerStart = "Nuo evcc starto {since}."
since = "Nuo {since}."
percentTitle = "Saulės energija"
percentSelf = "{self} kWh saulės"
percentGrid = "{grid} kWh tinklas"
Expand Down
2 changes: 1 addition & 1 deletion assets/js/components/Savings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<div class="modal-body">
<p>
{{
$t("footer.savings.sinceServerStart", {
$t("footer.savings.since", {
since: fmtTimeAgo(secondsSinceStart()),
})
}}
Expand Down
23 changes: 23 additions & 0 deletions core/savings.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/benbjohnson/clock"
"github.com/evcc-io/evcc/server/db/settings"
"github.com/evcc-io/evcc/tariff"
)

Expand Down Expand Up @@ -41,9 +42,29 @@ func NewSavings(tariffs tariff.Tariffs) *Savings {
updated: clock.Now(),
}

savings.load()

return savings
}

func (s *Savings) load() {
s.started, _ = settings.Time("savings.started")
s.gridCharged, _ = settings.Float("savings.gridCharged")
s.gridCost, _ = settings.Float("savings.gridCost")
s.gridSavedCost, _ = settings.Float("savings.gridSavedCost")
s.selfConsumptionCharged, _ = settings.Float("savings.selfConsumptionCharged")
s.selfConsumptionCost, _ = settings.Float("savings.selfConsumptionCost")
}

func (s *Savings) save() {
settings.SetTime("savings.started", s.started)
settings.SetFloat("savings.gridCharged", s.gridCharged)
settings.SetFloat("savings.gridCost", s.gridCost)
settings.SetFloat("savings.gridSavedCost", s.gridSavedCost)
settings.SetFloat("savings.selfConsumptionCharged", s.selfConsumptionCharged)
settings.SetFloat("savings.selfConsumptionCost", s.selfConsumptionCost)
}

func (s *Savings) Since() time.Time {
return s.started
}
Expand Down Expand Up @@ -155,5 +176,7 @@ func (s *Savings) Update(p publisher, gridPower, pvPower, batteryPower, chargePo
p.publish("savingsEffectivePrice", s.EffectivePrice())
p.publish("savingsAmount", s.SavingsAmount())

s.save()

return deltaCharged, deltaSelf
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<meta name="theme-color" content="#020318" />

<title>evcc</title>
<script type="module" crossorigin src="./assets/index.34c57bb3.js"></script>
<link rel="stylesheet" href="./assets/index.0f7f1beb.css">
<script type="module" crossorigin src="./assets/index.170c5f46.js"></script>
<link rel="stylesheet" href="./assets/index.08f10448.css">
</head>

<body>
Expand Down
13 changes: 13 additions & 0 deletions server/db/settings/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"strconv"
"sync/atomic"
"time"

"github.com/evcc-io/evcc/server/db"
"golang.org/x/exp/slices"
Expand Down Expand Up @@ -62,6 +63,10 @@ func SetFloat(key string, val float64) {
SetString(key, strconv.FormatFloat(val, 'f', -1, 64))
}

func SetTime(key string, val time.Time) {
SetString(key, val.Format(time.RFC3339))
}

func SetJson(key string, val any) error {
b, err := json.Marshal(val)
if err == nil {
Expand Down Expand Up @@ -96,6 +101,14 @@ func Float(key string) (float64, error) {
return strconv.ParseFloat(s, 64)
}

func Time(key string) (time.Time, error) {
s, err := String(key)
if err != nil {
return time.Now(), err
}
return time.Parse(time.RFC3339, s)
}

func Json(key string, res any) error {
s, err := String(key)
if err == nil {
Expand Down

0 comments on commit cef8bb1

Please sign in to comment.