47 lines
952 B
Go
47 lines
952 B
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) GetBus(name string) *Bus {
|
|
for _, b := range d.Buses {
|
|
if b.Name == name {
|
|
return b
|
|
}
|
|
}
|
|
return nil
|
|
}
|