-
-
Notifications
You must be signed in to change notification settings - Fork 706
/
e3dc.go
240 lines (197 loc) Β· 5.39 KB
/
e3dc.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package meter
import (
"errors"
"net"
"strconv"
"sync"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/templates"
"github.com/sirupsen/logrus"
"github.com/spali/go-rscp/rscp"
"github.com/spf13/cast"
)
type E3dc struct {
dischargeLimit uint32
usage templates.Usage // TODO check if we really want to depend on templates
conn *rscp.Client
}
func init() {
registry.Add("e3dc-rscp", NewE3dcFromConfig)
}
//go:generate go run ../cmd/tools/decorate.go -f decorateE3dc -b *E3dc -r api.Meter -t "api.BatteryCapacity,Capacity,func() float64" -t "api.Battery,Soc,func() (float64, error)" -t "api.BatteryController,SetBatteryMode,func(api.BatteryMode) error"
func NewE3dcFromConfig(other map[string]interface{}) (api.Meter, error) {
cc := struct {
capacity `mapstructure:",squash"`
Usage templates.Usage
Uri string
User string
Password string
Key string
_ uint16 `mapstructure:"battery"` // deprecated
DischargeLimit uint32
Timeout time.Duration
}{
Timeout: request.Timeout,
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
host, port_, err := net.SplitHostPort(util.DefaultPort(cc.Uri, 5033))
if err != nil {
return nil, err
}
port, _ := strconv.Atoi(port_)
cfg := rscp.ClientConfig{
Address: host,
Port: uint16(port),
Username: cc.User,
Password: cc.Password,
Key: cc.Key,
ConnectionTimeout: cc.Timeout,
SendTimeout: cc.Timeout,
ReceiveTimeout: cc.Timeout,
}
return NewE3dc(cfg, cc.Usage, cc.DischargeLimit, cc.capacity.Decorator())
}
var e3dcOnce sync.Once
func NewE3dc(cfg rscp.ClientConfig, usage templates.Usage, dischargeLimit uint32, capacity func() float64) (api.Meter, error) {
e3dcOnce.Do(func() {
log := util.NewLogger("e3dc")
rscp.Log.SetLevel(logrus.DebugLevel)
rscp.Log.SetOutput(log.TRACE.Writer())
})
conn, err := rscp.NewClient(cfg)
if err != nil {
return nil, err
}
m := &E3dc{
usage: usage,
conn: conn,
dischargeLimit: dischargeLimit,
}
// decorate api.BatterySoc
var (
batteryCapacity func() float64
batterySoc func() (float64, error)
batteryMode func(api.BatteryMode) error
)
if usage == templates.UsageBattery {
batteryCapacity = capacity
batterySoc = m.batterySoc
batteryMode = m.setBatteryMode
}
return decorateE3dc(m, batteryCapacity, batterySoc, batteryMode), nil
}
func (m *E3dc) CurrentPower() (float64, error) {
switch m.usage {
case templates.UsageGrid:
res, err := m.conn.Send(*rscp.NewMessage(rscp.EMS_REQ_POWER_GRID, nil))
if err != nil {
return 0, err
}
return rscpValue(*res, cast.ToFloat64E)
case templates.UsagePV:
res, err := m.conn.SendMultiple([]rscp.Message{
*rscp.NewMessage(rscp.EMS_REQ_POWER_PV, nil),
*rscp.NewMessage(rscp.EMS_REQ_POWER_ADD, nil),
})
if err != nil {
return 0, err
}
values, err := rscpValues(res, cast.ToFloat64E)
if err != nil {
return 0, err
}
return values[0] - values[1], nil
case templates.UsageBattery:
res, err := m.conn.Send(*rscp.NewMessage(rscp.EMS_REQ_POWER_BAT, nil))
if err != nil {
return 0, err
}
pwr, err := rscpValue(*res, cast.ToFloat64E)
if err != nil {
return 0, err
}
return -pwr, nil
default:
return 0, api.ErrNotAvailable
}
}
func (m *E3dc) batterySoc() (float64, error) {
res, err := m.conn.Send(*rscp.NewMessage(rscp.EMS_REQ_BAT_SOC, nil))
if err != nil {
return 0, err
}
return rscpValue(*res, cast.ToFloat64E)
}
func (m *E3dc) setBatteryMode(mode api.BatteryMode) error {
var (
res []rscp.Message
err error
)
switch mode {
case api.BatteryNormal:
res, err = m.conn.SendMultiple([]rscp.Message{
e3dcDischargeBatteryLimit(false, 0),
e3dcBatteryCharge(0),
})
case api.BatteryHold:
res, err = m.conn.SendMultiple([]rscp.Message{
e3dcDischargeBatteryLimit(true, m.dischargeLimit),
e3dcBatteryCharge(0),
})
case api.BatteryCharge:
res, err = m.conn.SendMultiple([]rscp.Message{
e3dcDischargeBatteryLimit(false, 0),
e3dcBatteryCharge(10000), // 10kWh
})
default:
return api.ErrNotAvailable
}
if err == nil {
err = rscpError(res...)
}
return err
}
func e3dcDischargeBatteryLimit(active bool, limit uint32) rscp.Message {
contents := []rscp.Message{
*rscp.NewMessage(rscp.EMS_POWER_LIMITS_USED, active),
}
if active {
contents = append(contents, *rscp.NewMessage(rscp.EMS_MAX_DISCHARGE_POWER, limit))
}
return *rscp.NewMessage(rscp.EMS_REQ_SET_POWER_SETTINGS, contents)
}
func e3dcBatteryCharge(amount uint32) rscp.Message {
return *rscp.NewMessage(rscp.EMS_REQ_START_MANUAL_CHARGE, amount)
}
func rscpError(msg ...rscp.Message) error {
var errs []error
for _, m := range msg {
if m.DataType == rscp.Error {
errs = append(errs, errors.New(rscp.RscpError(cast.ToUint32(m.Value)).String()))
}
}
return errors.Join(errs...)
}
func rscpValue[T any](msg rscp.Message, fun func(any) (T, error)) (T, error) {
var zero T
if err := rscpError(msg); err != nil {
return zero, err
}
return fun(msg.Value)
}
func rscpValues[T any](msg []rscp.Message, fun func(any) (T, error)) ([]T, error) {
res := make([]T, 0, len(msg))
for _, m := range msg {
v, err := rscpValue(m, fun)
if err != nil {
return nil, err
}
res = append(res, v)
}
return res, nil
}