Skip to main content

Crate ocpp_rs

Crate ocpp_rs 

Source
Expand description

§OCPP-RS

OCPP-RS is a Rust library for implementing the Open Charge Point Protocol (OCPP) in Rust.
It supports OCPP 1.6 and OCPP 2.1.

§Usage

In Cargo.toml, add the following dependency:

[dependencies]
ocpp-rs = "^0.4"

§Particularities

§OCPP 1.6

CALLRESULT has no action on the wire. Blind parse yields v16::call_result::CallResultRaw. Correlate with v16::pending::PendingCalls (or action-name store / v16::pending::resolve_with_action_name). Do not guess from JSON shape. Datetime: parse always accepts RFC3339; serialize defaults to %Y-%m-%dT%H:%M:%S%.3fZ (enable datetime_serialize_rfc3339 to emit RFC3339 millis — see datetime). Migration from 0.2.x: guides/migration-0.4.md.

§OCPP 2.1

Same CallResult model under v21::pending. Framing includes types 2–6 (CALL, CALLRESULT, CALLERROR, CALLRESULTERROR, SEND). Datetime helpers share datetime with v16 (RFC3339 parse; %.3fZ serialize by default).

§Example (1.6 CALL)

use ocpp_rs::v16::parse::{self, Message};
use ocpp_rs::v16::call::Action;

let incoming_text = "[2, \"19223201\", \"BootNotification\", { \"chargePointVendor\": \"VendorX\", \"chargePointModel\": \"SingleSocketCharger\" }]";
let incoming_message = parse::deserialize_to_message(incoming_text);
if let Ok(Message::Call(call)) = incoming_message {
    match call.payload {
        Action::BootNotification(_boot_notification) => {}
        _ => {}
    }
}

§Example (1.6 CallResult with PendingCalls)

use ocpp_rs::v16::call::{Action, Call, Heartbeat};
use ocpp_rs::v16::parse::TypedMessage;
use ocpp_rs::v16::pending::PendingCalls;
use ocpp_rs::v16::typed_call_result::TypedCallResult;

let mut pending = PendingCalls::new();
let _ = pending
    .send_call(Call::new("1".to_string(), Action::Heartbeat(Heartbeat {})))
    .expect("serialize");
let typed = pending
    .deserialize_typed(r#"[3, "1", {"currentTime": "2024-01-01T00:00:00.000Z"}]"#)
    .expect("typed");
assert!(matches!(
    typed,
    TypedMessage::CallResult(TypedCallResult::Heartbeat(_))
));

§Example (2.1)

use ocpp_rs::v21::call::{Action, Call};
use ocpp_rs::v21::messages::heartbeat::HeartbeatRequest;
use ocpp_rs::v21::parse::TypedMessage;
use ocpp_rs::v21::pending::PendingCalls;
use ocpp_rs::v21::typed_call_result::TypedCallResult;

let mut pending = PendingCalls::new();
let call = Call::new(
    "1".to_string(),
    Action::Heartbeat(HeartbeatRequest { custom_data: None }),
);
let _outgoing = pending.send_call(call).expect("serialize");
let typed = pending
    .deserialize_typed(r#"[3, "1", {"currentTime": "2024-01-01T00:00:00.000Z"}]"#)
    .expect("typed result");
assert!(matches!(
    typed,
    TypedMessage::CallResult(TypedCallResult::Heartbeat(_))
));

Modules§

datetime
Shared datetime wire helpers for OCPP 1.6 and 2.1.
errors
lenient_str_enum
Lenient stringly OCPP enums: unknown wire values become Unknown(String) / Unrecognized(String) instead of failing deserialize.
v16
OCPP 1.6 protocol types and OCPP-J framing.
v21
OCPP 2.1 protocol types and OCPP-J framing.
validate
Schema constraint validation (feature schema_validate).

Macros§

lenient_str_enum
Define a string enum that preserves non-standard wire values.