ocmf_go

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 17, 2024 License: MIT Imports: 12 Imported by: 0

README

OCMF-go

OCMF-go is an implementation of the Open Charge Metering Format (OCMF) in Go. It provides a simple library for generating and parsing OCMF messages. The provided message builder generates OCMF-compatible messages and signs the data with the provided private key, desired algorithm and encoding, so you don't have to.

Installation

go get github.com/ChargePi/ocmf-go

Usage

package main

import (
	"fmt"

	ocmf_go "github.com/ChargePi/ocmf-go"
	"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
)

func main() {
	// Generate a new message builder
	builder := ocmf_go.NewBuilder()

	// Set the signature algorithm
	builder.SetSignatureAlgorithm(ocmf_go.SignatureAlgorithmECDSAsecp256r1Sha256)

	// Set the signature encoding
	builder.SetSignatureEncoding(ocmf_go.SignatureEncodingBase64)

	// ... set the desired fields
	message, err := builder.Build()
	if err != nil {
		fmt.Println(err)
	}

	// Create a MeterValue message with the generated message as value 
	meterValueExample := types.MeterValue{
		SampledValue: []types.SampledValue{
			{
				Value:  message,
				Format: types.ValueFormatSignedData,
			},
		},
	}

	// Send the message via OCPP 1.5/1.6.
}

Contributing

Contributions are welcome! Please check out the contributing guide for more information.

License

OCMF-go is licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	MeterNotPresent   = MeterError("N")
	MeterOk           = MeterError("G")
	MeterTimeout      = MeterError("T")
	MeterDisconnected = MeterError("D")
	MeterRemoved      = MeterError("R")
	MeterManipulated  = MeterError("M")
	MeterExchanged    = MeterError("X")
	MeterIncompatible = MeterError("I")
	MeterOutOfRange   = MeterError("O")
	MeterSubstitute   = MeterError("S")
	MeterOtherError   = MeterError("E")
	MeterReadError    = MeterError("F")
)
View Source
const (
	UserAssignmentStateNONE      = UserAssignmentState("NONE")
	UserAssignmentStateHearsay   = UserAssignmentState("HEARSAY")
	UserAssignmentStateTrusted   = UserAssignmentState("TRUSTED")
	UserAssignmentStateVerified  = UserAssignmentState("VERIFIED")
	UserAssignmentStateCertified = UserAssignmentState("CERTIFIED")
	UserAssignmentStateSecure    = UserAssignmentState("SECURE")
	UserAssignmentStateMismatch  = UserAssignmentState("MISMATCH")
	UserAssignmentStateInvalid   = UserAssignmentState("INVALID")
	UserAssignmentStateOutdated  = UserAssignmentState("OUTDATED")
	UserAssignmentStateUnknown   = UserAssignmentState("UNKNOWN")
)
View Source
const (
	ISO15118None          = ISO15118State("ISO15118_NONE")
	ISO15118PlugAndCharge = ISO15118State("ISO15118_PNC")
)
View Source
const (
	OcppNone               = OcppState("OCPP_NONE")
	OcppRemoteStart        = OcppState("OCPP_RS")
	OcppAuthorizeMethod    = OcppState("OCPP_AUTH")
	OcppRemoteStartTLS     = OcppState("OCPP_RS_TLS")
	OcppAuthorizeMethodTLS = OcppState("OCPP_AUTH_TLS")
	OcppCache              = OcppState("OCPP_CACHE")
	OcppWhiteList          = OcppState("OCPP_WHITELIST")
	OcppCertified          = OcppState("OCPP_CERTIFIED")
)
View Source
const (
	RfidNone         = RfidState("RFID_NONE")
	RfidPlain        = RfidState("RFID_PLAIN")
	RfidRelated      = RfidState("RFID_RELATED")
	RfidPreSharedKey = RfidState("RFID_PSK")
)
View Source
const (
	ChargePointAssignmentTypeEVSEID = ChargePointAssignmentType("EVSE_ID")
	ChargePointAssignmentTypeCBIDC  = ChargePointAssignmentType("CBIDC")
)
View Source
const (
	TimeStatusUnknown      = TimeStatus("U")
	TimeStatusInformative  = TimeStatus("I")
	TimeStatusSynchronized = TimeStatus("S")
	TimeStatusRelative     = TimeStatus("R")
)
View Source
const (
	UnitsWh       = Units("Wh")
	UnitskWh      = Units("kWh")
	UnitsMilliOhm = Units("mOhm")
	UnitsMicroOhm = Units("uOhm")
)
View Source
const (
	CurrentTypeAC = CurrentType("AC")
	CurrentTypeDC = CurrentType("DC")
)
View Source
const (
	SignatureEncodingBase64 = SignatureEncoding("base64")
	SignatureEncodingHex    = SignatureEncoding("hex")
)
View Source
const (
	SignatureAlgorithmECDSAsecp192k1SHA256       = SignatureAlgorithm("ECDSA-secp192k1-SHA256")
	SignatureAlgorithmECDSAsecp256k1SHA256       = SignatureAlgorithm("ECDSA-secp256k1-SHA256")
	SignatureAlgorithmECDSAsecp384r1SHA256       = SignatureAlgorithm("ECDSA-secp384r1-SHA256")
	SignatureAlgorithmECDSAbrainpool256r11SHA256 = SignatureAlgorithm("ECDSA-brainpool256r1-SHA256")
	SignatureAlgorithmECDSAsecp256r1SHA256       = SignatureAlgorithm("ECDSA-secp256r1-SHA256")
	SignatureAlgorithmECDSAsecp192r1SHA256       = SignatureAlgorithm("ECDSA-secp192r1-SHA256")
)
View Source
const OcmfVersion = "0.4"
View Source
const (
	SignatureMimeTypeDer = SignatureMimeType("application/x-der")
)

