Ocpp: fix chargepoint registration and startup (#4420)
This commit is contained in:
parent
2492e4b0ae
commit
6bbfeffb8f
7 changed files with 250 additions and 70 deletions
78
cmd/ocpp/handler.go
Normal file
78
cmd/ocpp/handler.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
|
||||
)
|
||||
|
||||
type ChargePointHandler struct {
|
||||
triggerC chan remotetrigger.MessageTrigger
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnChangeAvailability(request *core.ChangeAvailabilityRequest) (confirmation *core.ChangeAvailabilityConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewChangeAvailabilityConfirmation(core.AvailabilityStatusAccepted), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnChangeConfiguration(request *core.ChangeConfigurationRequest) (confirmation *core.ChangeConfigurationConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewChangeConfigurationConfirmation(core.ConfigurationStatusAccepted), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnClearCache(request *core.ClearCacheRequest) (confirmation *core.ClearCacheConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewClearCacheConfirmation(core.ClearCacheStatusAccepted), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnDataTransfer(request *core.DataTransferRequest) (confirmation *core.DataTransferConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewDataTransferConfirmation(core.DataTransferStatusAccepted), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnGetConfiguration(request *core.GetConfigurationRequest) (confirmation *core.GetConfigurationConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
one := "1"
|
||||
return core.NewGetConfigurationConfirmation([]core.ConfigurationKey{
|
||||
{Key: "NumberOfConnectors", Value: &one},
|
||||
{Key: "ChargeProfileMaxStackLevel", Value: &one},
|
||||
{Key: "ChargingScheduleMaxPeriods", Value: &one},
|
||||
{Key: "MaxChargingProfilesInstalled", Value: &one},
|
||||
{Key: "ChargingScheduleAllowedChargingRateUnit", Value: &one},
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnRemoteStartTransaction(request *core.RemoteStartTransactionRequest) (confirmation *core.RemoteStartTransactionConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewRemoteStartTransactionConfirmation(types.RemoteStartStopStatusAccepted), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnRemoteStopTransaction(request *core.RemoteStopTransactionRequest) (confirmation *core.RemoteStopTransactionConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewRemoteStopTransactionConfirmation(types.RemoteStartStopStatusAccepted), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnReset(request *core.ResetRequest) (confirmation *core.ResetConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewResetConfirmation(core.ResetStatusAccepted), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnUnlockConnector(request *core.UnlockConnectorRequest) (confirmation *core.UnlockConnectorConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
return core.NewUnlockConnectorConfirmation(core.UnlockStatusUnlocked), nil
|
||||
}
|
||||
|
||||
func (handler *ChargePointHandler) OnTriggerMessage(request *remotetrigger.TriggerMessageRequest) (confirmation *remotetrigger.TriggerMessageConfirmation, err error) {
|
||||
fmt.Printf("%T %+v\n", request, request)
|
||||
|
||||
if c := handler.triggerC; request != nil && c != nil {
|
||||
select {
|
||||
case c <- request.RequestedMessage:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
return remotetrigger.NewTriggerMessageConfirmation(remotetrigger.TriggerMessageStatusAccepted), nil
|
||||
}
|
||||
68
cmd/ocpp/main.go
Normal file
68
cmd/ocpp/main.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
ocpp16 "github.com/lorenzodonini/ocpp-go/ocpp1.6"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/core"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/remotetrigger"
|
||||
"github.com/lorenzodonini/ocpp-go/ocpp1.6/types"
|
||||
)
|
||||
|
||||
const (
|
||||
chargePointId = "cp0001"
|
||||
url = "ws://localhost:8887"
|
||||
)
|
||||
|
||||
func main() {
|
||||
chargePoint := ocpp16.NewChargePoint(chargePointId, nil, nil)
|
||||
|
||||
// Set a handler for all callback functions
|
||||
handler := &ChargePointHandler{triggerC: make(chan remotetrigger.MessageTrigger, 1)}
|
||||
chargePoint.SetCoreHandler(handler)
|
||||
chargePoint.SetRemoteTriggerHandler(handler)
|
||||
|
||||
go func() {
|
||||
for err := range chargePoint.Errors() {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for msg := range handler.triggerC {
|
||||
fmt.Println("msg:", msg)
|
||||
switch msg {
|
||||
case core.StatusNotificationFeatureName:
|
||||
if res, err := chargePoint.StatusNotification(1, core.NoError, core.ChargePointStatusAvailable); err != nil {
|
||||
log.Println("StatusNotification:", err)
|
||||
} else {
|
||||
log.Println("StatusNotification:", res)
|
||||
}
|
||||
|
||||
case core.MeterValuesFeatureName:
|
||||
if _, err := chargePoint.MeterValues(1, []types.MeterValue{
|
||||
{SampledValue: []types.SampledValue{
|
||||
{Measurand: types.MeasurandPowerActiveImport, Value: "1000"},
|
||||
}},
|
||||
}); err != nil {
|
||||
log.Println("MeterValues:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Connects to central system
|
||||
if err := chargePoint.Start(url); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Printf("connected to central system at %v", url)
|
||||
if res, err := chargePoint.BootNotification("model1", "vendor1"); err != nil {
|
||||
log.Fatal("BootNotification", err)
|
||||
} else {
|
||||
log.Printf("status: %v, interval: %v", res.Status, res.Interval)
|
||||
}
|
||||
|
||||
select {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue