add remove buses when empty

This commit is contained in:
Adrian Zuercher
2025-07-30 17:14:46 +02:00
parent 2924d95f4b
commit c15710ddec
3 changed files with 15 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
package models package models
import "slices" import (
"slices"
)
// driver model // driver model
type Driver struct { type Driver struct {
@@ -46,9 +48,11 @@ func (d *Driver) RemoveBuses(buses []*Bus) {
} }
currentBus.RemoveAddress(bus.Address...) currentBus.RemoveAddress(bus.Address...)
if bus.Topic != nil { if bus.Topic != nil {
currentBus.RemoveSubscription(bus.Topic.Subscribe...) currentBus.RemoveSubscription(bus.Topic.Subscribe...)
currentBus.RemovePublish(bus.Topic.Publish...) currentBus.RemovePublish(bus.Topic.Publish...)
if len(currentBus.Address) == 0 { if len(currentBus.Address) == 0 {
var remove bool = true var remove bool = true
if currentBus.Topic != nil { if currentBus.Topic != nil {

View File

@@ -23,6 +23,12 @@ func (d *Drivers) AddDriver(drv *Driver) {
func (d *Drivers) RemoveDriver(drv *Driver) { func (d *Drivers) RemoveDriver(drv *Driver) {
if driver, ok := (*d)[drv.Type]; ok { if driver, ok := (*d)[drv.Type]; ok {
driver.RemoveBuses(drv.Buses) driver.RemoveBuses(drv.Buses)
if len(driver.Buses) == 1 {
if len(driver.Buses[0].Address) == 0 && driver.Buses[0].Topic == nil {
(*d)[drv.Type] = &Driver{}
return
}
}
(*d)[drv.Type] = driver (*d)[drv.Type] = driver
return return
} }

View File

@@ -1,6 +1,8 @@
package models package models
import "slices" import (
"slices"
)
// topic model // topic model
type Topic struct { type Topic struct {
@@ -33,6 +35,7 @@ func (t *Topic) RemoveSubscription(subs ...string) {
t.Subscribe = []string{} t.Subscribe = []string{}
return return
} }
for _, toDelete := range subs { for _, toDelete := range subs {
for i, a := range t.Subscribe { for i, a := range t.Subscribe {
if a == toDelete { if a == toDelete {