forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsemp.go
541 lines (446 loc) · 12.3 KB
/
semp.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
package semp
import (
"encoding/binary"
"encoding/hex"
"encoding/xml"
"fmt"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/core/loadpoint"
"github.com/evcc-io/evcc/core/site"
"github.com/evcc-io/evcc/server"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/machine"
"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/koron/go-ssdp"
)
const (
sempController = "Sunny Home Manager"
sempBaseURLEnv = "SEMP_BASE_URL"
sempGateway = "urn:schemas-simple-energy-management-protocol:device:Gateway:1"
sempDeviceId = "F-%s-%.12x-00" // 6 bytes
sempSerialNumber = "%s-%d"
sempCharger = "EVCharger"
basePath = "/semp"
maxAge = 1800
)
var serverName = "EVCC SEMP Server " + server.Version
// SEMP is the SMA SEMP server
type SEMP struct {
log *util.Logger
closeC chan struct{}
doneC chan struct{}
controllable bool
vid string
did []byte
uid string
hostURI string
port int
site site.API
}
// New generates SEMP Gateway listening at /semp endpoint
func New(conf map[string]interface{}, site site.API, httpd *server.HTTPd) (*SEMP, error) {
cc := struct {
VendorID string
DeviceID string
AllowControl bool
}{
VendorID: "28081973",
}
if err := util.DecodeOther(conf, &cc); err != nil {
return nil, err
}
uid, err := uuid.NewUUID()
if err != nil {
return nil, err
}
if len(cc.VendorID) != 8 {
return nil, fmt.Errorf("invalid vendor id: %v", cc.VendorID)
}
var did []byte
if cc.DeviceID == "" {
if did, err = UniqueDeviceID(); err != nil {
return nil, fmt.Errorf("creating device id: %w", err)
}
} else {
if did, err = hex.DecodeString(cc.DeviceID); err != nil {
return nil, fmt.Errorf("device id: %w", err)
}
}
if len(did) != 6 {
return nil, fmt.Errorf("invalid device id: %v", cc.DeviceID)
}
s := &SEMP{
doneC: make(chan struct{}),
log: util.NewLogger("semp"),
site: site,
uid: uid.String(),
vid: cc.VendorID,
did: did,
controllable: cc.AllowControl,
}
// find external port
_, port, err := net.SplitHostPort(httpd.Addr)
if err == nil {
s.port, err = strconv.Atoi(port)
}
s.hostURI = s.callbackURI()
s.handlers(httpd.Router())
return s, err
}
func (s *SEMP) advertise(st, usn string) (*ssdp.Advertiser, error) {
descriptor := s.hostURI + basePath + "/description.xml"
return ssdp.Advertise(st, usn, descriptor, serverName, maxAge)
}
// Run executes the SEMP runtime
func (s *SEMP) Run() {
if s.closeC != nil {
panic("already running")
}
s.closeC = make(chan struct{})
uid := "uuid:" + s.uid
var ads []*ssdp.Advertiser
for _, ad := range []struct{ st, usn string }{
{ssdp.RootDevice, uid + "::" + ssdp.RootDevice},
{sempGateway, uid + "::" + sempGateway},
{uid, uid},
} {
ad, err := s.advertise(ad.st, ad.usn)
if err != nil {
s.log.ERROR.Printf("advertise: %v", err)
continue
}
ads = append(ads, ad)
}
ticker := time.NewTicker(maxAge * time.Second / 2)
ANNOUNCE:
for {
select {
case <-ticker.C:
for _, ad := range ads {
if err := ad.Alive(); err != nil {
s.log.ERROR.Println(err)
}
}
case <-s.closeC:
break ANNOUNCE
}
}
for _, ad := range ads {
if err := ad.Bye(); err != nil {
s.log.ERROR.Println(err)
}
}
close(s.doneC)
}
// Stop stops the SEMP runtime
func (s *SEMP) Stop() {
if s.closeC == nil {
panic("not running")
}
close(s.closeC)
}
// Done returns the done channel. The channel is closed after byebye has been sent.
func (s *SEMP) Done() chan struct{} {
return s.doneC
}
func (s *SEMP) callbackURI() string {
if uri := os.Getenv(sempBaseURLEnv); uri != "" {
return strings.TrimSuffix(uri, "/")
}
ip := "localhost"
ips := util.LocalIPs()
if len(ips) > 0 {
ip = ips[0].IP.String()
} else {
s.log.ERROR.Printf("couldn't determine ip address- specify %s to override", sempBaseURLEnv)
}
uri := fmt.Sprintf("http://%s:%d", ip, s.port)
s.log.WARN.Printf("%s unspecified, using %s instead", sempBaseURLEnv, uri)
return uri
}
func (s *SEMP) handlers(router *mux.Router) {
sempRouter := router.PathPrefix(basePath).Subrouter()
getRouter := sempRouter.Methods(http.MethodGet).Subrouter()
// get description / root / info / status
getRouter.HandleFunc("/description.xml", s.gatewayDescription)
getRouter.HandleFunc("/", s.deviceRootHandler)
getRouter.HandleFunc("/DeviceInfo", s.deviceInfoQuery)
getRouter.HandleFunc("/DeviceStatus", s.deviceStatusQuery)
getRouter.HandleFunc("/PlanningRequest", s.devicePlanningQuery)
// post control messages
postRouter := sempRouter.Methods(http.MethodPost).Subrouter()
postRouter.HandleFunc("/", s.deviceControlHandler)
}
func (s *SEMP) writeXML(w http.ResponseWriter, msg interface{}) {
s.log.TRACE.Printf("send: %+v", msg)
b, err := xml.MarshalIndent(msg, "", " ")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/xml")
_, _ = w.Write([]byte(xml.Header))
_, _ = w.Write(b)
}
func (s *SEMP) gatewayDescription(w http.ResponseWriter, r *http.Request) {
uid := "uuid:" + s.uid
msg := DeviceDescription{
Xmlns: urnUPNPDevice,
SpecVersion: SpecVersion{Major: 1},
Device: Device{
DeviceType: sempGateway,
FriendlyName: "evcc",
Manufacturer: "github.com/evcc-io/evcc",
ModelName: serverName,
PresentationURL: s.hostURI,
UDN: uid,
ServiceDefinition: ServiceDefinition{
Xmlns: urnSEMPService,
Server: s.hostURI,
BasePath: basePath,
Transport: "HTTP/Pull",
ExchangeFormat: "XML",
WsVersion: "1.1.0",
},
},
}
s.writeXML(w, msg)
}
func (s *SEMP) deviceRootHandler(w http.ResponseWriter, r *http.Request) {
msg := Device2EMMsg()
msg.DeviceInfo = append(msg.DeviceInfo, s.allDeviceInfo()...)
msg.DeviceStatus = append(msg.DeviceStatus, s.allDeviceStatus()...)
msg.PlanningRequest = append(msg.PlanningRequest, s.allPlanningRequest()...)
s.writeXML(w, msg)
}
// deviceInfoQuery answers /semp/DeviceInfo
func (s *SEMP) deviceInfoQuery(w http.ResponseWriter, r *http.Request) {
msg := Device2EMMsg()
did := r.URL.Query().Get("DeviceId")
if did == "" {
msg.DeviceInfo = append(msg.DeviceInfo, s.allDeviceInfo()...)
} else {
for id, lp := range s.site.Loadpoints() {
if did != s.deviceID(id) {
continue
}
msg.DeviceInfo = append(msg.DeviceInfo, s.deviceInfo(id, lp))
}
if len(msg.DeviceInfo) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
}
s.writeXML(w, msg)
}
// deviceStatusQuery answers /semp/DeviceStatus
func (s *SEMP) deviceStatusQuery(w http.ResponseWriter, r *http.Request) {
msg := Device2EMMsg()
did := r.URL.Query().Get("DeviceId")
if did == "" {
msg.DeviceStatus = append(msg.DeviceStatus, s.allDeviceStatus()...)
} else {
for id, lp := range s.site.Loadpoints() {
if did != s.deviceID(id) {
continue
}
msg.DeviceStatus = append(msg.DeviceStatus, s.deviceStatus(id, lp))
}
if len(msg.DeviceStatus) == 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
}
s.writeXML(w, msg)
}
// devicePlanningQuery answers /semp/PlanningRequest
func (s *SEMP) devicePlanningQuery(w http.ResponseWriter, r *http.Request) {
msg := Device2EMMsg()
did := r.URL.Query().Get("DeviceId")
if did == "" {
msg.PlanningRequest = append(msg.PlanningRequest, s.allPlanningRequest()...)
} else {
for id, lp := range s.site.Loadpoints() {
if did != s.deviceID(id) {
continue
}
if pr := s.planningRequest(id, lp); len(pr.Timeframe) > 0 {
msg.PlanningRequest = append(msg.PlanningRequest, pr)
}
}
}
s.writeXML(w, msg)
}
func (s *SEMP) serialNumber(id int) string {
uidParts := strings.SplitN(s.uid, "-", 5)
ser := uidParts[len(uidParts)-1]
return fmt.Sprintf(sempSerialNumber, ser, id)
}
// UniqueDeviceID creates a 6-bytes base device id from machine id
func UniqueDeviceID() ([]byte, error) {
bytes := 6
mid, err := machine.ProtectedID("evcc-semp")
if err != nil {
return nil, err
}
b, err := hex.DecodeString(mid)
if err != nil {
return nil, err
}
for i, v := range b {
b[i%bytes] += v
}
return b[:bytes], nil
}
// deviceID combines base device id with device number
func (s *SEMP) deviceID(id int) string {
// numerically add device number
did := append([]byte{0, 0}, s.did...)
return fmt.Sprintf(sempDeviceId, s.vid, ^uint64(0xffff<<48)&(binary.BigEndian.Uint64(did)+uint64(id)))
}
func (s *SEMP) deviceInfo(id int, lp loadpoint.API) DeviceInfo {
method := MethodEstimation
if lp.HasChargeMeter() {
method = MethodMeasurement
}
res := DeviceInfo{
Identification: Identification{
DeviceID: s.deviceID(id),
DeviceName: lp.Title(),
DeviceType: sempCharger,
DeviceSerial: s.serialNumber(id),
DeviceVendor: "github.com/evcc-io/evcc",
},
Capabilities: Capabilities{
CurrentPowerMethod: method,
InterruptionsAllowed: true,
OptionalEnergy: true,
},
Characteristics: Characteristics{
MinPowerConsumption: int(lp.GetMinPower()),
MaxPowerConsumption: int(lp.GetMaxPower()),
},
}
return res
}
func (s *SEMP) allDeviceInfo() (res []DeviceInfo) {
for id, lp := range s.site.Loadpoints() {
res = append(res, s.deviceInfo(id, lp))
}
return res
}
func (s *SEMP) deviceStatus(id int, lp loadpoint.API) DeviceStatus {
chargePower := lp.GetChargePower()
status := lp.GetStatus()
mode := lp.GetMode()
isPV := mode == api.ModeMinPV || mode == api.ModePV
deviceStatus := StatusOff
if status == api.StatusC {
deviceStatus = StatusOn
}
connected := status == api.StatusB || status == api.StatusC
res := DeviceStatus{
DeviceID: s.deviceID(id),
EMSignalsAccepted: s.controllable && isPV && connected,
PowerInfo: PowerInfo{
AveragePower: int(chargePower),
AveragingInterval: 60,
},
Status: deviceStatus,
}
return res
}
func (s *SEMP) allDeviceStatus() (res []DeviceStatus) {
for id, lp := range s.site.Loadpoints() {
res = append(res, s.deviceStatus(id, lp))
}
return res
}
func (s *SEMP) planningRequest(id int, lp loadpoint.API) (res PlanningRequest) {
mode := lp.GetMode()
charging := lp.GetStatus() == api.StatusC
connected := charging || lp.GetStatus() == api.StatusB
// remaining max demand duration in seconds
chargeRemainingDuration := lp.GetRemainingDuration()
latestEnd := int(chargeRemainingDuration / time.Second)
if mode == api.ModeMinPV || mode == api.ModePV || latestEnd <= 0 {
latestEnd = 24 * 3600
}
// remaining max energy demand in Wh
chargeRemainingEnergy := lp.GetRemainingEnergy()
maxEnergy := int(chargeRemainingEnergy)
// add 1kWh in case we're charging but battery claims full
if charging && maxEnergy == 0 {
maxEnergy = 1e3 // 1kWh
}
minEnergy := maxEnergy
if mode == api.ModePV {
minEnergy = 0
}
maxPowerConsumption := int(lp.GetMaxPower())
minPowerConsumption := int(lp.GetMinPower())
if mode == api.ModeNow {
minPowerConsumption = maxPowerConsumption
}
if mode != api.ModeOff && connected && maxEnergy > 0 {
res = PlanningRequest{
Timeframe: []Timeframe{{
DeviceID: s.deviceID(id),
EarliestStart: 0,
LatestEnd: latestEnd,
MinEnergy: &minEnergy,
MaxEnergy: &maxEnergy,
MaxPowerConsumption: &maxPowerConsumption,
MinPowerConsumption: &minPowerConsumption,
}},
}
}
return res
}
func (s *SEMP) allPlanningRequest() (res []PlanningRequest) {
for id, lp := range s.site.Loadpoints() {
if pr := s.planningRequest(id, lp); len(pr.Timeframe) > 0 {
res = append(res, pr)
}
}
return res
}
func (s *SEMP) deviceControlHandler(w http.ResponseWriter, r *http.Request) {
var msg EM2Device
err := xml.NewDecoder(r.Body).Decode(&msg)
s.log.TRACE.Printf("recv: %+v", msg)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
for _, dev := range msg.DeviceControl {
did := dev.DeviceID
for id, lp := range s.site.Loadpoints() {
if did != s.deviceID(id) {
continue
}
if mode := lp.GetMode(); mode != api.ModeMinPV && mode != api.ModePV {
w.WriteHeader(http.StatusBadRequest)
return
}
// ignore requests if not controllable
if !s.controllable {
w.WriteHeader(http.StatusBadRequest)
return
}
demand := loadpoint.RemoteSoftDisable
if dev.On {
demand = loadpoint.RemoteEnable
}
lp.RemoteControl(sempController, demand)
}
}
w.WriteHeader(http.StatusOK)
}