Mqtt: cleanup on start (#12621)
This commit is contained in:
parent
c347dfab33
commit
ec8357739c
2 changed files with 27 additions and 19 deletions
|
|
@ -121,6 +121,28 @@ func (m *Client) ConnectionHandler(client paho.Client) {
|
|||
}
|
||||
}
|
||||
|
||||
// Cleanup recursively removes a topic
|
||||
func (m *Client) Cleanup(topic string, retained bool) error {
|
||||
if m.Client.Subscribe(topic, m.Qos, func(c paho.Client, msg paho.Message) {
|
||||
if len(msg.Payload()) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
m.log.TRACE.Printf("delete: %s", msg.Topic())
|
||||
m.Client.Publish(msg.Topic(), m.Qos, true, []byte{})
|
||||
}).WaitTimeout(request.Timeout) {
|
||||
return api.ErrTimeout
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
if m.Client.Unsubscribe(topic).WaitTimeout(request.Timeout) {
|
||||
return api.ErrTimeout
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Publish synchronously publishes payload using client qos
|
||||
func (m *Client) Publish(topic string, retained bool, payload interface{}) error {
|
||||
m.log.TRACE.Printf("send %s: '%v'", topic, payload)
|
||||
|
|
|
|||
|
|
@ -16,12 +16,6 @@ import (
|
|||
"github.com/evcc-io/evcc/util"
|
||||
)
|
||||
|
||||
var deprecatedTopics = []string{
|
||||
"activePhases", "range", "socCharge",
|
||||
"vehicleSoC", "batterySoC", "bufferSoC", "minSoC", "prioritySoC", "targetSoC", "vehicleTargetSoC", "vehicles",
|
||||
"savingsAmount", "savingsEffectivePrice", "savingsGridCharged", "savingsSelfConsumptionCharged", "savingsSelfConsumptionPercent", "savingsTotalCharged",
|
||||
}
|
||||
|
||||
// MQTT is the MQTT server. It uses the MQTT client for publishing.
|
||||
type MQTT struct {
|
||||
log *util.Logger
|
||||
|
|
@ -39,7 +33,11 @@ func NewMQTT(root string, site site.API) (*MQTT, error) {
|
|||
}
|
||||
m.publisher = m.publishString
|
||||
|
||||
err := m.Listen(site)
|
||||
err := m.Handler.Cleanup(m.root+"/#", true)
|
||||
if err == nil {
|
||||
err = m.Listen(site)
|
||||
}
|
||||
|
||||
return m, err
|
||||
}
|
||||
|
||||
|
|
@ -259,18 +257,6 @@ func (m *MQTT) Run(site site.API, in <-chan util.Param) {
|
|||
topic = fmt.Sprintf("%s/vehicles", m.root)
|
||||
m.publish(topic, true, len(site.Vehicles().Settings()))
|
||||
|
||||
// TODO remove deprecated topics
|
||||
for _, dep := range deprecatedTopics {
|
||||
m.publish(fmt.Sprintf("%s/site/%s", m.root, dep), true, nil)
|
||||
}
|
||||
|
||||
for id := range site.Loadpoints() {
|
||||
topic := fmt.Sprintf("%s/loadpoints/%d", m.root, id+1)
|
||||
for _, dep := range deprecatedTopics {
|
||||
m.publish(fmt.Sprintf("%s/%s", topic, dep), true, nil)
|
||||
}
|
||||
}
|
||||
|
||||
for i := 0; i < 10; i++ {
|
||||
m.publish(fmt.Sprintf("%s/site/pv/%d", m.root, i), true, nil)
|
||||
m.publish(fmt.Sprintf("%s/site/battery/%d", m.root, i), true, nil)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue