Mqtt: reset /set listener to be able to receive same command multiple times (#1697)

This commit is contained in:
andig 2021-10-04 11:46:21 +02:00 • committed by GitHub
parent acb6c03d5b
commit 712e3cba11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 12 deletions

View file

@ -627,6 +627,8 @@ The MQTT API follows the REST API's structure, with loadpoint ids starting at `1
- `evcc/loadpoints/<id>/mode`: loadpoint charge mode (writable)
- `evcc/loadpoints/<id>/minSoC`: loadpoint minimum SoC (writable)
- `evcc/loadpoints/<id>/targetSoC`: loadpoint target SoC (writable)
- `evcc/loadpoints/<id>/minCurrent`: loadpoint minimum current (writable)
- `evcc/loadpoints/<id>/maxCurrent`: loadpoint maximum current (writable)
- `evcc/loadpoints/<id>/phases`: loadpoint enabled phases (writable)
Note: to modify writable settings append `/set` to the topic for writing.

View file

@ -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

View file

@ -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) {

View file

@ -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)
}
})