Ocpp: simplify async handling (#15644)

This commit is contained in:
andig 2024-08-24 13:36:52 +02:00 • committed by GitHub
parent 5cf124af50
commit e622e6706e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 22 deletions

View file

@ -24,7 +24,7 @@ type Connector struct {
status *core.StatusNotificationRequest
statusC chan struct{}
meterC chan map[types.Measurand]types.SampledValue
meterC chan struct{}
meterUpdated time.Time
measurements map[types.Measurand]types.SampledValue
@ -41,9 +41,9 @@ func NewConnector(log *util.Logger, id int, cp *CP, timeout time.Duration) (*Con
cp: cp,
id: id,
clock: clock.New(),
statusC: make(chan struct{}),
statusC: make(chan struct{}, 1),
meterC: make(chan struct{}, 1),
measurements: make(map[types.Measurand]types.SampledValue),
meterC: make(chan map[types.Measurand]types.SampledValue),
timeout: timeout,
}
@ -56,7 +56,7 @@ func (conn *Connector) TestClock(clock clock.Clock) {
conn.clock = clock
}
func (conn *Connector) MeterSampled() <-chan map[types.Measurand]types.SampledValue {
func (conn *Connector) MeterSampled() <-chan struct{} {
return conn.meterC
}

View file

@ -72,7 +72,7 @@ func (conn *Connector) MeterValues(request *core.MeterValuesRequest) (*core.Mete
}
select {
case conn.meterC <- conn.measurements:
case conn.meterC <- struct{}{}:
default:
}

View file

@ -12,17 +12,14 @@ import (
// Since ocpp-go interfaces at charge point level, we need to manage multiple connector separately
type CP struct {
mu sync.RWMutex
log *util.Logger
onceConnect sync.Once
onceBoot sync.Once
bootNotificationRequestC chan *core.BootNotificationRequest
mu sync.RWMutex
log *util.Logger
id string
connected bool
connectC chan struct{}
connected bool
connectC chan struct{}
bootNotificationRequestC chan *core.BootNotificationRequest
connectors map[int]*Connector
}
@ -32,10 +29,10 @@ func NewChargePoint(log *util.Logger, id string) *CP {
log: log,
id: id,
connectC: make(chan struct{}),
connectors: make(map[int]*Connector),
connectC: make(chan struct{}, 1),
bootNotificationRequestC: make(chan *core.BootNotificationRequest, 1),
connectors: make(map[int]*Connector),
}
}
@ -100,9 +97,10 @@ func (cp *CP) connect(connect bool) {
cp.connected = connect
if connect {
cp.onceConnect.Do(func() {
close(cp.connectC)
})
select {
case cp.connectC <- struct{}{}:
default:
}
}
}

View file

@ -38,9 +38,10 @@ func (cp *CP) BootNotification(request *core.BootNotificationRequest) (*core.Boo
Status: core.RegistrationStatusAccepted,
}
cp.onceBoot.Do(func() {
cp.bootNotificationRequestC <- request
})
select {
case cp.bootNotificationRequestC <- request:
default:
}
return res, nil
}