3 Commits

Author SHA1 Message Date
Adrian Zuercher
6dffd1fad4 bug fix and improvment remove driver 2025-07-31 08:35:18 +02:00
Adrian Zuercher
c15710ddec add remove buses when empty 2025-07-30 17:14:46 +02:00
Adrian Zuercher
2924d95f4b add remove driver function 2025-07-30 12:12:18 +02:00
4 changed files with 46 additions and 36 deletions

View File

@@ -1,7 +1,5 @@
package models package models
import "slices"
// driver model // driver model
type Driver struct { type Driver struct {
Type string `json:"type"` // driver type name of driver in collection Type string `json:"type"` // driver type name of driver in collection
@@ -38,32 +36,25 @@ next:
} }
} }
func (d *Driver) RemoveBus(bus *Bus) { func (d *Driver) RemoveBuses(buses []*Bus) {
for _, currentBus := range d.Buses { for _, bus := range buses {
if currentBus.Name != bus.Name { for i := 0; i < len(d.Buses); i++ {
continue currentBus := d.Buses[i]
} if currentBus.Name != bus.Name {
continue
}
currentBus.RemoveAddress(bus.Address...) currentBus.RemoveAddress(bus.Address...)
if bus.Topic != nil {
currentBus.RemoveSubscription(bus.Topic.Subscribe...) shouldRemove := true
currentBus.RemovePublish(bus.Topic.Publish...) if bus.Topic != nil {
if len(currentBus.Address) == 0 { currentBus.RemoveSubscription(bus.Topic.Subscribe...)
var remove bool = true currentBus.RemovePublish(bus.Topic.Publish...)
if currentBus.Topic != nil { shouldRemove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
remove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0 }
} if len(currentBus.Address) == 0 && shouldRemove {
if remove { d.Buses = append(d.Buses[:i], d.Buses[i+1:]...)
if len(d.Buses) == 1 { i-- // adjust index after removal
d.Buses = []*Bus{}
return
}
for i, b := range d.Buses {
if bus.Name == b.Name {
d.Buses = slices.Delete(d.Buses, i, i+1)
}
}
}
} }
break break
} }

View File

@@ -7,15 +7,30 @@ func (d *Drivers) AddNewDriver(typ string) *Driver {
if drv, ok := (*d)[typ]; ok { if drv, ok := (*d)[typ]; ok {
return drv return drv
} }
(*d)[typ] = &Driver{Type: typ} drv := &Driver{Type: typ}
return (*d)[typ] (*d)[typ] = drv
return drv
} }
func (d *Drivers) AddDriver(drv *Driver) { func (d *Drivers) AddDriver(drv *Driver) {
if driver, ok := (*d)[drv.Type]; ok { if driver, ok := (*d)[drv.Type]; ok {
driver.AddBuses(drv.Buses) driver.AddBuses(drv.Buses)
(*d)[drv.Type] = driver
return return
} }
(*d)[drv.Type] = drv (*d)[drv.Type] = drv
} }
func (d *Drivers) RemoveDriver(drv *Driver) {
if driver, ok := (*d)[drv.Type]; ok {
driver.RemoveBuses(drv.Buses)
}
d.CleanEmptyDrivers()
}
func (d *Drivers) CleanEmptyDrivers() {
for key, drv := range *d {
if len(drv.Buses) == 0 {
delete(*d, key)
}
}
}

View File

@@ -9,11 +9,12 @@ import (
// publish model // publish model
type Publish struct { type Publish struct {
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
Path string `json:"path,omitempty"` // dbm path Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
Type Type `json:"type,omitempty"` // dbm datatype Path string `json:"path,omitempty"` // dbm path
Value any `json:"value,omitempty"` // dbm value Type Type `json:"type,omitempty"` // dbm datatype
Value any `json:"value,omitempty"` // dbm value
} }
// returns input parameter as assigned dbm datatype // returns input parameter as assigned dbm datatype

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 {