Cardata: delete obsolete streaming containers (#32376)
This commit is contained in:
parent
c727537702
commit
58c4b446d6
3 changed files with 24 additions and 4 deletions
|
|
@ -33,7 +33,10 @@ var requiredKeys = []string{
|
|||
"vehicle.vehicle.travelledDistance",
|
||||
}
|
||||
|
||||
const requiredVersion = "v6"
|
||||
const (
|
||||
containerName = "evcc.io"
|
||||
requiredVersion = "v6"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
*request.Helper
|
||||
|
|
|
|||
|
|
@ -205,7 +205,7 @@ func (v *MqttConnector) handler(_ mqtt.Client, m mqtt.Message) {
|
|||
return
|
||||
}
|
||||
|
||||
v.log.TRACE.Println("recv: " + string(m.Payload()))
|
||||
v.log.TRACE.Printf("recv %s: %s", m.Topic(), string(m.Payload()))
|
||||
|
||||
v.mu.RLock()
|
||||
defer v.mu.RUnlock()
|
||||
|
|
|
|||
|
|
@ -71,14 +71,17 @@ func (v *Provider) findOrCreateContainer() (string, error) {
|
|||
return "", err
|
||||
}
|
||||
|
||||
// obsolete containers keep streaming, resulting in duplicate messages
|
||||
defer v.deleteObsoleteContainers(containers)
|
||||
|
||||
if i := slices.IndexFunc(containers, func(c Container) bool {
|
||||
return c.Name == "evcc.io" && c.Purpose == requiredVersion
|
||||
return c.Name == containerName && c.Purpose == requiredVersion
|
||||
}); i >= 0 {
|
||||
return containers[i].ContainerId, nil
|
||||
}
|
||||
|
||||
res, err := v.api.CreateContainer(CreateContainer{
|
||||
Name: "evcc.io",
|
||||
Name: containerName,
|
||||
Purpose: requiredVersion,
|
||||
TechnicalDescriptors: requiredKeys,
|
||||
})
|
||||
|
|
@ -86,6 +89,20 @@ func (v *Provider) findOrCreateContainer() (string, error) {
|
|||
return res.ContainerId, err
|
||||
}
|
||||
|
||||
func (v *Provider) deleteObsoleteContainers(containers []Container) {
|
||||
for _, c := range containers {
|
||||
if c.Name != containerName || c.Purpose == requiredVersion {
|
||||
continue
|
||||
}
|
||||
|
||||
v.log.DEBUG.Printf("deleting obsolete container %s (%s)", c.ContainerId, c.Purpose)
|
||||
|
||||
if err := v.api.DeleteContainer(c.ContainerId); err != nil {
|
||||
v.log.WARN.Printf("delete container %s: %v", c.ContainerId, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (v *Provider) setupContainer() error {
|
||||
container, err := v.findOrCreateContainer()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue