ocpp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2022 License: MIT Imports: 9 Imported by: 0

README

ocpp

MIT License

Golang package implementing the JSON version of the Open Charge Point Protocol (OCPP). Currently OCPP 1.6 is supported. The project is inspired by mobility/ocpp

Installation

Go version 1.18+ is required

  go get github.com/aliml92/ocpp

Usage

Cental System
package main

import (
	"fmt"
	"log"
	"net/http"
	"strings"
	"time"

	"github.com/aliml92/ocpp"
	"github.com/aliml92/ocpp/v16"
	"github.com/gorilla/websocket"
)


var upgrader = websocket.Upgrader{
	Subprotocols: []string{"ocpp1.6"},
}
var cp *ocpp.ChargePoint


func main(){
	http.HandleFunc("/", wsHandler)
	http.ListenAndServe("localhost:8080", nil)
}

func wsHandler(w http.ResponseWriter, r *http.Request) {
	c, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Print("upgrade:", err)
		return
	}
	subProtocol := r.Header.Get("Sec-WebSocket-Protocol")	
	if subProtocol== "" {
		fmt.Println("Client hasn't requested any Subprotocol. Closing Connection")
		c.Close()
	}
	if subProtocol != "ocpp1.6" {
		fmt.Println("Client has requested an unsupported Subprotocol. Closing Connection")
		c.Close()
	}
	chargePointId := strings.Split(r.URL.Path, "/")[3]

	
	// create a ChargePoint
	cp = ocpp.NewChargePoint(c, chargePointId)
	

	// register handlers for CP initiated calls
	cp.On("BootNotification", BootNotificationHandler)

	// make CS initiated calls
	var req ocpp.Payload = &v16.ChangeAvailabilityReq{
		ConnectorId: 1,
		Type: "Operative",
	}
	res, err := cp.Call("ChangeAvailability", req)
	if err != nil {
		fmt.Printf("error calling: %v", err)
		return
	}
	fmt.Printf("ChangeAvailabilityRes: %v\n", res)

}


func BootNotificationHandler(p ocpp.Payload) ocpp.Payload {
	req := p.(*v16.BootNotificationReq)
	fmt.Printf("BootNotificationReq: %v\n", req)

	var res ocpp.Payload = &v16.BootNotificationConf{
		CurrentTime: time.Now().Format("2006-01-02T15:04:05.000Z"),
		Interval:    60,
		Status:      "Accepted",
	}
	return res
}

ChargePoint represents a single Charge Point (CP) connected to Central System and after it is created, register CP initiated call handlers using cp.On method. Making Central System initiated call can be created using cp.Call method. To make a Call to multiple charge points concurrently refer to examples/ folder.

Charge Point
package main

import (
	"fmt"
	"net/http"

	"github.com/aliml92/ocpp"
	"github.com/aliml92/ocpp/v16"
	"github.com/gorilla/websocket"
)


var cp *ocpp.ChargePoint

func main() {
	
	chargePointId := "client_01"
	url := fmt.Sprintf("ws://localhost:8080/ocpp/v16/%s", chargePointId)
	header := http.Header{
		"Sec-WebSocket-Protocol": []string{"ocpp1.6"},
	}

	c, _, err := websocket.DefaultDialer.Dial(url, header)
	if err != nil {
		fmt.Printf("error dialing: %v", err)
		return
	}
	defer c.Close()


	// create a ChargePoint
	cp = ocpp.NewChargePoint(c, chargePointId)


	// register handlers for CS initiated calls
	cp.On("ChangeAvailability", ChangeAvailabilityHandler)


	// make a BootNotification Call to Central System
	req := &v16.BootNotificationReq{
		ChargePointModel: "ModelX",
		ChargePointVendor: "VendorX",
	} 
	res, err := cp.Call("BootNotification", req)
	if err != nil {
		fmt.Printf("error calling: %v", err)
		return
	}
	fmt.Printf("BootNotificationRes: %v\n", res)

}


func ChangeAvailabilityHandler(p ocpp.Payload) ocpp.Payload {
	req := p.(*v16.ChangeAvailabilityReq)
	fmt.Printf("ChangeAvailabilityReq: %v\n", req)
	
	var res ocpp.Payload = &v16.ChangeAvailabilityConf{
		Status: "Accepted",
	}
	
	return res
}

After creating ChargePoint register CS (Central System) initiated call handlers. Making a call to CS is same as the above snippet where just call cp.Call method.

Contributing

Contributions are always welcome! Implementing higher versions of ocpp is highly appreciated!

See CONTRIBUTING.md for ways to get started.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChargePoints = make(map[string]*ChargePoint)

map to store websocket connections, keys are the chargepoint ids and values are the chargepoint structs which contain the websocket connection

Functions

This section is empty.

Types

type Call

type Call struct {
	MessageTypeId uint8
	UniqueId      string
	Action        string
	Payload       Payload
}

Represents OCPP Call

type CallError

type CallError struct {
	MessageTypeId    uint8
	UniqueId         string
	ErrorCode        string
	ErrorDescription string
	ErrorDetails     interface{}
}

Represents OCPP CallError

func (*CallError) Error

func (ce *CallError) Error() string

type CallResult

type CallResult struct {
	MessageTypeId uint8
	UniqueId      string
	Payload       *json.RawMessage
}

Represents OCPP CallResult

type ChargePoint

type ChargePoint struct {
	Conn            *websocket.Conn                  // the websocket connection
	Id              string                           // chargePointId
	Out             chan *[]byte                     // channel to send messages to the ChargePoint
	In              chan *[]byte                     // channel to receive messages from the ChargePoint
	MessageHandlers map[string]func(Payload) Payload // map to store CP initiated actions
	AfterHandlers   map[string]func(Payload)         // map to store functions to be called after a CP initiated action
	Mu              sync.Mutex                       // mutex ensuring that only one message is sent at a time
	Cr              chan *CallResult
	Ce              chan *CallError
	Timeout         time.Duration // timeout for waiting for a response
}

Represents a connected ChargePoint (also known as a Charging Station)

func NewChargePoint

func NewChargePoint(conn *websocket.Conn, id string) *ChargePoint

Creates a new ChargePoint

func (*ChargePoint) After

func (cp *ChargePoint) After(action string, f func(Payload)) *ChargePoint

The function to be used by the implementers to register functions to be called after a CP initiated action

func (*ChargePoint) Call

func (cp *ChargePoint) Call(action string, p Payload) (Payload, error)

function to be used by the implementers to execute a CSMS initiated action

func (*ChargePoint) On

func (cp *ChargePoint) On(action string, f func(Payload) Payload) *ChargePoint

The function to be used by the implementers to register CP initiated actions

type OCPPError

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

func (*OCPPError) Error

func (e *OCPPError) Error() string

type Payload

type Payload interface{}

Used as a container is for both Call and CallResult' Payload

type TimeoutError

type TimeoutError struct {
	Message string
}

func (*TimeoutError) Error

func (e *TimeoutError) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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