ocppsim 0.3.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
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
#![allow(non_camel_case_types)]

use super::super::{
  ChargingRateUnit, ConnectorStatus, Result, Value, anyhow,
  normalize_identifier, optional_u16_field, required_i64_field,
  required_string_field, required_u16_field, required_u64_field,
};

#[derive(Debug, Clone)]
pub(in crate::simulator) struct RemoteStartTransactionRequestV1_6 {
  pub connector: Option<u16>,
  pub id_token: String,
  pub charging_profile: Option<Value>,
}

impl RemoteStartTransactionRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: optional_u16_field(payload, "connectorId")?,
      id_token: required_string_field(payload, "idTag")?.to_string(),
      charging_profile: optional_object_field(payload, "chargingProfile")?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct RemoteStopTransactionRequestV1_6 {
  pub transaction_id: i64,
}

impl RemoteStopTransactionRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      transaction_id: required_i64_field(payload, "transactionId")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct RequestStartTransactionRequest_V2_X {
  pub connector: Option<u16>,
  pub remote_start_id: i64,
  pub id_token: String,
  pub charging_profile: Option<Value>,
}

impl RequestStartTransactionRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let id_token =
      required_nested_string_field(payload, "idToken", "idToken")?.to_string();
    let _ = required_nested_string_field(payload, "idToken", "type")?;
    Ok(Self {
      connector: optional_u16_field(payload, "evseId")?,
      remote_start_id: required_i64_field(payload, "remoteStartId")?,
      id_token,
      charging_profile: optional_object_field(payload, "chargingProfile")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct RequestStopTransactionRequest_V2_X {
  pub transaction_id: String,
}

impl RequestStopTransactionRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      transaction_id: required_string_field(payload, "transactionId")?
        .to_string(),
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct AvailabilityRequest {
  pub connector: Option<u16>,
  pub target_status: Option<ConnectorStatus>,
}

impl AvailabilityRequest {
  pub(in crate::simulator) fn parse_v1_6(payload: &Value) -> Result<Self> {
    let requested = required_string_field(payload, "type")?;
    Ok(Self {
      connector: Some(required_u16_field(payload, "connectorId")?),
      target_status: parse_operational_status(requested),
    })
  }

  pub(in crate::simulator) fn parse_v2_x(payload: &Value) -> Result<Self> {
    let requested = required_string_field(payload, "operationalStatus")?;
    Ok(Self {
      connector: optional_nested_u16_field(payload, "evse", "id")?,
      target_status: parse_operational_status(requested),
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct ReserveNowRequestV1_6 {
  pub connector: u16,
  pub reservation_id: i64,
}

impl ReserveNowRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let _ = required_string_field(payload, "expiryDate")?;
    let _ = required_string_field(payload, "idTag")?;
    Ok(Self {
      connector: required_u16_field(payload, "connectorId")?,
      reservation_id: required_i64_field(payload, "reservationId")?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct ReserveNowRequest_V2_X {
  pub connector: Option<u16>,
  pub reservation_id: i64,
}

impl ReserveNowRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let _ = required_string_field(payload, "expiryDateTime")?;
    let _ = required_nested_string_field(payload, "idToken", "idToken")?;
    let _ = required_nested_string_field(payload, "idToken", "type")?;
    Ok(Self {
      connector: optional_u16_field(payload, "evseId")?,
      reservation_id: required_i64_any_field(payload, &["id", "reservationId"])
        .ok_or_else(|| anyhow!("id is required."))?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct CancelReservationRequest {
  pub reservation_id: i64,
}

impl CancelReservationRequest {
  pub(in crate::simulator) fn parse_v1_6(payload: &Value) -> Result<Self> {
    Ok(Self {
      reservation_id: required_i64_field(payload, "reservationId")?,
    })
  }

  pub(in crate::simulator) fn parse_v2_x(payload: &Value) -> Result<Self> {
    Ok(Self {
      reservation_id: required_i64_any_field(payload, &["reservationId", "id"])
        .ok_or_else(|| anyhow!("reservationId is required."))?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct UnlockConnectorRequestV1_6 {
  pub connector: u16,
}

impl UnlockConnectorRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: required_u16_field(payload, "connectorId")?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct UnlockConnectorRequest_V2_X {
  pub evse_id: u16,
  pub connector_id: u16,
}

impl UnlockConnectorRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      evse_id: required_u16_field(payload, "evseId")?,
      connector_id: required_u16_field(payload, "connectorId")?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct SendLocalListRequestV1_6 {
  pub list_version: i64,
}

impl SendLocalListRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let _ = required_string_field(payload, "updateType")?;
    Ok(Self {
      list_version: required_i64_field(payload, "listVersion")?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct SendLocalListRequest_V2_X {
  pub version_number: i64,
}

impl SendLocalListRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let _ = required_string_field(payload, "updateType")?;
    Ok(Self {
      version_number: required_i64_field(payload, "versionNumber")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct SetChargingProfileRequestV1_6 {
  pub connector: u16,
  pub profile: Value,
}

impl SetChargingProfileRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: required_u16_field(payload, "connectorId")?,
      profile: required_object_field(payload, "csChargingProfiles")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct SetChargingProfileRequest_V2_X {
  pub connector: u16,
  pub profile: Value,
}

impl SetChargingProfileRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: required_u16_field(payload, "evseId")?,
      profile: required_object_field(payload, "chargingProfile")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct ClearChargingProfileRequestV1_6 {
  pub connector: Option<u16>,
  pub profile_id: Option<i64>,
  pub purpose: Option<String>,
  pub stack_level: Option<i64>,
}

impl ClearChargingProfileRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: optional_u16_field(payload, "connectorId")?,
      profile_id: optional_i64_field(payload, "id")?,
      purpose: optional_string_field(payload, "chargingProfilePurpose")?,
      stack_level: optional_i64_field(payload, "stackLevel")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct ClearChargingProfileCriteria_V2_X {
  pub connector: Option<u16>,
  pub purpose: Option<String>,
  pub stack_level: Option<i64>,
}

impl ClearChargingProfileCriteria_V2_X {
  fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: optional_u16_field(payload, "evseId")?,
      purpose: optional_string_field(payload, "chargingProfilePurpose")?,
      stack_level: optional_i64_field(payload, "stackLevel")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct ClearChargingProfileRequest_V2_X {
  pub profile_id: Option<i64>,
  pub criteria: Option<ClearChargingProfileCriteria_V2_X>,
}

impl ClearChargingProfileRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let criteria = optional_object_field(payload, "chargingProfileCriteria")?
      .map(|value| ClearChargingProfileCriteria_V2_X::parse(&value))
      .transpose()?;
    Ok(Self {
      profile_id: optional_i64_field(payload, "chargingProfileId")?,
      criteria,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct DataTransferRequestV1_6 {
  pub data: Option<String>,
}

impl DataTransferRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let _ = required_string_field(payload, "vendorId")?;
    Ok(Self {
      data: optional_string_field(payload, "data")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct DataTransferRequest_V2_X {
  pub data: Option<Value>,
}

impl DataTransferRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let _ = required_string_field(payload, "vendorId")?;
    Ok(Self {
      data: payload.get("data").cloned(),
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct CompositeScheduleRequestV1_6 {
  pub connector: u16,
  pub duration: u64,
  pub charging_rate_unit: Option<String>,
}

impl CompositeScheduleRequestV1_6 {
  pub(in crate::simulator) fn invalid_charging_rate_unit(
    payload: &Value,
  ) -> Result<Option<String>> {
    let Some(value) = optional_string_field(payload, "chargingRateUnit")?
    else {
      return Ok(None);
    };
    Ok(ChargingRateUnit::parse(&value).is_none().then_some(value))
  }

  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: required_u16_field(payload, "connectorId")?,
      duration: required_u64_field(payload, "duration")?,
      charging_rate_unit: optional_string_field(payload, "chargingRateUnit")?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct CompositeScheduleRequest_V2_X {
  pub connector: u16,
  pub duration: u64,
}

impl CompositeScheduleRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      connector: required_u16_field(payload, "evseId")?,
      duration: required_u64_field(payload, "duration")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct TriggerMessageRequestV1_6 {
  pub requested_message: String,
  pub connector: Option<u16>,
}

impl TriggerMessageRequestV1_6 {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      requested_message: required_string_field(payload, "requestedMessage")?
        .to_string(),
      connector: optional_u16_field(payload, "connectorId")?,
    })
  }
}

#[derive(Debug, Clone)]
pub(in crate::simulator) struct TriggerMessageRequest_V2_X {
  pub requested_message: String,
  pub connector: Option<u16>,
}

impl TriggerMessageRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    Ok(Self {
      requested_message: required_string_field(payload, "requestedMessage")?
        .to_string(),
      connector: optional_nested_u16_field(payload, "evse", "id")?,
    })
  }
}

#[derive(Debug, Clone, Copy)]
pub(in crate::simulator) struct UpdateFirmwareRequest_V2_X {
  pub request_id: i64,
}

impl UpdateFirmwareRequest_V2_X {
  pub(in crate::simulator) fn parse(payload: &Value) -> Result<Self> {
    let _ = required_nested_string_field(payload, "firmware", "location")?;
    let _ =
      required_nested_string_field(payload, "firmware", "retrieveDateTime")?;
    Ok(Self {
      request_id: required_i64_field(payload, "requestId")?,
    })
  }
}

fn parse_operational_status(value: &str) -> Option<ConnectorStatus> {
  let normalized = normalize_identifier(value);
  match normalized.as_str() {
    "operative" => Some(ConnectorStatus::Available),
    "inoperative" => Some(ConnectorStatus::Unavailable),
    _ => None,
  }
}

fn required_nested_string_field<'a>(
  payload: &'a Value,
  object_field: &str,
  field: &str,
) -> Result<&'a str> {
  payload
    .get(object_field)
    .and_then(Value::as_object)
    .and_then(|object| object.get(field))
    .and_then(Value::as_str)
    .filter(|value| !value.is_empty())
    .ok_or_else(|| anyhow!("{object_field}.{field} is required."))
}

fn required_i64_any_field(payload: &Value, fields: &[&str]) -> Option<i64> {
  fields
    .iter()
    .find_map(|field| payload.get(field).and_then(Value::as_i64))
}

fn optional_string_field(
  payload: &Value,
  field: &str,
) -> Result<Option<String>> {
  let Some(value) = payload.get(field) else {
    return Ok(None);
  };
  value
    .as_str()
    .map(|item| Some(item.to_string()))
    .ok_or_else(|| anyhow!("{field} must be a string."))
}

fn optional_i64_field(payload: &Value, field: &str) -> Result<Option<i64>> {
  let Some(value) = payload.get(field) else {
    return Ok(None);
  };
  value
    .as_i64()
    .map(Some)
    .ok_or_else(|| anyhow!("{field} must be an integer."))
}

fn required_object_field(payload: &Value, field: &str) -> Result<Value> {
  let Some(value) = payload.get(field) else {
    return Err(anyhow!("{field} is required."));
  };
  if !value.is_object() {
    return Err(anyhow!("{field} must be an object."));
  }
  Ok(value.clone())
}

fn optional_object_field(
  payload: &Value,
  field: &str,
) -> Result<Option<Value>> {
  let Some(value) = payload.get(field) else {
    return Ok(None);
  };
  if !value.is_object() {
    return Err(anyhow!("{field} must be an object."));
  }
  Ok(Some(value.clone()))
}

fn optional_nested_u16_field(
  payload: &Value,
  object_field: &str,
  field: &str,
) -> Result<Option<u16>> {
  let Some(value) = payload.get(object_field) else {
    return Ok(None);
  };
  let object = value
    .as_object()
    .ok_or_else(|| anyhow!("{object_field} must be an object."))?;
  let Some(value) = object.get(field) else {
    return Ok(None);
  };
  let raw = value.as_u64().ok_or_else(|| {
    anyhow!("{object_field}.{field} must be an unsigned integer.")
  })?;
  let parsed = u16::try_from(raw).map_err(|_| {
    anyhow!("{object_field}.{field} is outside the supported connector range.")
  })?;
  Ok(Some(parsed))
}