Skip to content

Commit

Permalink
move transactionId generator to CS
Browse files Browse the repository at this point in the history
  • Loading branch information
premultiply committed Sep 7, 2024
1 parent 58cf1fb commit 993b6a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 2 additions & 3 deletions charger/ocpp/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ type Connector struct {
meterUpdated time.Time
measurements map[types.Measurand]types.SampledValue

txnCount int // change initial value to the last known global transaction. Needs persistence
txnId int
idTag string
txnId int
idTag string

remoteIdTag string
}
Expand Down
3 changes: 1 addition & 2 deletions charger/ocpp/connector_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ func (conn *Connector) StartTransaction(request *core.StartTransactionRequest) (
return res, nil
}

conn.txnCount++
conn.txnId = conn.txnCount
conn.txnId = instance.NewTransactionID()
conn.idTag = request.IdTag

res := &core.StartTransactionConfirmation{
Expand Down
17 changes: 16 additions & 1 deletion charger/ocpp/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"sync"
"time"

"github.com/evcc-io/evcc/util"
ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
Expand All @@ -13,7 +14,8 @@ type CS struct {
mu sync.Mutex
log *util.Logger
ocpp16.CentralSystem
cps map[string]*CP
cps map[string]*CP
txnId int
}

// Register registers a charge point with the central system.
Expand Down Expand Up @@ -106,3 +108,16 @@ func (cs *CS) ChargePointDisconnected(chargePoint ocpp16.ChargePointConnection)
cp.connect(false)
}
}

// NewTransactionID returns a new unique transaction id
func (cs *CS) NewTransactionID() int {
cs.mu.Lock()
defer cs.mu.Unlock()

if cs.txnId == 0 {
cs.txnId = int(time.Now().UTC().Unix())
}

cs.txnId++
return cs.txnId
}

0 comments on commit 993b6a7

Please sign in to comment.