forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs.go
108 lines (84 loc) · 2.46 KB
/
cs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ocpp
import (
"errors"
"fmt"
"sync"
"github.com/connectorjs/evcm/util"
ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
)
type CS struct {
mu sync.Mutex
log *util.Logger
ocpp16.CentralSystem
cps map[string]*CP
}
// Register registers a charge point with the central system.
// The charge point identified by id may already be connected in which case initial connection is triggered.
func (cs *CS) Register(id string, cp *CP) error {
cs.mu.Lock()
defer cs.mu.Unlock()
if _, ok := cs.cps[id]; ok && id == "" {
return errors.New("cannot have >1 charge point with empty station id")
}
// trigger unknown charge point connected
if unknown, ok := cs.cps[id]; ok && unknown == nil {
cp.connect(true)
}
cs.cps[id] = cp
return nil
}
// errorHandler logs error channel
func (cs *CS) errorHandler(errC <-chan error) {
for err := range errC {
cs.log.ERROR.Println(err)
}
}
func (cs *CS) ChargepointByID(id string) (*CP, error) {
cs.mu.Lock()
defer cs.mu.Unlock()
cp, ok := cs.cps[id]
if !ok {
return nil, fmt.Errorf("unknown charge point: %s", id)
}
if cp == nil {
return nil, fmt.Errorf("charge point not configured: %s", id)
}
return cp, nil
}
// NewChargePoint implements ocpp16.ChargePointConnectionHandler
func (cs *CS) NewChargePoint(chargePoint ocpp16.ChargePointConnection) {
cs.mu.Lock()
defer cs.mu.Unlock()
// check for configured charge point
cp, ok := cs.cps[chargePoint.ID()]
if ok {
cs.log.DEBUG.Printf("charge point connected: %s", chargePoint.ID())
// trigger initial connection if charge point is already setup
if cp != nil {
cp.connect(true)
}
return
}
// check for configured anonymous charge point
cp, ok = cs.cps[""]
if 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
delete(cs.cps, "")
cp.connect(true)
return
}
cs.log.WARN.Printf("unknown charge point connected: %s", chargePoint.ID())
// register unknown charge point
// when charge point setup is complete, it will eventually be associated with the connected id
cs.cps[chargePoint.ID()] = nil
}
// ChargePointDisconnected implements ocpp16.ChargePointConnectionHandler
func (cs *CS) ChargePointDisconnected(chargePoint ocpp16.ChargePointConnection) {
cs.log.DEBUG.Printf("charge point disconnected: %s", chargePoint.ID())
if cp, err := cs.ChargepointByID(chargePoint.ID()); err == nil {
cp.connect(false)
}
}