From 119c37cd77e02e4180019d024ce29a44bbef82bb Mon Sep 17 00:00:00 2001 From: andig Date: Fri, 6 Sep 2024 15:51:35 +0200 Subject: [PATCH] chore: remove ocpp public wait (#15911) --- charger/ocpp.go | 30 +++----- charger/ocpp/connector.go | 2 +- charger/ocpp/cp_setup.go | 152 +++++++++++++++++++------------------- charger/ocpp/cs_core.go | 12 ++- charger/ocpp/helper.go | 4 +- 5 files changed, 97 insertions(+), 103 deletions(-) diff --git a/charger/ocpp.go b/charger/ocpp.go index ddf87a60f..b2a8cb532 100644 --- a/charger/ocpp.go +++ b/charger/ocpp.go @@ -381,28 +381,22 @@ func (c *OCPP) Diagnose() { } fmt.Printf("\tConfiguration:\n") - rc := make(chan error, 1) - err := ocpp.Instance().GetConfiguration(c.cp.ID(), func(resp *core.GetConfigurationConfirmation, err error) { - if err == nil { - // sort configuration keys for printing - slices.SortFunc(resp.ConfigurationKey, func(i, j core.ConfigurationKey) int { - return cmp.Compare(i.Key, j.Key) - }) + if resp, err := c.cp.GetConfiguration(); err == nil { + // sort configuration keys for printing + slices.SortFunc(resp.ConfigurationKey, func(i, j core.ConfigurationKey) int { + return cmp.Compare(i.Key, j.Key) + }) - rw := map[bool]string{false: "r/w", true: "r/o"} + rw := map[bool]string{false: "r/w", true: "r/o"} - for _, opt := range resp.ConfigurationKey { - if opt.Value == nil { - continue - } - - fmt.Printf("\t\t%s (%s): %s\n", opt.Key, rw[opt.Readonly], *opt.Value) + for _, opt := range resp.ConfigurationKey { + if opt.Value == nil { + continue } - } - rc <- err - }, nil) - ocpp.Wait(err, rc) + fmt.Printf("\t\t%s (%s): %s\n", opt.Key, rw[opt.Readonly], *opt.Value) + } + } } var _ loadpoint.Controller = (*OCPP)(nil) diff --git a/charger/ocpp/connector.go b/charger/ocpp/connector.go index 34bd49e02..45b5318f7 100644 --- a/charger/ocpp/connector.go +++ b/charger/ocpp/connector.go @@ -89,7 +89,7 @@ func (conn *Connector) remoteStartTransactionRequest() { request.ConnectorId = &connector }) - if err := Wait(err, rc); err != nil { + if err := wait(err, rc); err != nil { conn.log.ERROR.Printf("failed to start remote transaction: %v", err) } } diff --git a/charger/ocpp/cp_setup.go b/charger/ocpp/cp_setup.go index b8499926b..d21b27b2d 100644 --- a/charger/ocpp/cp_setup.go +++ b/charger/ocpp/cp_setup.go @@ -22,84 +22,73 @@ func (cp *CP) Setup(meterValues string, meterInterval time.Duration) error { var meterValuesSampledData string meterValuesSampledDataMaxLength := len(strings.Split(desiredMeasurands, ",")) - rc := make(chan error, 1) + resp, err := cp.GetConfiguration() + if err != nil { + return err + } - err := Instance().GetConfiguration(cp.ID(), func(resp *core.GetConfigurationConfirmation, err error) { - if err == nil { - for _, opt := range resp.ConfigurationKey { - if opt.Value == nil { - continue - } - - switch opt.Key { - case KeyChargeProfileMaxStackLevel: - if val, err := strconv.Atoi(*opt.Value); err == nil { - cp.StackLevel = val - } - - case KeyChargingScheduleAllowedChargingRateUnit: - if *opt.Value == "Power" || *opt.Value == "W" { // "W" is not allowed by spec but used by some CPs - cp.ChargingRateUnit = types.ChargingRateUnitWatts - } - - case KeyConnectorSwitch3to1PhaseSupported: - var val bool - if val, err = strconv.ParseBool(*opt.Value); err == nil { - cp.PhaseSwitching = val - } - - case KeyMaxChargingProfilesInstalled: - if val, err := strconv.Atoi(*opt.Value); err == nil { - cp.ChargingProfileId = val - } - - case KeyMeterValuesSampledData: - if opt.Readonly { - meterValuesSampledDataMaxLength = 0 - } - meterValuesSampledData = *opt.Value - - case KeyMeterValuesSampledDataMaxLength: - if val, err := strconv.Atoi(*opt.Value); err == nil { - meterValuesSampledDataMaxLength = val - } - - case KeyNumberOfConnectors: - if val, err := strconv.Atoi(*opt.Value); err == nil { - cp.NumberOfConnectors = val - } - - case KeySupportedFeatureProfiles: - if !hasProperty(*opt.Value, smartcharging.ProfileName) { - cp.log.WARN.Printf("the required SmartCharging feature profile is not indicated as supported") - } - // correct the availability assumption of RemoteTrigger only in case of a valid looking FeatureProfile list - if hasProperty(*opt.Value, core.ProfileName) { - cp.HasRemoteTriggerFeature = hasProperty(*opt.Value, remotetrigger.ProfileName) - } - - // vendor-specific keys - case KeyAlfenPlugAndChargeIdentifier: - cp.IdTag = *opt.Value - cp.log.DEBUG.Printf("overriding default `idTag` with Alfen-specific value: %s", cp.IdTag) - - case KeyEvBoxSupportedMeasurands: - if meterValues == "" { - meterValues = *opt.Value - } - } - - if err != nil { - break - } - } + for _, opt := range resp.ConfigurationKey { + if opt.Value == nil { + continue } - rc <- err - }, nil) + switch opt.Key { + case KeyChargeProfileMaxStackLevel: + if val, err := strconv.Atoi(*opt.Value); err == nil { + cp.StackLevel = val + } - if err := Wait(err, rc); err != nil { - return err + case KeyChargingScheduleAllowedChargingRateUnit: + if *opt.Value == "Power" || *opt.Value == "W" { // "W" is not allowed by spec but used by some CPs + cp.ChargingRateUnit = types.ChargingRateUnitWatts + } + + case KeyConnectorSwitch3to1PhaseSupported: + var val bool + if val, err = strconv.ParseBool(*opt.Value); err == nil { + cp.PhaseSwitching = val + } + + case KeyMaxChargingProfilesInstalled: + if val, err := strconv.Atoi(*opt.Value); err == nil { + cp.ChargingProfileId = val + } + + case KeyMeterValuesSampledData: + if opt.Readonly { + meterValuesSampledDataMaxLength = 0 + } + meterValuesSampledData = *opt.Value + + case KeyMeterValuesSampledDataMaxLength: + if val, err := strconv.Atoi(*opt.Value); err == nil { + meterValuesSampledDataMaxLength = val + } + + case KeyNumberOfConnectors: + if val, err := strconv.Atoi(*opt.Value); err == nil { + cp.NumberOfConnectors = val + } + + case KeySupportedFeatureProfiles: + if !hasProperty(*opt.Value, smartcharging.ProfileName) { + cp.log.WARN.Printf("the required SmartCharging feature profile is not indicated as supported") + } + // correct the availability assumption of RemoteTrigger only in case of a valid looking FeatureProfile list + if hasProperty(*opt.Value, core.ProfileName) { + cp.HasRemoteTriggerFeature = hasProperty(*opt.Value, remotetrigger.ProfileName) + } + + // vendor-specific keys + case KeyAlfenPlugAndChargeIdentifier: + cp.IdTag = *opt.Value + cp.log.DEBUG.Printf("overriding default `idTag` with Alfen-specific value: %s", cp.IdTag) + + case KeyEvBoxSupportedMeasurands: + if meterValues == "" { + meterValues = *opt.Value + } + } } // see who's there @@ -158,6 +147,19 @@ func (cp *CP) Setup(meterValues string, meterInterval time.Duration) error { return nil } +// GetConfiguration +func (cp *CP) GetConfiguration() (*core.GetConfigurationConfirmation, error) { + rc := make(chan error, 1) + + var res *core.GetConfigurationConfirmation + err := Instance().GetConfiguration(cp.ID(), func(resp *core.GetConfigurationConfirmation, err error) { + res = resp + rc <- err + }, nil) + + return res, wait(err, rc) +} + // HasMeasurement checks if meterValuesSample contains given measurement func (cp *CP) HasMeasurement(val types.Measurand) bool { return hasProperty(cp.meterValuesSample, string(val)) @@ -185,5 +187,5 @@ func (cp *CP) configure(key, val string) error { rc <- err }, key, val) - return Wait(err, rc) + return wait(err, rc) } diff --git a/charger/ocpp/cs_core.go b/charger/ocpp/cs_core.go index 873f07e74..e0ee241c9 100644 --- a/charger/ocpp/cs_core.go +++ b/charger/ocpp/cs_core.go @@ -23,7 +23,7 @@ func (cs *CS) TriggerResetRequest(id string, resetType core.ResetType) error { rc <- err }, resetType) - return Wait(err, rc) + return wait(err, rc) } func (cs *CS) TriggerMessageRequest(id string, requestedMessage remotetrigger.MessageTrigger, props ...func(request *remotetrigger.TriggerMessageRequest)) error { @@ -37,7 +37,7 @@ func (cs *CS) TriggerMessageRequest(id string, requestedMessage remotetrigger.Me rc <- err }, requestedMessage, props...) - return Wait(err, rc) + return wait(err, rc) } func (cs *CS) ChangeAvailabilityRequest(id string, connector int, availabilityType core.AvailabilityType) error { @@ -51,7 +51,7 @@ func (cs *CS) ChangeAvailabilityRequest(id string, connector int, availabilityTy rc <- err }, connector, availabilityType) - return Wait(err, rc) + return wait(err, rc) } func (cs *CS) SetChargingProfileRequest(id string, connector int, profile *types.ChargingProfile) error { @@ -65,7 +65,7 @@ func (cs *CS) SetChargingProfileRequest(id string, connector int, profile *types rc <- err }, connector, profile) - return Wait(err, rc) + return wait(err, rc) } func (cs *CS) GetCompositeScheduleRequest(id string, connector int, duration int) (*types.ChargingSchedule, error) { @@ -82,9 +82,7 @@ func (cs *CS) GetCompositeScheduleRequest(id string, connector int, duration int rc <- err }, connector, duration) - err = Wait(err, rc) - - return schedule, err + return schedule, wait(err, rc) } // cp actions diff --git a/charger/ocpp/helper.go b/charger/ocpp/helper.go index 7a996680b..db16d9a90 100644 --- a/charger/ocpp/helper.go +++ b/charger/ocpp/helper.go @@ -9,8 +9,8 @@ import ( "github.com/lorenzodonini/ocpp-go/ocpp1.6/types" ) -// Wait waits for a CP roundtrip with timeout -func Wait(err error, rc chan error) error { +// wait waits for a CP roundtrip with timeout +func wait(err error, rc chan error) error { if err == nil { select { case err = <-rc: