ocppsim 0.2.0

A terminal OCPP-J charge point simulator.
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
// Typed payload structs for all outbound and response OCPP messages.
//
// Each struct derives `Serialize` so that field names are validated at
// compile time, making the wire-format shape self-documenting.

#![allow(non_camel_case_types)]

use serde::Serialize;
use serde_json::Value;

// ── Common nested types ────────────────────────────────────────

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct ChargingStationInfo<'a> {
  pub vendor_name: &'a str,
  pub model: &'a str,
  pub firmware_version: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct IdTokenPayload<'a> {
  pub id_token: &'a str,
  #[serde(rename = "type")]
  pub token_type: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct SampledValueV1_6<'a> {
  pub value: &'a str,
  pub context: &'a str,
  pub measurand: &'a str,
  pub unit: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct UnitOfMeasure<'a> {
  pub unit: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct SampledValue_V2_X<'a> {
  pub value: i64,
  pub context: &'a str,
  pub measurand: &'a str,
  pub unit_of_measure: UnitOfMeasure<'a>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct MeterValueEntry<'a, T: Serialize> {
  pub timestamp: &'a str,
  pub sampled_value: Vec<T>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct TransactionInfoPayload<'a> {
  pub transaction_id: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub remote_start_id: Option<i64>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub stopped_reason: Option<&'a str>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct EvsePayload {
  pub id: u16,
  pub connector_id: u16,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct ChargingSchedulePeriod {
  pub start_period: i64,
  pub limit: f64,
}

// ── Outbound CALL payloads ─────────────────────────────────────

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct BootNotificationV1_6Request<'a> {
  pub charge_point_vendor: &'a str,
  pub charge_point_model: &'a str,
  pub firmware_version: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct BootNotification_V2_X_Request<'a> {
  pub reason: &'a str,
  pub charging_station: ChargingStationInfo<'a>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct AuthorizeV1_6Request<'a> {
  pub id_tag: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct Authorize_V2_X_Request<'a> {
  pub id_token: IdTokenPayload<'a>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct HeartbeatRequest {}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct DataTransferRequestPayload<'a> {
  pub vendor_id: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub message_id: Option<&'a str>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub data: Option<&'a str>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct StatusPayload<'a> {
  pub status: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct FirmwareStatus_V2_X_Payload<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub request_id: Option<i64>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct LogStatusPayload<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub request_id: Option<i64>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct SecurityEventNotificationRequest<'a> {
  #[serde(rename = "type")]
  pub event_type: &'a str,
  pub timestamp: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub tech_info: Option<&'a str>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct SignCertificateRequest<'a> {
  pub csr: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub certificate_type: Option<&'a str>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub request_id: Option<i64>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct StatusNotificationV1_6Request<'a> {
  pub connector_id: u16,
  pub error_code: &'a str,
  pub status: &'a str,
  pub timestamp: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct StatusNotification_V2_X_Request<'a> {
  pub timestamp: &'a str,
  pub connector_status: &'a str,
  pub evse_id: u16,
  pub connector_id: u16,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct MeterValuesV1_6Request<'a> {
  pub connector_id: u16,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub transaction_id: Option<i64>,
  pub meter_value: Vec<MeterValueEntry<'a, SampledValueV1_6<'a>>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct MeterValues_V2_X_Request<'a> {
  pub evse_id: u16,
  pub meter_value: Vec<MeterValueEntry<'a, SampledValue_V2_X<'a>>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct StartTransactionV1_6Request<'a> {
  pub connector_id: u16,
  pub id_tag: &'a str,
  pub meter_start: i64,
  pub timestamp: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct StopTransactionV1_6Request<'a> {
  pub transaction_id: i64,
  pub timestamp: &'a str,
  pub meter_stop: i64,
  pub id_tag: &'a str,
  pub reason: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct TransactionEvent_V2_X_Request<'a> {
  pub event_type: &'a str,
  pub timestamp: &'a str,
  pub trigger_reason: &'a str,
  pub seq_no: u64,
  pub transaction_info: TransactionInfoPayload<'a>,
  pub evse: EvsePayload,
  pub meter_value: Vec<MeterValueEntry<'a, SampledValue_V2_X<'a>>>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub id_token: Option<IdTokenPayload<'a>>,
}

// ── Response payloads ──────────────────────────────────────────

#[derive(Serialize)]
pub(super) struct ConfigurationKeyEntry<'a> {
  pub key: &'a str,
  pub readonly: bool,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub value: Option<&'a str>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetConfigurationV1_6Response<'a> {
  pub configuration_key: Vec<ConfigurationKeyEntry<'a>>,
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub unknown_key: Vec<String>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct ListVersionV1_6Response {
  pub list_version: i64,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct ListVersion_V2_X_Response {
  pub version_number: i64,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetDiagnosticsV1_6Response<'a> {
  #[serde(skip_serializing_if = "Option::is_none")]
  pub file_name: Option<&'a str>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct DataTransferResponse<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub data: Option<Value>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct StatusResponse<'a> {
  pub status: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetLog_V2_X_Response<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub filename: Option<&'a str>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct CertificateHashDataPayload<'a> {
  pub hash_algorithm: &'a str,
  pub issuer_name_hash: &'a str,
  pub issuer_key_hash: &'a str,
  pub serial_number: &'a str,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct CertificateHashDataChainPayload<'a> {
  pub certificate_type: &'a str,
  pub certificate_hash_data: CertificateHashDataPayload<'a>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetInstalledCertificateIdsV1_6Response<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub certificate_hash_data: Vec<CertificateHashDataPayload<'a>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetInstalledCertificateIds_V2_X_Response<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Vec::is_empty")]
  pub certificate_hash_data_chain: Vec<CertificateHashDataChainPayload<'a>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetVariables_V2_X_Response {
  pub get_variable_result: Vec<VariableResult_V2_X>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct SetVariables_V2_X_Response {
  pub set_variable_result: Vec<VariableResult_V2_X>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct VariableResult_V2_X {
  pub attribute_status: &'static str,
  pub component: Value,
  pub variable: Value,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub attribute_type: Option<Value>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub attribute_value: Option<String>,
}

impl VariableResult_V2_X {
  /// Constructs a base result from a get/set variable entry.
  pub(super) fn from_entry(
    entry: &Value,
    status: &'static str,
  ) -> anyhow::Result<Self> {
    Ok(Self {
      attribute_status: status,
      component: entry
        .get("component")
        .cloned()
        .ok_or_else(|| anyhow::anyhow!("component is required."))?,
      variable: entry
        .get("variable")
        .cloned()
        .ok_or_else(|| anyhow::anyhow!("variable is required."))?,
      attribute_type: entry.get("attributeType").cloned(),
      attribute_value: None,
    })
  }
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetCompositeScheduleV1_6Response<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub connector_id: Option<u16>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub schedule_start: Option<&'a str>,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub charging_schedule: Option<ChargingScheduleV1_6<'a>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct ChargingScheduleV1_6<'a> {
  pub duration: u64,
  pub charging_rate_unit: &'a str,
  pub charging_schedule_period: Vec<ChargingSchedulePeriod>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct GetCompositeSchedule_V2_X_Response<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub schedule: Option<CompositeSchedule_V2_X<'a>>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct CompositeSchedule_V2_X<'a> {
  pub evse_id: u16,
  pub duration: u64,
  pub schedule_start: &'a str,
  pub charging_rate_unit: &'a str,
  pub charging_schedule_period: Vec<ChargingSchedulePeriod>,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct RequestStartTransactionResponse<'a> {
  pub status: &'a str,
  #[serde(skip_serializing_if = "Option::is_none")]
  pub transaction_id: Option<&'a str>,
}

/// Converts a `Serialize` value to `serde_json::Value`.
///
/// Panics only on serialization bugs (not possible for simple data
/// types), so calling code can treat this as infallible.
pub(super) fn to_value<T: Serialize>(payload: &T) -> Value {
  serde_json::to_value(payload).expect("payload serialization")
}