Ocpp: process meter values in order (#15623)
This commit is contained in:
parent
b6c1540477
commit
da42dddb29
3 changed files with 39 additions and 1 deletions
|
|
@ -60,7 +60,7 @@ func (conn *Connector) MeterValues(request *core.MeterValuesRequest) (*core.Mete
|
|||
conn.txnId = *request.TransactionId
|
||||
}
|
||||
|
||||
for _, meterValue := range request.MeterValue {
|
||||
for _, meterValue := range sortByAge(request.MeterValue) {
|
||||
// ignore old meter value requests
|
||||
if meterValue.Timestamp.Time.After(conn.meterUpdated) {
|
||||
for _, sample := range meterValue.SampledValue {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
package ocpp
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/evcc-io/evcc/api"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
|
||||
)
|
||||
|
||||
// Wait waits for a CP roundtrip with timeout
|
||||
|
|
@ -18,3 +20,16 @@ func Wait(err error, rc chan error, timeout time.Duration) error {
|
|||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func sortByAge(values []types.MeterValue) []types.MeterValue {
|
||||
return slices.SortedFunc(slices.Values(values), func(a, b types.MeterValue) int {
|
||||
var at, bt time.Time
|
||||
if a.Timestamp != nil {
|
||||
at = a.Timestamp.Time
|
||||
}
|
||||
if b.Timestamp != nil {
|
||||
bt = b.Timestamp.Time
|
||||
}
|
||||
return at.Compare(bt)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
23
charger/ocpp/helper_test.go
Normal file
23
charger/ocpp/helper_test.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package ocpp
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSortByAge(t *testing.T) {
|
||||
assert.Equal(t, []types.MeterValue{
|
||||
{Timestamp: nil},
|
||||
{Timestamp: types.NewDateTime(time.UnixMilli(1))},
|
||||
{Timestamp: types.NewDateTime(time.UnixMilli(2))},
|
||||
{Timestamp: types.NewDateTime(time.UnixMilli(3))},
|
||||
}, sortByAge([]types.MeterValue{
|
||||
{Timestamp: types.NewDateTime(time.UnixMilli(3))},
|
||||
{Timestamp: types.NewDateTime(time.UnixMilli(1))},
|
||||
{Timestamp: nil},
|
||||
{Timestamp: types.NewDateTime(time.UnixMilli(2))},
|
||||
}))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue