forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cp.go
108 lines (80 loc) · 1.68 KB
/
cp.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 (
"fmt"
"sync"
"github.com/connectorjs/evcm/util"
)
// TODO support multiple connectors
// Since ocpp-go interfaces at charge point level, we need to manage multiple connector separately
type CP struct {
mu sync.RWMutex
once sync.Once
log *util.Logger
id string
connected bool
connectC chan struct{}
connectors map[int]*Connector
}
func NewChargePoint(log *util.Logger, id string) *CP {
return &CP{
log: log,
id: id,
connectC: make(chan struct{}),
connectors: make(map[int]*Connector),
}
}
func (cp *CP) registerConnector(id int, conn *Connector) error {
cp.mu.Lock()
defer cp.mu.Unlock()
if _, ok := cp.connectors[id]; ok {
return fmt.Errorf("connector already registered: %d", id)
}
cp.connectors[id] = conn
return nil
}
func (cp *CP) connectorByID(id int) *Connector {
cp.mu.RLock()
defer cp.mu.RUnlock()
return cp.connectors[id]
}
func (cp *CP) connectorByTransactionID(id int) *Connector {
cp.mu.RLock()
defer cp.mu.RUnlock()
for _, conn := range cp.connectors {
if txn, err := conn.TransactionID(); err == nil && txn == id {
return conn
}
}
return nil
}
func (cp *CP) ID() string {
cp.mu.RLock()
defer cp.mu.RUnlock()
return cp.id
}
func (cp *CP) RegisterID(id string) {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.id != "" {
panic("ocpp: cannot re-register id")
}
cp.id = id
}
func (cp *CP) connect(connect bool) {
cp.mu.Lock()
defer cp.mu.Unlock()
cp.connected = connect
if connect {
cp.once.Do(func() {
close(cp.connectC)
})
}
}
func (cp *CP) Connected() bool {
cp.mu.RLock()
defer cp.mu.RUnlock()
return cp.connected
}
func (cp *CP) HasConnected() <-chan struct{} {
return cp.connectC
}