forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alphatec.go
160 lines (125 loc) Β· 3.68 KB
/
alphatec.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
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"
)
// https://shop.alphatec-systeme.de/media/pdf/4d/0e/64/MontageanleitungwlFxbRgs4NKK3.pdf
// Alphatec charger implementation
type Alphatec struct {
conn *modbus.Connection
curr uint16
}
const (
alphatecRegStatus = 0
alphatecRegAmpsConfig = 5
)
func init() {
registry.Add("alphatec", NewAlphatecFromConfig)
}
// NewAlphatecFromConfig creates a Alphatec charger from generic config
func NewAlphatecFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.Settings{
ID: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewAlphatec(cc.URI, cc.Device, cc.Comset, cc.Baudrate, cc.Protocol(), cc.ID)
}
// NewAlphatec creates Alphatec charger
func NewAlphatec(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
}
conn.Delay(20 * time.Millisecond)
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("alphatec")
conn.Logger(log.TRACE)
wb := &Alphatec{
conn: conn,
curr: 6,
}
// get initial state from charger
curr, err := wb.getCurrent()
if err != nil {
return nil, fmt.Errorf("current limit: %w", err)
}
if curr > 0 {
wb.curr = curr
}
return wb, err
}
func (wb *Alphatec) setCurrent(current uint16) error {
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, current)
_, err := wb.conn.WriteMultipleRegisters(alphatecRegAmpsConfig, 1, b)
return err
}
func (wb *Alphatec) getCurrent() (uint16, error) {
b, err := wb.conn.ReadHoldingRegisters(alphatecRegAmpsConfig, 1)
if err != nil {
return 0, err
}
return binary.BigEndian.Uint16(b), nil
}
// Status implements the api.Charger interface
func (wb *Alphatec) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadHoldingRegisters(alphatecRegStatus, 1)
if err != nil {
return api.StatusNone, err
}
switch u := binary.BigEndian.Uint16(b); u {
case 1:
return api.StatusA, nil
case 2, 8:
return api.StatusB, nil
case 3:
return api.StatusC, nil
default:
return api.StatusNone, fmt.Errorf("invalid status: %d", u)
}
}
// Enabled implements the api.Charger interface
func (wb *Alphatec) Enabled() (bool, error) {
curr, err := wb.getCurrent()
return curr >= 6, err
}
// Enable implements the api.Charger interface
func (wb *Alphatec) Enable(enable bool) error {
var curr uint16
if enable {
curr = wb.curr
}
return wb.setCurrent(curr)
}
// MaxCurrent implements the api.Charger interface
func (wb *Alphatec) MaxCurrent(current int64) error {
if current < 6 {
return fmt.Errorf("invalid current %d", current)
}
err := wb.setCurrent(uint16(current))
if err == nil {
wb.curr = uint16(current)
}
return err
}