ocpp-client 0.1.19

OCPP Client Implementation. Use this library to implement an OCPP charge point
Documentation
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
805
806
use std::collections::{BTreeMap, VecDeque};
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use futures::stream::SplitSink;
use futures::{SinkExt, StreamExt, TryStreamExt};
use rust_ocpp::v2_0_1::messages::authorize::{AuthorizeRequest, AuthorizeResponse};
use rust_ocpp::v2_0_1::messages::boot_notification::{BootNotificationRequest, BootNotificationResponse};
use rust_ocpp::v2_0_1::messages::cancel_reservation::{CancelReservationRequest, CancelReservationResponse};
use rust_ocpp::v2_0_1::messages::certificate_signed::{CertificateSignedRequest, CertificateSignedResponse};
use rust_ocpp::v2_0_1::messages::change_availability::{ChangeAvailabilityRequest, ChangeAvailabilityResponse};
use rust_ocpp::v2_0_1::messages::clear_cache::{ClearCacheRequest, ClearCacheResponse};
use rust_ocpp::v2_0_1::messages::clear_charging_profile::{ClearChargingProfileRequest, ClearChargingProfileResponse};
use rust_ocpp::v2_0_1::messages::clear_display_message::{ClearDisplayMessageRequest, ClearDisplayMessageResponse};
use rust_ocpp::v2_0_1::messages::clear_variable_monitoring::{ClearVariableMonitoringRequest, ClearVariableMonitoringResponse};
use rust_ocpp::v2_0_1::messages::cleared_charging_limit::{ClearedChargingLimitRequest, ClearedChargingLimitResponse};
use rust_ocpp::v2_0_1::messages::cost_updated::{CostUpdatedRequest, CostUpdatedResponse};
use rust_ocpp::v2_0_1::messages::customer_information::{CustomerInformationRequest, CustomerInformationResponse};
use rust_ocpp::v2_0_1::messages::datatransfer::{DataTransferRequest, DataTransferResponse};
use rust_ocpp::v2_0_1::messages::delete_certificate::{DeleteCertificateRequest, DeleteCertificateResponse};
use rust_ocpp::v2_0_1::messages::firmware_status_notification::{FirmwareStatusNotificationRequest, FirmwareStatusNotificationResponse};
use rust_ocpp::v2_0_1::messages::get_15118ev_certificate::{Get15118EVCertificateRequest, Get15118EVCertificateResponse};
use rust_ocpp::v2_0_1::messages::get_base_report::{GetBaseReportRequest, GetBaseReportResponse};
use rust_ocpp::v2_0_1::messages::get_certificate_status::{GetCertificateStatusRequest, GetCertificateStatusResponse};
use rust_ocpp::v2_0_1::messages::get_charging_profiles::{GetChargingProfilesRequest, GetChargingProfilesResponse};
use rust_ocpp::v2_0_1::messages::get_composite_schedule::{GetCompositeScheduleRequest, GetCompositeScheduleResponse};
use rust_ocpp::v2_0_1::messages::get_display_message::{GetDisplayMessagesRequest, GetDisplayMessagesResponse};
use rust_ocpp::v2_0_1::messages::get_installed_certificate_ids::{GetInstalledCertificateIdsRequest, GetInstalledCertificateIdsResponse};
use rust_ocpp::v2_0_1::messages::get_local_list_version::{GetLocalListVersionRequest, GetLocalListVersionResponse};
use rust_ocpp::v2_0_1::messages::get_log::{GetLogRequest, GetLogResponse};
use rust_ocpp::v2_0_1::messages::get_monitoring_report::{GetMonitoringReportRequest, GetMonitoringReportResponse};
use rust_ocpp::v2_0_1::messages::get_report::{GetReportRequest, GetReportResponse};
use rust_ocpp::v2_0_1::messages::get_transaction_status::{GetTransactionStatusRequest, GetTransactionStatusResponse};
use rust_ocpp::v2_0_1::messages::get_variables::{GetVariablesRequest, GetVariablesResponse};
use rust_ocpp::v2_0_1::messages::heartbeat::{HeartbeatRequest, HeartbeatResponse};
use rust_ocpp::v2_0_1::messages::install_certificate::{InstallCertificateRequest, InstallCertificateResponse};
use rust_ocpp::v2_0_1::messages::log_status_notification::{LogStatusNotificationRequest, LogStatusNotificationResponse};
use rust_ocpp::v2_0_1::messages::meter_values::{MeterValuesRequest, MeterValuesResponse};
use rust_ocpp::v2_0_1::messages::notify_charging_limit::{NotifyChargingLimitRequest, NotifyChargingLimitResponse};
use rust_ocpp::v2_0_1::messages::notify_customer_information::{NotifyCustomerInformationRequest, NotifyCustomerInformationResponse};
use rust_ocpp::v2_0_1::messages::notify_display_messages::{NotifyDisplayMessagesRequest, NotifyDisplayMessagesResponse};
use rust_ocpp::v2_0_1::messages::notify_ev_charging_needs::{NotifyEVChargingNeedsRequest, NotifyEVChargingNeedsResponse};
use rust_ocpp::v2_0_1::messages::notify_ev_charging_schedule::{NotifyEVChargingScheduleRequest, NotifyEVChargingScheduleResponse};
use rust_ocpp::v2_0_1::messages::notify_event::{NotifyEventRequest, NotifyEventResponse};
use rust_ocpp::v2_0_1::messages::notify_monitoring_report::{NotifyMonitoringReportRequest, NotifyMonitoringReportResponse};
use rust_ocpp::v2_0_1::messages::notify_report::{NotifyReportRequest, NotifyReportResponse};
use rust_ocpp::v2_0_1::messages::publish_firmware::{PublishFirmwareRequest, PublishFirmwareResponse};
use rust_ocpp::v2_0_1::messages::publish_firmware_status_notification::{PublishFirmwareStatusNotificationRequest, PublishFirmwareStatusNotificationResponse};
use rust_ocpp::v2_0_1::messages::report_charging_profiles::{ReportChargingProfilesRequest, ReportChargingProfilesResponse};
use rust_ocpp::v2_0_1::messages::request_start_transaction::{RequestStartTransactionRequest, RequestStartTransactionResponse};
use rust_ocpp::v2_0_1::messages::request_stop_transaction::{RequestStopTransactionRequest, RequestStopTransactionResponse};
use rust_ocpp::v2_0_1::messages::reservation_status_update::{ReservationStatusUpdateRequest, ReservationStatusUpdateResponse};
use rust_ocpp::v2_0_1::messages::reserve_now::{ReserveNowRequest, ReserveNowResponse};
use rust_ocpp::v2_0_1::messages::reset::{ResetRequest, ResetResponse};
use rust_ocpp::v2_0_1::messages::send_local_list::{SendLocalListRequest, SendLocalListResponse};
use rust_ocpp::v2_0_1::messages::set_charging_profile::{SetChargingProfileRequest, SetChargingProfileResponse};
use rust_ocpp::v2_0_1::messages::set_display_message::{SetDisplayMessageRequest, SetDisplayMessageResponse};
use rust_ocpp::v2_0_1::messages::set_monitoring_base::{SetMonitoringBaseRequest, SetMonitoringBaseResponse};
use rust_ocpp::v2_0_1::messages::set_monitoring_level::{SetMonitoringLevelRequest, SetMonitoringLevelResponse};
use rust_ocpp::v2_0_1::messages::set_network_profile::{SetNetworkProfileRequest, SetNetworkProfileResponse};
use rust_ocpp::v2_0_1::messages::set_variable_monitoring::{SetVariableMonitoringRequest, SetVariableMonitoringResponse};
use rust_ocpp::v2_0_1::messages::set_variables::{SetVariablesRequest, SetVariablesResponse};
use rust_ocpp::v2_0_1::messages::sign_certificate::{SignCertificateRequest, SignCertificateResponse};
use rust_ocpp::v2_0_1::messages::status_notification::{StatusNotificationRequest, StatusNotificationResponse};
use rust_ocpp::v2_0_1::messages::transaction_event::{TransactionEventRequest, TransactionEventResponse};
use rust_ocpp::v2_0_1::messages::trigger_message::{TriggerMessageRequest, TriggerMessageResponse};
use rust_ocpp::v2_0_1::messages::unlock_connector::{UnlockConnectorRequest, UnlockConnectorResponse};
use rust_ocpp::v2_0_1::messages::unpublish_firmware::{UnpublishFirmwareRequest, UnpublishFirmwareResponse};
use rust_ocpp::v2_0_1::messages::update_firmware::{UpdateFirmwareRequest, UpdateFirmwareResponse};
use serde::de::DeserializeOwned;
use serde::Serialize;
use serde_json::Value;
use tokio::net::TcpStream;
use tokio::sync::{Mutex, oneshot, mpsc};
use tokio::sync::broadcast::Sender;
use tokio::time::timeout;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use tokio_tungstenite::tungstenite::{Message, Utf8Bytes};
use uuid::Uuid;
use crate::ocpp_1_6::OCPP1_6Error;
use crate::ocpp_2_0_1::OCPP2_0_1Error;
use crate::ocpp_2_0_1::raw_ocpp_2_0_1_call::RawOcpp2_0_1Call;
use crate::ocpp_2_0_1::raw_ocpp_2_0_1_error::RawOcpp2_0_1Error;
use crate::ocpp_2_0_1::raw_ocpp_2_0_1_result::RawOcpp2_0_1Result;

