Ocpp: match connector id (#6826)

This commit is contained in:
andig 2023-03-13 17:42:30 +01:00 • committed by GitHub
parent 69cb47c6d7
commit 14d2ba9bb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 21 deletions

View file

@ -113,7 +113,7 @@ func NewOCPP(id string, connector int, idtag string, meterValues string, meterIn
}
log := util.NewLogger(unit)
cp := ocpp.NewChargePoint(log, id, timeout)
cp := ocpp.NewChargePoint(log, id, connector, timeout)
if err := ocpp.Instance().Register(id, cp); err != nil {
return nil, err
}

View file

@ -32,12 +32,16 @@ const (
KeyAlfenPlugAndChargeIdentifier = "PlugAndChargeIdentifier"
)
// TODO support multiple connectors
// Since ocpp-go interfaces at charge point level, we need to manage multiple connector separately
type CP struct {
mu sync.Mutex
log *util.Logger
once sync.Once
id string
id string
connector int
connectC, statusC chan struct{}
updated time.Time
@ -51,10 +55,11 @@ type CP struct {
txnId int
}
func NewChargePoint(log *util.Logger, id string, timeout time.Duration) *CP {
func NewChargePoint(log *util.Logger, id string, connector int, timeout time.Duration) *CP {
return &CP{
log: log,
id: id,
connector: connector,
connectC: make(chan struct{}),
statusC: make(chan struct{}),
measurements: make(map[string]types.SampledValue),

View file

@ -51,7 +51,7 @@ func (cp *CP) timestampValid(t time.Time) bool {
}
func (cp *CP) StatusNotification(request *core.StatusNotificationRequest) (*core.StatusNotificationConfirmation, error) {
if request != nil {
if request != nil && request.ConnectorId == cp.connector {
cp.mu.Lock()
defer cp.mu.Unlock()
@ -92,15 +92,17 @@ func (cp *CP) Heartbeat(request *core.HeartbeatRequest) (*core.HeartbeatConfirma
}
func (cp *CP) MeterValues(request *core.MeterValuesRequest) (*core.MeterValuesConfirmation, error) {
cp.mu.Lock()
defer cp.mu.Unlock()
if request != nil && request.ConnectorId == cp.connector {
cp.mu.Lock()
defer cp.mu.Unlock()
for _, meterValue := range request.MeterValue {
// ignore old meter value requests
if meterValue.Timestamp.Time.After(cp.meterUpdated) {
for _, sample := range meterValue.SampledValue {
cp.measurements[getSampleKey(sample)] = sample
cp.meterUpdated = time.Now()
for _, meterValue := range request.MeterValue {
// ignore old meter value requests
if meterValue.Timestamp.Time.After(cp.meterUpdated) {
for _, sample := range meterValue.SampledValue {
cp.measurements[getSampleKey(sample)] = sample
cp.meterUpdated = time.Now()
}
}
}
}
@ -117,6 +119,10 @@ func getSampleKey(s types.SampledValue) string {
}
func (cp *CP) StartTransaction(request *core.StartTransactionRequest) (*core.StartTransactionConfirmation, error) {
if request == nil || request.ConnectorId != cp.connector {
return new(core.StartTransactionConfirmation), nil
}
cp.mu.Lock()
defer cp.mu.Unlock()
@ -139,17 +145,19 @@ func (cp *CP) StartTransaction(request *core.StartTransactionRequest) (*core.Sta
}
func (cp *CP) StopTransaction(request *core.StopTransactionRequest) (*core.StopTransactionConfirmation, error) {
cp.mu.Lock()
defer cp.mu.Unlock()
if request != nil {
cp.mu.Lock()
defer cp.mu.Unlock()
// reset transaction
if request != nil && time.Since(request.Timestamp.Time) < transactionExpiry { // only respect transactions in the last hour
// log mismatching id but close transaction anyway
if request.TransactionId != cp.txnId {
cp.log.ERROR.Printf("stop transaction: invalid id %d", request.TransactionId)
// reset transaction
if time.Since(request.Timestamp.Time) < transactionExpiry { // only respect transactions in the last hour
// log mismatching id but close transaction anyway
if request.TransactionId != cp.txnId {
cp.log.ERROR.Printf("stop transaction: invalid id %d", request.TransactionId)
}
cp.txnId = 0
}
cp.txnId = 0
}
res := &core.StopTransactionConfirmation{