72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package models
|
|
|
|
// driver model
|
|
type Driver struct {
|
|
Type string `json:"type"` // driver type name of driver in collection
|
|
Buses []*Bus `json:"buses,omitempty"` // name of driver bus
|
|
}
|
|
|
|
func (d *Driver) AddNewBus(name string) *Bus {
|
|
for _, b := range d.Buses {
|
|
if b.Name == name {
|
|
return b
|
|
}
|
|
}
|
|
b := &Bus{Name: name}
|
|
d.Buses = append(d.Buses, b)
|
|
return b
|
|
}
|
|
|
|
func (d *Driver) AddBuses(buses []*Bus) {
|
|
next:
|
|
for _, newBus := range buses {
|
|
for _, currentBus := range d.Buses {
|
|
if currentBus.Name != newBus.Name {
|
|
continue
|
|
}
|
|
currentBus.AddAddress(newBus.Address...)
|
|
if newBus.Topic == nil {
|
|
continue next
|
|
}
|
|
currentBus.AddSubscription(newBus.Topic.Subscribe...)
|
|
currentBus.AddPublish(newBus.Topic.Publish...)
|
|
continue next
|
|
}
|
|
d.Buses = append(d.Buses, newBus)
|
|
}
|
|
}
|
|
|
|
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...)
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|
|
|
|
func (d *Driver) GetBus(name string) *Bus {
|
|
for _, b := range d.Buses {
|
|
if b.Name == name {
|
|
return b
|
|
}
|
|
}
|
|
return nil
|
|
}
|