Variables

View Source
var (
	ErrInvalidFormat       = errors.New("invalid OCMF message format")
	ErrVerificationFailure = errors.New("verification failed")
	ErrPayloadEmpty        = errors.New("payload is empty")
)

Functions

This section is empty.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

func NewBuilder

func NewBuilder(privateKey *ecdsa.PrivateKey, opts ...BuilderOption) *Builder

func (*Builder) AddIdentificationFlag

func (b *Builder) AddIdentificationFlag(flag string) *Builder

func (*Builder) AddLossCompensation

func (b *Builder) AddLossCompensation(lossCompensation LossCompensation) *Builder

func (*Builder) AddReading

func (b *Builder) AddReading(reading Reading) *Builder

func (*Builder) Build

func (b *Builder) Build() (*string, error)

func (*Builder) ClearPayloadSection

func (b *Builder) ClearPayloadSection()

func (*Builder) WithChargeControllerVersion

func (b *Builder) WithChargeControllerVersion(version string) *Builder

func (*Builder) WithChargePointIdentification

func (b *Builder) WithChargePointIdentification(serial string) *Builder

func (*Builder) WithChargePointIdentificationType

func (b *Builder) WithChargePointIdentificationType(serial string) *Builder

func (*Builder) WithGatewayID

func (b *Builder) WithGatewayID(gatewayID string) *Builder

func (*Builder) WithGatewaySerial

func (b *Builder) WithGatewaySerial(gatewaySerial string) *Builder

func (*Builder) WithGatewayVersion

func (b *Builder) WithGatewayVersion(gatewayVersion string) *Builder

func (*Builder) WithIdentificationData

func (b *Builder) WithIdentificationData(data string) *Builder

func (*Builder) WithIdentificationLevel

func (b *Builder) WithIdentificationLevel(level string) *Builder

func (*Builder) WithIdentificationStatus

func (b *Builder) WithIdentificationStatus(status bool) *Builder

func (*Builder) WithIdentificationType

func (b *Builder) WithIdentificationType(idType string) *Builder

func (*Builder) WithMeterFirmware added in v0.2.1

func (b *Builder) WithMeterFirmware(firmware string) *Builder

func (*Builder) WithMeterModel added in v0.2.1

func (b *Builder) WithMeterModel(model string) *Builder

func (*Builder) WithMeterSerial

func (b *Builder) WithMeterSerial(serial string) *Builder

func (*Builder) WithMeterVendor added in v0.2.1

func (b *Builder) WithMeterVendor(vendor string) *Builder

func (*Builder) WithPagination

func (b *Builder) WithPagination(pagination string) *Builder

func (*Builder) WithTariffText

func (b *Builder) WithTariffText(text string) *Builder

type BuilderOption

type BuilderOption func(*Builder)

func WithSignature

func WithSignature(signature Signature) BuilderOption

func WithSignatureAlgorithm

func WithSignatureAlgorithm(algorithm SignatureAlgorithm) BuilderOption

func WithSignatureEncoding

func WithSignatureEncoding(encoding SignatureEncoding) BuilderOption

type ChargePointAssignmentType

type ChargePointAssignmentType string

type CurrentType

type CurrentType string

type ISO15118State

type ISO15118State string

type LossCompensation

type LossCompensation struct {
	Naming              string  `json:"LN"`
	Identification      int     `json:"LI"`
	CableResistance     float64 `json:"LR"`
	CableResistanceUnit string  `json:"LU"`
}

type MeterError

type MeterError string

type OcppState

type OcppState string

type Opt added in v0.2.0

type Opt func(*ParserOpts)

