modify drivers
This commit is contained in:
@@ -4,28 +4,33 @@ import "slices"
|
||||
|
||||
// bus model
|
||||
type Bus struct {
|
||||
Name string `json:"name,omitemtpy"`
|
||||
Address []uint `json:"address,omitemtpy"` // address of bus
|
||||
Topic *Topic `json:"topic,omitemtpy"` // address of bus
|
||||
Name string `json:"name,omitempty"`
|
||||
Address []uint `json:"address,omitempty"` // address of bus
|
||||
Topic *Topic `json:"topic,omitempty"` // address of bus
|
||||
}
|
||||
|
||||
func (b *Bus) AddAddress(address uint) {
|
||||
if !slices.Contains(b.Address, address) {
|
||||
b.Address = append(b.Address, address)
|
||||
func (b *Bus) AddAddress(address ...uint) {
|
||||
for _, a := range address {
|
||||
if slices.Contains(b.Address, a) {
|
||||
continue
|
||||
}
|
||||
b.Address = append(b.Address, a)
|
||||
slices.Sort(b.Address)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (b *Bus) AddSubscription(sub string) {
|
||||
}
|
||||
|
||||
func (b *Bus) AddSubscription(sub ...string) {
|
||||
if b.Topic == nil {
|
||||
b.Topic = &Topic{}
|
||||
}
|
||||
b.Topic.AddSubscription(sub)
|
||||
b.Topic.AddSubscription(sub...)
|
||||
}
|
||||
|
||||
func (b *Bus) AddPublish(pub string) {
|
||||
func (b *Bus) AddPublish(pub ...string) {
|
||||
if b.Topic == nil {
|
||||
b.Topic = &Topic{}
|
||||
}
|
||||
b.Topic.AddPublish(pub)
|
||||
b.Topic.AddPublish(pub...)
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@ type Driver struct {
|
||||
Buses map[string]*Bus `json:"buses"` // name of driver bus
|
||||
}
|
||||
|
||||
func (d *Driver) AddBus(name string) *Bus {
|
||||
func (d *Driver) AddNewBus(name string) *Bus {
|
||||
if d.Buses == nil {
|
||||
d.Buses = make(map[string]*Bus)
|
||||
}
|
||||
@@ -17,6 +17,24 @@ func (d *Driver) AddBus(name string) *Bus {
|
||||
return d.Buses[name]
|
||||
}
|
||||
|
||||
func (d *Driver) AddBuses(buses map[string]*Bus) {
|
||||
if d.Buses == nil {
|
||||
d.Buses = make(map[string]*Bus)
|
||||
}
|
||||
for key, newBus := range buses {
|
||||
if currentBus, ok := d.Buses[key]; ok {
|
||||
currentBus.AddAddress(newBus.Address...)
|
||||
if newBus.Topic == nil {
|
||||
continue
|
||||
}
|
||||
currentBus.AddSubscription(newBus.Topic.Subscribe...)
|
||||
currentBus.AddPublish(newBus.Topic.Publish...)
|
||||
continue
|
||||
}
|
||||
d.Buses[key] = newBus
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Driver) GetBus(name string) *Bus {
|
||||
if b, ok := d.Buses[name]; ok {
|
||||
return b
|
||||
|
@@ -3,10 +3,18 @@ package models
|
||||
// collection of drivers ordered in map
|
||||
type Drivers map[string]*Driver
|
||||
|
||||
func (d *Drivers) AddDriver(typ string) *Driver {
|
||||
func (d *Drivers) AddNewDriver(typ string) *Driver {
|
||||
if drv, ok := (*d)[typ]; ok {
|
||||
return drv
|
||||
}
|
||||
(*d)[typ] = &Driver{Type: typ}
|
||||
return (*d)[typ]
|
||||
}
|
||||
|
||||
func (d *Drivers) AddDriver(drv Driver) {
|
||||
if driver, ok := (*d)[drv.Type]; ok {
|
||||
driver.AddBuses(drv.Buses)
|
||||
(*d)[drv.Type] = driver
|
||||
}
|
||||
(*d)[drv.Type] = &drv
|
||||
}
|
||||
|
@@ -41,8 +41,8 @@ func (s *Set) NewDriverAddress(typ, bus string, address uint) {
|
||||
if s.Drivers == nil {
|
||||
s.Drivers = &Drivers{}
|
||||
}
|
||||
drv := s.Drivers.AddDriver(typ)
|
||||
b := drv.AddBus(bus)
|
||||
drv := s.Drivers.AddNewDriver(typ)
|
||||
b := drv.AddNewBus(bus)
|
||||
b.AddAddress(address)
|
||||
}
|
||||
|
||||
@@ -51,8 +51,8 @@ func (s *Set) NewDriverSubscribe(typ, bus string, sub string) {
|
||||
if s.Drivers == nil {
|
||||
s.Drivers = &Drivers{}
|
||||
}
|
||||
drv := s.Drivers.AddDriver(typ)
|
||||
b := drv.AddBus(bus)
|
||||
drv := s.Drivers.AddNewDriver(typ)
|
||||
b := drv.AddNewBus(bus)
|
||||
b.AddSubscription(sub)
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ func (s *Set) NewDriverPublish(typ, bus string, pub string) {
|
||||
if s.Drivers == nil {
|
||||
s.Drivers = &Drivers{}
|
||||
}
|
||||
drv := s.Drivers.AddDriver(typ)
|
||||
b := drv.AddBus(bus)
|
||||
drv := s.Drivers.AddNewDriver(typ)
|
||||
b := drv.AddNewBus(bus)
|
||||
b.AddPublish(pub)
|
||||
}
|
||||
|
@@ -4,20 +4,26 @@ import "slices"
|
||||
|
||||
// topic model
|
||||
type Topic struct {
|
||||
Publish []string `json:"Publish,omitemtpy"`
|
||||
Subscribe []string `json:"subscribe,omitemtpy"`
|
||||
Publish []string `json:"Publish,omitempty"`
|
||||
Subscribe []string `json:"subscribe,omitempty"`
|
||||
}
|
||||
|
||||
func (t *Topic) AddSubscription(sub string) {
|
||||
if !slices.Contains(t.Subscribe, sub) {
|
||||
func (t *Topic) AddSubscription(subs ...string) {
|
||||
for _, sub := range subs {
|
||||
if slices.Contains(t.Subscribe, sub) {
|
||||
continue
|
||||
}
|
||||
t.Subscribe = append(t.Subscribe, sub)
|
||||
slices.Sort(t.Subscribe)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Topic) AddPublish(pub string) {
|
||||
if !slices.Contains(t.Publish, pub) {
|
||||
t.Publish = append(t.Publish, pub)
|
||||
func (t *Topic) AddPublish(pubs ...string) {
|
||||
for _, pub := range pubs {
|
||||
if slices.Contains(t.Publish, pub) {
|
||||
continue
|
||||
}
|
||||
t.Subscribe = append(t.Publish, pub)
|
||||
slices.Sort(t.Publish)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user