Add schedule for planner (#16091)

This commit is contained in:
Maschga 2024-12-01 12:53:11 +01:00 • committed by GitHub
parent 35c9452d76
commit a35a705e48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
33 changed files with 1771 additions and 421 deletions

View file

@ -38,6 +38,7 @@ const (
// effective values
EffectivePriority = "effectivePriority" // effective priority
EffectivePlanId = "effectivePlanId" // effective plan id
EffectivePlanTime = "effectivePlanTime" // effective plan time
EffectivePlanSoc = "effectivePlanSoc" // effective plan soc
EffectiveMinCurrent = "effectiveMinCurrent" // effective min current
@ -67,6 +68,9 @@ const (
PlanProjectedEnd = "planProjectedEnd" // charge plan ends (end of last slot)
PlanOverrun = "planOverrun" // charge plan goal not reachable in time
// repeating plans
RepeatingPlans = "repeatingPlans" // key to access all repeating plans in db
// remote control
RemoteDisabled = "remoteDisabled" // remote disabled
RemoteDisabledSource = "remoteDisabledSource" // remote disabled source

View file

@ -67,6 +67,8 @@ type API interface {
// EffectivePriority returns the effective priority
EffectivePriority() int
// EffectivePlanId returns the effective plan id
EffectivePlanId() int
// EffectivePlanTime returns the effective plan time
EffectivePlanTime() time.Time
// EffectiveMinPower returns the min charging power for the minimum active phases

View file

@ -83,6 +83,20 @@ func (mr *MockAPIMockRecorder) EffectiveMinPower() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EffectiveMinPower", reflect.TypeOf((*MockAPI)(nil).EffectiveMinPower))
}
// EffectivePlanId mocks base method.
func (m *MockAPI) EffectivePlanId() int {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "EffectivePlanId")
ret0, _ := ret[0].(int)
return ret0
}
// EffectivePlanId indicates an expected call of EffectivePlanId.
func (mr *MockAPIMockRecorder) EffectivePlanId() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EffectivePlanId", reflect.TypeOf((*MockAPI)(nil).EffectivePlanId))
}
// EffectivePlanTime mocks base method.
func (m *MockAPI) EffectivePlanTime() time.Time {
m.ctrl.T.Helper()

View file

@ -1,16 +1,19 @@
package core
import (
"sort"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/keys"
"github.com/evcc-io/evcc/core/vehicle"
"github.com/evcc-io/evcc/util"
)
// PublishEffectiveValues publishes all effective values
func (lp *Loadpoint) PublishEffectiveValues() {
lp.publish(keys.EffectivePriority, lp.EffectivePriority())
lp.publish(keys.EffectivePlanId, lp.EffectivePlanId())
lp.publish(keys.EffectivePlanTime, lp.EffectivePlanTime())
lp.publish(keys.EffectivePlanSoc, lp.EffectivePlanSoc())
lp.publish(keys.EffectiveMinCurrent, lp.effectiveMinCurrent())
@ -28,24 +31,72 @@ func (lp *Loadpoint) EffectivePriority() int {
return lp.GetPriority()
}
// vehiclePlanSoc returns the next vehicle plan time and soc
func (lp *Loadpoint) vehiclePlanSoc() (time.Time, int) {
// nextVehiclePlan returns the next vehicle plan time, soc and id
func (lp *Loadpoint) nextVehiclePlan() (time.Time, int, int) {
if v := lp.GetVehicle(); v != nil {
return vehicle.Settings(lp.log, v).GetPlanSoc()
type plan struct {
Time time.Time
Soc int
Id int
}
var plans []plan
// static plan
if planTime, soc := vehicle.Settings(lp.log, v).GetPlanSoc(); soc != 0 {
plans = append(plans, plan{Id: 1, Soc: soc, Time: planTime})
}
// repeating plans
for index, rp := range vehicle.Settings(lp.log, v).GetRepeatingPlans() {
if !rp.Active || len(rp.Weekdays) == 0 {
continue
}
time, err := util.GetNextOccurrence(rp.Weekdays, rp.Time, rp.Tz)
if err != nil {
lp.log.DEBUG.Printf("invalid repeating plan: weekdays=%v, time=%s, tz=%s, error=%v", rp.Weekdays, rp.Time, rp.Tz, err)
continue
}
plans = append(plans, plan{Id: index + 2, Soc: rp.Soc, Time: time})
}
// sort plans by time
sort.Slice(plans, func(i, j int) bool {
return plans[i].Time.Before(plans[j].Time)
})
if len(plans) > 0 {
return plans[0].Time, plans[0].Soc, plans[0].Id
}
}
return time.Time{}, 0
return time.Time{}, 0, 0
}
// EffectivePlanSoc returns the soc target for the current plan
func (lp *Loadpoint) EffectivePlanSoc() int {
_, soc := lp.vehiclePlanSoc()
_, soc, _ := lp.nextVehiclePlan()
return soc
}
// EffectivePlanSoc returns the id for the current plan
func (lp *Loadpoint) EffectivePlanId() int {
if lp.socBasedPlanning() {
_, _, id := lp.nextVehiclePlan()
return id
}
if lp.planEnergy > 0 {
return 1
}
// no plan
return 0
}
// EffectivePlanTime returns the effective plan time
func (lp *Loadpoint) EffectivePlanTime() time.Time {
if lp.socBasedPlanning() {
ts, _ := lp.vehiclePlanSoc()
ts, _, _ := lp.nextVehiclePlan()
return ts
}

View file

@ -64,7 +64,7 @@ func (lp *Loadpoint) GetPlanGoal() (float64, bool) {
defer lp.RUnlock()
if lp.socBasedPlanning() {
_, soc := vehicle.Settings(lp.log, lp.GetVehicle()).GetPlanSoc()
_, soc, _ := lp.nextVehiclePlan()
return float64(soc), true
}

View file

@ -18,17 +18,18 @@ type planStruct struct {
}
type vehicleStruct struct {
Title string `json:"title"`
Icon string `json:"icon,omitempty"`
Capacity float64 `json:"capacity,omitempty"`
Phases int `json:"phases,omitempty"`
MinSoc int `json:"minSoc,omitempty"`
LimitSoc int `json:"limitSoc,omitempty"`
MinCurrent float64 `json:"minCurrent,omitempty"`
MaxCurrent float64 `json:"maxCurrent,omitempty"`
Priority int `json:"priority,omitempty"`
Features []string `json:"features,omitempty"`
Plans []planStruct `json:"plans,omitempty"`
Title string `json:"title"`
Icon string `json:"icon,omitempty"`
Capacity float64 `json:"capacity,omitempty"`
Phases int `json:"phases,omitempty"`
MinSoc int `json:"minSoc,omitempty"`
LimitSoc int `json:"limitSoc,omitempty"`
MinCurrent float64 `json:"minCurrent,omitempty"`
MaxCurrent float64 `json:"maxCurrent,omitempty"`
Priority int `json:"priority,omitempty"`
Features []string `json:"features,omitempty"`
Plan *planStruct `json:"plan,omitempty"`
RepeatingPlans []api.RepeatingPlanStruct `json:"repeatingPlans"`
}
// publishVehicles returns a list of vehicle titles
@ -37,28 +38,28 @@ func (site *Site) publishVehicles() {
res := make(map[string]vehicleStruct, len(vv))
for _, v := range vv {
var plans []planStruct
var plan *planStruct
// TODO: add support for multiple plans
if time, soc := v.GetPlanSoc(); !time.IsZero() {
plans = append(plans, planStruct{Soc: soc, Time: time})
plan = &planStruct{Soc: soc, Time: time}
}
instance := v.Instance()
ac := instance.OnIdentified()
res[v.Name()] = vehicleStruct{
Title: instance.Title(),
Icon: instance.Icon(),
Capacity: instance.Capacity(),
Phases: instance.Phases(),
MinSoc: v.GetMinSoc(),
LimitSoc: v.GetLimitSoc(),
MinCurrent: ac.MinCurrent,
MaxCurrent: ac.MaxCurrent,
Priority: ac.Priority,
Features: lo.Map(instance.Features(), func(f api.Feature, _ int) string { return f.String() }),
Plans: plans,
Title: instance.Title(),
Icon: instance.Icon(),
Capacity: instance.Capacity(),
Phases: instance.Phases(),
MinSoc: v.GetMinSoc(),
LimitSoc: v.GetLimitSoc(),
MinCurrent: ac.MinCurrent,
MaxCurrent: ac.MaxCurrent,
Priority: ac.Priority,
Features: lo.Map(instance.Features(), func(f api.Feature, _ int) string { return f.String() }),
Plan: plan,
RepeatingPlans: v.GetRepeatingPlans(),
}
if lp := site.coordinator.Owner(instance); lp != nil {

View file

@ -103,3 +103,38 @@ func (v *adapter) SetPlanSoc(ts time.Time, soc int) error {
return nil
}
func (v *adapter) SetRepeatingPlans(plans []api.RepeatingPlanStruct) error {
for _, plan := range plans {
for _, day := range plan.Weekdays {
if day < 0 || day > 6 {
return fmt.Errorf("weekday out of range: %v", day)
}
}
if _, err := time.LoadLocation(plan.Tz); err != nil {
return fmt.Errorf("invalid timezone: %v", err)
}
if _, err := time.Parse("15:04", plan.Time); err != nil {
return fmt.Errorf("invalid time: %v", err)
}
}
v.log.DEBUG.Printf("update repeating plans for %s to: %v", v.name, plans)
settings.SetJson(v.key()+keys.RepeatingPlans, plans)
v.publish()
return nil
}
func (v *adapter) GetRepeatingPlans() []api.RepeatingPlanStruct {
var plans []api.RepeatingPlanStruct
err := settings.Json(v.key()+keys.RepeatingPlans, &plans)
if err == nil {
return plans
}
return []api.RepeatingPlanStruct{}
}

View file

@ -43,6 +43,11 @@ type API interface {
// SetPlanSoc sets the charge plan time and soc
SetPlanSoc(time.Time, int) error
// GetRepeatingPlans returns every repeating plan
GetRepeatingPlans() []api.RepeatingPlanStruct
// SetRepeatingPlans stores every repeating plan
SetRepeatingPlans([]api.RepeatingPlanStruct) error
// // GetMinCurrent returns the min charging current
// GetMinCurrent() float64
// // SetMinCurrent sets the min charging current

View file

@ -47,3 +47,12 @@ func (v *dummy) GetPlanSoc() (time.Time, int) {
func (v *dummy) SetPlanSoc(ts time.Time, soc int) error {
return nil
}
// SetRepeatingPlans stores every repeating plan
func (v *dummy) SetRepeatingPlans(plans []api.RepeatingPlanStruct) error {
return nil
}
func (v *dummy) GetRepeatingPlans() []api.RepeatingPlanStruct {
return []api.RepeatingPlanStruct{}
}

View file

@ -84,6 +84,20 @@ func (mr *MockAPIMockRecorder) GetPlanSoc() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetPlanSoc", reflect.TypeOf((*MockAPI)(nil).GetPlanSoc))
}
// GetRepeatingPlans mocks base method.
func (m *MockAPI) GetRepeatingPlans() []api.RepeatingPlanStruct {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetRepeatingPlans")
ret0, _ := ret[0].([]api.RepeatingPlanStruct)
return ret0
}
// GetRepeatingPlans indicates an expected call of GetRepeatingPlans.
func (mr *MockAPIMockRecorder) GetRepeatingPlans() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRepeatingPlans", reflect.TypeOf((*MockAPI)(nil).GetRepeatingPlans))
}
// Instance mocks base method.
func (m *MockAPI) Instance() api.Vehicle {
m.ctrl.T.Helper()
@ -149,3 +163,17 @@ func (mr *MockAPIMockRecorder) SetPlanSoc(arg0, arg1 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetPlanSoc", reflect.TypeOf((*MockAPI)(nil).SetPlanSoc), arg0, arg1)
}
// SetRepeatingPlans mocks base method.
func (m *MockAPI) SetRepeatingPlans(arg0 []api.RepeatingPlanStruct) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SetRepeatingPlans", arg0)
ret0, _ := ret[0].(error)
return ret0
}
// SetRepeatingPlans indicates an expected call of SetRepeatingPlans.
func (mr *MockAPIMockRecorder) SetRepeatingPlans(arg0 any) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetRepeatingPlans", reflect.TypeOf((*MockAPI)(nil).SetRepeatingPlans), arg0)
}