ocpp_rs 0.2.5

Protocol implementation for Open Charge Point Protocol (OCPP) in Rust.
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
//! # Call Result    
//! This module contains all response messages to a given `Call` message.\\
//! Note that `CallResult` does not contain a type field, so you need to handle special cases where JSON can be ambiguous.\\
//! These special cases are inside specific enum variants, `PossibleEmptyResponse` and `PossibleStatusResponse`.\
//! Inside `PossibleEmptyResponse` you will find `PossibleIdTagInfoResponse` that also contain two different cases.   
//!
//! ## Example
//!
//! Sending a payload to a client:
//! ```rust
//! use ocpp_rs::v16::call::StartTransaction;
//! use ocpp_rs::v16::call_result::{self, CallResult, ResultPayload};
//! use ocpp_rs::v16::data_types::IdTagInfo;
//! use ocpp_rs::v16::enums::ChargePointStatus;
//! use ocpp_rs::v16::parse::Message;
//!
//! let response = Message::CallResult(CallResult::new(
//!     "1234".to_string(),
//!     ResultPayload::StartTransaction(call_result::StartTransaction {
//!         transaction_id: 0,
//!         id_tag_info: IdTagInfo {
//!             status: ocpp_rs::v16::enums::ParsedGenericStatus::Accepted,
//!             expiry_date: None,
//!             parent_id_tag: None,
//!         },
//!     }),
//! ));
//!```

use super::data_types::{DateTimeWrapper, IdTagInfo, KeyValue};
use super::enums::ParsedGenericStatus;
use super::utils::{iso8601_date_time, iso8601_date_time_optional};
use alloc::collections::btree_map::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use serde_tuple::{Deserialize_tuple, Serialize_tuple};
use strum_macros::AsRefStr;

#[derive(AsRefStr, Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[serde(untagged)]
/// This enum is used to handle the different types of responses that might come from the server.  
pub enum ResultPayload {
    StartTransaction(StartTransaction),
    BootNotification(BootNotification),
    Heartbeat(Heartbeat),
    GetLocalListVersion(GetLocalListVersion),
    PossibleStatusResponse(StatusResponses),
    PossibleEmptyResponse(EmptyResponses),
}

#[derive(AsRefStr, Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[serde(untagged)]
/// Enum containing all possible empty responses that might come from the server.
///
///  Since some structs might come as empty due to the optional fields,\\
/// this enum is used to handle those cases, since the serializer has no way\\
/// to know which struct to use when deserializing, since there is no type field\\
/// in the `CallResult` spec.    
pub enum EmptyResponses {
    /// Matches all empty responses
    EmptyResponse(EmptyResponse),
    /// Remember to handle `EmptyResponse` before this since it is ambiguous when deserializing,
    /// and might get interpreted as `EmptyResponse` instead of `GenericIdTagInfoResponse`.
    GenericIdTagInfoResponse(GenericIdTagInfo),
    /// Remember to handle `EmptyResponse` before this since it is ambiguous when deserializing,
    /// and might get interpreted as `EmptyResponse` instead of `GetConfiguration`.
    GetConfiguration(GetConfiguration),
    /// Remember to handle `EmptyResponse` before this since it is ambiguous when deserializing,
    /// and might get interpreted as `EmptyResponse` instead of `GetDiagnostics`.
    GetDiagnostics(GetDiagnostics),
}

impl EmptyResponses {
    #[must_use]
    pub fn is_empty(&self) -> bool {
        match self {
            Self::EmptyResponse(_) => true,
            Self::GetConfiguration(get_configuration) => get_configuration.is_empty(),
            Self::GetDiagnostics(get_diagnostics) => get_diagnostics.is_empty(),
            Self::GenericIdTagInfoResponse(generic_id_tag_info) => generic_id_tag_info.is_empty(),
        }
    }
}

#[derive(AsRefStr, Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
#[serde(untagged)]
/// Enum containing all possible status responses that might come from the server.
///
///  IMPORTANT: When deserializing data from JSON, optional fields might not be present,\\
/// even when fields are present and null, the deserializer will transform data with only `status` as\\
/// `GenericStatusResponse` instead of other structs that implement status plus other **optional** fields.\\
/// (cases that contain non optional fields are not affected)    
///     
/// This means, in case you are waiting for a response that matches a struct that contains `status`, you should\\
/// also check if first you receive a `GenericStatusResponse` that matches the same `unique_id` you've sent.    
///     
/// This is mostly due to the protocol not being properly projected, because Call does have the type field,\\
/// but `CallResult` does not.    
pub enum StatusResponses {
    StatusResponse(GenericStatusResponse),
    GetInstalledCertificateIds(GetInstalledCertificateIds),
    GetCompositeSchedule(GetCompositeSchedule),
    GetLog(GetLog),
    DataTransfer(DataTransfer),
}

impl StatusResponses {
    #[must_use]
    pub const fn get_status(&self) -> &ParsedGenericStatus {
        match self {
            Self::StatusResponse(generic_status_response) => &generic_status_response.status,
            Self::GetInstalledCertificateIds(get_installed_certificate_ids) => {
                &get_installed_certificate_ids.status
            }
            Self::GetCompositeSchedule(get_composite_schedule) => &get_composite_schedule.status,
            Self::GetLog(get_log) => &get_log.status,
            Self::DataTransfer(data_transfer) => &data_transfer.status,
        }
    }

    #[must_use]
    pub fn is_only_status(&self) -> bool {
        match self {
            Self::StatusResponse(generic_status_response) => {
                generic_status_response.is_only_status()
            }
            Self::GetInstalledCertificateIds(get_installed_certificate_ids) => {
                get_installed_certificate_ids.is_only_status()
            }
            Self::GetCompositeSchedule(get_composite_schedule) => {
                get_composite_schedule.is_only_status()
            }
            Self::GetLog(get_log) => get_log.is_only_status(),
            Self::DataTransfer(data_transfer) => data_transfer.is_only_status(),
        }
    }
}

pub trait Status {
    /// This return true if the type contains only the status field.    
    fn is_only_status(&self) -> bool;
}

pub trait PossibleEmpty {
    fn is_empty(&self) -> bool;
}

#[derive(Debug, PartialEq, Eq, Serialize_tuple, Deserialize_tuple, Clone)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct CallResult {
    pub(super) message_id: i32,
    pub unique_id: String,
    pub payload: ResultPayload,
}

