forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinnogy.go
202 lines (160 loc) Β· 5.19 KB
/
innogy.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
199
200
201
202
package charger
// LICENSE
// Copyright (c) 2019-2022 andig
// 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"
"math"
"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"
)
const (
igyRegID = 0 // Input
igyRegSerial = 25 // Input
igyRegProtocol = 50 // Input
igyRegManufacturer = 100 // Input
igyRegFirmware = 200 // Input
igyRegStatus = 275 // Input
igyRegCurrents = 1006 // current readings per phase
)
var igyRegMaxCurrents = []uint16{1012, 1014, 1016} // max current per phase
// https://www.innogy-emobility.com/content/dam/revu-global/emobility-solutions/neue-website-feb-2021/downloadcenter/digital-services/eld_instman_modbustcpde.pdf
// Innogy is an api.Charger implementation for Innogy eBox wallboxes.
type Innogy struct {
conn *modbus.Connection
current float64
}
func init() {
registry.Add("innogy", NewInnogyFromConfig)
}
// NewInnogyFromConfig creates a Innogy charger from generic config
func NewInnogyFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.TcpSettings{
ID: 1,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
return NewInnogy(cc.URI, cc.ID)
}
// NewInnogy creates a Innogy charger
func NewInnogy(uri string, id uint8) (*Innogy, error) {
conn, err := modbus.NewConnection(uri, "", "", 0, modbus.Tcp, id)
if err != nil {
return nil, err
}
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
log := util.NewLogger("innogy")
conn.Logger(log.TRACE)
wb := &Innogy{
conn: conn,
current: 6,
}
return wb, nil
}
// Status implements the api.Charger interface
func (wb *Innogy) Status() (api.ChargeStatus, error) {
b, err := wb.conn.ReadInputRegisters(igyRegStatus, 2)
if err != nil {
return api.StatusNone, err
}
return api.ChargeStatusStringWithMapping(string(b), api.StatusEasA)
}
// Enabled implements the api.Charger interface
func (wb *Innogy) Enabled() (bool, error) {
b, err := wb.conn.ReadHoldingRegisters(igyRegMaxCurrents[0], 2)
if err != nil {
return false, err
}
return math.Float32frombits(binary.BigEndian.Uint32(b)) >= 6, nil
}
// Enable implements the api.Charger interface
func (wb *Innogy) Enable(enable bool) error {
var current float64
if enable {
current = wb.current
}
return wb.setCurrent(current)
}
func (wb *Innogy) setCurrent(current float64) error {
b := make([]byte, 4)
binary.BigEndian.PutUint32(b, math.Float32bits(float32(current)))
for _, reg := range igyRegMaxCurrents {
if _, err := wb.conn.WriteMultipleRegisters(reg, 2, b); err != nil {
return err
}
}
return nil
}
// MaxCurrent implements the api.Charger interface
func (wb *Innogy) MaxCurrent(current int64) error {
return wb.MaxCurrentMillis(float64(current))
}
var _ api.ChargerEx = (*Innogy)(nil)
// MaxCurrentMillis implements the api.ChargerEx interface
func (wb *Innogy) MaxCurrentMillis(current float64) error {
if current < 6 {
return fmt.Errorf("invalid current %.5g", current)
}
err := wb.setCurrent(current)
if err == nil {
wb.current = current
}
return err
}
var _ api.Meter = (*Innogy)(nil)
// CurrentPower implements the api.Meter interface
func (wb *Innogy) CurrentPower() (float64, error) {
// https://github.com/evcc-io/evcc/issues/6848
if status, err := wb.Status(); status != api.StatusC || err != nil {
return 0, err
}
l1, l2, l3, err := wb.Currents()
return 230 * (l1 + l2 + l3), err
}
var _ api.PhaseCurrents = (*Innogy)(nil)
// Currents implements the api.PhaseCurrents interface
func (wb *Innogy) Currents() (float64, float64, float64, error) {
b, err := wb.conn.ReadInputRegisters(igyRegCurrents, 6)
if err != nil {
return 0, 0, 0, err
}
var res [3]float64
for i := range res {
res[i] = float64(math.Float32frombits(binary.BigEndian.Uint32(b[4*i:])))
}
return res[0], res[1], res[2], nil
}
var _ api.Diagnosis = (*Innogy)(nil)
// Diagnose implements the api.Diagnosis interface
func (wb *Innogy) Diagnose() {
if b, err := wb.conn.ReadInputRegisters(igyRegManufacturer, 25); err == nil {
fmt.Printf("Manufacturer:\t%s\n", b)
}
if b, err := wb.conn.ReadInputRegisters(igyRegID, 25); err == nil {
fmt.Printf("Id:\t%s\n", b)
}
if b, err := wb.conn.ReadInputRegisters(igyRegSerial, 25); err == nil {
fmt.Printf("Serial:\t%s\n", b)
}
if b, err := wb.conn.ReadInputRegisters(igyRegProtocol, 25); err == nil {
fmt.Printf("Protocol:\t%s\n", b)
}
if b, err := wb.conn.ReadInputRegisters(igyRegFirmware, 25); err == nil {
fmt.Printf("Firmware:\t%s\n", b)
}
}