change map to slice
This commit is contained in:
@@ -4,7 +4,7 @@ import "slices"
|
|||||||
|
|
||||||
// bus model
|
// bus model
|
||||||
type Bus struct {
|
type Bus struct {
|
||||||
Name string `json:"name,omitempty"`
|
Name string `json:"name"`
|
||||||
Address []uint `json:"address,omitempty"` // address of bus
|
Address []uint `json:"address,omitempty"` // address of bus
|
||||||
Topic *Topic `json:"topic,omitempty"` // address of bus
|
Topic *Topic `json:"topic,omitempty"` // address of bus
|
||||||
}
|
}
|
||||||
|
@@ -3,41 +3,44 @@ package models
|
|||||||
// 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
|
||||||
Buses map[string]*Bus `json:"buses"` // name of driver bus
|
Buses []*Bus `json:"buses,omitempty"` // name of driver bus
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) AddNewBus(name string) *Bus {
|
func (d *Driver) AddNewBus(name string) *Bus {
|
||||||
if d.Buses == nil {
|
for _, b := range d.Buses {
|
||||||
d.Buses = make(map[string]*Bus)
|
if b.Name == name {
|
||||||
}
|
|
||||||
if b, ok := d.Buses[name]; ok {
|
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
d.Buses[name] = &Bus{Name: name}
|
}
|
||||||
return d.Buses[name]
|
b := &Bus{Name: name}
|
||||||
|
d.Buses = append(d.Buses, b)
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) AddBuses(buses map[string]*Bus) {
|
func (d *Driver) AddBuses(buses []*Bus) {
|
||||||
if d.Buses == nil {
|
next:
|
||||||
d.Buses = make(map[string]*Bus)
|
for _, newBus := range buses {
|
||||||
|
for _, currentBus := range d.Buses {
|
||||||
|
if currentBus.Name != newBus.Name {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
for key, newBus := range buses {
|
|
||||||
if currentBus, ok := d.Buses[key]; ok {
|
|
||||||
currentBus.AddAddress(newBus.Address...)
|
currentBus.AddAddress(newBus.Address...)
|
||||||
if newBus.Topic == nil {
|
if newBus.Topic == nil {
|
||||||
continue
|
continue next
|
||||||
}
|
}
|
||||||
currentBus.AddSubscription(newBus.Topic.Subscribe...)
|
currentBus.AddSubscription(newBus.Topic.Subscribe...)
|
||||||
currentBus.AddPublish(newBus.Topic.Publish...)
|
currentBus.AddPublish(newBus.Topic.Publish...)
|
||||||
continue
|
continue next
|
||||||
}
|
}
|
||||||
d.Buses[key] = newBus
|
d.Buses = append(d.Buses, newBus)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Driver) GetBus(name string) *Bus {
|
func (d *Driver) GetBus(name string) *Bus {
|
||||||
if b, ok := d.Buses[name]; ok {
|
for _, b := range d.Buses {
|
||||||
|
if b.Name == name {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user