Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dffd1fad4 | ||
|
|
c15710ddec | ||
|
|
2924d95f4b |
@@ -1,7 +1,5 @@
|
||||
package models
|
||||
|
||||
import "slices"
|
||||
|
||||
// driver model
|
||||
type Driver struct {
|
||||
Type string `json:"type"` // driver type name of driver in collection
|
||||
@@ -38,32 +36,25 @@ next:
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Driver) RemoveBus(bus *Bus) {
|
||||
for _, currentBus := range d.Buses {
|
||||
if currentBus.Name != bus.Name {
|
||||
continue
|
||||
}
|
||||
func (d *Driver) RemoveBuses(buses []*Bus) {
|
||||
for _, bus := range buses {
|
||||
for i := 0; i < len(d.Buses); i++ {
|
||||
currentBus := d.Buses[i]
|
||||
if currentBus.Name != bus.Name {
|
||||
continue
|
||||
}
|
||||
|
||||
currentBus.RemoveAddress(bus.Address...)
|
||||
if bus.Topic != nil {
|
||||
currentBus.RemoveSubscription(bus.Topic.Subscribe...)
|
||||
currentBus.RemovePublish(bus.Topic.Publish...)
|
||||
if len(currentBus.Address) == 0 {
|
||||
var remove bool = true
|
||||
if currentBus.Topic != nil {
|
||||
remove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
|
||||
}
|
||||
if remove {
|
||||
if len(d.Buses) == 1 {
|
||||
d.Buses = []*Bus{}
|
||||
return
|
||||
}
|
||||
for i, b := range d.Buses {
|
||||
if bus.Name == b.Name {
|
||||
d.Buses = slices.Delete(d.Buses, i, i+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
currentBus.RemoveAddress(bus.Address...)
|
||||
|
||||
shouldRemove := true
|
||||
if bus.Topic != nil {
|
||||
currentBus.RemoveSubscription(bus.Topic.Subscribe...)
|
||||
currentBus.RemovePublish(bus.Topic.Publish...)
|
||||
shouldRemove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
|
||||
}
|
||||
if len(currentBus.Address) == 0 && shouldRemove {
|
||||
d.Buses = append(d.Buses[:i], d.Buses[i+1:]...)
|
||||
i-- // adjust index after removal
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
@@ -7,15 +7,30 @@ func (d *Drivers) AddNewDriver(typ string) *Driver {
|
||||
if drv, ok := (*d)[typ]; ok {
|
||||
return drv
|
||||
}
|
||||
(*d)[typ] = &Driver{Type: typ}
|
||||
return (*d)[typ]
|
||||
drv := &Driver{Type: typ}
|
||||
(*d)[typ] = drv
|
||||
return drv
|
||||
}
|
||||
|
||||
func (d *Drivers) AddDriver(drv *Driver) {
|
||||
if driver, ok := (*d)[drv.Type]; ok {
|
||||
driver.AddBuses(drv.Buses)
|
||||
(*d)[drv.Type] = driver
|
||||
return
|
||||
}
|
||||
(*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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
|
||||
// publish model
|
||||
type Publish struct {
|
||||
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||
Path string `json:"path,omitempty"` // dbm path
|
||||
Type Type `json:"type,omitempty"` // dbm datatype
|
||||
Value any `json:"value,omitempty"` // dbm value
|
||||
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||
Path string `json:"path,omitempty"` // dbm path
|
||||
Type Type `json:"type,omitempty"` // dbm datatype
|
||||
Value any `json:"value,omitempty"` // dbm value
|
||||
}
|
||||
|
||||
// returns input parameter as assigned dbm datatype
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
import "slices"
|
||||
import (
|
||||
"slices"
|
||||
)
|
||||
|
||||
// topic model
|
||||
type Topic struct {
|
||||
@@ -33,6 +35,7 @@ func (t *Topic) RemoveSubscription(subs ...string) {
|
||||
t.Subscribe = []string{}
|
||||
return
|
||||
}
|
||||
|
||||
for _, toDelete := range subs {
|
||||
for i, a := range t.Subscribe {
|
||||
if a == toDelete {
|
||||
|
||||
Reference in New Issue
Block a user