/
prachtalpha.go
198 lines (164 loc) Β· 5.44 KB
/
prachtalpha.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) 2019-2023 andig, 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"
"github.com/evcc-io/evcc/util/sponsor"
)
// PrachtAlpha charger implementation
type PrachtAlpha struct {
conn *modbus.Connection
vehicle uint16
curr uint16
}
const (
prachtTotalCurrent = 40003 - 40001 // 2 total limit of all connectors
prachtConnCurrent = 40004 - 40001 // 3 (+1 for second connector)
prachtRfidCount = 30075 - 30001 // 74
prachtFirmwareVer1 = 30101 - 30001 // 100
prachtFirmwareVer2 = 30102 - 30001 // 101
prachtEnclTemp = 30104 - 30001 // 103
prachtConnStatus = 30107 - 30001 // 106 (+1 for second connector)
)
func init() {
registry.Add("pracht-alpha", NewPrachtAlphaFromConfig)
}
// NewPrachtAlphaFromConfig creates a PrachtAlpha charger from generic config
func NewPrachtAlphaFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := struct {
Connector uint16
modbus.Settings `mapstructure:",squash"`
Timeout time.Duration
}{
Connector: 1,
Settings: modbus.Settings{
ID: 1,
},
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewPrachtAlpha(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID, cc.Timeout, cc.Connector)
}
// NewPrachtAlpha creates PrachtAlpha charger
func NewPrachtAlpha(uri, device, comset string, baudrate int, proto modbus.Protocol, slaveID uint8, timeout time.Duration, vehicle uint16) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, device, comset, baudrate, proto, slaveID)
if err != nil {
return nil, err
}
if timeout > 0 {
conn.Timeout(timeout)
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("pracht")
conn.Logger(log.TRACE)
wb := &PrachtAlpha{
conn: conn,
vehicle: vehicle,
curr: 6,
}
return wb, err
}
func (wb *PrachtAlpha) register(reg int) uint16 {
res := uint16(reg)
if wb.vehicle > 1 {
res++
}
return res
}
// Status implements the api.Charger interface
func (wb *PrachtAlpha) Status() (api.ChargeStatus, error) {
reg := wb.register(prachtConnStatus)
b, err := wb.conn.ReadHoldingRegisters(reg, 1)
if err != nil {
return api.StatusNone, err
}
switch u := binary.BigEndian.Uint16(b); u {
case 0:
return api.StatusA, nil
case 1:
return api.StatusB, nil
case 2, 3:
return api.StatusC, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %d", u)
}
}
// Enabled implements the api.Charger interface
func (wb *PrachtAlpha) Enabled() (bool, error) {
reg := wb.register(prachtConnCurrent)
b, err := wb.conn.ReadHoldingRegisters(reg, 1)
if err != nil {
return false, err
}
// get total current (https://github.com/evcc-io/evcc/issues/3738)
t, err := wb.conn.ReadHoldingRegisters(prachtTotalCurrent, 1)
if err != nil {
return false, err
}
return binary.BigEndian.Uint16(b) > 0 && binary.BigEndian.Uint16(t) > 0, nil
}
// Enable implements the api.Charger interface
func (wb *PrachtAlpha) Enable(enable bool) error {
var curr uint16
if enable {
curr = wb.curr
}
return wb.setCurrent(curr)
}
func (wb *PrachtAlpha) setCurrent(current uint16) error {
reg := wb.register(prachtConnCurrent)
_, err := wb.conn.WriteSingleRegister(reg, current)
// set total current (https://github.com/evcc-io/evcc/issues/3738)
if err == nil {
_, err = wb.conn.WriteSingleRegister(prachtTotalCurrent, current)
}
return err
}
// MaxCurrent implements the api.Charger interface
func (wb *PrachtAlpha) MaxCurrent(current int64) error {
err := wb.setCurrent(uint16(current))
if err == nil {
wb.curr = uint16(current)
}
return err
}
var _ api.Diagnosis = (*PrachtAlpha)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *PrachtAlpha) Diagnose() {
if b, err := wb.conn.ReadHoldingRegisters(prachtTotalCurrent, 1); err == nil {
fmt.Printf("\tTotal Current Limit:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(wb.register(prachtConnCurrent), 1); err == nil {
fmt.Printf("\tConn %d Current Limit:\t%d\n", wb.vehicle, binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(prachtRfidCount, 1); err == nil {
fmt.Printf("\tRFID cards learned:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(prachtFirmwareVer1, 1); err == nil {
fmt.Printf("\tFirmware Version 1:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(prachtFirmwareVer2, 1); err == nil {
fmt.Printf("\tFirmware Version 2:\t%d\n", binary.BigEndian.Uint16(b))
}
if b, err := wb.conn.ReadHoldingRegisters(prachtEnclTemp, 1); err == nil {
fmt.Printf("\tEnclosure Temperature:\t%d\n", binary.BigEndian.Uint16(b))
}
}