use ocpp_types::v16::{AuthorizeRequest, HeartbeatResponse, IdTag, RpcErrorCode};
use ocpp_types::{Call, CallError, CallResult, EmptyPayload, MessageId};
fn main() {
let call = Call {
message_id: MessageId::try_from("19223201").unwrap(),
payload: AuthorizeRequest {
id_tag: IdTag::try_from("ABC123").unwrap(),
},
};
let mut buf = [0u8; 256];
let len = serde_json_core::to_slice(&call, &mut buf).unwrap();
let json = core::str::from_utf8(&buf[..len]).unwrap();
println!("CALL: {json}");
let (parsed, _): (Call<AuthorizeRequest>, usize) =
serde_json_core::from_slice(&buf[..len]).unwrap();
assert_eq!(parsed, call);
#[cfg(not(feature = "alloc"))]
let current_time = heapless::String::try_from("2024-01-01T00:00:00Z").unwrap();
#[cfg(feature = "alloc")]
let current_time = String::from("2024-01-01T00:00:00Z");
let result: CallResult<HeartbeatResponse> = CallResult {
message_id: MessageId::try_from("19223201").unwrap(),
payload: HeartbeatResponse { current_time },
};
let mut buf = [0u8; 256];
let len = serde_json_core::to_slice(&result, &mut buf).unwrap();
println!("CALLRESULT: {}", core::str::from_utf8(&buf[..len]).unwrap());
let error: CallError<RpcErrorCode> = CallError {
message_id: MessageId::try_from("19223201").unwrap(),
error_code: RpcErrorCode::NotImplemented,
error_description: heapless::String::try_from("unrecognized action").unwrap(),
error_details: EmptyPayload,
};
let mut buf = [0u8; 256];
let len = serde_json_core::to_slice(&error, &mut buf).unwrap();
println!("CALLERROR: {}", core::str::from_utf8(&buf[..len]).unwrap());
let wrong_type: Result<(Call<AuthorizeRequest>, usize), _> =
serde_json_core::from_str(r#"[3,"1","Authorize",{"idTag":"ABC123"}]"#);
println!(
"a CALLRESULT-shaped frame parsed as Call<T> is rejected: {}",
wrong_type.is_err()
);
}