forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobo.go
136 lines (113 loc) Β· 2.96 KB
/
obo.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package charger
import (
"encoding/binary"
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
)
// Obo charger implementation
type Obo struct {
log *util.Logger
conn *modbus.Connection
}
const (
oboRegEnable = 5
oboRegAmpsConfig = 6
oboRegStatus = 11
oboRegTimeout = 28
)
func init() {
registry.Add("obo", NewOboFromConfig)
}
// NewOboFromConfig creates a OBO Bettermann charger from generic config
func NewOboFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.Settings{
Baudrate: 19200,
Comset: "8E1",
ID: 101,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewObo(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID)
}
// NewObo creates OBO Bettermann charger
func NewObo(uri, device, comset string, baudrate int, proto modbus.Protocol, slaveID uint8) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, device, comset, baudrate, proto, slaveID)
if err != nil {
return nil, err
}
log := util.NewLogger("obo")
conn.Logger(log.TRACE)
wb := &Obo{
log: log,
conn: conn,
}
// get failsafe timeout from charger
b, err := conn.ReadHoldingRegisters(oboRegTimeout, 1)
if err != nil {
return nil, fmt.Errorf("failsafe timeout: %w", err)
}
if u := binary.BigEndian.Uint16(b); u > 0 {
go wb.heartbeat(time.Duration(u) * time.Millisecond / 2)
}
// lightshow
// go func() {
// conn.WriteSingleRegister(3, 1)
// for {
// for i := range res {
// u := rand.Int31n(256)
// conn.WriteSingleRegister(uint16(i), uint16(u))
// }
// time.Sleep(10 * time.Millisecond)
// }
// }()
// time.Sleep(10 * time.Second)
return wb, nil
}
func (wb *Obo) heartbeat(timeout time.Duration) {
for range time.Tick(timeout) {
if _, err := wb.conn.ReadHoldingRegisters(dlRegSafeCurrent, 1); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
// Status implements the api.Charger interface
func (wb *Obo) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(oboRegStatus, 1)
if err != nil {
return api.StatusNone, err
}
switch u := binary.BigEndian.Uint16(b); u {
case 0, 1, 2:
// A..C
return api.ChargeStatus(string('A' + rune(u))), nil
default:
// D, F
return api.StatusNone, fmt.Errorf("invalid status: %d", u)
}
}
// Enabled implements the api.Charger interface
func (wb *Obo) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(oboRegEnable, 1)
if err != nil {
return false, err
}
return binary.BigEndian.Uint16(b) > 0, nil
}
// Enable implements the api.Charger interface
func (wb *Obo) Enable(enable bool) error {
var u uint16
if enable {
u = 1
}
_, err := wb.conn.WriteSingleRegister(oboRegEnable, u)
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *Obo) MaxCurrent(current int64) error {
_, err := wb.conn.WriteSingleRegister(oboRegAmpsConfig, uint16(current))
return err
}