From 16107f9c2ba59cbe205a520b4999b7340fbb6a77 Mon Sep 17 00:00:00 2001 From: premultiply <4681172+premultiply@users.noreply.github.com> Date: Thu, 5 Sep 2024 18:47:19 +0200 Subject: [PATCH] Ocpp: move get/set charging profile to connector (#15660) --- charger/ocpp.go | 47 ++------------------------------------- charger/ocpp/connector.go | 21 +++++++++++++++++ charger/ocpp/cs_core.go | 35 +++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 45 deletions(-) diff --git a/charger/ocpp.go b/charger/ocpp.go index 40f0cc85fa..4319ff02e0 100644 --- a/charger/ocpp.go +++ b/charger/ocpp.go @@ -13,7 +13,6 @@ import ( "github.com/evcc-io/evcc/core/loadpoint" "github.com/evcc-io/evcc/util" "github.com/lorenzodonini/ocpp-go/ocpp1.6/core" - "github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging" "github.com/lorenzodonini/ocpp-go/ocpp1.6/types" ) @@ -289,7 +288,7 @@ func (c *OCPP) Enabled() (bool, error) { } // fallback to querying the active charging profile schedule limit - if v, err := c.getScheduleLimit(); err == nil { + if v, err := c.conn.GetScheduleLimit(60); err == nil { return v > 0, nil } @@ -329,22 +328,9 @@ func (c *OCPP) initTransaction() error { return ocpp.Wait(err, rc) } -func (c *OCPP) setChargingProfile(profile *types.ChargingProfile) error { - rc := make(chan error, 1) - err := ocpp.Instance().SetChargingProfile(c.cp.ID(), func(resp *smartcharging.SetChargingProfileConfirmation, err error) { - if err == nil && resp != nil && resp.Status != smartcharging.ChargingProfileStatusAccepted { - err = errors.New(string(resp.Status)) - } - - rc <- err - }, c.conn.ID(), profile) - - return ocpp.Wait(err, rc) -} - // setCurrent sets the TxDefaultChargingProfile with given current func (c *OCPP) setCurrent(current float64) error { - err := c.setChargingProfile(c.createTxDefaultChargingProfile(math.Trunc(10*current) / 10)) + err := c.conn.SetChargingProfile(c.createTxDefaultChargingProfile(math.Trunc(10*current) / 10)) if err != nil { err = fmt.Errorf("set charging profile: %w", err) } @@ -352,35 +338,6 @@ func (c *OCPP) setCurrent(current float64) error { return err } -// getScheduleLimit queries the current or power limit the charge point is currently set to offer -func (c *OCPP) getScheduleLimit() (float64, error) { - const duration = 60 // duration of requested schedule in seconds - - var limit float64 - - rc := make(chan error, 1) - err := ocpp.Instance().GetCompositeSchedule(c.cp.ID(), func(resp *smartcharging.GetCompositeScheduleConfirmation, err error) { - if err == nil && resp != nil && resp.Status != smartcharging.GetCompositeScheduleStatusAccepted { - err = errors.New(string(resp.Status)) - } - - if err == nil { - if resp.ChargingSchedule != nil && len(resp.ChargingSchedule.ChargingSchedulePeriod) > 0 { - // return first (current) period limit - limit = resp.ChargingSchedule.ChargingSchedulePeriod[0].Limit - } else { - err = fmt.Errorf("invalid ChargingSchedule") - } - } - - rc <- err - }, c.conn.ID(), duration) - - err = ocpp.Wait(err, rc) - - return limit, err -} - // createTxDefaultChargingProfile returns a TxDefaultChargingProfile with given current func (c *OCPP) createTxDefaultChargingProfile(current float64) *types.ChargingProfile { phases := c.phases diff --git a/charger/ocpp/connector.go b/charger/ocpp/connector.go index d032cf9560..d2363061cf 100644 --- a/charger/ocpp/connector.go +++ b/charger/ocpp/connector.go @@ -71,6 +71,27 @@ func (conn *Connector) TriggerMessageRequest(feature remotetrigger.MessageTrigge }) } +func (conn *Connector) SetChargingProfile(profile *types.ChargingProfile) error { + return Instance().SetChargingProfileRequest(conn.cp.ID(), conn.id, profile) +} + +// getScheduleLimit queries the current or power limit the charge point is currently set to offer +func (conn *Connector) GetScheduleLimit(duration int) (float64, error) { + var limit float64 + schedule, err := Instance().GetCompositeScheduleRequest(conn.cp.ID(), conn.id, duration) + + if err == nil { + if schedule != nil && len(schedule.ChargingSchedulePeriod) > 0 { + // return first (current) period limit + limit = schedule.ChargingSchedulePeriod[0].Limit + } else { + err = fmt.Errorf("invalid ChargingSchedule") + } + } + + return limit, err +} + // WatchDog triggers meter values messages if older than timeout. // Must be wrapped in a goroutine. func (conn *Connector) WatchDog(timeout time.Duration) { diff --git a/charger/ocpp/cs_core.go b/charger/ocpp/cs_core.go index 6351bb8a20..873f07e745 100644 --- a/charger/ocpp/cs_core.go +++ b/charger/ocpp/cs_core.go @@ -6,6 +6,8 @@ import ( "github.com/lorenzodonini/ocpp-go/ocpp1.6/core" "github.com/lorenzodonini/ocpp-go/ocpp1.6/firmware" "github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger" + "github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging" + "github.com/lorenzodonini/ocpp-go/ocpp1.6/types" ) // cs actions @@ -52,6 +54,39 @@ func (cs *CS) ChangeAvailabilityRequest(id string, connector int, availabilityTy return Wait(err, rc) } +func (cs *CS) SetChargingProfileRequest(id string, connector int, profile *types.ChargingProfile) error { + rc := make(chan error, 1) + + err := cs.SetChargingProfile(id, func(request *smartcharging.SetChargingProfileConfirmation, err error) { + if err == nil && request != nil && request.Status != smartcharging.ChargingProfileStatusAccepted { + err = errors.New(string(request.Status)) + } + + rc <- err + }, connector, profile) + + return Wait(err, rc) +} + +func (cs *CS) GetCompositeScheduleRequest(id string, connector int, duration int) (*types.ChargingSchedule, error) { + var schedule *types.ChargingSchedule + rc := make(chan error, 1) + + err := cs.GetCompositeSchedule(id, func(request *smartcharging.GetCompositeScheduleConfirmation, err error) { + if err == nil && request != nil && request.Status != smartcharging.GetCompositeScheduleStatusAccepted { + err = errors.New(string(request.Status)) + } + + schedule = request.ChargingSchedule + + rc <- err + }, connector, duration) + + err = Wait(err, rc) + + return schedule, err +} + // cp actions func (cs *CS) OnAuthorize(id string, request *core.AuthorizeRequest) (*core.AuthorizeConfirmation, error) {