Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions cmd/demo_history.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"time"

"github.com/evcc-io/evcc/core/metrics"
"github.com/evcc-io/evcc/core/session"
"github.com/evcc-io/evcc/db"
"gorm.io/gorm"
)

func seedDemoHistory(now time.Time) error {
return db.Instance.Transaction(func(tx *gorm.DB) error {
var count int64
if err := tx.Model(&session.Session{}).Count(&count).Error; err != nil || count != 0 {
Comment thread
Maschga marked this conversation as resolved.
Resolved
return err
}
seeded, err := metrics.SeedDemoHistory(tx, now)
if err != nil || !seeded {
return err
}

const historyDays = 40
energy := [...]float64{12.5, 23, 16, 8.5, 19, 14}
var sessions []session.Session
for daysAgo := historyDays - 1; daysAgo >= 0; daysAgo-- {
if daysAgo%10 == 3 {
continue
}
day := now.AddDate(0, 0, -daysAgo)
finished := time.Date(day.Year(), day.Month(), day.Day(), 13+daysAgo%7, 0, 0, 0, day.Location())
if finished.After(now) {
finished = now
}
duration := 2 * time.Hour
created := finished.Add(-duration)
if created.Day() != day.Day() {
continue
}
i := daysAgo % len(energy)
solar := float64(95 - 14*i)
s := session.Session{
Created: created, Finished: finished,
Loadpoint: "Carport", Vehicle: "blue e-Golf", ChargedEnergy: energy[i],
SolarPercentage: &solar, ChargeDuration: &duration,
}
if i%2 == 1 {
s.Loadpoint, s.Vehicle = "Garage", "white Model 3"
}
sessions = append(sessions, s)
}
return tx.Create(&sessions).Error
})
}
34 changes: 34 additions & 0 deletions cmd/demo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package cmd

import (
"testing"
"time"

"github.com/evcc-io/evcc/core/metrics"
"github.com/evcc-io/evcc/core/session"
"github.com/evcc-io/evcc/db"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSeedDemoHistory(t *testing.T) {
require.NoError(t, db.NewInstance("sqlite", ":memory:"))
t.Cleanup(func() { require.NoError(t, db.Close()) })

now := time.Date(2026, time.December, 10, 17, 0, 0, 0, time.UTC)
require.NoError(t, seedDemoHistory(now))

var sessions int64
require.NoError(t, db.Instance.Model(&session.Session{}).Count(&sessions).Error)
assert.Positive(t, sessions)

flow, err := metrics.QueryFlow(now.Add(-24*time.Hour), now)
require.NoError(t, err)
assert.NotEmpty(t, flow.Flows)

batteries, err := metrics.QueryEnergy(now.Add(-24*time.Hour), now, "15m", false, metrics.EnergyFilter{Group: metrics.Battery})
require.NoError(t, err)
require.Len(t, batteries, 1)
require.NotEmpty(t, batteries[0].Data)
assert.NotNil(t, batteries[0].Data[0].SocTemp)
}
8 changes: 6 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func runRoot(cmd *cobra.Command, args []string) {

// load config and re-configure logging after reading config file
var err error
if ok, _ := cmd.Flags().GetBool(flagDemoMode); ok {
demo, _ := cmd.Flags().GetBool(flagDemoMode)
if demo {
log.INFO.Println("switching into demo mode as requested through the --demo flag")
if err := demoConfig(&conf); err != nil {
log.FATAL.Fatal(err)
Expand All @@ -201,6 +202,9 @@ func runRoot(cmd *cobra.Command, args []string) {
if err == nil {
err = configureEnvironment(cmd, &conf)
}
if err == nil && demo {
err = wrapErrorWithClass(ClassDatabase, seedDemoHistory(time.Now()))
}

// configure plugin external url
if err == nil {
Expand Down Expand Up @@ -517,7 +521,7 @@ func runRoot(cmd *cobra.Command, args []string) {
valueChan <- util.Param{Key: keys.AuthDisabled, Val: true}
}

if ok, _ := cmd.Flags().GetBool(flagDemoMode); ok {
if demo {
log.WARN.Println("Authentication is locked in demo mode. Login-features are disabled.")
authObject.SetAuthMode(auth.Locked)
valueChan <- util.Param{Key: keys.DemoMode, Val: true}
Expand Down
64 changes: 64 additions & 0 deletions core/metrics/demo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package metrics

import (
"math"
"time"

"github.com/evcc-io/evcc/tariff"
"gorm.io/gorm"
)

// SeedDemoHistory adds synthetic meter history if the transaction has none.
func SeedDemoHistory(tx *gorm.DB, now time.Time) (bool, error) {
var count int64
if err := tx.Model(&meter{}).Count(&count).Error; err != nil || count != 0 {
return false, err
}

devices := []entity{
{Group: PV, Name: "pv", Title: "pv"},
{Group: Home, Name: Home, Title: Home},
{Group: Grid, Name: "grid", Title: Grid},
{Group: Battery, Name: "battery", Title: "battery"},
{Group: Loadpoint, Name: "lp-1", Title: "Carport"},
Comment thread
Maschga marked this conversation as resolved.
Resolved
}
for i := range devices {
d := &devices[i]
if err := tx.Where(entity{Group: d.Group, Name: d.Name}).FirstOrCreate(d).Error; err != nil {
return false, err
}
}

end := now.Truncate(tariff.SlotDuration)
var history []meter
for t := dayStart(now).AddDate(0, 0, -6); t.Before(end); t = t.Add(tariff.SlotDuration) {
hour := float64(t.Hour()) + float64(t.Minute())/60
pv := 1.8 * max(0, math.Sin(math.Pi*(hour-6)/12))
home := 0.12
if hour >= 18 {
home = 0.32
}
var charge, discharge, car float64
if hour >= 9 && hour < 12 {
charge = 0.45
}
if hour >= 19 && hour < 23 {
discharge = 0.25
}
if hour >= 12 && hour < 15 {
car = 0.9
}
grid := home + charge + car - pv - discharge
for i, value := range [][2]float64{{pv, 0}, {home, 0}, {max(0, grid), max(0, -grid)}, {charge, discharge}, {car, 0}} {
slot := meter{Meter: devices[i].Id, Timestamp: t.Unix(), Energy: value[0], ReturnEnergy: value[1], Recovered: slotMeasured}
if devices[i].Group == Battery {
charged := min(1, max(0, (hour-9)/3))
discharged := min(1, max(0, (hour-19)/4))
slot.SocTemp = new(35 + 45*(charged-discharged))
}
history = append(history, slot)
}
}

return true, tx.CreateInBatches(history, 100).Error
}
Loading