Skip to content

Commit

Permalink
Mqtt: refactor error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Jun 4, 2023
1 parent bf5342f commit 38777d1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 66 deletions.
6 changes: 4 additions & 2 deletions provider/mqtt/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ func (m *Client) Listen(topic string, callback func(string)) {
}

// ListenSetter creates a /set listener that resets the payload after handling
func (m *Client) ListenSetter(topic string, callback func(string)) {
func (m *Client) ListenSetter(topic string, callback func(string) error) {
m.Listen(topic, func(payload string) {
callback(payload)
if err := callback(payload); err != nil {
m.log.ERROR.Printf("set %s: %v", strings.TrimSuffix(topic, "/set"), err)
}
if err := m.Publish(topic, true, ""); err != nil {
m.log.ERROR.Printf("clear: %s: %v", topic, err)
}
Expand Down
112 changes: 48 additions & 64 deletions server/mqtt.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,69 +118,65 @@ func (m *MQTT) publish(topic string, retained bool, payload interface{}) {
}

func (m *MQTT) listenSetters(topic string, site site.API, lp loadpoint.API) {
m.Handler.ListenSetter(topic+"/mode/set", func(payload string) {
if mode, err := api.ChargeModeString(payload); err == nil {
m.Handler.ListenSetter(topic+"/mode/set", func(payload string) error {
mode, err := api.ChargeModeString(payload)
if err == nil {
lp.SetMode(mode)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/mode", err)
}
return err
})
m.Handler.ListenSetter(topic+"/minSoc/set", func(payload string) {
if soc, err := strconv.Atoi(payload); err == nil {
m.Handler.ListenSetter(topic+"/minSoc/set", func(payload string) error {
soc, err := strconv.Atoi(payload)
if err == nil {
lp.SetMinSoc(soc)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/minSoc", err)
}
return err
})
m.Handler.ListenSetter(topic+"/targetEnergy/set", func(payload string) {
if val, err := parseFloat(payload); err == nil {
m.Handler.ListenSetter(topic+"/targetEnergy/set", func(payload string) error {
val, err := parseFloat(payload)
if err == nil {
lp.SetTargetEnergy(val)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/targetEnergy", err)
}
return err
})
m.Handler.ListenSetter(topic+"/targetSoc/set", func(payload string) {
if soc, err := strconv.Atoi(payload); err == nil {
m.Handler.ListenSetter(topic+"/targetSoc/set", func(payload string) error {
soc, err := strconv.Atoi(payload)
if err == nil {
lp.SetTargetSoc(soc)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/targetSoc", err)
}
return err
})
m.Handler.ListenSetter(topic+"/targetTime/set", func(payload string) {
m.Handler.ListenSetter(topic+"/targetTime/set", func(payload string) error {
val, err := time.Parse(time.RFC3339, payload)
if err == nil {
err = lp.SetTargetTime(val)
} else if string(payload) == "null" {
err = lp.SetTargetTime(time.Time{})
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", topic+"/targetTime", err)
}
return err
})
m.Handler.ListenSetter(topic+"/minCurrent/set", func(payload string) {
if current, err := parseFloat(payload); err == nil {
m.Handler.ListenSetter(topic+"/minCurrent/set", func(payload string) error {
current, err := parseFloat(payload)
if err == nil {
lp.SetMinCurrent(current)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/minCurrent", err)
}
return err
})
m.Handler.ListenSetter(topic+"/maxCurrent/set", func(payload string) {
if current, err := parseFloat(payload); err == nil {
m.Handler.ListenSetter(topic+"/maxCurrent/set", func(payload string) error {
current, err := parseFloat(payload)
if err == nil {
lp.SetMaxCurrent(current)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/maxCurrent", err)
}
return err
})
m.Handler.ListenSetter(topic+"/phases/set", func(payload string) {
m.Handler.ListenSetter(topic+"/phases/set", func(payload string) error {
phases, err := strconv.Atoi(payload)
if err == nil {
err = lp.SetPhases(phases)
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", topic+"/phases", err)
}
return err
})
m.Handler.ListenSetter(topic+"/vehicle/set", func(payload string) {
m.Handler.ListenSetter(topic+"/vehicle/set", func(payload string) error {
vehicle, err := strconv.Atoi(payload)
if err == nil {
if vehicle > 0 {
Expand All @@ -193,23 +189,21 @@ func (m *MQTT) listenSetters(topic string, site site.API, lp loadpoint.API) {
lp.SetVehicle(nil)
}
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", topic+"/vehicle", err)
}
return err
})
m.Handler.ListenSetter(topic+"/enableThreshold/set", func(payload string) {
if threshold, err := parseFloat(payload); err == nil {
m.Handler.ListenSetter(topic+"/enableThreshold/set", func(payload string) error {
threshold, err := parseFloat(payload)
if err == nil {
lp.SetEnableThreshold(threshold)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/enableThreshold", err)
}
return err
})
m.Handler.ListenSetter(topic+"/disableThreshold/set", func(payload string) {
if threshold, err := parseFloat(payload); err == nil {
m.Handler.ListenSetter(topic+"/disableThreshold/set", func(payload string) error {
threshold, err := parseFloat(payload)
if err == nil {
lp.SetDisableThreshold(threshold)
} else {
m.log.ERROR.Printf("set %s: %v", topic+"/disableThreshold", err)
}
return err
})
}

Expand All @@ -220,54 +214,44 @@ func (m *MQTT) Run(site site.API, in <-chan util.Param) {
m.publish(topic, true, "online")

// site setters
m.Handler.ListenSetter(m.root+"/site/prioritySoc/set", func(payload string) {
m.Handler.ListenSetter(m.root+"/site/prioritySoc/set", func(payload string) error {
val, err := parseFloat(payload)
if err == nil {
err = site.SetPrioritySoc(val)
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", m.root+"/site/prioritySoc", err)
}
return err
})

m.Handler.ListenSetter(m.root+"/site/bufferSoc/set", func(payload string) {
m.Handler.ListenSetter(m.root+"/site/bufferSoc/set", func(payload string) error {
val, err := parseFloat(payload)
if err == nil {
err = site.SetBufferSoc(val)
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", m.root+"/site/bufferSoc", err)
}
return err
})

m.Handler.ListenSetter(m.root+"/site/bufferStartSoc/set", func(payload string) {
m.Handler.ListenSetter(m.root+"/site/bufferStartSoc/set", func(payload string) error {
val, err := parseFloat(payload)
if err == nil {
err = site.SetBufferStartSoc(val)
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", m.root+"/site/bufferStartSoc", err)
}
return err
})

m.Handler.ListenSetter(m.root+"/site/residualPower/set", func(payload string) {
m.Handler.ListenSetter(m.root+"/site/residualPower/set", func(payload string) error {
val, err := parseFloat(payload)
if err == nil {
err = site.SetResidualPower(val)
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", m.root+"/site/residualPower", err)
}
return err
})

m.Handler.ListenSetter(m.root+"/site/smartcostlimit/set", func(payload string) {
m.Handler.ListenSetter(m.root+"/site/smartcostlimit/set", func(payload string) error {
val, err := parseFloat(payload)
if err == nil {
err = site.SetSmartCostLimit(val)
}
if err != nil {
m.log.ERROR.Printf("set %s: %v", m.root+"/site/smartcostlimit", err)
}
return err
})

// number of loadpoints
Expand Down

0 comments on commit 38777d1

Please sign in to comment.