Skip to content

Commit

Permalink
Ocpp: simplify async handling (#15644)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Aug 24, 2024
1 parent 5cf124a commit e622e67
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
8 changes: 4 additions & 4 deletions charger/ocpp/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
}

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion charger/ocpp/connector_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (conn *Connector) MeterValues(request *core.MeterValuesRequest) (*core.Mete
}

select {
case conn.meterC <- conn.measurements:
case conn.meterC <- struct{}{}:
default:
}

Expand Down
26 changes: 12 additions & 14 deletions charger/ocpp/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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:
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions charger/ocpp/cp_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit e622e67

Please sign in to comment.