diff --git a/README.md b/README.md index ddb8c7d20b..5fc6063846 100644 --- a/README.md +++ b/README.md @@ -627,6 +627,8 @@ The MQTT API follows the REST API's structure, with loadpoint ids starting at `1 - `evcc/loadpoints//mode`: loadpoint charge mode (writable) - `evcc/loadpoints//minSoC`: loadpoint minimum SoC (writable) - `evcc/loadpoints//targetSoC`: loadpoint target SoC (writable) +- `evcc/loadpoints//minCurrent`: loadpoint minimum current (writable) +- `evcc/loadpoints//maxCurrent`: loadpoint maximum current (writable) - `evcc/loadpoints//phases`: loadpoint enabled phases (writable) Note: to modify writable settings append `/set` to the topic for writing. diff --git a/core/loadpoint/api.go b/core/loadpoint/api.go index 8521e7e7a7..1e0d31e3bf 100644 --- a/core/loadpoint/api.go +++ b/core/loadpoint/api.go @@ -59,11 +59,11 @@ type API interface { GetChargePower() float64 // GetMinCurrent returns the min charging current GetMinCurrent() float64 - // SetMinCurrent returns the min charging current + // SetMinCurrent sets the min charging current SetMinCurrent(float64) // GetMaxCurrent returns the max charging current GetMaxCurrent() float64 - // SetMaxCurrent returns the max charging current + // SetMaxCurrent sets the max charging current SetMaxCurrent(float64) // GetMinPower returns the min charging power for a single phase GetMinPower() float64 diff --git a/provider/mqtt/client.go b/provider/mqtt/client.go index 2d135b2cb8..c683988eaf 100644 --- a/provider/mqtt/client.go +++ b/provider/mqtt/client.go @@ -120,6 +120,16 @@ func (m *Client) Listen(topic string, callback func(string)) { m.listen(topic) } +// ListenSetter creates a /set listener that resets the payload after handling +func (m *Client) ListenSetter(topic string, callback func(string)) { + m.Listen(topic, func(payload string) { + callback(payload) + if err := m.Publish(topic, false, nil); err != nil { + m.log.ERROR.Printf("clear: %v", err) + } + }) +} + // listen attaches listener to topic func (m *Client) listen(topic string) { token := m.Client.Subscribe(topic, m.Qos, func(c paho.Client, msg paho.Message) { diff --git a/server/mqtt.go b/server/mqtt.go index 2be4529f7d..b9189bb9a1 100644 --- a/server/mqtt.go +++ b/server/mqtt.go @@ -66,24 +66,31 @@ func (m *MQTT) publish(topic string, retained bool, payload interface{}) { } func (m *MQTT) listenSetters(topic string, apiHandler loadpoint.API) { - m.Handler.Listen(topic+"/mode/set", func(payload string) { + m.Handler.ListenSetter(topic+"/mode/set", func(payload string) { apiHandler.SetMode(api.ChargeMode(payload)) }) - m.Handler.Listen(topic+"/minSoC/set", func(payload string) { - soc, err := strconv.Atoi(payload) - if err == nil { + m.Handler.ListenSetter(topic+"/minSoC/set", func(payload string) { + if soc, err := strconv.Atoi(payload); err == nil { _ = apiHandler.SetMinSoC(soc) } }) - m.Handler.Listen(topic+"/targetSoC/set", func(payload string) { - soc, err := strconv.Atoi(payload) - if err == nil { + m.Handler.ListenSetter(topic+"/targetSoC/set", func(payload string) { + if soc, err := strconv.Atoi(payload); err == nil { _ = apiHandler.SetTargetSoC(soc) } }) - m.Handler.Listen(topic+"/phases/set", func(payload string) { - phases, err := strconv.Atoi(payload) - if err == nil { + m.Handler.ListenSetter(topic+"/minCurrent/set", func(payload string) { + if current, err := strconv.ParseFloat(payload, 64); err == nil { + apiHandler.SetMinCurrent(current) + } + }) + m.Handler.ListenSetter(topic+"/maxCurrent/set", func(payload string) { + if current, err := strconv.ParseFloat(payload, 64); err == nil { + apiHandler.SetMaxCurrent(current) + } + }) + m.Handler.ListenSetter(topic+"/phases/set", func(payload string) { + if phases, err := strconv.Atoi(payload); err == nil { _ = apiHandler.SetPhases(phases) } })