Skip to content

A .NET implementation of the Open Charge Metering Format (OCMF).

License

Notifications You must be signed in to change notification settings

Namoshek/OpenChargeMeteringFormat

Repository files navigation

OpenChargeMeteringFormat Nuget

Coverage Quality Gate Status Maintainability Rating Reliability Rating Security Rating Vulnerabilities

OpenChargeMeteringFormat was created by, and is maintained by Marvin Mall. It provides a .NET parser and verifier for the Open Charge Metering Format (OCMF).

Installation

The package can be found on nuget.org. You can install the package with:

Install-Package OpenChargeMeteringFormat

Usage

Parse OCMF

To parse an OCMF message, use the OpenChargeMeteringFormatParser. It provides you with two methods to validate and parse OCMF messages:

var message = "OCMF|{...}|{...}";

// Optional: validate messages before usage
if (!OpenChargeMeteringFormatParser.IsValidMessage(message))
{
    Console.WriteLine("Not a valid OCMF message.");

    return;
}

// Mandatory: parse messages with implicit validation
var parseResult = OpenChargeMeteringFormatParser.ParseMessage(message);
if (parseResult.IsFailed)
{
    if (parseResult.HasError<InputIsNotAnOpenChargeMeteringFormatMessage>())
    {
        Console.WriteLine("The given string is not a valid OCMF message according to the specification.");
    }
    else if (parseResult.HasError<PayloadHasAnInvalidFormat>())
    {
        Console.WriteLine("The given OCMF message contains an invalid payload.");
    }
    else
    {
        Console.WriteLine($"Not a valid OCMF message: {parseResult.Errors.First().Message}");
    }

    return;
}

Console.WriteLine($"Identification status: {parseResult.Value.Payload.IdentificationStatus}");
Console.WriteLine($"Identification type: {parseResult.Value.Payload.IdentificationType}");
Console.WriteLine($"Identification data: {parseResult.Value.Payload.IdentificationData}");

IsValidMessage(message) will only perform structural tests, no actual verification of the signature.

Verify OCMF signature

To verify the signature of a parsed OCMF message, use the OpenChargeMeteringFormatVerifier. It provides a Verify(OpenChargeMeteringFormatMessage message, string publicKey) method, where you have to pass the output of OpenChargeMeteringFormatParser.ParseMessage(message) as well as the public key of the charge point meter.

var message = "OCMF|{...}|{...}";
var publicKey = "A0B1C2...";

var parseResult = OpenChargeMeteringFormatParser.ParseMessage(message);
if (parseResult.IsFailed)
{
    return;
}

var verificationResult = OpenChargeMeteringFormatVerifier.Verify(parseResult.Value, publicKey);
if (verificationResult.IsFailed)
{
    Console.WriteLine("The OCMF message has an invalid signature or the provided public key is invalid.");
}

Method results

This library makes use of FluentResults, which allows returning a Result<OpenChargeMeteringFormatMessage> covering both, the success and the error scenario.

To check for success, use Result<OpenChargeMeteringFormatMessage>.IsSuccess. A success result will also contain a valid Result<OpenChargeMeteringFormatMessage>.Value with the parsed OCMF message as content.

To check for failure, use Result<OpenChargeMeteringFormatMessage>.IsFailed. In case of failure, the result may contains errors which can be retrieved using Result<OpenChargeMeteringFormatMessage>.Errors. To check for presence of errors, use Result<OpenChargeMeteringFormatMessage>.Errors.Any().

IsSuccess and IsFailed are mutually exclusive, i.e. when one is true, the other is false.

License

This library is open-sourced software licensed under the MIT license.

About

A .NET implementation of the Open Charge Metering Format (OCMF).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages