https://github.com/Namoshek/OpenChargeMeteringFormat
A .NET implementation of the Open Charge Metering Format (OCMF).
https://github.com/Namoshek/OpenChargeMeteringFormat
csharp dotnet ocmf
Last synced: 10 months ago
JSON representation
A .NET implementation of the Open Charge Metering Format (OCMF).
- Host: GitHub
- URL: https://github.com/Namoshek/OpenChargeMeteringFormat
- Owner: Namoshek
- License: mit
- Archived: true
- Created: 2022-06-10T14:39:03.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2025-01-20T05:07:55.000Z (over 1 year ago)
- Last Synced: 2025-08-01T02:25:50.079Z (11 months ago)
- Topics: csharp, dotnet, ocmf
- Language: C#
- Homepage:
- Size: 136 KB
- Stars: 4
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-ev-charging - Namoshek/OpenChargeMeteringFormat
README
# OpenChargeMeteringFormat [](https://nuget.org/packages/OpenChargeMeteringFormat)
[](https://sonarcloud.io/summary/new_code?id=Namoshek_OpenChargeMeteringFormat)
[](https://sonarcloud.io/summary/new_code?id=Namoshek_OpenChargeMeteringFormat)
[](https://sonarcloud.io/summary/new_code?id=Namoshek_OpenChargeMeteringFormat)
[](https://sonarcloud.io/summary/new_code?id=Namoshek_OpenChargeMeteringFormat)
[](https://sonarcloud.io/summary/new_code?id=Namoshek_OpenChargeMeteringFormat)
[](https://sonarcloud.io/summary/new_code?id=Namoshek_OpenChargeMeteringFormat)
[`OpenChargeMeteringFormat`](https://www.nuget.org/packages/OpenChargeMeteringFormat/) was created by, and is maintained
by [Marvin Mall](https://github.com/namoshek).
It provides a .NET parser and verifier for the [Open Charge Metering Format (OCMF)](https://github.com/SAFE-eV/OCMF-Open-Charge-Metering-Format).
## Installation
The package can be found on [nuget.org](https://www.nuget.org/packages/OpenChargeMeteringFormat/).
You can install the package with:
```pwsh
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:
```csharp
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())
{
Console.WriteLine("The given string is not a valid OCMF message according to the specification.");
}
else if (parseResult.HasError())
{
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.
```csharp
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`](https://github.com/altmann/FluentResults),
which allows returning a `Result` covering both,
the success and the error scenario.
To check for success, use `Result.IsSuccess`.
A success result will also contain a valid `Result.Value`
with the parsed OCMF message as content.
To check for failure, use `Result.IsFailed`.
In case of failure, the result _may_ contains errors which can be retrieved using
`Result.Errors`. To check for presence of errors,
use `Result.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](LICENSE).