Skip to content

Commit

Permalink
Ocpp: support 1p measurements (#15646)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Aug 24, 2024
1 parent c836a69 commit 95ad434
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions charger/ocpp/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,32 @@ func (conn *Connector) GetMaxPower() (float64, error) {
return 0, api.ErrNotAvailable
}

func (conn *Connector) phaseMeasurements(measurement, suffix types.Measurand) ([3]float64, bool, error) {
var (
res [3]float64
found bool
)

for i := range res {
key := getPhaseKey(measurement, i+1) + suffix

m, ok := conn.measurements[key]
if !ok {
continue
}
found = true

f, err := strconv.ParseFloat(m.Value, 64)
if err != nil {
return res, found, fmt.Errorf("invalid phase value %s: %w", key, err)
}

res[i] = scale(f, m.Unit)
}

return res, found, nil
}

var _ api.Meter = (*Connector)(nil)

func (conn *Connector) CurrentPower() (float64, error) {
Expand All @@ -237,22 +263,13 @@ func (conn *Connector) CurrentPower() (float64, error) {
}

// fallback for missing total power
var res float64
for phase := 1; phase <= 3; phase++ {
m, ok := conn.measurements[getPhaseKey(types.MeasurandPowerActiveImport, phase)]
if !ok {
return 0, api.ErrNotAvailable
}

f, err := strconv.ParseFloat(m.Value, 64)
if err != nil {
return 0, fmt.Errorf("invalid power for phase %d: %w", phase, err)
}

res += scale(f, m.Unit)
res, found, err := conn.phaseMeasurements(types.MeasurandPowerActiveImport, "")
if found {
return res[0] + res[1] + res[2], err
}

return res, nil
return 0, api.ErrNotAvailable
}

func (conn *Connector) TotalEnergy() (float64, error) {
Expand Down Expand Up @@ -328,23 +345,12 @@ func (conn *Connector) Currents() (float64, float64, float64, error) {
return 0, 0, 0, nil
}

currents := make([]float64, 0, 3)

for phase := 1; phase <= 3; phase++ {
m, ok := conn.measurements[getPhaseKey(types.MeasurandCurrentImport, phase)]
if !ok {
return 0, 0, 0, api.ErrNotAvailable
}

f, err := strconv.ParseFloat(m.Value, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("invalid current for phase %d: %w", phase, err)
}

currents = append(currents, scale(f, m.Unit))
res, found, err := conn.phaseMeasurements(types.MeasurandCurrentImport, "")
if found {
return res[0], res[1], res[2], err
}

return currents[0], currents[1], currents[2], nil
return 0, 0, 0, api.ErrNotAvailable
}

func (conn *Connector) Voltages() (float64, float64, float64, error) {
Expand All @@ -360,25 +366,15 @@ func (conn *Connector) Voltages() (float64, float64, float64, error) {
return 0, 0, 0, api.ErrTimeout
}

voltages := make([]float64, 0, 3)

for phase := 1; phase <= 3; phase++ {
m, ok := conn.measurements[getPhaseKey(types.MeasurandVoltage, phase)+"-N"]
if !ok {
// fallback for wrong voltage phase labeling
m, ok = conn.measurements[getPhaseKey(types.MeasurandVoltage, phase)]
if !ok {
return 0, 0, 0, api.ErrNotAvailable
}
}

f, err := strconv.ParseFloat(m.Value, 64)
if err != nil {
return 0, 0, 0, fmt.Errorf("invalid voltage for phase %d: %w", phase, err)
}
res, found, err := conn.phaseMeasurements(types.MeasurandVoltage, "-N")
if found {
return res[0], res[1], res[2], err
}

voltages = append(voltages, scale(f, m.Unit))
res, found, err = conn.phaseMeasurements(types.MeasurandVoltage, "")
if found {
return res[0], res[1], res[2], err
}

return voltages[0], voltages[1], voltages[2], nil
return 0, 0, 0, api.ErrNotAvailable
}

0 comments on commit 95ad434

Please sign in to comment.