func WithAutomaticSignatureVerification added in v0.2.0

func WithAutomaticSignatureVerification(publicKey *ecdsa.PublicKey) Opt

func WithAutomaticValidation added in v0.2.0

func WithAutomaticValidation() Opt

type Parser added in v0.2.0

type Parser struct {
	// contains filtered or unexported fields
}

func NewParser added in v0.2.0

func NewParser(opts ...Opt) *Parser

func (*Parser) GetPayload added in v0.2.0

func (p *Parser) GetPayload() (*PayloadSection, error)

func (*Parser) GetSignature added in v0.2.0

func (p *Parser) GetSignature() (*Signature, error)

func (*Parser) ParseOcmfMessageFromString added in v0.2.0

func (p *Parser) ParseOcmfMessageFromString(data string) *Parser

ParseOcmfMessageFromString Returns a new Parser instance with the payload and signature fields set

type ParserOpts added in v0.2.0

type ParserOpts struct {
	// contains filtered or unexported fields
}

type PayloadSection

type PayloadSection struct {
	// General information
	FormatVersion  string `json:"FV,omitempty"`
	GatewayID      string `json:"GI,omitempty"`
	GatewaySerial  string `json:"GS,omitempty"`
	GatewayVersion string `json:"GV,omitempty"`
	// Pagination
	Pagination string `json:"PG" validate:"required"`
	// Meter identification
	MeterVendor   string `json:"MV,omitempty"`
	MeterModel    string `json:"MM,omitempty"`
	MeterSerial   string `json:"MS" validate:"required"`
	MeterFirmware string `json:"MF,omitempty"`
	// User assignment
	IdentificationStatus bool     `json:"IS"`
	IdentificationLevel  string   `json:"IL,omitempty" validate:"omitempty,userAssignmentState"`
	IdentificationFlags  []string `json:"IF" validate:"omitempty,max=4"`
	IdentificationType   string   `json:"IT" validate:"required,rfidState"`
	IdentificationData   string   `json:"ID,omitempty" validate:"omitempty,hexadecimal"`
	TariffText           string   `json:"TT,omitempty" validate:"omitempty,max=250"`
	// EVSE metrologic parameters
	LossCompensation LossCompensation `json:"LC,omitempty"`
	// Assignment of the charge point
	ChargeControllerVersion       string `json:"CF,omitempty" validate:"omitempty,max=25"`
	ChargePointIdentificationType string `json:"CT,omitempty" validate:"omitempty,chargePointAssignment"`
	ChargePointIdentification     string `json:"CI,omitempty"`
	// Readings
	Readings []Reading `json:"RD" validate:"required,dive"`
}

func (*PayloadSection) Validate

func (p *PayloadSection) Validate() error

type Reading

type Reading struct {
	Time              string  `json:"TM" validate:"required,iso8601"`
	Transaction       string  `json:"TX,omitempty" validate:"omitempty,oneof=B C X E L R A P S T"`
	ReadingValue      float64 `json:"RV" validate:"required"`
	ReadingIdentifier string  `json:"RI,omitempty"`
	ReadingUnit       string  `json:"RU" validate:"required,unit"`
	ReadingType       string  `json:"RT,omitempty" validate:"omitempty,currentType"`
	CumulatedLoss     float64 `json:"CL,omitempty"`
	ErrorFlags        string  `json:"EF,omitempty" validate:"omitempty,oneof=E t"`
	Status            string  `json:"ST" validate:"required,meterError"`
}

func (*Reading) Validate

func (r *Reading) Validate() error

type RfidState

type RfidState string

type Signature

type Signature struct {
	Algorithm SignatureAlgorithm `json:"SA" validate:"required,signatureAlgorithm"`
	Encoding  SignatureEncoding  `json:"SE,omitempty" validate:"required,signatureEncoding"`
	MimeType  SignatureMimeType  `json:"SM,omitempty" validate:"required,oneof=application/x-der"`
	Data      string             `json:"SD" validate:"required"`
}

func NewDefaultSignature

func NewDefaultSignature() *Signature

func (*Signature) Sign

func (s *Signature) Sign(payload PayloadSection, privateKey *ecdsa.PrivateKey) error

func (*Signature) Validate

func (s *Signature) Validate() error

func (*Signature) Verify

func (s *Signature) Verify(payload PayloadSection, publicKey *ecdsa.PublicKey) (bool, error)

type SignatureAlgorithm

type SignatureAlgorithm string

type SignatureEncoding

type SignatureEncoding string

type SignatureMimeType

type SignatureMimeType string

type TimeStatus

type TimeStatus string

type Units

type Units string

type UserAssignmentState

type UserAssignmentState string

type UserAssignmentType

type UserAssignmentType string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL