6 Commits

Author SHA1 Message Date
Adrian Zuercher
2924d95f4b add remove driver function 2025-07-30 12:12:18 +02:00
Adrian Zuercher
a39dcc06f4 add remove driver function 2025-07-30 11:55:30 +02:00
Adrian Zuercher
e720f0d2d3 change parameter to pointer 2025-07-29 22:17:13 +02:00
Adrian Zuercher
ce3ea4746b add missing return 2025-07-29 22:15:01 +02:00
Adrian Zuercher
0150a0d120 fix tipo 2025-07-29 14:56:37 +02:00
Adrian Zuercher
b9c6aa9d02 change map to slice 2025-07-29 14:20:54 +02:00
4 changed files with 122 additions and 26 deletions

View File

@@ -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
} }
@@ -16,9 +16,7 @@ func (b *Bus) AddAddress(address ...uint) {
} }
b.Address = append(b.Address, a) b.Address = append(b.Address, a)
slices.Sort(b.Address) slices.Sort(b.Address)
} }
} }
func (b *Bus) AddSubscription(sub ...string) { func (b *Bus) AddSubscription(sub ...string) {
@@ -34,3 +32,25 @@ func (b *Bus) AddPublish(pub ...string) {
} }
b.Topic.AddPublish(pub...) b.Topic.AddPublish(pub...)
} }
func (b *Bus) RemoveAddress(address ...uint) {
if len(b.Address) == 1 {
b.Address = []uint{}
return
}
for _, toDelete := range address {
for i, a := range b.Address {
if a == toDelete {
b.Address = slices.Delete(b.Address, i, i+1)
}
}
}
}
func (b *Bus) RemoveSubscription(sub ...string) {
b.Topic.RemoveSubscription(sub...)
}
func (b *Bus) RemovePublish(pub ...string) {
b.Topic.RemovePublish(pub...)
}

View File

@@ -1,43 +1,82 @@
package models package models
import "slices"
// 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 next
}
d.Buses = append(d.Buses, newBus)
}
}
func (d *Driver) RemoveBuses(buses []*Bus) {
for _, bus := range buses {
for _, currentBus := range d.Buses {
if currentBus.Name != bus.Name {
continue continue
} }
d.Buses[key] = newBus
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)
}
}
}
}
break
}
}
} }
} }
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
} }

View File

@@ -11,10 +11,19 @@ func (d *Drivers) AddNewDriver(typ string) *Driver {
return (*d)[typ] return (*d)[typ]
} }
func (d *Drivers) AddDriver(drv Driver) { func (d *Drivers) AddDriver(drv *Driver) {
if driver, ok := (*d)[drv.Type]; ok { if driver, ok := (*d)[drv.Type]; ok {
driver.AddBuses(drv.Buses) driver.AddBuses(drv.Buses)
(*d)[drv.Type] = driver (*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)[drv.Type] = driver
return
} }
(*d)[drv.Type] = &drv
} }

View File

@@ -4,7 +4,7 @@ import "slices"
// topic model // topic model
type Topic struct { type Topic struct {
Publish []string `json:"Publish,omitempty"` Publish []string `json:"publish,omitempty"`
Subscribe []string `json:"subscribe,omitempty"` Subscribe []string `json:"subscribe,omitempty"`
} }
@@ -27,3 +27,31 @@ func (t *Topic) AddPublish(pubs ...string) {
slices.Sort(t.Publish) slices.Sort(t.Publish)
} }
} }
func (t *Topic) RemoveSubscription(subs ...string) {
if len(t.Subscribe) == 1 {
t.Subscribe = []string{}
return
}
for _, toDelete := range subs {
for i, a := range t.Subscribe {
if a == toDelete {
t.Subscribe = slices.Delete(t.Subscribe, i, i+1)
}
}
}
}
func (t *Topic) RemovePublish(pubs ...string) {
if len(t.Publish) == 1 {
t.Publish = []string{}
return
}
for _, toDelete := range pubs {
for i, a := range t.Publish {
if a == toDelete {
t.Publish = slices.Delete(t.Publish, i, i+1)
}
}
}
}