Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ocpp: cache and re-use initial status (3rd attempt) #16908

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
Better naming
  • Loading branch information
andig committed Oct 25, 2024
commit 3333a20e440b46362e9a185f4e22a029bf9add6e
44 changes: 22 additions & 22 deletions charger/ocpp/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
)

type cpState struct {
type registration struct {
mu sync.RWMutex
setup sync.RWMutex // serialises chargepoint setup
cp *CP // guarded by setup and CS mutexes
Expand All @@ -22,7 +22,7 @@ type CS struct {
ocpp16.CentralSystem
mu sync.Mutex
log *util.Logger
cps map[string]*cpState
regs map[string]*registration
txnId atomic.Int64
}

Expand All @@ -37,26 +37,26 @@ func (cs *CS) ChargepointByID(id string) (*CP, error) {
cs.mu.Lock()
defer cs.mu.Unlock()

state, ok := cs.cps[id]
reg, ok := cs.regs[id]
if !ok {
return nil, fmt.Errorf("unknown charge point: %s", id)
}
if state.cp == nil {
if reg.cp == nil {
return nil, fmt.Errorf("charge point not configured: %s", id)
}
return state.cp, nil
return reg.cp, nil
}

func (cs *CS) WithChargepointStatusByID(id string, fun func(status *core.StatusNotificationRequest)) {
cs.mu.Lock()
defer cs.mu.Unlock()

if state, ok := cs.cps[id]; ok {
state.mu.RLock()
if state.status != nil {
fun(state.status)
if reg, ok := cs.regs[id]; ok {
reg.mu.RLock()
if reg.status != nil {
fun(reg.status)
}
state.mu.RUnlock()
reg.mu.RUnlock()
}
}

Expand All @@ -65,17 +65,17 @@ func (cs *CS) RegisterChargepoint(id string, newfun func() *CP, init func(*CP) e
cs.mu.Lock()

// prepare shadow state
state, ok := cs.cps[id]
reg, ok := cs.regs[id]
if !ok {
state = new(cpState)
cs.cps[id] = state
reg = new(registration)
cs.regs[id] = reg
}

// serialise on chargepoint id
state.setup.Lock()
defer state.setup.Unlock()
reg.setup.Lock()
defer reg.setup.Unlock()

cp := state.cp
cp := reg.cp

cs.mu.Unlock()

Expand All @@ -93,7 +93,7 @@ func (cs *CS) RegisterChargepoint(id string, newfun func() *CP, init func(*CP) e
cp = newfun()

cs.mu.Lock()
state.cp = cp
reg.cp = cp
cs.mu.Unlock()

cp.connect(true)
Expand All @@ -107,7 +107,7 @@ func (cs *CS) NewChargePoint(chargePoint ocpp16.ChargePointConnection) {
defer cs.mu.Unlock()

// check for configured charge point
state, ok := cs.cps[chargePoint.ID()]
state, ok := cs.regs[chargePoint.ID()]
if ok {
cs.log.DEBUG.Printf("charge point connected: %s", chargePoint.ID())

Expand All @@ -120,15 +120,15 @@ func (cs *CS) NewChargePoint(chargePoint ocpp16.ChargePointConnection) {
}

// check for configured anonymous charge point
state, ok = cs.cps[""]
state, ok = cs.regs[""]
if cp := state.cp; ok && cp != nil {
cs.log.INFO.Printf("charge point connected, registering: %s", chargePoint.ID())

// update id
cp.RegisterID(chargePoint.ID())

cs.cps[chargePoint.ID()].cp = cp
delete(cs.cps, "")
cs.regs[chargePoint.ID()].cp = cp
delete(cs.regs, "")

cp.connect(true)

Expand All @@ -139,7 +139,7 @@ func (cs *CS) NewChargePoint(chargePoint ocpp16.ChargePointConnection) {

// register unknown charge point
// when charge point setup is complete, it will eventually be associated with the connected id
cs.cps[chargePoint.ID()] = new(cpState)
cs.regs[chargePoint.ID()] = new(registration)
}

// ChargePointDisconnected implements ocpp16.ChargePointConnectionHandler
Expand Down
8 changes: 4 additions & 4 deletions charger/ocpp/cs_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ func (cs *CS) OnStatusNotification(id string, request *core.StatusNotificationRe
defer cs.mu.Unlock()

// cache status for future cp connection
if state, ok := cs.cps[id]; ok {
state.mu.Lock()
state.status = request
state.mu.Unlock()
if reg, ok := cs.regs[id]; ok {
reg.mu.Lock()
reg.status = request
reg.mu.Unlock()
}

return new(core.StatusNotificationConfirmation), nil
Expand Down
2 changes: 1 addition & 1 deletion charger/ocpp/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Instance() *CS {

instance = &CS{
log: log,
cps: make(map[string]*cpState),
regs: make(map[string]*registration),
CentralSystem: cs,
}
instance.txnId.Store(time.Now().UTC().Unix())
Expand Down