-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathcharging_station.go
804 lines (743 loc) · 31.1 KB
/
charging_station.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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
package ocpp2
import (
"fmt"
"reflect"
"github.com/lorenzodonini/ocpp-go/internal/callbackqueue"
"github.com/lorenzodonini/ocpp-go/ocpp"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/authorization"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/availability"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/data"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/diagnostics"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/display"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/firmware"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/iso15118"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/localauth"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/meter"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/provisioning"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/remotecontrol"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/reservation"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/security"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/smartcharging"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/tariffcost"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/transactions"
"github.com/lorenzodonini/ocpp-go/ocpp2.0.1/types"
"github.com/lorenzodonini/ocpp-go/ocppj"
)
type chargingStation struct {
client *ocppj.Client
securityHandler security.ChargingStationHandler
provisioningHandler provisioning.ChargingStationHandler
authorizationHandler authorization.ChargingStationHandler
localAuthListHandler localauth.ChargingStationHandler
transactionsHandler transactions.ChargingStationHandler
remoteControlHandler remotecontrol.ChargingStationHandler
availabilityHandler availability.ChargingStationHandler
reservationHandler reservation.ChargingStationHandler
tariffCostHandler tariffcost.ChargingStationHandler
meterHandler meter.ChargingStationHandler
smartChargingHandler smartcharging.ChargingStationHandler
firmwareHandler firmware.ChargingStationHandler
iso15118Handler iso15118.ChargingStationHandler
diagnosticsHandler diagnostics.ChargingStationHandler
displayHandler display.ChargingStationHandler
dataHandler data.ChargingStationHandler
responseHandler chan ocpp.Response
errorHandler chan error
callbacks callbackqueue.CallbackQueue
stopC chan struct{}
errC chan error // external error channel
}
func (cs *chargingStation) error(err error) {
if cs.errC != nil {
cs.errC <- err
}
}
// Errors returns a channel for error messages. If it doesn't exist it es created.
func (cs *chargingStation) Errors() <-chan error {
if cs.errC == nil {
cs.errC = make(chan error, 1)
}
return cs.errC
}
// Callback invoked whenever a queued request is canceled, due to timeout.
// By default, the callback returns a GenericError to the caller, who sent the original request.
func (cs *chargingStation) onRequestTimeout(_ string, _ ocpp.Request, err *ocpp.Error) {
cs.errorHandler <- err
}
func (cs *chargingStation) BootNotification(reason provisioning.BootReason, model string, vendor string, props ...func(request *provisioning.BootNotificationRequest)) (*provisioning.BootNotificationResponse, error) {
request := provisioning.NewBootNotificationRequest(reason, model, vendor)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*provisioning.BootNotificationResponse), err
}
}
func (cs *chargingStation) Authorize(idToken string, tokenType types.IdTokenType, props ...func(request *authorization.AuthorizeRequest)) (*authorization.AuthorizeResponse, error) {
request := authorization.NewAuthorizationRequest(idToken, tokenType)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*authorization.AuthorizeResponse), err
}
}
func (cs *chargingStation) ClearedChargingLimit(chargingLimitSource types.ChargingLimitSourceType, props ...func(request *smartcharging.ClearedChargingLimitRequest)) (*smartcharging.ClearedChargingLimitResponse, error) {
request := smartcharging.NewClearedChargingLimitRequest(chargingLimitSource)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*smartcharging.ClearedChargingLimitResponse), err
}
}
func (cs *chargingStation) DataTransfer(vendorId string, props ...func(request *data.DataTransferRequest)) (*data.DataTransferResponse, error) {
request := data.NewDataTransferRequest(vendorId)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*data.DataTransferResponse), err
}
}
func (cs *chargingStation) FirmwareStatusNotification(status firmware.FirmwareStatus, props ...func(request *firmware.FirmwareStatusNotificationRequest)) (*firmware.FirmwareStatusNotificationResponse, error) {
request := firmware.NewFirmwareStatusNotificationRequest(status)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*firmware.FirmwareStatusNotificationResponse), err
}
}
func (cs *chargingStation) Get15118EVCertificate(schemaVersion string, action iso15118.CertificateAction, exiRequest string, props ...func(request *iso15118.Get15118EVCertificateRequest)) (*iso15118.Get15118EVCertificateResponse, error) {
request := iso15118.NewGet15118EVCertificateRequest(schemaVersion, action, exiRequest)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*iso15118.Get15118EVCertificateResponse), err
}
}
func (cs *chargingStation) GetCertificateStatus(ocspRequestData types.OCSPRequestDataType, props ...func(request *iso15118.GetCertificateStatusRequest)) (*iso15118.GetCertificateStatusResponse, error) {
request := iso15118.NewGetCertificateStatusRequest(ocspRequestData)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*iso15118.GetCertificateStatusResponse), err
}
}
func (cs *chargingStation) Heartbeat(props ...func(request *availability.HeartbeatRequest)) (*availability.HeartbeatResponse, error) {
request := availability.NewHeartbeatRequest()
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*availability.HeartbeatResponse), err
}
}
func (cs *chargingStation) LogStatusNotification(status diagnostics.UploadLogStatus, requestID int, props ...func(request *diagnostics.LogStatusNotificationRequest)) (*diagnostics.LogStatusNotificationResponse, error) {
request := diagnostics.NewLogStatusNotificationRequest(status, requestID)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*diagnostics.LogStatusNotificationResponse), err
}
}
func (cs *chargingStation) MeterValues(evseID int, meterValues []types.MeterValue, props ...func(request *meter.MeterValuesRequest)) (*meter.MeterValuesResponse, error) {
request := meter.NewMeterValuesRequest(evseID, meterValues)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*meter.MeterValuesResponse), err
}
}
func (cs *chargingStation) NotifyChargingLimit(chargingLimit smartcharging.ChargingLimit, props ...func(request *smartcharging.NotifyChargingLimitRequest)) (*smartcharging.NotifyChargingLimitResponse, error) {
request := smartcharging.NewNotifyChargingLimitRequest(chargingLimit)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*smartcharging.NotifyChargingLimitResponse), err
}
}
func (cs *chargingStation) NotifyCustomerInformation(data string, seqNo int, generatedAt types.DateTime, requestID int, props ...func(request *diagnostics.NotifyCustomerInformationRequest)) (*diagnostics.NotifyCustomerInformationResponse, error) {
request := diagnostics.NewNotifyCustomerInformationRequest(data, seqNo, generatedAt, requestID)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*diagnostics.NotifyCustomerInformationResponse), err
}
}
func (cs *chargingStation) NotifyDisplayMessages(requestID int, props ...func(request *display.NotifyDisplayMessagesRequest)) (*display.NotifyDisplayMessagesResponse, error) {
request := display.NewNotifyDisplayMessagesRequest(requestID)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*display.NotifyDisplayMessagesResponse), err
}
}
func (cs *chargingStation) NotifyEVChargingNeeds(evseID int, chargingNeeds smartcharging.ChargingNeeds, props ...func(request *smartcharging.NotifyEVChargingNeedsRequest)) (*smartcharging.NotifyEVChargingNeedsResponse, error) {
request := smartcharging.NewNotifyEVChargingNeedsRequest(evseID, chargingNeeds)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*smartcharging.NotifyEVChargingNeedsResponse), err
}
}
func (cs *chargingStation) NotifyEVChargingSchedule(timeBase *types.DateTime, evseID int, schedule types.ChargingSchedule, props ...func(request *smartcharging.NotifyEVChargingScheduleRequest)) (*smartcharging.NotifyEVChargingScheduleResponse, error) {
request := smartcharging.NewNotifyEVChargingScheduleRequest(timeBase, evseID, schedule)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*smartcharging.NotifyEVChargingScheduleResponse), err
}
}
func (cs *chargingStation) NotifyEvent(generatedAt *types.DateTime, seqNo int, eventData []diagnostics.EventData, props ...func(request *diagnostics.NotifyEventRequest)) (*diagnostics.NotifyEventResponse, error) {
request := diagnostics.NewNotifyEventRequest(generatedAt, seqNo, eventData)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*diagnostics.NotifyEventResponse), err
}
}
func (cs *chargingStation) NotifyMonitoringReport(requestID int, seqNo int, generatedAt *types.DateTime, monitorData []diagnostics.MonitoringData, props ...func(request *diagnostics.NotifyMonitoringReportRequest)) (*diagnostics.NotifyMonitoringReportResponse, error) {
request := diagnostics.NewNotifyMonitoringReportRequest(requestID, seqNo, generatedAt, monitorData)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*diagnostics.NotifyMonitoringReportResponse), err
}
}
func (cs *chargingStation) NotifyReport(requestID int, generatedAt *types.DateTime, seqNo int, props ...func(request *provisioning.NotifyReportRequest)) (*provisioning.NotifyReportResponse, error) {
request := provisioning.NewNotifyReportRequest(requestID, generatedAt, seqNo)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*provisioning.NotifyReportResponse), err
}
}
func (cs *chargingStation) PublishFirmwareStatusNotification(status firmware.PublishFirmwareStatus, props ...func(request *firmware.PublishFirmwareStatusNotificationRequest)) (*firmware.PublishFirmwareStatusNotificationResponse, error) {
request := firmware.NewPublishFirmwareStatusNotificationRequest(status)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*firmware.PublishFirmwareStatusNotificationResponse), err
}
}
func (cs *chargingStation) ReportChargingProfiles(requestID int, chargingLimitSource types.ChargingLimitSourceType, evseID int, chargingProfile []types.ChargingProfile, props ...func(request *smartcharging.ReportChargingProfilesRequest)) (*smartcharging.ReportChargingProfilesResponse, error) {
request := smartcharging.NewReportChargingProfilesRequest(requestID, chargingLimitSource, evseID, chargingProfile)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*smartcharging.ReportChargingProfilesResponse), err
}
}
func (cs *chargingStation) ReservationStatusUpdate(reservationID int, status reservation.ReservationUpdateStatus, props ...func(request *reservation.ReservationStatusUpdateRequest)) (*reservation.ReservationStatusUpdateResponse, error) {
request := reservation.NewReservationStatusUpdateRequest(reservationID, status)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*reservation.ReservationStatusUpdateResponse), err
}
}
func (cs *chargingStation) SecurityEventNotification(typ string, timestamp *types.DateTime, props ...func(request *security.SecurityEventNotificationRequest)) (*security.SecurityEventNotificationResponse, error) {
request := security.NewSecurityEventNotificationRequest(typ, timestamp)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*security.SecurityEventNotificationResponse), err
}
}
func (cs *chargingStation) SignCertificate(csr string, props ...func(request *security.SignCertificateRequest)) (*security.SignCertificateResponse, error) {
request := security.NewSignCertificateRequest(csr)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*security.SignCertificateResponse), err
}
}
func (cs *chargingStation) StatusNotification(timestamp *types.DateTime, status availability.ConnectorStatus, evseID int, connectorID int, props ...func(request *availability.StatusNotificationRequest)) (*availability.StatusNotificationResponse, error) {
request := availability.NewStatusNotificationRequest(timestamp, status, evseID, connectorID)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*availability.StatusNotificationResponse), err
}
}
func (cs *chargingStation) TransactionEvent(t transactions.TransactionEvent, timestamp *types.DateTime, reason transactions.TriggerReason, seqNo int, info transactions.Transaction, props ...func(request *transactions.TransactionEventRequest)) (*transactions.TransactionEventResponse, error) {
request := transactions.NewTransactionEventRequest(t, timestamp, reason, seqNo, info)
for _, fn := range props {
fn(request)
}
response, err := cs.SendRequest(request)
if err != nil {
return nil, err
} else {
return response.(*transactions.TransactionEventResponse), err
}
}
func (cs *chargingStation) SetSecurityHandler(handler security.ChargingStationHandler) {
cs.securityHandler = handler
}
func (cs *chargingStation) SetProvisioningHandler(handler provisioning.ChargingStationHandler) {
cs.provisioningHandler = handler
}
func (cs *chargingStation) SetAuthorizationHandler(handler authorization.ChargingStationHandler) {
cs.authorizationHandler = handler
}
func (cs *chargingStation) SetLocalAuthListHandler(handler localauth.ChargingStationHandler) {
cs.localAuthListHandler = handler
}
func (cs *chargingStation) SetTransactionsHandler(handler transactions.ChargingStationHandler) {
cs.transactionsHandler = handler
}
func (cs *chargingStation) SetRemoteControlHandler(handler remotecontrol.ChargingStationHandler) {
cs.remoteControlHandler = handler
}
func (cs *chargingStation) SetAvailabilityHandler(handler availability.ChargingStationHandler) {
cs.availabilityHandler = handler
}
func (cs *chargingStation) SetReservationHandler(handler reservation.ChargingStationHandler) {
cs.reservationHandler = handler
}
func (cs *chargingStation) SetTariffCostHandler(handler tariffcost.ChargingStationHandler) {
cs.tariffCostHandler = handler
}
func (cs *chargingStation) SetMeterHandler(handler meter.ChargingStationHandler) {
cs.meterHandler = handler
}
func (cs *chargingStation) SetSmartChargingHandler(handler smartcharging.ChargingStationHandler) {
cs.smartChargingHandler = handler
}
func (cs *chargingStation) SetFirmwareHandler(handler firmware.ChargingStationHandler) {
cs.firmwareHandler = handler
}
func (cs *chargingStation) SetISO15118Handler(handler iso15118.ChargingStationHandler) {
cs.iso15118Handler = handler
}
func (cs *chargingStation) SetDiagnosticsHandler(handler diagnostics.ChargingStationHandler) {
cs.diagnosticsHandler = handler
}
func (cs *chargingStation) SetDisplayHandler(handler display.ChargingStationHandler) {
cs.displayHandler = handler
}
func (cs *chargingStation) SetDataHandler(handler data.ChargingStationHandler) {
cs.dataHandler = handler
}
func (cs *chargingStation) SendRequest(request ocpp.Request) (ocpp.Response, error) {
featureName := request.GetFeatureName()
if _, found := cs.client.GetProfileForFeature(featureName); !found {
return nil, fmt.Errorf("feature %v is unsupported on charging station (missing profile), cannot send request", featureName)
}
// Wraps an asynchronous response
type asyncResponse struct {
r ocpp.Response
e error
}
// Create channel and pass it to a callback function, for retrieving asynchronous response
asyncResponseC := make(chan asyncResponse, 1)
send := func() error {
return cs.client.SendRequest(request)
}
err := cs.callbacks.TryQueue("main", send, func(confirmation ocpp.Response, err error) {
asyncResponseC <- asyncResponse{r: confirmation, e: err}
})
if err != nil {
return nil, err
}
asyncResult, ok := <-asyncResponseC
if !ok {
return nil, fmt.Errorf("internal error while receiving result for %v request", request.GetFeatureName())
}
return asyncResult.r, asyncResult.e
}
func (cs *chargingStation) SendRequestAsync(request ocpp.Request, callback func(response ocpp.Response, err error)) error {
featureName := request.GetFeatureName()
if _, found := cs.client.GetProfileForFeature(featureName); !found {
return fmt.Errorf("feature %v is unsupported on charging station (missing profile), cannot send request", featureName)
}
switch featureName {
case authorization.AuthorizeFeatureName,
provisioning.BootNotificationFeatureName,
smartcharging.ClearedChargingLimitFeatureName,
data.DataTransferFeatureName,
firmware.FirmwareStatusNotificationFeatureName,
iso15118.Get15118EVCertificateFeatureName,
iso15118.GetCertificateStatusFeatureName,
availability.HeartbeatFeatureName,
diagnostics.LogStatusNotificationFeatureName,
meter.MeterValuesFeatureName,
smartcharging.NotifyChargingLimitFeatureName,
diagnostics.NotifyCustomerInformationFeatureName,
display.NotifyDisplayMessagesFeatureName,
smartcharging.NotifyEVChargingNeedsFeatureName,
smartcharging.NotifyEVChargingScheduleFeatureName,
diagnostics.NotifyEventFeatureName,
diagnostics.NotifyMonitoringReportFeatureName,
provisioning.NotifyReportFeatureName,
firmware.PublishFirmwareStatusNotificationFeatureName,
smartcharging.ReportChargingProfilesFeatureName,
reservation.ReservationStatusUpdateFeatureName,
security.SecurityEventNotificationFeatureName,
security.SignCertificateFeatureName,
availability.StatusNotificationFeatureName,
transactions.TransactionEventFeatureName:
break
default:
return fmt.Errorf("unsupported action %v on charging station, cannot send request", featureName)
}
// Response will be retrieved asynchronously via asyncHandler
send := func() error {
return cs.client.SendRequest(request)
}
err := cs.callbacks.TryQueue("main", send, callback)
return err
}
func (cs *chargingStation) asyncCallbackHandler() {
for {
select {
case confirmation := <-cs.responseHandler:
// Get and invoke callback
if callback, ok := cs.callbacks.Dequeue("main"); ok {
callback(confirmation, nil)
} else {
cs.error(fmt.Errorf("no callback available for incoming response %v", confirmation.GetFeatureName()))
}
case protoError := <-cs.errorHandler:
// Get and invoke callback
if callback, ok := cs.callbacks.Dequeue("main"); ok {
callback(nil, protoError)
} else {
cs.error(fmt.Errorf("no callback available for incoming error %w", protoError))
}
case <-cs.stopC:
return
}
}
}
func (cs *chargingStation) sendResponse(response ocpp.Response, err error, requestId string) {
if err != nil {
// Send error response
if ocppError, ok := err.(*ocpp.Error); ok {
err = cs.client.SendError(requestId, ocppError.Code, ocppError.Description, nil)
} else {
err = cs.client.SendError(requestId, ocppj.InternalError, err.Error(), nil)
}
if err != nil {
// Error while sending an error. Will attempt to send a default error instead
cs.client.HandleFailedResponseError(requestId, err, "")
// Notify client implementation
err = fmt.Errorf("replying to request %s with 'internal error' failed: %w", requestId, err)
cs.error(err)
}
return
}
if response == nil || reflect.ValueOf(response).IsNil() {
err = fmt.Errorf("empty response to request %s", requestId)
// Sending a dummy error to server instead, then notify client implementation
_ = cs.client.SendError(requestId, ocppj.GenericError, err.Error(), nil)
cs.error(err)
return
}
// send confirmation response
err = cs.client.SendResponse(requestId, response)
if err != nil {
// Error while sending an error. Will attempt to send a default error instead
cs.client.HandleFailedResponseError(requestId, err, response.GetFeatureName())
// Notify client implementation
err = fmt.Errorf("failed responding to request %s: %w", requestId, err)
cs.error(err)
}
}
func (cs *chargingStation) Start(csmsUrl string) error {
// Start client
cs.stopC = make(chan struct{}, 1)
err := cs.client.Start(csmsUrl)
// Async response handler receives incoming responses/errors and triggers callbacks
if err == nil {
go cs.asyncCallbackHandler()
}
return err
}
func (cs *chargingStation) StartWithRetries(csmsUrl string) {
// Start client
cs.stopC = make(chan struct{}, 1)
cs.client.StartWithRetries(csmsUrl)
// Async response handler receives incoming responses/errors and triggers callbacks
go cs.asyncCallbackHandler()
}
func (cs *chargingStation) Stop() {
cs.client.Stop()
}
func (cs *chargingStation) IsConnected() bool {
return cs.client.IsConnected()
}
func (cs *chargingStation) notImplementedError(requestId string, action string) {
err := cs.client.SendError(requestId, ocppj.NotImplemented, fmt.Sprintf("no handler for action %v implemented", action), nil)
if err != nil {
cs.error(fmt.Errorf("replying csms to request %v with error: %w", requestId, err))
}
}
func (cs *chargingStation) notSupportedError(requestId string, action string) {
err := cs.client.SendError(requestId, ocppj.NotSupported, fmt.Sprintf("unsupported action %v on charging station", action), nil)
if err != nil {
cs.error(fmt.Errorf("replying csms to request %s with 'not supported': %w", requestId, err))
}
}
func (cs *chargingStation) handleIncomingRequest(request ocpp.Request, requestId string, action string) {
profile, found := cs.client.GetProfileForFeature(action)
// Check whether action is supported and a listener for it exists
if !found {
cs.notImplementedError(requestId, action)
return
} else {
supported := true
switch profile.Name {
case authorization.ProfileName:
if cs.authorizationHandler == nil {
supported = false
}
case availability.ProfileName:
if cs.availabilityHandler == nil {
supported = false
}
case data.ProfileName:
if cs.dataHandler == nil {
supported = false
}
case diagnostics.ProfileName:
if cs.diagnosticsHandler == nil {
supported = false
}
case display.ProfileName:
if cs.displayHandler == nil {
supported = false
}
case firmware.ProfileName:
if cs.firmwareHandler == nil {
supported = false
}
case iso15118.ProfileName:
if cs.iso15118Handler == nil {
supported = false
}
case localauth.ProfileName:
if cs.localAuthListHandler == nil {
supported = false
}
case meter.ProfileName:
if cs.meterHandler == nil {
supported = false
}
case provisioning.ProfileName:
if cs.provisioningHandler == nil {
supported = false
}
case remotecontrol.ProfileName:
if cs.remoteControlHandler == nil {
supported = false
}
case reservation.ProfileName:
if cs.reservationHandler == nil {
supported = false
}
case security.ProfileName:
if cs.securityHandler == nil {
supported = false
}
case smartcharging.ProfileName:
if cs.smartChargingHandler == nil {
supported = false
}
case tariffcost.ProfileName:
if cs.tariffCostHandler == nil {
supported = false
}
case transactions.ProfileName:
if cs.transactionsHandler == nil {
supported = false
}
}
if !supported {
cs.notSupportedError(requestId, action)
return
}
}
// Process request
var response ocpp.Response
var err error
switch action {
case reservation.CancelReservationFeatureName:
response, err = cs.reservationHandler.OnCancelReservation(request.(*reservation.CancelReservationRequest))
case security.CertificateSignedFeatureName:
response, err = cs.securityHandler.OnCertificateSigned(request.(*security.CertificateSignedRequest))
case availability.ChangeAvailabilityFeatureName:
response, err = cs.availabilityHandler.OnChangeAvailability(request.(*availability.ChangeAvailabilityRequest))
case authorization.ClearCacheFeatureName:
response, err = cs.authorizationHandler.OnClearCache(request.(*authorization.ClearCacheRequest))
case smartcharging.ClearChargingProfileFeatureName:
response, err = cs.smartChargingHandler.OnClearChargingProfile(request.(*smartcharging.ClearChargingProfileRequest))
case display.ClearDisplayMessageFeatureName:
response, err = cs.displayHandler.OnClearDisplay(request.(*display.ClearDisplayRequest))
case diagnostics.ClearVariableMonitoringFeatureName:
response, err = cs.diagnosticsHandler.OnClearVariableMonitoring(request.(*diagnostics.ClearVariableMonitoringRequest))
case tariffcost.CostUpdatedFeatureName:
response, err = cs.tariffCostHandler.OnCostUpdated(request.(*tariffcost.CostUpdatedRequest))
case diagnostics.CustomerInformationFeatureName:
response, err = cs.diagnosticsHandler.OnCustomerInformation(request.(*diagnostics.CustomerInformationRequest))
case data.DataTransferFeatureName:
response, err = cs.dataHandler.OnDataTransfer(request.(*data.DataTransferRequest))
case iso15118.DeleteCertificateFeatureName:
response, err = cs.iso15118Handler.OnDeleteCertificate(request.(*iso15118.DeleteCertificateRequest))
case provisioning.GetBaseReportFeatureName:
response, err = cs.provisioningHandler.OnGetBaseReport(request.(*provisioning.GetBaseReportRequest))
case smartcharging.GetChargingProfilesFeatureName:
response, err = cs.smartChargingHandler.OnGetChargingProfiles(request.(*smartcharging.GetChargingProfilesRequest))
case smartcharging.GetCompositeScheduleFeatureName:
response, err = cs.smartChargingHandler.OnGetCompositeSchedule(request.(*smartcharging.GetCompositeScheduleRequest))
case display.GetDisplayMessagesFeatureName:
response, err = cs.displayHandler.OnGetDisplayMessages(request.(*display.GetDisplayMessagesRequest))
case iso15118.GetInstalledCertificateIdsFeatureName:
response, err = cs.iso15118Handler.OnGetInstalledCertificateIds(request.(*iso15118.GetInstalledCertificateIdsRequest))
case localauth.GetLocalListVersionFeatureName:
response, err = cs.localAuthListHandler.OnGetLocalListVersion(request.(*localauth.GetLocalListVersionRequest))
case diagnostics.GetLogFeatureName:
response, err = cs.diagnosticsHandler.OnGetLog(request.(*diagnostics.GetLogRequest))
case diagnostics.GetMonitoringReportFeatureName:
response, err = cs.diagnosticsHandler.OnGetMonitoringReport(request.(*diagnostics.GetMonitoringReportRequest))
case provisioning.GetReportFeatureName:
response, err = cs.provisioningHandler.OnGetReport(request.(*provisioning.GetReportRequest))
case transactions.GetTransactionStatusFeatureName:
response, err = cs.transactionsHandler.OnGetTransactionStatus(request.(*transactions.GetTransactionStatusRequest))
case provisioning.GetVariablesFeatureName:
response, err = cs.provisioningHandler.OnGetVariables(request.(*provisioning.GetVariablesRequest))
case iso15118.InstallCertificateFeatureName:
response, err = cs.iso15118Handler.OnInstallCertificate(request.(*iso15118.InstallCertificateRequest))
case firmware.PublishFirmwareFeatureName:
response, err = cs.firmwareHandler.OnPublishFirmware(request.(*firmware.PublishFirmwareRequest))
case remotecontrol.RequestStartTransactionFeatureName:
response, err = cs.remoteControlHandler.OnRequestStartTransaction(request.(*remotecontrol.RequestStartTransactionRequest))
case remotecontrol.RequestStopTransactionFeatureName:
response, err = cs.remoteControlHandler.OnRequestStopTransaction(request.(*remotecontrol.RequestStopTransactionRequest))
case reservation.ReserveNowFeatureName:
response, err = cs.reservationHandler.OnReserveNow(request.(*reservation.ReserveNowRequest))
case provisioning.ResetFeatureName:
response, err = cs.provisioningHandler.OnReset(request.(*provisioning.ResetRequest))
case localauth.SendLocalListFeatureName:
response, err = cs.localAuthListHandler.OnSendLocalList(request.(*localauth.SendLocalListRequest))
case smartcharging.SetChargingProfileFeatureName:
response, err = cs.smartChargingHandler.OnSetChargingProfile(request.(*smartcharging.SetChargingProfileRequest))
case display.SetDisplayMessageFeatureName:
response, err = cs.displayHandler.OnSetDisplayMessage(request.(*display.SetDisplayMessageRequest))
case diagnostics.SetMonitoringBaseFeatureName:
response, err = cs.diagnosticsHandler.OnSetMonitoringBase(request.(*diagnostics.SetMonitoringBaseRequest))
case diagnostics.SetMonitoringLevelFeatureName:
response, err = cs.diagnosticsHandler.OnSetMonitoringLevel(request.(*diagnostics.SetMonitoringLevelRequest))
case provisioning.SetNetworkProfileFeatureName:
response, err = cs.provisioningHandler.OnSetNetworkProfile(request.(*provisioning.SetNetworkProfileRequest))
case diagnostics.SetVariableMonitoringFeatureName:
response, err = cs.diagnosticsHandler.OnSetVariableMonitoring(request.(*diagnostics.SetVariableMonitoringRequest))
case provisioning.SetVariablesFeatureName:
response, err = cs.provisioningHandler.OnSetVariables(request.(*provisioning.SetVariablesRequest))
case remotecontrol.TriggerMessageFeatureName:
response, err = cs.remoteControlHandler.OnTriggerMessage(request.(*remotecontrol.TriggerMessageRequest))
case remotecontrol.UnlockConnectorFeatureName:
response, err = cs.remoteControlHandler.OnUnlockConnector(request.(*remotecontrol.UnlockConnectorRequest))
case firmware.UnpublishFirmwareFeatureName:
response, err = cs.firmwareHandler.OnUnpublishFirmware(request.(*firmware.UnpublishFirmwareRequest))
case firmware.UpdateFirmwareFeatureName:
response, err = cs.firmwareHandler.OnUpdateFirmware(request.(*firmware.UpdateFirmwareRequest))
default:
cs.notSupportedError(requestId, action)
return
}
cs.sendResponse(response, err, requestId)
}