/// OCPP 2.0.1 client
#[derive(Debug, Clone)]
pub struct OCPP2_0_1Client {
    sink: Arc<Mutex<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>>,
    response_channels: Arc<Mutex<BTreeMap<Uuid, oneshot::Sender<Result<Value, OCPP2_0_1Error>>>>>,
    request_senders: Arc<Mutex<BTreeMap<String, mpsc::Sender<RawOcpp2_0_1Call>>>>,
    pong_channels: Arc<Mutex<VecDeque<oneshot::Sender<()>>>>,
    ping_sender: Sender<()>,
    timeout: Duration
}

impl OCPP2_0_1Client {
    pub(crate) fn new(stream: WebSocketStream<MaybeTlsStream<TcpStream>>) -> Self {
        let (sink, stream) = stream.split();
        let sink = Arc::new(Mutex::new(sink));
        let response_channels = Arc::new(Mutex::new(BTreeMap::<Uuid, oneshot::Sender<Result<Value, OCPP2_0_1Error>>>::new()));
        let response_channels2 = Arc::clone(&response_channels);

        let pong_channels = Arc::new(Mutex::new(VecDeque::<oneshot::Sender<()>>::new()));
        let pong_channels2 = Arc::clone(&pong_channels);
        let request_senders: Arc<Mutex<BTreeMap<String, mpsc::Sender<RawOcpp2_0_1Call>>>> = Arc::new(Mutex::new(BTreeMap::new()));

        let request_senders2 = request_senders.clone();

        let (ping_sender, _) = tokio::sync::broadcast::channel(10);
        let ping_sender2 = ping_sender.clone();
        let sink2 = sink.clone();

        tokio::spawn(async move {
            stream
                .map_err(|e| Box::<dyn std::error::Error + Send + Sync>::from(e))
                .try_for_each(|message| {
                    let request_senders = request_senders2.clone();
                    let response_channels2 = response_channels2.clone();
                    let ping_sender = ping_sender2.clone();
                    let sink = sink2.clone();
                    let pong_channels2 = pong_channels2.clone();{
                        async move {
                            match message {
                                Message::Text(raw_payload) => {
                                    let raw_value = serde_json::from_str(&raw_payload)?;

                                    match raw_value {
                                        Value::Array(list) => {
                                            if let Some(message_type_item) = list.get(0) {
                                                if let Value::Number(message_type_raw) = message_type_item {
                                                    if let Some(message_type) = message_type_raw.as_u64() {
                                                        match message_type {
                                                            // CALL
                                                            2 => {
                                                                let call: RawOcpp2_0_1Call =
                                                                    serde_json::from_str(&raw_payload).unwrap();
                                                                let action  = &call.2;
                                                                let sender_opt = {
                                                                    let lock = request_senders.lock().await;
                                                                    lock.get(action).cloned()
                                                                };
                                                                match sender_opt {
                                                                    None => {
                                                                        let error = OCPP1_6Error::new_not_implemented(&format!("Action '{}' is not implemented", action));
                                                                        let payload = serde_json::to_string(&RawOcpp2_0_1Error(4, call.1.to_string(), error.code().to_string(), error.description().to_string(), error.details().to_owned())).unwrap();
                                                                        let mut lock = sink.lock().await;
                                                                        if let Err(err) = lock.send(Message::Text(Utf8Bytes::from(payload))).await {
                                                                            println!("Failed to send response: {:?}", err)
                                                                        }
                                                                    }
                                                                    Some(sender) => {
                                                                        if let Err(err) = sender.send(call).await {
                                                                            println!("Error sending request: {:?}", err);
                                                                        };
                                                                    }
                                                                }

                                                            },
                                                            // RESPONSE
                                                            3 => {
                                                                let result: RawOcpp2_0_1Result =
                                                                    serde_json::from_str(&raw_payload).unwrap();
                                                                let mut lock = response_channels2.lock().await;
                                                                if let Some(sender) = lock.remove(&Uuid::parse_str(&result.1)?) {
                                                                    sender.send(Ok(result.2)).unwrap();
                                                                }
                                                            },
                                                            // ERROR
                                                            4 => {
                                                                let error: RawOcpp2_0_1Error =
                                                                    serde_json::from_str(&raw_payload)?;
                                                                let mut lock = response_channels2.lock().await;
                                                                if let Some(sender) = lock.remove(&Uuid::parse_str(&error.1)?) {
                                                                    sender.send(Err(error.into())).unwrap();
                                                                }
                                                            },
                                                            _ => println!("Unknown message type"),
                                                        }
                                                    } else {
                                                        println!("The message type has to be an integer, it cant have decimals")
                                                    }
                                                } else {
                                                    println!("The first item in the array was not a number")
                                                }
                                            } else {
                                                println!("The root list was empty")
                                            }
                                        }
                                        _ => println!("A message should be an array of items"),
                                    }

                                }
                                Message::Ping(_) => {
                                    if ping_sender.receiver_count() > 0 {
                                        if let Err(err) = ping_sender.send(()) {
                                            println!("Error sending websocket ping: {:?}", err);
                                        };
                                    }
                                }
                                Message::Pong(_) => {
                                    let mut lock = pong_channels2.lock().await;
                                    if let Some(sender) = lock.pop_back() {
                                        sender.send(()).unwrap();
                                    }
                                }
                                _ => {}
                            }
                            Ok(())
                        }
                    }
                }).await?;
            Ok::<(), Box<dyn std::error::Error + Send + Sync>>(())
        });

        Self {
            sink,
            response_channels,
            request_senders,
            pong_channels,
            ping_sender,
            timeout: Duration::from_secs(5)
        }
    }