impl CallResult {
    #[must_use]
    pub const fn new(unique_id: String, payload: ResultPayload) -> Self {
        Self {
            message_id: 3,
            unique_id,
            payload,
        }
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct BootNotification {
    /// ISO 8601 timestamp    
    #[serde(with = "iso8601_date_time")]
    pub current_time: DateTimeWrapper,
    /// Interval in seconds    
    pub interval: i32,
    pub status: ParsedGenericStatus,
}

impl Status for BootNotification {
    fn is_only_status(&self) -> bool {
        false
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Heartbeat {
    #[serde(with = "iso8601_date_time")]
    pub current_time: DateTimeWrapper,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// This struct will come as empty.\\
/// This might be ill interpreted by the deserializer.    
pub struct EmptyResponse {}

impl PossibleEmpty for EmptyResponse {
    fn is_empty(&self) -> bool {
        true
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct StartTransaction {
    pub transaction_id: i32,
    pub id_tag_info: IdTagInfo,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// This struct might come as empty due to the optional fields.\\
/// This might be ill interpreted by the deserializer.    
pub struct GenericIdTagInfo {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id_tag_info: Option<IdTagInfo>,
}

impl PossibleEmpty for GenericIdTagInfo {
    fn is_empty(&self) -> bool {
        self.id_tag_info.is_none()
    }
}

impl GenericIdTagInfo {
    #[must_use]
    /// When waiting for a response that might contain `IdTagInfo` or `StopTransaction`,\\
    /// this function will return the `IdTagInfo` if it exists.\\
    /// No need for matching    
    pub fn get_id_tag_info(self) -> Option<IdTagInfo> {
        self.id_tag_info
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// Generic status response that might come as empty.
///
/// IMPORTANT: When deserializing data from JSON, optional fields might not be present,\\
/// even when fields are present and null, the deserializer will transform data with only `status` as\\
/// `GenericStatusResponse` instead of other structs that implement status plus other **optional** fields.\\
/// (cases that contain non optional fields are not affected)    
///     
/// This means, in case you are waiting for a response that matches a struct that contains `status`, you should\\
/// also check if first you receive a `GenericStatusResponse` that matches the same `unique_id` you've sent.    
///     
/// This is mostly due to the protocol not being properly projected, because Call does have the type field,\\
/// but `CallResult` does not.\\
/// TODO: create a generic and use strict types for each type
pub struct GenericStatusResponse {
    pub status: ParsedGenericStatus,
}

impl Status for GenericStatusResponse {
    fn is_only_status(&self) -> bool {
        true
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// Result including the certificate hash data
///
/// IMPORTANT: When deserializing data from JSON, optional fields might not be present,\\
/// even when present and null, the deserializer will transform data with only `status` as\\
/// `GenericStatusResponse` instead of this struct.    
///     
/// This means, in case you are waiting for a response that matches this struct, you should\\
/// also check if first you receive a `GenericStatusResponse` that matches the same `unique_id` you've sent.    
///     
/// This is mostly due to the protocol not being properly projected, because Call does have the type field,\\
/// but `CallResult` does not.    
pub struct GetInstalledCertificateIds {
    pub status: ParsedGenericStatus,
    pub certificate_hash_data: Option<Vec<String>>,
}

impl Status for GetInstalledCertificateIds {
    fn is_only_status(&self) -> bool {
        self.certificate_hash_data.is_none()
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// Result including the composite schedule
///
///  IMPORTANT: When deserializing data from JSON, optional fields might not be present,\\
/// even when present and null, the deserializer will transform data with only `status` as\\
/// `GenericStatusResponse` instead of this struct.    
///     
/// This means, in case you are waiting for a response that matches this struct, you should\\
/// also check if first you receive a `GenericStatusResponse` that matches the same `unique_id` you've sent.    
///     
/// This is mostly due to the protocol not being properly projected, because Call does have the type field,\\
/// but `CallResult` does not.    
pub struct GetCompositeSchedule {
    pub status: ParsedGenericStatus,
    pub connector_id: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(with = "iso8601_date_time_optional")]
    pub schedule_start: Option<DateTimeWrapper>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub charging_schedule: Option<BTreeMap<String, String>>,
}

impl Status for GetCompositeSchedule {
    fn is_only_status(&self) -> bool {
        self.connector_id.is_none()
            && self.schedule_start.is_none()
            && self.charging_schedule.is_none()
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// This struct might come as empty due to the optional fields.\\
/// This might be ill interpreted by the deserializer.    
pub struct GetConfiguration {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub configuration_key: Option<Vec<KeyValue>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub unknown_key: Option<Vec<String>>,
}

impl PossibleEmpty for GetConfiguration {
    fn is_empty(&self) -> bool {
        self.configuration_key.is_none() && self.unknown_key.is_none()
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// This struct might come as empty due to the optional fields.\\
/// This might be ill interpreted by the deserializer.    
pub struct GetDiagnostics {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub file_name: Option<String>,
}

impl PossibleEmpty for GetDiagnostics {
    fn is_empty(&self) -> bool {
        self.file_name.is_none()
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct GetLocalListVersion {
    pub list_version: i32,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// Non-standard for returning the filename for the logs
///
///  IMPORTANT: When deserializing data from JSON, optional fields might not be present,\\
/// even when present and null, the deserializer will transform data with only `status` as\\
/// `GenericStatusResponse` instead of this struct.    
///     
/// This means, in case you are waiting for a response that matches this struct, you should\\
/// also check if first you receive a `GenericStatusResponse` that matches the same `unique_id` you've sent.    
///     
/// This is mostly due to the protocol not being properly projected, because Call does have the type field,\\
/// but `CallResult` does not.    
pub struct GetLog {
    pub status: ParsedGenericStatus,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filename: Option<String>,
}

impl Status for GetLog {
    fn is_only_status(&self) -> bool {
        self.filename.is_none()
    }
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct UnlockConnector {
    pub status: GenericStatusResponse,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
/// Generic data transfer response
///
///  IMPORTANT: When deserializing data from JSON, optional fields might not be present,\\
/// even when present and null, the deserializer will transform data with only `status` as\\
/// `GenericStatusResponse` instead of this struct.    
///     
/// This means, in case you are waiting for a response that matches this struct, you should\\
/// also check if first you receive a `GenericStatusResponse` that matches the same `unique_id` you've sent.    
///     
/// This is mostly due to the protocol not being properly projected, because Call does have the type field,\\
/// but `CallResult` does not.    
pub struct DataTransfer {
    pub status: ParsedGenericStatus,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<String>,
}

impl Status for DataTransfer {
    fn is_only_status(&self) -> bool {
        self.data.is_none()
    }
}