ocpp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 1, 2020 License: GPL-3.0 Imports: 2 Imported by: 0

README

go-ocpp

OCPP(1.5/1.6) implementation in Golang.

  • v1.5, it's assumed it is SOAP
  • v1.6, it's assumed it is JSON

Usage

Central System

Just pass a handler that takes in a cpreq.ChargePointRequest and returns a (cpresp.ChargePointResponse, error).

In SOAP, error messages will be sent back via Fault, as specified in OCPP v1.5

In Websockets, error messages will be sent back as specified in OCPP-J v1.6

csys := cs.New()
go csys.Run(":12811", func(req cpreq.ChargePointRequest, cpID string) (cpresp.ChargePointResponse, error) {
    // Return an error to the Station communicating to the Central System
    //
    // station, isAuthorized := getStation(cpID)
    // if err != nil {
    //   return nil, errors.New("charger not authorized to join network")
    // }

    switch req := req.(type) {
    case *cpreq.BootNotification:
        // accept chargepoint in the network
        return &cpresp.BootNotification{
            Status:      "Accepted",
            CurrentTime: time.Now(),
            Interval:    60,
        }, nil

    case *cpreq.Heartbeat:
        return &cpresp.Heartbeat{CurrentTime: time.Now()}, nil

    case *cpreq.StatusNotification:
        if req.Status != "Available" {
            // chargepoint is unavailable
        }
        return &cpresp.StatusNotification{}, nil

    default:
        return nil, errors.New("Response not supported")
    }
}
Charge Point

Pass the required parameters to the constructor function, and then just send any request(cpreq.*).

TODO: assertion of the response type should be done inside the .Send?

stationID := "id01"
centralSystemURL := "ws://localhost:12811"
st, err := cp.NewChargePoint(stationID, centralSystemURL, ocpp.V16, ocpp.JSON) // or ocpp.SOAP
if err != nil {
    fmt.Println("could not create charge point:", err)
    return
}
rawResp, err := st.Send(&cpreq.Heartbeat{})
if err != nil {
    fmt.Println("could't send heartbeat:", err)
    return
}
resp, ok := rawResp.(*cpresp.Heartbeat)
if !ok {
    fmt.Println("response is not a heartbeat response")
    return
}
fmt.Println("got reply:", resp)
Logs

For more useful logging, do:

ocpp.SetDebugLogger(log.New(os.Stdout, "DEBUG:", log.Ltime))
ocpp.SetErrorLogger(log.New(os.Stderr, "ERROR:", log.Ltime))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetDebugLogger added in v1.0.0

func SetDebugLogger(logger log.Logger)

func SetErrorLogger added in v1.0.0

func SetErrorLogger(logger log.Logger)

Types

type MessageHandler added in v1.0.0

type MessageHandler func(request messages.Request, cpID string) (messages.Response, error)

type Transport added in v1.0.0

type Transport string
const (
	SOAP Transport = "soap"
	JSON Transport = "json"
)

type Version added in v1.0.0

type Version string
const (
	V15 Version = "1.5"
	V16 Version = "1.6"
)

Directories

Path Synopsis
example
cp
cs
internal
log
req
res

Jump to

Keyboard shortcuts

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