From 0ff07da9053fc792c5a86a9b785961e08c7b8e28 Mon Sep 17 00:00:00 2001 From: andig Date: Wed, 2 Mar 2022 13:00:24 +0100 Subject: [PATCH] Add vehicle api (#2743) --- core/loadpoint/api.go | 2 ++ core/loadpoint_api.go | 8 ++++++++ server/http.go | 1 + server/http_handler.go | 9 +++++++++ 4 files changed, 20 insertions(+) diff --git a/core/loadpoint/api.go b/core/loadpoint/api.go index 6a9dcefc4..1ab4271ab 100644 --- a/core/loadpoint/api.go +++ b/core/loadpoint/api.go @@ -48,6 +48,8 @@ type API interface { // SetTargetCharge sets the charge targetSoC SetTargetCharge(time.Time, int) + // SetVehicle sets the active vehicle + SetVehicle(vehicle api.Vehicle) // RemoteControl sets remote status demand RemoteControl(string, RemoteDemand) diff --git a/core/loadpoint_api.go b/core/loadpoint_api.go index dc6cb1bc1..76d113957 100644 --- a/core/loadpoint_api.go +++ b/core/loadpoint_api.go @@ -139,6 +139,14 @@ func (lp *LoadPoint) SetTargetCharge(finishAt time.Time, soc int) { } } +// SetVehicle sets the active vehicle +func (lp *LoadPoint) SetVehicle(vehicle api.Vehicle) { + lp.Lock() + defer lp.Unlock() + + lp.setActiveVehicle(vehicle) +} + // RemoteControl sets remote status demand func (lp *LoadPoint) RemoteControl(source string, demand loadpoint.RemoteDemand) { lp.Lock() diff --git a/server/http.go b/server/http.go index 9cbb2dcf5..85e34bba2 100644 --- a/server/http.go +++ b/server/http.go @@ -87,6 +87,7 @@ func NewHTTPd(url string, site site.API, hub *SocketHub, cache *util.Cache) *HTT "phases": {[]string{"POST", "OPTIONS"}, "/phases/{value:[0-9]+}", phasesHandler(lp)}, "targetcharge": {[]string{"POST", "OPTIONS"}, "/targetcharge/{soc:[0-9]+}/{time:[0-9TZ:-]+}", targetChargeHandler(lp)}, "targetcharge2": {[]string{"DELETE", "OPTIONS"}, "/targetcharge", targetChargeRemoveHandler(lp)}, + "vehicle": {[]string{"DELETE", "OPTIONS"}, "/vehicle", vehicleRemoveHandler(lp)}, "remotedemand": {[]string{"POST", "OPTIONS"}, "/remotedemand/{demand:[a-z]+}/{source::[0-9a-zA-Z_-]+}", remoteDemandHandler(lp)}, } diff --git a/server/http_handler.go b/server/http_handler.go index 80c00acb5..46796b1c4 100644 --- a/server/http_handler.go +++ b/server/http_handler.go @@ -274,6 +274,15 @@ func targetChargeRemoveHandler(loadpoint loadpoint.API) http.HandlerFunc { } } +// vehicleRemoveHandler removes vehicle +func vehicleRemoveHandler(loadpoint loadpoint.API) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + loadpoint.SetVehicle(nil) + res := struct{}{} + jsonResult(w, res) + } +} + // socketHandler attaches websocket handler to uri func socketHandler(hub *SocketHub) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) {