example

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2022 License: MIT Imports: 11 Imported by: 0

README

ocpp1.6-go

Open Charge Point Protocol (OCPP) version 1.6 in Go.

Example

Example shows implementation ocppj version 1.6 for central system.

Folder structure
  • callbacks.go - Includes handlers for each OCPP request (Implementation DB logic)
  • configs.json - File to specify list of chargers for the demo in JSON format
  • simplequeue.go - Simple messages queue and charger objects for the demo only
  • api.go - Handlers for the client API requests
  • README.md - this file
Docker

To spin contaienr on docker, please use commands below:

docker build -t ocpp16:latest -f Dockerfile .
docker run --rm --name ocpp16-example -p "9033:8080" ocpp16:latest
Endpoint for the chargers
ws://localhost:9033/ocppj/1.6/{chargerName}

Central System Example

To use library in your project, you must implement the callbacks with your business logic, as shown below:

import (
	"time"
	"net/http"
	"github.com/CoderSergiy/golib/logging"
	"github.com/CoderSergiy/ocpp16-go/core"
	"github.com/CoderSergiy/ocpp16-go/messages"
)

type OCPPHandlers struct {
    // ... Add required variables for your implementation
}

func (cs *OCPPHandlers) BootNotificationRequestHandler (callMessage messages.CallMessage) (string, error, bool) {

    // ... Implement your business logic

	// Create CallResult message
	callMessageResponse := messages.CallResultMessageWithParam (
		callMessage.UniqueID,
		bootNotificationResp.GetPayload(),
	)

	return callMessageResponse.ToString(), nil, WEBSOCKET_KEEP_OPEN
}

// further callbacks... 
Requirements for the design

Name of the methods for the call requests has to in in the format: action + "RequestHandler", as example "HeartbeatRequestHandler".

For the responses handlers have to be in the formar: action + "ResponseHandler", as example "AuthorizeResponseHandler".

The error handler named "OCPPErrorHandler".

API to work with server

Get status of the charger

Charger needs to be add to the connfigs.json Example:

curl --request GET 'http://localhost:9033/charger/{chargerName}/status'
Get status of the message

All messages are using unique ID. Please, use it to inquire status from the server Example:

curl --request GET 'http://localhost:9033/message/{messageUniqueID}/status'
Initiate the TriggerAction by server (from CS to CP)

API to inject message for the charger, to make possible for Central System trigger Charge Point-initiated message. In response for successful created message server will returns 'uniqueid' which you can use to obtain status using Get Message Satatus API. Example:

curl --request POST 'http://localhost:9033/command/{chargerName}/triggeraction/{action}'

Permitted values for the 'action' parameter, regarding OCPP document you can find in the list below:

  • BootNotification
  • DiagnosticsStatusNotification
  • FirmwareStatusNotification
  • Heartbeat
  • MeterValues
  • StatusNotification

Documentation

Index

Constants

View Source
const (
	WEBSOCKET_KEEP_OPEN bool = true
	WEBSOCKET_CLOSE     bool = false
)

Variables

This section is empty.

Functions

func CreateFailResponse added in v0.3.0

func CreateFailResponse(description string) string

***************************************************************************************

*
* Function : CreateFailResponse
*
*  Purpose : Create Failed API response in json format
*
*	  Input : description string - description of the failed status
*
*	 Return : string - json format of the APIResponse

func CreateSuccessResponse added in v0.3.0

func CreateSuccessResponse(reference string) []byte

***************************************************************************************

*
* Function : CreateSuccessResponse
*
*  Purpose : Create Successfull API response in json format
*
*	  Input : reference string - reference for the response
*
*	 Return : []byte - json format of the APIResponse

func GetChargerStatusAPI added in v0.3.0

func GetChargerStatusAPI(chargerName string, serverConfigs *Configs, log *logging.Log, w http.ResponseWriter)

***************************************************************************************

*
* Function : GetMessageStatusAPI
*
*  Purpose : Get message from the queue and send to the client by http
*
*    Input : chargerName string - charger name
*            serverConfigs *Configs - pointer to the chargers arrays
*            log *logging.Log - pointer to the log
*            w http.ResponseWriter - http response
*
*   Return : Nothing

func GetMessageStatusAPI added in v0.3.0

func GetMessageStatusAPI(reference string, MQueue *SimpleMessageQueue, log *logging.Log, w http.ResponseWriter)

***************************************************************************************

*
* Function : GetMessageStatusAPI
*
*  Purpose : Get message from the queue and send to the client by http
*
*    Input : reference string - unique reference
*            MQueue *SimpleMessageQueue - pointer to the Message Queue
*            log *logging.Log - pointer to the log
*            w http.ResponseWriter - http response
*
*   Return : Nothing

func TriggerActionAPI added in v0.3.0

func TriggerActionAPI(serverConfigs *Configs, MQueue *SimpleMessageQueue, log *logging.Log, ps httprouter.Params, w http.ResponseWriter)

***************************************************************************************

*
* Function : TriggerActionAPI
*
*  Purpose : Handles TriggerAction API request
*
*    Input : serverConfigs *Configs - pointer to the chargers arrays
*            MQueue *SimpleMessageQueue - pointer to the Message Queue
*            log *logging.Log - pointer to the log
*            ps httprouter.Params - router parameters
*            w http.ResponseWriter - http response
*
*   Return : Nothing

Types

type APIResponse added in v0.3.0

type APIResponse struct {
	Status      string `json:"status"`
	Reference   string `json:"reference"`
	Description string `json:"description"`
}

***************************************************************************************

  • Struct : APIResponse *
  • Purpose : Object handles API response parameters *

****************************************************************************************

type Charger

type Charger struct {
	AuthToken          string
	HeartBeatInterval  int
	AuthConnection     bool
	WebSocketConnected bool        `json:"Connected"`
	InboundIP          string      `json:"RemoteIP"`
	WriteChannel       chan string `json:"-"`
}

***************************************************************************************

  • Struct : Charger *
  • Purpose : Struct handles charger parameters in the gorutines *

****************************************************************************************

func ChargerConstructor added in v0.3.0

func ChargerConstructor() Charger

***************************************************************************************

*
* Function : ChargerConstructor (Constructor)
*
*  Purpose : Creates a new instance of the Charger
*
*	  Input : Nothing
*
*	Return : Charger object

func (*Charger) Disconnected added in v0.3.0

func (charger *Charger) Disconnected()

***************************************************************************************

*
* Function : Charger::Disconnected
*
*  Purpose : Clear Charger parameters when disconnected from socket
*
*    Input : Nothing
*
*   Return : Nothing

type ChargerFromFile

type ChargerFromFile struct {
	Name              string `json:"Name"`
	Authorization     string `json:"Authorization"`
	HeartBeatInterval int    `json:"HeartBeatInterval"`
}

***************************************************************************************

  • Struct : ChargerFromFile *
  • Purpose : Struct handles charger's parameters from config file *

****************************************************************************************

type Configs

type Configs struct {
	Chargers     map[string]*Charger `json:"Chargers"`
	MaxQueueSize int                 `json:"MaxQueueSize"`
}

***************************************************************************************

  • Struct : Configs *
  • Purpose : Object handles configurations from the file *

****************************************************************************************

func ServerConfigsConstructor

func ServerConfigsConstructor() Configs

***************************************************************************************

*
* Function : ServerConfigsConstructor (Constructor)
*
*  Purpose : Creates a new instance of the Configs
*
*	  Input : Nothing
*
*	Return : Configs object

func SetConfigsFromFile

func SetConfigsFromFile(fileName string) (Configs, error)

***************************************************************************************

*
* Function : SetConfigsFromFile (Constructor)
*
*  Purpose : Set configs struct from the file
*
*	  Input : fileName string - filename with settings for the test server
*
*	 Return : Configs - Configs object
* 			  error - error if happened

func (*Configs) GetChargerObj

func (conf *Configs) GetChargerObj(chargerName string) (*Charger, error)

***************************************************************************************

*
* Function : Configs::GetChargerObj
*
*  Purpose : Get charger from the configuration structure
*
*	  Input : chargerName string - Name of the charger in the queue
*
*	 Return : Charger - charger object
* 			  error - error if happened

type FileConfigs

type FileConfigs struct {
	Chargers     []ChargerFromFile `json:"Chargers"`
	MaxQueueSize int               `json:"MaxQueueSize"`
}

***************************************************************************************

  • Struct : FileConfigs *
  • Purpose : Struct handles configurations from file *

****************************************************************************************

type Message

type Message struct {
	Action   string // Action of the message
	Received string // Message content
	Status   QueueMessageType
	Sent     string
}

***************************************************************************************

  • Struct : Message *
  • Purpose : Struct handles message's parameters *

****************************************************************************************

type OCPPHandlers

type OCPPHandlers struct {
	Charger *Charger            // Charger struct which connected to the server
	Log     logging.Log         // Pointer to the log
	MQueue  *SimpleMessageQueue // For example queue will be here
}

***************************************************************************************

  • Struct : OCPPHandlers *
  • Purpose : Handles struct for each connected charger *

****************************************************************************************

func OCPPHandlersConstructor

func OCPPHandlersConstructor() OCPPHandlers

***************************************************************************************

*
* Function : OCPPHandlersConstructor (Constructor)
*
*  Purpose : Creates a new instance of the OCPPHandlers
*
*	  Input : Nothing
*
*	Return : OCPPHandlers object

func (*OCPPHandlers) Authorisation

func (cs *OCPPHandlers) Authorisation(chargerName string, request *http.Request) bool

***************************************************************************************

*
* Function : OCPPHandlers::Authorisation
*
* Purpose : Using to Authorise charger before allow websocket connection
*
*   Input : chargerName string - charger name to be validated
*           request *http.Request - http request object
*
*  Return : true - when charger is authorised, otherwise false
*

func (*OCPPHandlers) AuthorizeRequestHandler

func (cs *OCPPHandlers) AuthorizeRequestHandler(callMessage messages.CallMessage) (string, error, bool)

***************************************************************************************

*
* Function : OCPPHandlers::AuthorizeRequestHandler
*
*  Purpose : Handle AuthorizeRequest
*
*    Input : callMessage messages.CallMessage - original Call message
*
*   Return : string - response message in string format
*			  error - if happened, nil otherwise
*			  bool - false - when connection to charger needs to be closed, otherwise true
*

func (*OCPPHandlers) AuthorizeResponseHandler

func (cs *OCPPHandlers) AuthorizeResponseHandler(callResultMessage messages.CallResultMessage) (error, bool)

***************************************************************************************

*
* Function : OCPPHandlers::AuthorizeResponseHandler
*
*  Purpose : Handle AuthorizeResponse
*
*    Input : callMessage messages.CallMessage - original Call message
*
*   Return : string - response message in string format
*			  error - if happened, nil otherwise
*			  bool - false - when connection to charger needs to be closed, otherwise true
*

func (*OCPPHandlers) BootNotificationRequestHandler

func (cs *OCPPHandlers) BootNotificationRequestHandler(callMessage messages.CallMessage) (string, error, bool)

***************************************************************************************

*
* Function : OCPPHandlers::BootNotificationRequestHandler
*
*  Purpose : Handle BootNotificationRequest
*
*    Input : callMessage messages.CallMessage - original Call message
*
*   Return : string - response message in string format
*			  error - if happened, nil otherwise
*			  bool - false - when connection to charger needs to be closed, otherwise true
*

func (*OCPPHandlers) GetActionHandler

func (cs *OCPPHandlers) GetActionHandler(uniqueID string) string

***************************************************************************************

*
* Function : OCPPHandlers::GetActionHandler
*
*  Purpose : Get Action of the message from queue by unique ID
*
*    Input : uniqueID string - message's unique ID
*
*   Return : message's action in string format
*

func (*OCPPHandlers) HeartbeatRequestHandler

func (cs *OCPPHandlers) HeartbeatRequestHandler(callMessage messages.CallMessage) (string, error, bool)

***************************************************************************************

*
* Function : OCPPHandlers::HeartbeatRequestHandler
*
*  Purpose : Handle HeartbeatRequest
*
*    Input : callMessage messages.CallMessage - original Call message
*
*   Return : string - response message in string format
*			  error - if happened, nil otherwise
*			  bool - false - when connection to charger needs to be closed, otherwise true
*

func (*OCPPHandlers) OCPPErrorHandler

func (cs *OCPPHandlers) OCPPErrorHandler(callErrortMessage messages.CallErrorMessage) (error, bool)

***************************************************************************************

*
* Function : OCPPHandlers::OCPPErrorHandler
*
*  Purpose : Handle OCPP Error
*
*    Input : callMessage messages.CallMessage - original Call message
*
*   Return : string - response message in string format
*			  error - if happened, nil otherwise
*			  bool - false - when connection to charger needs to be closed, otherwise true
*

type QueueMessageType

type QueueMessageType int
const (
	MESSAGE_TYPE_NEW       QueueMessageType = 1
	MESSAGE_TYPE_SENT      QueueMessageType = 2
	MESSAGE_TYPE_RECEIVED  QueueMessageType = 3
	MESSAGE_TYPE_COMPLETED QueueMessageType = 4
	MESSAGE_TYPE_ERROR     QueueMessageType = 5
)

type SimpleMessageQueue

type SimpleMessageQueue struct {
	MaxSize      int
	MessageQueue map[string]Message
	// contains filtered or unexported fields
}

***************************************************************************************

  • Struct : SimpleMessageQueue *
  • Purpose : Struct handles messages queue routines *

****************************************************************************************

func SimpleMessageQueueConstructor added in v0.3.0

func SimpleMessageQueueConstructor() SimpleMessageQueue

***************************************************************************************

*
* Function : SimpleMessageQueue (Constructor)
*
*  Purpose : Creates a new instance of the SimpleMessageQueue
*
*	  Input : Nothing
*
*	Return : SimpleMessageQueue object

func (*SimpleMessageQueue) Add

func (queue *SimpleMessageQueue) Add(uniqueID string, message Message) error

***************************************************************************************

*
* Function : SimpleMessageQueue::Add
*
*  Purpose : Add message to the queue
*
*    Input : uniqueID string - id of the message
*			  message Message - raw message to parse and validate
*
*   Return : error - if happened, nil otherwise
*

func (*SimpleMessageQueue) DeleteByUniqueID

func (queue *SimpleMessageQueue) DeleteByUniqueID(uniqueID string) error

***************************************************************************************

*
* Function : SimpleMessageQueue::DeleteByUniqueID
*
*  Purpose : Delete message from the queue by unique id
*
*    Input : uniqueID string - id of the message
*
*   Return : error - if happened, nil otherwise
*

func (*SimpleMessageQueue) GetMessage

func (queue *SimpleMessageQueue) GetMessage(uniqueID string) (Message, bool)

***************************************************************************************

*
* Function : SimpleMessageQueue::GetMessage
*
*  Purpose : Get message from the queue by unique id
*
*    Input : uniqueID string - id of the message
*
*   Return : Message
*			  bool - true when message exists, false otherwise
*

func (*SimpleMessageQueue) UpdateByUniqueID added in v0.3.0

func (queue *SimpleMessageQueue) UpdateByUniqueID(uniqueID string, message Message) error

***************************************************************************************

*
* Function : SimpleMessageQueue::UpdateByUniqueID
*
*  Purpose : Update message in the queue by unique id
*
*    Input : uniqueID string - id of the message
*            message Message - message obj to update in the queue
*
*   Return : error - if happened, nil otherwise
*

Jump to

Keyboard shortcuts

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