Skip to content

Commit

Permalink
Ocpp: simplify transaction id generation
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Sep 22, 2024
1 parent df353c6 commit 9dc383a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 11 deletions.
2 changes: 1 addition & 1 deletion charger/ocpp/connector_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (conn *Connector) OnStartTransaction(request *core.StartTransactionRequest)
conn.mu.Lock()
defer conn.mu.Unlock()

conn.txnId = instance.NewTransactionID()
conn.txnId = int(instance.txnId.Add(1))
conn.idTag = request.IdTag

res := &core.StartTransactionConfirmation{
Expand Down
12 changes: 2 additions & 10 deletions charger/ocpp/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"sync"
"sync/atomic"

"github.com/evcc-io/evcc/util"
ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
Expand All @@ -15,7 +16,7 @@ type CS struct {
ocpp16.CentralSystem
cps map[string]*CP
init map[string]*sync.Mutex
txnId int
txnId atomic.Int64
}

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

// NewTransactionID returns a CS-wide unique transactionId
func (cs *CS) NewTransactionID() int {
cs.mu.Lock()
defer cs.mu.Unlock()

cs.txnId++
return cs.txnId
}

0 comments on commit 9dc383a

Please sign in to comment.