Skip to content

Commit

Permalink
Ocpp: assume 0 measurement when transaction stops (evcc-io#10642)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Nov 4, 2023
1 parent 6403895 commit 4b5d018
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
14 changes: 7 additions & 7 deletions charger/ocpp/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Connector struct {
statusC chan struct{}

meterUpdated time.Time
measurements map[string]types.SampledValue
measurements map[types.Measurand]types.SampledValue
timeout time.Duration

txnCount int // change initial value to the last known global transaction. Needs persistence
Expand All @@ -40,7 +40,7 @@ func NewConnector(log *util.Logger, id int, cp *CP, timeout time.Duration) (*Con
id: id,
clock: clock.New(),
statusC: make(chan struct{}),
measurements: make(map[string]types.SampledValue),
measurements: make(map[types.Measurand]types.SampledValue),
timeout: timeout,
}

Expand Down Expand Up @@ -175,7 +175,7 @@ func (conn *Connector) CurrentPower() (float64, error) {
return 0, nil
}

if m, ok := conn.measurements[string(types.MeasurandPowerActiveImport)]; ok {
if m, ok := conn.measurements[types.MeasurandPowerActiveImport]; ok {
f, err := strconv.ParseFloat(m.Value, 64)
return scale(f, m.Unit), err
}
Expand All @@ -198,7 +198,7 @@ func (conn *Connector) TotalEnergy() (float64, error) {
return 0, api.ErrTimeout
}

if m, ok := conn.measurements[string(types.MeasurandEnergyActiveImportRegister)]; ok {
if m, ok := conn.measurements[types.MeasurandEnergyActiveImportRegister]; ok {
f, err := strconv.ParseFloat(m.Value, 64)
return scale(f, m.Unit) / 1e3, err
}
Expand All @@ -217,8 +217,8 @@ func scale(f float64, scale types.UnitOfMeasure) float64 {
}
}

func getKeyCurrentPhase(phase int) string {
return string(types.MeasurandCurrentImport) + "@L" + strconv.Itoa(phase)
func getPhaseKey(key types.Measurand, phase int) types.Measurand {
return key + types.Measurand("@L"+strconv.Itoa(phase))
}

var _ api.PhaseCurrents = (*Connector)(nil)
Expand All @@ -243,7 +243,7 @@ func (conn *Connector) Currents() (float64, float64, float64, error) {
currents := make([]float64, 0, 3)

for phase := 1; phase <= 3; phase++ {
m, ok := conn.measurements[getKeyCurrentPhase(phase)]
m, ok := conn.measurements[getPhaseKey(types.MeasurandCurrentImport, phase)]
if !ok {
return 0, 0, 0, api.ErrNotAvailable
}
Expand Down
38 changes: 30 additions & 8 deletions charger/ocpp/connector_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ func (conn *Connector) StatusNotification(request *core.StatusNotificationReques
return new(core.StatusNotificationConfirmation), nil
}

func getSampleKey(s types.SampledValue) types.Measurand {
if s.Phase != "" {
return s.Measurand + types.Measurand("@"+string(s.Phase))
}

return s.Measurand
}

func (conn *Connector) MeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
Expand All @@ -61,14 +69,6 @@ func (conn *Connector) MeterValues(request *core.MeterValuesRequest) (*core.Mete
return new(core.MeterValuesConfirmation), nil
}

func getSampleKey(s types.SampledValue) string {
if s.Phase != "" {
return string(s.Measurand) + "@" + string(s.Phase)
}

return string(s.Measurand)
}

func (conn *Connector) StartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
Expand Down Expand Up @@ -97,6 +97,26 @@ func (conn *Connector) StartTransaction(request *core.StartTransactionRequest) (
return res, nil
}

func (conn *Connector) assumeMeterStopped() {
conn.meterUpdated = conn.clock.Now()

if _, ok := conn.measurements[types.MeasurandPowerActiveImport]; ok {
conn.measurements[types.MeasurandPowerActiveImport] = types.SampledValue{
Value: "0",
Unit: types.UnitOfMeasureW,
}
}

for phase := 1; phase <= 3; phase++ {
if _, ok := conn.measurements[getPhaseKey(types.MeasurandCurrentImport, phase)]; ok {
conn.measurements[getPhaseKey(types.MeasurandCurrentImport, phase)] = types.SampledValue{
Value: "0",
Unit: types.UnitOfMeasureA,
}
}
}
}

func (conn *Connector) StopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
conn.mu.Lock()
defer conn.mu.Unlock()
Expand All @@ -120,5 +140,7 @@ func (conn *Connector) StopTransaction(request *core.StopTransactionRequest) (*c
},
}

conn.assumeMeterStopped()

return res, nil
}

0 comments on commit 4b5d018

Please sign in to comment.