forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpulsares.go
198 lines (159 loc) Β· 4.44 KB
/
pulsares.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package charger
// LICENSE
// Copyright (c) 2023 premultiply
// This module is NOT covered by the MIT license. All rights reserved.
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import (
"encoding/binary"
"fmt"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/modbus"
)
// Pulsares charger implementation
type Pulsares struct {
log *util.Logger
conn *modbus.Connection
curr uint16
}
const (
pulsaresRegStatus = 0x1f
pulsaresRegCurrent = 0x5d
pulsaresRegBackup = 0x61
)
func init() {
registry.Add("pulsares", NewPulsaresFromConfig)
}
// NewPulsaresFromConfig creates a Pulsares charger from generic config
func NewPulsaresFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
modbus.Settings `mapstructure:",squash"`
}{
Settings: modbus.Settings{
ID: 1,
},
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewPulsares(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID)
}
// NewPulsares creates Pulsares charger
func NewPulsares(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("pulsares")
conn.Logger(log.TRACE)
wb := &Pulsares{
conn: conn,
curr: 6000,
}
// get initial state from charger
curr, err := wb.getCurrent()
if err != nil {
return nil, fmt.Errorf("current limit: %w", err)
}
if curr >= 6000 {
wb.curr = curr
}
// get failsafe timeout from charger
b, err := wb.conn.ReadHoldingRegisters(pulsaresRegBackup, 1)
if err != nil {
return nil, fmt.Errorf("failsafe timeout: %w", err)
}
var t time.Duration
switch u := binary.BigEndian.Uint16(b); u {
case 2:
t = 2 * time.Second
case 3:
t = 5 * time.Second
case 4:
t = 10 * time.Second
case 5:
t = 30 * time.Second
case 6:
t = 60 * time.Second
}
if t > 0 {
go wb.heartbeat(t / 2)
}
return wb, err
}
func (wb *Pulsares) heartbeat(timeout time.Duration) {
for range time.Tick(timeout) {
if _, err := wb.conn.ReadHoldingRegisters(pulsaresRegBackup, 1); err != nil {
wb.log.ERROR.Println("heartbeat:", err)
}
}
}
func (wb *Pulsares) setCurrent(current uint16) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, current)
_, err := wb.conn.WriteMultipleRegisters(pulsaresRegCurrent, 1, b)
return err
}
func (wb *Pulsares) getCurrent() (uint16, error) {
b, err := wb.conn.ReadHoldingRegisters(pulsaresRegCurrent, 1)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(b), nil
}
// Status implements the api.Charger interface
func (wb *Pulsares) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(pulsaresRegStatus, 1)
if err != nil {
return api.StatusNone, err
}
switch u := binary.BigEndian.Uint16(b); u {
case 0:
return api.StatusA, nil
case 1, 2:
return api.StatusB, nil
case 3, 4:
return api.StatusC, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %d", u)
}
}
// Enabled implements the api.Charger interface
func (wb *Pulsares) Enabled() (bool, error) {
curr, err := wb.getCurrent()
return curr >= 6000, err
}
// Enable implements the api.Charger interface
func (wb *Pulsares) Enable(enable bool) error {
var curr uint16
if enable {
curr = wb.curr
}
return wb.setCurrent(curr)
}
// MaxCurrent implements the api.Charger interface
func (wb *Pulsares) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*Pulsares)(nil)
// MaxCurrent implements the api.ChargerEx interface
func (wb *Pulsares) MaxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %.1f", current)
}
curr := uint16(current * 1e3)
err := wb.setCurrent(curr)
if err == nil {
wb.curr = curr
}
return err
}