Skip to content

Commit

Permalink
Mqtt: reset /set listener to be able to receive same command multiple…
Browse files Browse the repository at this point in the history
… times (evcc-io#1697)
  • Loading branch information
andig authored Oct 4, 2021
1 parent acb6c03 commit 712e3cb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions core/loadpoint/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions provider/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
27 changes: 17 additions & 10 deletions server/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down

0 comments on commit 712e3cb

Please sign in to comment.