Type-safe, transport-neutral OCPP 1.6J protocol and EV charging session toolkit for MoonBit.
moon add ggbond44439/mooncharge@0.1.0let frame = match
@mooncharge.parse_frame(
"[2,\"req-1\",\"StartTransaction\",{\"connectorId\":1,\"idTag\":\"RFID-7\",\"meterStart\":12000,\"timestamp\":\"2026-11-18T08:00:00Z\"}]",
) {
Ok(frame) => frame
Err(error) => fail(error.message())
}
let request = match frame {
@mooncharge.Call(_, _, payload) =>
match @mooncharge.StartTransactionRequest::from_json(payload) {
Ok(request) => request
Err(error) => fail(error.message())
}
_ => fail("expected CALL")
}
assert_eq(request.connector_id, 1)
assert_eq(request.meter_start, 12000)let available = match @mooncharge.ConnectorSession::new(1) {
Ok(value) => value
Err(_) => fail("invalid connector")
}
let preparing = match available.begin_transaction(
99,
"RFID-7",
12000,
"2026-11-18T08:00:00Z",
) {
Ok(value) => value
Err(_) => fail("transaction did not start")
}
let charging = match preparing.begin_charging() {
Ok(value) => value
Err(_) => fail("charging did not start")
}
let finishing = match charging.finish_transaction(
99,
21000,
"2026-11-18T09:00:00Z",
Some(@mooncharge.StopReason::Local),
) {
Ok(value) => value
Err(_) => fail("transaction did not stop")
}moon run cmd/main -- frame '[2,"req-1","Heartbeat",{}]'
# kind=call id=req-1 action=Heartbeat
moon run cmd/main -- action SetChargingProfile request
# central-system-to-charge-point
moon run cmd/main -- dispatch cp '[2,"auth-1","Authorize",{"idTag":"RFID-1"}]'
# typed=Authorize direction=charge-point-to-central-system
moon run cmd/main -- profile '<profile-json>' 120 42
# limit=22000 unit=W profile=8 capped=falsemoon run examples/frame_roundtrip
moon run examples/session_walkthrough
moon run examples/profile_evalmoon fmt --check
moon check --target all
moon test --target all
moon build
moon run examples/frame_roundtrip
moon run examples/session_walkthrough
moon run examples/profile_eval
moon package --listfn BootNotificationRequest::from_json(value : Json) -> Result[BootNotificationRequest, ProtocolError]fn BootNotificationRequest::not_equal(x : BootNotificationRequest, y : BootNotificationRequest) -> Boolpub(all) struct BootNotificationResponse {
status : RegistrationStatus
current_time : String
heartbeat_interval : Int
} derive(Eq, Debug)fn BootNotificationResponse::from_json(value : Json) -> Result[BootNotificationResponse, ProtocolError]fn BootNotificationResponse::not_equal(x : BootNotificationResponse, y : BootNotificationResponse) -> Boolpub(all) struct ChangeAvailabilityRequest {
connector_id : Int
kind : AvailabilityType
} derive(Eq, Debug)fn ChangeAvailabilityRequest::from_json(value : Json) -> Result[ChangeAvailabilityRequest, ProtocolError]fn ChangeAvailabilityRequest::not_equal(x : ChangeAvailabilityRequest, y : ChangeAvailabilityRequest) -> Boolfn ChangeAvailabilityResponse::equal(ChangeAvailabilityResponse, ChangeAvailabilityResponse) -> Boolfn ChangeAvailabilityResponse::from_json(value : Json) -> Result[ChangeAvailabilityResponse, ProtocolError]fn ChangeAvailabilityResponse::not_equal(x : ChangeAvailabilityResponse, y : ChangeAvailabilityResponse) -> Boolfn ChangeConfigurationRequest::equal(ChangeConfigurationRequest, ChangeConfigurationRequest) -> Boolfn ChangeConfigurationRequest::from_json(value : Json) -> Result[ChangeConfigurationRequest, ProtocolError]fn ChangeConfigurationRequest::not_equal(x : ChangeConfigurationRequest, y : ChangeConfigurationRequest) -> Boolfn ChangeConfigurationResponse::equal(ChangeConfigurationResponse, ChangeConfigurationResponse) -> Boolfn ChangeConfigurationResponse::from_json(value : Json) -> Result[ChangeConfigurationResponse, ProtocolError]fn ChangeConfigurationResponse::not_equal(x : ChangeConfigurationResponse, y : ChangeConfigurationResponse) -> Boolpub(all) struct ChargingDecision {
limit : Double
unit : ChargingRateUnit
profile_id : Int
capped_by_charge_point_max : Bool
} derive(Eq, Debug)pub(all) struct ChargingProfile {
charging_profile_id : Int
transaction_id : Int?
stack_level : Int
charging_profile_purpose : ChargingProfilePurpose
charging_profile_kind : ChargingProfileKind
recurrency_kind : String?
valid_from : String?
valid_to : String?
charging_schedule : ChargingSchedule
} derive(Eq, Debug)fn ChargingProfilePurpose::not_equal(x : ChargingProfilePurpose, y : ChargingProfilePurpose) -> Boolpub(all) struct ChargingSchedule {
start_schedule : String?
duration : Int?
min_charging_rate : Double?
charging_rate_unit : ChargingRateUnit
periods : Array[ChargingSchedulePeriod]
} derive(Eq, Debug)fn ChargingSchedulePeriod::not_equal(x : ChargingSchedulePeriod, y : ChargingSchedulePeriod) -> Boolpub(all) struct ChargingTransaction {
transaction_id : Int
id_tag : String
meter_start : Int
start_timestamp : String
meter_stop : Int?
stop_timestamp : String?
stop_reason : StopReason?
} derive(Eq, Debug)pub(all) struct ClearChargingProfileRequest {
id : Int?
connector_id : Int?
charging_profile_purpose : ChargingProfilePurpose?
stack_level : Int?
} derive(Eq, Debug)fn ClearChargingProfileRequest::equal(ClearChargingProfileRequest, ClearChargingProfileRequest) -> Boolfn ClearChargingProfileRequest::from_json(value : Json) -> Result[ClearChargingProfileRequest, ProtocolError]fn ClearChargingProfileRequest::not_equal(x : ClearChargingProfileRequest, y : ClearChargingProfileRequest) -> Boolpub(all) struct ClearChargingProfileResponse {
status : ClearChargingProfileStatus
} derive(Eq, Debug)fn ClearChargingProfileResponse::equal(ClearChargingProfileResponse, ClearChargingProfileResponse) -> Boolfn ClearChargingProfileResponse::from_json(value : Json) -> Result[ClearChargingProfileResponse, ProtocolError]fn ClearChargingProfileResponse::not_equal(x : ClearChargingProfileResponse, y : ClearChargingProfileResponse) -> Boolfn ClearChargingProfileStatus::equal(ClearChargingProfileStatus, ClearChargingProfileStatus) -> Boolfn ClearChargingProfileStatus::not_equal(x : ClearChargingProfileStatus, y : ClearChargingProfileStatus) -> Boolpub(all) struct ConnectorSession {
connector_id : Int
status : ChargePointStatus
transaction : ChargingTransaction?
} derive(Eq, Debug)fn ConnectorSession::apply_status(self : ConnectorSession, status : ChargePointStatus) -> ConnectorSessionfn ConnectorSession::begin_charging(self : ConnectorSession) -> Result[ConnectorSession, SessionError]fn ConnectorSession::begin_transaction(self : ConnectorSession, transaction_id : Int, id_tag : String, meter_start : Int, timestamp : String) -> Result[ConnectorSession, SessionError]fn ConnectorSession::finish_transaction(self : ConnectorSession, transaction_id : Int, meter_stop : Int, timestamp : String, reason : StopReason?) -> Result[ConnectorSession, SessionError]fn ConnectorSession::release_transaction(self : ConnectorSession, transaction_id : Int) -> Result[ConnectorSession, SessionError]pub(all) struct DataTransferResponse {
status : DataTransferStatus
data : String?
} derive(Eq, Debug)pub(all) enum DispatchError {
NotACall
InvalidAction(ProtocolError)
WrongDirection(MessageDirection, MessageDirection)
InvalidPayload(ProtocolError)
} derive(Eq, Debug)fn GetConfigurationRequest::from_json(value : Json) -> Result[GetConfigurationRequest, ProtocolError]fn GetConfigurationRequest::not_equal(x : GetConfigurationRequest, y : GetConfigurationRequest) -> Boolpub(all) struct GetConfigurationResponse {
configuration_key : Array[ConfigurationKey]
unknown_key : Array[String]
} derive(Eq, Debug)fn GetConfigurationResponse::from_json(value : Json) -> Result[GetConfigurationResponse, ProtocolError]fn GetConfigurationResponse::not_equal(x : GetConfigurationResponse, y : GetConfigurationResponse) -> Boolpub(all) struct IdTagInfo {
status : AuthorizationStatus
expiry_date : String?
parent_id_tag : String?
} derive(Eq, Debug)pub(all) struct MeterValue {
timestamp : String
sampled_value : Array[SampledValue]
} derive(Eq, Debug)pub(all) struct MeterValuesRequest {
connector_id : Int
transaction_id : Int?
meter_value : Array[MeterValue]
} derive(Eq, Debug)pub(all) enum OcppAction {
Authorize
BootNotification
DataTransfer
DiagnosticsStatusNotification
FirmwareStatusNotification
Heartbeat
MeterValues
StartTransaction
StatusNotification
StopTransaction
CancelReservation
ChangeAvailability
ChangeConfiguration
ClearCache
GetConfiguration
RemoteStartTransaction
RemoteStopTransaction
Reset
SetChargingProfile
UnlockConnector
} derive(Eq, Debug)impl Show for ProtocolErrorpub(all) struct RemoteStartTransactionRequest {
id_tag : String
connector_id : Int?
charging_profile : ChargingProfile?
} derive(Eq, Debug)fn RemoteStartTransactionRequest::equal(RemoteStartTransactionRequest, RemoteStartTransactionRequest) -> Boolfn RemoteStartTransactionRequest::from_json(value : Json) -> Result[RemoteStartTransactionRequest, ProtocolError]fn RemoteStartTransactionRequest::not_equal(x : RemoteStartTransactionRequest, y : RemoteStartTransactionRequest) -> Boolfn RemoteStartTransactionResponse::from_json(value : Json) -> Result[RemoteStartTransactionResponse, ProtocolError]fn RemoteStartTransactionResponse::not_equal(x : RemoteStartTransactionResponse, y : RemoteStartTransactionResponse) -> Boolfn RemoteStopTransactionRequest::equal(RemoteStopTransactionRequest, RemoteStopTransactionRequest) -> Boolfn RemoteStopTransactionRequest::from_json(value : Json) -> Result[RemoteStopTransactionRequest, ProtocolError]fn RemoteStopTransactionRequest::not_equal(x : RemoteStopTransactionRequest, y : RemoteStopTransactionRequest) -> Boolfn RemoteStopTransactionResponse::equal(RemoteStopTransactionResponse, RemoteStopTransactionResponse) -> Boolfn RemoteStopTransactionResponse::from_json(value : Json) -> Result[RemoteStopTransactionResponse, ProtocolError]fn RemoteStopTransactionResponse::not_equal(x : RemoteStopTransactionResponse, y : RemoteStopTransactionResponse) -> Boolpub(all) struct RpcError {
code : RpcErrorCode
description : String
details : Json
} derive(Eq, Debug)pub(all) struct SetChargingProfileRequest {
connector_id : Int
cs_charging_profiles : ChargingProfile
} derive(Eq, Debug)fn SetChargingProfileRequest::from_json(value : Json) -> Result[SetChargingProfileRequest, ProtocolError]fn SetChargingProfileRequest::not_equal(x : SetChargingProfileRequest, y : SetChargingProfileRequest) -> Boolfn SetChargingProfileResponse::equal(SetChargingProfileResponse, SetChargingProfileResponse) -> Boolfn SetChargingProfileResponse::from_json(value : Json) -> Result[SetChargingProfileResponse, ProtocolError]fn SetChargingProfileResponse::not_equal(x : SetChargingProfileResponse, y : SetChargingProfileResponse) -> Boolfn StartTransactionRequest::from_json(value : Json) -> Result[StartTransactionRequest, ProtocolError]fn StartTransactionRequest::not_equal(x : StartTransactionRequest, y : StartTransactionRequest) -> Boolfn StartTransactionResponse::from_json(value : Json) -> Result[StartTransactionResponse, ProtocolError]fn StartTransactionResponse::not_equal(x : StartTransactionResponse, y : StartTransactionResponse) -> Boolpub(all) struct StatusNotificationRequest {
connector_id : Int
error_code : ChargePointErrorCode
status : ChargePointStatus
timestamp : String?
info : String?
vendor_id : String?
vendor_error_code : String?
} derive(Eq, Debug)fn StatusNotificationRequest::from_json(value : Json) -> Result[StatusNotificationRequest, ProtocolError]fn StatusNotificationRequest::not_equal(x : StatusNotificationRequest, y : StatusNotificationRequest) -> Boolpub(all) struct StopTransactionRequest {
transaction_id : Int
meter_stop : Int
timestamp : String
reason : StopReason?
id_tag : String?
} derive(Eq, Debug)fn StopTransactionRequest::not_equal(x : StopTransactionRequest, y : StopTransactionRequest) -> Boolpub(all) enum TypedCall {
Authorize(AuthorizeRequest)
BootNotification(BootNotificationRequest)
DataTransfer(DataTransferRequest)
StatusNotification(StatusNotificationRequest)
StartTransaction(StartTransactionRequest)
StopTransaction(StopTransactionRequest)
MeterValues(MeterValuesRequest)
RemoteStartTransaction(RemoteStartTransactionRequest)
RemoteStopTransaction(RemoteStopTransactionRequest)
Reset(ResetRequest)
ChangeAvailability(ChangeAvailabilityRequest)
ChangeConfiguration(ChangeConfigurationRequest)
GetConfiguration(GetConfigurationRequest)
SetChargingProfile(SetChargingProfileRequest)
ClearChargingProfile(ClearChargingProfileRequest)
UnsupportedAction(String, Json)
} derive(Eq, Debug)fn evaluate_charging_profiles(profiles : Array[ChargingProfile], transaction_id : Int?, elapsed_seconds : Int) -> Result[ChargingDecision, ChargingProfileError]Install
Download zipType-safe, transport-neutral OCPP 1.6J protocol and EV charging session toolkit for MoonBit.