    /// Disconnect from the server
    pub async fn disconnect(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        let mut lock = self.sink.lock().await;
        lock.close().await?;
        Ok(())
    }

    pub async fn send_authorize(&self, request: AuthorizeRequest) -> Result<Result<AuthorizeResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "Authorize").await
    }

    pub async fn send_boot_notification(&self, request: BootNotificationRequest) -> Result<Result<BootNotificationResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "BootNotification").await
    }

    pub async fn send_cleared_charging_limit_request(&self, request: ClearedChargingLimitRequest) -> Result<Result<ClearedChargingLimitResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "ClearedChargingLimit").await
    }

    pub async fn send_data_transfer(&self, request: DataTransferRequest) -> Result<Result<DataTransferResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "DataTransfer").await
    }

    pub async fn send_firmware_status_notification(&self, request: FirmwareStatusNotificationRequest) -> Result<Result<FirmwareStatusNotificationResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "FirmwareStatusNotification").await
    }

    pub async fn send_get_15118_ev_certificate(&self, request: Get15118EVCertificateRequest) -> Result<Result<Get15118EVCertificateResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "Get15118EVCertificate").await
    }

    pub async fn send_get_certificate_status(&self, request: GetCertificateStatusRequest) -> Result<Result<GetCertificateStatusResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "GetCertificateStatus").await
    }

    pub async fn send_heartbeat(&self, request: HeartbeatRequest) -> Result<Result<HeartbeatResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "Heartbeat").await
    }

    pub async fn send_log_status_notification(&self, request: LogStatusNotificationRequest) -> Result<Result<LogStatusNotificationResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "LogStatusNotification").await
    }

    pub async fn send_meter_values(&self, request: MeterValuesRequest) -> Result<Result<MeterValuesResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "MeterValues").await
    }

    pub async fn send_notify_charging_limit(&self, request: NotifyChargingLimitRequest) -> Result<Result<NotifyChargingLimitResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyChargingLimit").await
    }

    pub async fn send_notify_customer_information(&self, request: NotifyCustomerInformationRequest) -> Result<Result<NotifyCustomerInformationResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyCustomerInformation").await
    }

    pub async fn send_notify_display_messages(&self, request: NotifyDisplayMessagesRequest) -> Result<Result<NotifyDisplayMessagesResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyDisplayMessages").await
    }

    pub async fn send_notify_ev_charging_needs(&self, request: NotifyEVChargingNeedsRequest) -> Result<Result<NotifyEVChargingNeedsResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyEVChargingNeeds").await
    }

    pub async fn send_notify_ev_charging_schedule(&self, request: NotifyEVChargingScheduleRequest) -> Result<Result<NotifyEVChargingScheduleResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyEVChargingSchedule").await
    }

    pub async fn send_notify_event(&self, request: NotifyEventRequest) -> Result<Result<NotifyEventResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyEvent").await
    }

    pub async fn send_notify_monitoring_report(&self, request: NotifyMonitoringReportRequest) -> Result<Result<NotifyMonitoringReportResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyMonitoringReport").await
    }

    pub async fn send_notify_report(&self, request: NotifyReportRequest) -> Result<Result<NotifyReportResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "NotifyReport").await
    }

    pub async fn send_publish_firmware_status_notification(&self, request: PublishFirmwareStatusNotificationRequest) -> Result<Result<PublishFirmwareStatusNotificationResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "PublishFirmwareStatusNotification").await
    }

    pub async fn send_report_charging_profiles(&self, request: ReportChargingProfilesRequest) -> Result<Result<ReportChargingProfilesResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "ReportChargingProfiles").await
    }

    pub async fn send_request_start_transaction(&self, request: RequestStartTransactionRequest) -> Result<Result<RequestStartTransactionResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "RequestStartTransaction").await
    }

    pub async fn send_request_stop_transaction(&self, request: RequestStopTransactionRequest) -> Result<Result<RequestStopTransactionResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "RequestStopTransaction").await
    }

    pub async fn send_reservation_status_update(&self, request: ReservationStatusUpdateRequest) -> Result<Result<ReservationStatusUpdateResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "ReservationStatusUpdate").await
    }

    pub async fn send_sign_certificate(&self, request: SignCertificateRequest) -> Result<Result<SignCertificateResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "SignCertificate").await
    }

    pub async fn send_status_notification(&self, request: StatusNotificationRequest) -> Result<Result<StatusNotificationResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "StatusNotification").await
    }

    pub async fn send_transaction_event(&self, request: TransactionEventRequest) -> Result<Result<TransactionEventResponse, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        self.do_send_request(request, "TransactionEvent").await
    }

    pub async fn send_ping(&self) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
        {
            let mut lock = self.sink.lock().await;
            lock.send(Message::Ping(vec![].into())).await?;
        }

        let (s, r) = oneshot::channel();
        {
            let mut pong_channels = self.pong_channels.lock().await;
            pong_channels.push_front(s);
        }

        r.await?;
        Ok(())
    }

    pub async fn on_cancel_reservation<F: FnMut(CancelReservationRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CancelReservationResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "CancelReservation").await
    }

    pub async fn on_certificate_signed<F: FnMut(CertificateSignedRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CertificateSignedResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "CertificateSigned").await
    }

    pub async fn on_change_availability<F: FnMut(ChangeAvailabilityRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ChangeAvailabilityResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "ChangeAvailability").await
    }

    pub async fn on_clear_cache<F: FnMut(ClearCacheRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearCacheResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "ClearCache").await
    }

    pub async fn on_clear_charging_profile<F: FnMut(ClearChargingProfileRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearChargingProfileResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "ClearChargingProfile").await
    }

    pub async fn on_clear_display_message<F: FnMut(ClearDisplayMessageRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearDisplayMessageResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "ClearDisplayMessage").await
    }

    pub async fn on_clear_variable_monitoring<F: FnMut(ClearVariableMonitoringRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearVariableMonitoringResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "ClearVariableMonitoring").await
    }

    pub async fn on_cost_updated<F: FnMut(CostUpdatedRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CostUpdatedResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "CostUpdated").await
    }

    pub async fn on_customer_information<F: FnMut(CustomerInformationRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CustomerInformationResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "CustomerInformation").await
    }

    pub async fn on_data_transfer<F: FnMut(DataTransferRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<DataTransferResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "DataTransfer").await
    }

    pub async fn on_delete_certificate<F: FnMut(DeleteCertificateRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<DeleteCertificateResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "DeleteCertificate").await
    }

    pub async fn on_get_base_report<F: FnMut(GetBaseReportRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetBaseReportResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetBaseReport").await
    }

    pub async fn on_get_charging_profiles<F: FnMut(GetChargingProfilesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetChargingProfilesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetChargingProfiles").await
    }

    pub async fn on_get_composite_schedule<F: FnMut(GetCompositeScheduleRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetCompositeScheduleResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetCompositeSchedule").await
    }

    pub async fn on_get_display_messages<F: FnMut(GetDisplayMessagesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetDisplayMessagesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetDisplayMessages").await
    }

    pub async fn on_get_installed_certificate_ids<F: FnMut(GetInstalledCertificateIdsRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetInstalledCertificateIdsResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetInstalledCertificateIds").await
    }

    pub async fn on_get_local_list_version<F: FnMut(GetLocalListVersionRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetLocalListVersionResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetLocalListVersion").await
    }

    pub async fn on_get_log<F: FnMut(GetLogRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetLogResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetLog").await
    }

    pub async fn on_get_monitoring_report<F: FnMut(GetMonitoringReportRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetMonitoringReportResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetMonitoringReport").await
    }

    pub async fn on_get_report<F: FnMut(GetReportRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetReportResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetReport").await
    }

    pub async fn on_get_transaction_status<F: FnMut(GetTransactionStatusRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetTransactionStatusResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetTransactionStatus").await
    }

    pub async fn on_get_variables<F: FnMut(GetVariablesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetVariablesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "GetVariables").await
    }

    pub async fn on_install_certificate<F: FnMut(InstallCertificateRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<InstallCertificateResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "InstallCertificate").await
    }

    pub async fn on_publish_firmware<F: FnMut(PublishFirmwareRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<PublishFirmwareResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "PublishFirmware").await
    }

    pub async fn on_reserve_now<F: FnMut(ReserveNowRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ReserveNowResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "ReserveNow").await
    }

    pub async fn on_reset<F: FnMut(ResetRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ResetResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "Reset").await
    }

    pub async fn on_send_local_list<F: FnMut(SendLocalListRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SendLocalListResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SendLocalList").await
    }

    pub async fn on_set_charging_profile<F: FnMut(SetChargingProfileRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetChargingProfileResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SetChargingProfile").await
    }

    pub async fn on_set_display_message<F: FnMut(SetDisplayMessageRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetDisplayMessageResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SetDisplayMessage").await
    }

    pub async fn on_set_monitoring_base<F: FnMut(SetMonitoringBaseRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetMonitoringBaseResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SetMonitoringBase").await
    }

    pub async fn on_set_monitoring_level<F: FnMut(SetMonitoringLevelRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetMonitoringLevelResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SetMonitoringLevel").await
    }

    pub async fn on_set_network_profile<F: FnMut(SetNetworkProfileRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetNetworkProfileResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SetNetworkProfile").await
    }

    pub async fn on_set_variable_monitoring<F: FnMut(SetVariableMonitoringRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetVariableMonitoringResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SetVariableMonitoring").await
    }

    pub async fn on_set_variables<F: FnMut(SetVariablesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetVariablesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "SetVariables").await
    }

    pub async fn on_trigger_message<F: FnMut(TriggerMessageRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<TriggerMessageResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "TriggerMessage").await
    }

    pub async fn on_unlock_connector<F: FnMut(UnlockConnectorRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<UnlockConnectorResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "UnlockConnector").await
    }

    pub async fn on_unpublish_firmware<F: FnMut(UnpublishFirmwareRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<UnpublishFirmwareResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "UnpublishFirmware").await
    }

    pub async fn on_update_firmware<F: FnMut(UpdateFirmwareRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<UpdateFirmwareResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) {
        self.handle_on_request(callback, "UpdateFirmware").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_cancel_reservation<F: FnMut(CancelReservationRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CancelReservationResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<CancelReservationRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "CancelReservation").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_certificate_signed<F: FnMut(CertificateSignedRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CertificateSignedResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<CertificateSignedRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "CertificateSigned").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_change_availability<F: FnMut(ChangeAvailabilityRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ChangeAvailabilityResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<ChangeAvailabilityRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "ChangeAvailability").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_clear_cache<F: FnMut(ClearCacheRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearCacheResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<ClearCacheRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "ClearCache").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_clear_charging_profile<F: FnMut(ClearChargingProfileRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearChargingProfileResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<ClearChargingProfileRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "ClearChargingProfile").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_clear_display_message<F: FnMut(ClearDisplayMessageRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearDisplayMessageResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<ClearDisplayMessageRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "ClearDisplayMessage").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_clear_variable_monitoring<F: FnMut(ClearVariableMonitoringRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ClearVariableMonitoringResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<ClearVariableMonitoringRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "ClearVariableMonitoring").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_cost_updated<F: FnMut(CostUpdatedRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CostUpdatedResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<CostUpdatedRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "CostUpdated").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_customer_information<F: FnMut(CustomerInformationRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<CustomerInformationResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<CustomerInformationRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "CustomerInformation").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_data_transfer<F: FnMut(DataTransferRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<DataTransferResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<DataTransferRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "DataTransfer").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_delete_certificate<F: FnMut(DeleteCertificateRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<DeleteCertificateResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<DeleteCertificateRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "DeleteCertificate").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_base_report<F: FnMut(GetBaseReportRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetBaseReportResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetBaseReportRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetBaseReport").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_charging_profiles<F: FnMut(GetChargingProfilesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetChargingProfilesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetChargingProfilesRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetChargingProfiles").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_composite_schedule<F: FnMut(GetCompositeScheduleRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetCompositeScheduleResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetCompositeScheduleRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetCompositeSchedule").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_display_messages<F: FnMut(GetDisplayMessagesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetDisplayMessagesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetDisplayMessagesRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetDisplayMessages").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_installed_certificate_ids<F: FnMut(GetInstalledCertificateIdsRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetInstalledCertificateIdsResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetInstalledCertificateIdsRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetInstalledCertificateIds").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_local_list_version<F: FnMut(GetLocalListVersionRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetLocalListVersionResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetLocalListVersionRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetLocalListVersion").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_log<F: FnMut(GetLogRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetLogResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetLogRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetLog").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_monitoring_report<F: FnMut(GetMonitoringReportRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetMonitoringReportResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetMonitoringReportRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetMonitoringReport").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_report<F: FnMut(GetReportRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetReportResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetReportRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetReport").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_transaction_status<F: FnMut(GetTransactionStatusRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetTransactionStatusResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetTransactionStatusRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetTransactionStatus").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_get_variables<F: FnMut(GetVariablesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<GetVariablesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<GetVariablesRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "GetVariables").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_install_certificate<F: FnMut(InstallCertificateRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<InstallCertificateResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<InstallCertificateRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "InstallCertificate").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_publish_firmware<F: FnMut(PublishFirmwareRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<PublishFirmwareResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<PublishFirmwareRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "PublishFirmware").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_reserve_now<F: FnMut(ReserveNowRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ReserveNowResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<ReserveNowRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "ReserveNow").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_reset<F: FnMut(ResetRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<ResetResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<ResetRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "Reset").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_send_local_list<F: FnMut(SendLocalListRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SendLocalListResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SendLocalListRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SendLocalList").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_set_charging_profile<F: FnMut(SetChargingProfileRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetChargingProfileResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SetChargingProfileRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SetChargingProfile").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_set_display_message<F: FnMut(SetDisplayMessageRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetDisplayMessageResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SetDisplayMessageRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SetDisplayMessage").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_set_monitoring_base<F: FnMut(SetMonitoringBaseRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetMonitoringBaseResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SetMonitoringBaseRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SetMonitoringBase").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_set_monitoring_level<F: FnMut(SetMonitoringLevelRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetMonitoringLevelResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SetMonitoringLevelRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SetMonitoringLevel").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_set_network_profile<F: FnMut(SetNetworkProfileRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetNetworkProfileResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SetNetworkProfileRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SetNetworkProfile").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_set_variable_monitoring<F: FnMut(SetVariableMonitoringRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetVariableMonitoringResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SetVariableMonitoringRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SetVariableMonitoring").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_set_variables<F: FnMut(SetVariablesRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<SetVariablesResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<SetVariablesRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "SetVariables").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_trigger_message<F: FnMut(TriggerMessageRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<TriggerMessageResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<TriggerMessageRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "TriggerMessage").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_unlock_connector<F: FnMut(UnlockConnectorRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<UnlockConnectorResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<UnlockConnectorRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "UnlockConnector").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_unpublish_firmware<F: FnMut(UnpublishFirmwareRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<UnpublishFirmwareResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<UnpublishFirmwareRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "UnpublishFirmware").await
    }

    #[cfg(feature = "test")]
    pub async fn wait_for_update_firmware<F: FnMut(UpdateFirmwareRequest, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<UpdateFirmwareResponse, OCPP2_0_1Error>> + Send + Sync>(&self, callback: F) -> Result<UpdateFirmwareRequest, Box<dyn std::error::Error + Send + Sync>> {
        self.handle_wait_for_request(callback, "UpdateFirmware").await
    }

    pub async fn on_ping<F: FnMut(Self) -> FF + Send + Sync + 'static, FF: Future<Output=()> + Send + Sync>(&self, mut callback: F) {
        let mut recv = self.ping_sender.subscribe();

        let s = self.clone();
        tokio::spawn(async move {
            while let Ok(()) = recv.recv().await {
                callback(s.clone()).await;
            }
        });
    }

    async fn handle_on_request<P: DeserializeOwned + Send + Sync, R: Serialize + Send + Sync, F: FnMut(P, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<R, OCPP2_0_1Error>> + Send + Sync>(&self, mut callback: F, action: &'static str) {
        let (sender, mut recv) = mpsc::channel(1000);
        {
            let mut lock = self.request_senders.lock().await;
            lock.insert(action.to_string(), sender);

        }

        let s = self.clone();
        tokio::spawn(async move {
            while let Some(call) = recv.recv().await {
                match serde_json::from_value(call.3) {
                    Ok(payload) => {
                        let response = callback(payload, s.clone()).await;
                        s.do_send_response(response, &call.1).await
                    }
                    Err(err) => {
                        println!("Failed to parse payload: {:?}", err)
                    }
                }
            }
        });
    }

    #[cfg(feature = "test")]
    async fn handle_wait_for_request<P: DeserializeOwned + Send + Sync, R: Serialize + Send + Sync, F: FnMut(P, Self) -> FF + Send + Sync + 'static, FF: Future<Output=Result<R, OCPP2_0_1Error>> + Send + Sync>(&self, mut callback: F, action: &'static str) -> Result<P, Box<dyn std::error::Error + Send + Sync>> {
        let (sender, mut recv) = mpsc::channel(1000);
        {
            let mut lock = self.request_senders.lock().await;
            lock.insert(action.to_string(), sender);

        }

        let s = self.clone();
        match timeout(self.timeout, recv.recv()).await {
            Ok(opt) => {
                match opt {
                    None => {
                        Err("No call received".into())
                    }
                    Some(call) => {
                        match serde_json::from_value(call.3.clone()) {
                            Ok(payload) => {
                                let response = callback(payload, s.clone()).await;
                                self.do_send_response(response, &call.1).await;
                                Ok(serde_json::from_value(call.3).unwrap())
                            }
                            Err(err) => {
                                println!("Failed to parse payload: {:?}", err);
                                Err("Failed to parse payload".into())
                            }
                        }
                    }
                }
            }
            Err(_) => {
                Err("Timeout".into())
            }
        }
    }

    async fn do_send_response<R: Serialize>(&self, response: Result<R, OCPP2_0_1Error>, message_id: &str) {
        let payload = match response {
            Ok(r) => {
                serde_json::to_string(&RawOcpp2_0_1Result(3, message_id.to_string(), serde_json::to_value(r).unwrap())).unwrap()
            }
            Err(e) => {
                serde_json::to_string(&RawOcpp2_0_1Error(4, message_id.to_string(), e.code().to_string(), e.description().to_string(), e.details().to_owned())).unwrap()
            }
        };

        let mut lock = self.sink.lock().await;
        if let Err(err) = lock.send(Message::Text(Utf8Bytes::from(payload))).await {
            println!("Failed to send response: {:?}", err)
        }
    }

    async fn do_send_request<P: Serialize, R: DeserializeOwned>(&self, request: P, action: &str) -> Result<Result<R, OCPP2_0_1Error>, Box<dyn std::error::Error + Send + Sync>> {
        let message_id = Uuid::new_v4();

        let call = RawOcpp2_0_1Call(2, message_id.to_string(), action.to_string(), serde_json::to_value(&request)?);

        {
            let mut lock = self.sink.lock().await;
            lock.send(Message::Text(Utf8Bytes::from(serde_json::to_string(&call)?))).await?;
        }

        let (s, r) = oneshot::channel();
        {
            let mut response_channels = self.response_channels.lock().await;
            response_channels.insert(message_id, s);
        }

        match r.await? {
            Ok(value) => {
                Ok(Ok(serde_json::from_value(value)?))
            }
            Err(e) => Ok(Err(e))
        }
    }
}