diff --git a/hems/ocpp/ocpp.go b/hems/ocpp/ocpp.go index df54620f8..7094aeb17 100644 --- a/hems/ocpp/ocpp.go +++ b/hems/ocpp/ocpp.go @@ -7,6 +7,7 @@ import ( "time" "github.com/andig/evcc/core" + "github.com/andig/evcc/hems/ocpp/profile" "github.com/andig/evcc/util" "github.com/denisbrodbeck/machineid" @@ -17,22 +18,16 @@ import ( // OCPP is an OCPP client type OCPP struct { - log *util.Logger - cache *util.Cache - site site - cp ocpp16.ChargePoint - configuration ConfigMap -} - -// site is the minimal interface for accessing site methods -type site interface { - LoadPoints() []core.LoadPointAPI + log *util.Logger + cache *util.Cache + site core.SiteAPI + cp ocpp16.ChargePoint } const retryTimeout = 5 * time.Second // New generates OCPP chargepoint client -func New(conf map[string]interface{}, site site, cache *util.Cache) (*OCPP, error) { +func New(conf map[string]interface{}, site core.SiteAPI, cache *util.Cache) (*OCPP, error) { cc := struct { URI string StationID string @@ -58,16 +53,17 @@ func New(conf map[string]interface{}, site site, cache *util.Cache) (*OCPP, erro cp := ocpp16.NewChargePoint(cc.StationID, nil, ws) s := &OCPP{ - log: log, - cache: cache, - site: site, - cp: cp, - configuration: getDefaultConfig(), + log: log, + cache: cache, + site: site, + cp: cp, } err := cp.Start(cc.URI) if err == nil { - cp.SetCoreHandler(s) + cp.SetCoreHandler(profile.NewCore(log, profile.GetDefaultConfig())) + cp.SetSmartChargingHandler(profile.NewSmartCharging(log)) + go s.errorHandler(ws.Errors()) go s.errorHandler(cp.Errors()) } diff --git a/hems/ocpp/configmap.go b/hems/ocpp/profile/configmap.go similarity index 99% rename from hems/ocpp/configmap.go rename to hems/ocpp/profile/configmap.go index b27f12001..e589aa252 100644 --- a/hems/ocpp/configmap.go +++ b/hems/ocpp/profile/configmap.go @@ -1,4 +1,4 @@ -package ocpp +package profile import ( "strconv" @@ -93,7 +93,7 @@ func (c ConfigMap) set(key string, readonly bool, value string) { } } -func getDefaultConfig() ConfigMap { +func GetDefaultConfig() ConfigMap { intBase := 10 var cfg ConfigMap = make(map[string]core.ConfigurationKey) diff --git a/hems/ocpp/core_transaction.go b/hems/ocpp/profile/core.go similarity index 71% rename from hems/ocpp/core_transaction.go rename to hems/ocpp/profile/core.go index e5fcba0f2..feab1ed6f 100644 --- a/hems/ocpp/core_transaction.go +++ b/hems/ocpp/profile/core.go @@ -1,30 +1,43 @@ -package ocpp +package profile import ( + "github.com/andig/evcc/util" "github.com/lorenzodonini/ocpp-go/ocpp1.6/core" "github.com/lorenzodonini/ocpp-go/ocpp1.6/types" ) +type Core struct { + log *util.Logger + configuration ConfigMap +} + +func NewCore(log *util.Logger, config ConfigMap) *Core { + return &Core{ + log: log, + configuration: config, + } +} + // OnChangeAvailability handles the CS message -func (s *OCPP) OnChangeAvailability(request *core.ChangeAvailabilityRequest) (confirmation *core.ChangeAvailabilityConfirmation, err error) { +func (s *Core) OnChangeAvailability(request *core.ChangeAvailabilityRequest) (confirmation *core.ChangeAvailabilityConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) return core.NewChangeAvailabilityConfirmation(core.AvailabilityStatusRejected), nil } // OnUnlockConnector handles the CS message -func (s *OCPP) OnUnlockConnector(request *core.UnlockConnectorRequest) (confirmation *core.UnlockConnectorConfirmation, err error) { +func (s *Core) OnUnlockConnector(request *core.UnlockConnectorRequest) (confirmation *core.UnlockConnectorConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) return core.NewUnlockConnectorConfirmation(core.UnlockStatusUnlocked), nil } // OnRemoteStartTransaction handles the CS message -func (s *OCPP) OnRemoteStartTransaction(request *core.RemoteStartTransactionRequest) (confirmation *core.RemoteStartTransactionConfirmation, err error) { +func (s *Core) OnRemoteStartTransaction(request *core.RemoteStartTransactionRequest) (confirmation *core.RemoteStartTransactionConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) return core.NewRemoteStartTransactionConfirmation(types.RemoteStartStopStatusRejected), nil } // OnRemoteStopTransaction handles the CS message -func (s *OCPP) OnRemoteStopTransaction(request *core.RemoteStopTransactionRequest) (confirmation *core.RemoteStopTransactionConfirmation, err error) { +func (s *Core) OnRemoteStopTransaction(request *core.RemoteStopTransactionRequest) (confirmation *core.RemoteStopTransactionConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) return core.NewRemoteStopTransactionConfirmation(types.RemoteStartStopStatusRejected), nil } diff --git a/hems/ocpp/core_configuration.go b/hems/ocpp/profile/core_configuration.go similarity index 88% rename from hems/ocpp/core_configuration.go rename to hems/ocpp/profile/core_configuration.go index 03b89653b..c16fdc4f4 100644 --- a/hems/ocpp/core_configuration.go +++ b/hems/ocpp/profile/core_configuration.go @@ -1,9 +1,9 @@ -package ocpp +package profile import "github.com/lorenzodonini/ocpp-go/ocpp1.6/core" // OnGetConfiguration handles the CS message -func (s *OCPP) OnGetConfiguration(request *core.GetConfigurationRequest) (confirmation *core.GetConfigurationConfirmation, err error) { +func (s *Core) OnGetConfiguration(request *core.GetConfigurationRequest) (confirmation *core.GetConfigurationConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) var resultKeys []core.ConfigurationKey @@ -34,7 +34,7 @@ func (s *OCPP) OnGetConfiguration(request *core.GetConfigurationRequest) (confir } // OnChangeConfiguration handles the CS message -func (s *OCPP) OnChangeConfiguration(request *core.ChangeConfigurationRequest) (confirmation *core.ChangeConfigurationConfirmation, err error) { +func (s *Core) OnChangeConfiguration(request *core.ChangeConfigurationRequest) (confirmation *core.ChangeConfigurationConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) return core.NewChangeConfigurationConfirmation(core.ConfigurationStatusAccepted), nil } diff --git a/hems/ocpp/core_other.go b/hems/ocpp/profile/core_other.go similarity index 78% rename from hems/ocpp/core_other.go rename to hems/ocpp/profile/core_other.go index 8674a2384..8b7851a45 100644 --- a/hems/ocpp/core_other.go +++ b/hems/ocpp/profile/core_other.go @@ -1,23 +1,23 @@ -package ocpp +package profile import ( "github.com/lorenzodonini/ocpp-go/ocpp1.6/core" ) // OnClearCache handles the CS message -func (s *OCPP) OnClearCache(request *core.ClearCacheRequest) (confirmation *core.ClearCacheConfirmation, err error) { +func (s *Core) OnClearCache(request *core.ClearCacheRequest) (confirmation *core.ClearCacheConfirmation, err error) { s.log.TRACE.Printf("recv: %s", request.GetFeatureName()) return core.NewClearCacheConfirmation(core.ClearCacheStatusAccepted), nil } // OnDataTransfer handles the CS message -func (s *OCPP) OnDataTransfer(request *core.DataTransferRequest) (confirmation *core.DataTransferConfirmation, err error) { +func (s *Core) OnDataTransfer(request *core.DataTransferRequest) (confirmation *core.DataTransferConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) return core.NewDataTransferConfirmation(core.DataTransferStatusAccepted), nil } // OnReset handles the CS message -func (s *OCPP) OnReset(request *core.ResetRequest) (confirmation *core.ResetConfirmation, err error) { +func (s *Core) OnReset(request *core.ResetRequest) (confirmation *core.ResetConfirmation, err error) { s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) return core.NewResetConfirmation(core.ResetStatusAccepted), nil } diff --git a/hems/ocpp/profile/smartcharging.go b/hems/ocpp/profile/smartcharging.go new file mode 100644 index 000000000..339bb33db --- /dev/null +++ b/hems/ocpp/profile/smartcharging.go @@ -0,0 +1,34 @@ +package profile + +import ( + "github.com/andig/evcc/util" + sc "github.com/lorenzodonini/ocpp-go/ocpp1.6/smartcharging" +) + +type SmartCharging struct { + log *util.Logger +} + +func NewSmartCharging(log *util.Logger) *SmartCharging { + return &SmartCharging{ + log: log, + } +} + +// OnSetChargingProfile handles the CS message +func (s *SmartCharging) OnSetChargingProfile(request *sc.SetChargingProfileRequest) (confirmation *sc.SetChargingProfileConfirmation, err error) { + s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) + return sc.NewSetChargingProfileConfirmation(sc.ChargingProfileStatusRejected), nil +} + +// OnClearChargingProfile handles the CS message +func (s *SmartCharging) OnClearChargingProfile(request *sc.ClearChargingProfileRequest) (confirmation *sc.ClearChargingProfileConfirmation, err error) { + s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) + return sc.NewClearChargingProfileConfirmation(sc.ClearChargingProfileStatusUnknown), nil +} + +// OnGetCompositeSchedule handles the CS message +func (s *SmartCharging) OnGetCompositeSchedule(request *sc.GetCompositeScheduleRequest) (confirmation *sc.GetCompositeScheduleConfirmation, err error) { + s.log.TRACE.Printf("recv: %s %+v", request.GetFeatureName(), request) + return sc.NewGetCompositeScheduleConfirmation(sc.GetCompositeScheduleStatusRejected), nil +}