To build a custom Central System, you must implement the callback interfaces defined in the profile packages (e.g., core.CentralSystemHandler).
Each callback function is invoked in a dedicated goroutine, so you do not need to manage synchronization for the callback itself. For every request received, you must return either a confirmation object or an error; the library handles sending the response back to the charge point automatically.
To initialize and start the server:
- Create a new instance using
ocpp16.NewCentralSystem. - Set connection handlers using
SetNewChargePointHandler and SetChargePointDisconnectedHandler. - Set your custom logic handler using
SetCoreHandler. - Call
Start(listenPort, path) to begin listening. Note that Start is a blocking call that runs in daemon mode.
centralSystem := ocpp16.NewCentralSystem(nil, nil)
// Set connection handlers
centralSystem.SetNewChargePointHandler(func (chargePointId string) {
log.Printf("new charge point %v connected", chargePointId)
})
centralSystem.SetChargePointDisconnectedHandler(func (chargePointId string) {
log.Printf("charge point %v disconnected", chargePointId)
})
// Set logic handler
handler := &CentralSystemHandler{}
centralSystem.SetCoreHandler(handler)
// Start server (blocking)
listenPort := 8887
centralSystem.Start(listenPort, "/{ws}")