3 Commits

Author SHA1 Message Date
Adrian Zuercher
b9c6aa9d02 change map to slice 2025-07-29 14:20:54 +02:00
Adrian Zuercher
4221815def modify drivers 2025-07-29 12:37:04 +02:00
Adrian Zuercher
eabac1b11b modify and add new fields for drivers 2025-07-29 12:05:03 +02:00
5 changed files with 150 additions and 23 deletions

36
models/bus.go Normal file
View File

@@ -0,0 +1,36 @@
package models
import "slices"
// bus model
type Bus struct {
Name string `json:"name"`
Address []uint `json:"address,omitempty"` // address of bus
Topic *Topic `json:"topic,omitempty"` // address of bus
}
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) {
if b.Topic == nil {
b.Topic = &Topic{}
}
b.Topic.AddSubscription(sub...)
}
func (b *Bus) AddPublish(pub ...string) {
if b.Topic == nil {
b.Topic = &Topic{}
}
b.Topic.AddPublish(pub...)
}

46
models/driver.go Normal file
View File

@@ -0,0 +1,46 @@
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
}

View File

@@ -3,23 +3,18 @@ package models
// collection of drivers ordered in map // collection of drivers ordered in map
type Drivers map[string]*Driver type Drivers map[string]*Driver
// driver model func (d *Drivers) AddNewDriver(typ string) *Driver {
type Driver struct { if drv, ok := (*d)[typ]; ok {
Type string `json:"type,omitempty"` // driver type name of driver in collection return drv
Bus string `json:"bus"` // name of driver bus }
Address uint `json:"address"` // address of bus (*d)[typ] = &Driver{Type: typ}
return (*d)[typ]
} }
// returns a new collection of drivers func (d *Drivers) AddDriver(drv Driver) {
func NewDrivers() Drivers { if driver, ok := (*d)[drv.Type]; ok {
return make(Drivers) driver.AddBuses(drv.Buses)
} (*d)[drv.Type] = driver
// Add new driver to driver collection
// typ is the name of the driver in driver collection
func (d *Drivers) AddDriver(typ, bus string, address uint) {
(*d)[typ] = &Driver{
Bus: bus,
Address: address,
} }
(*d)[drv.Type] = &drv
} }

View File

@@ -37,10 +37,31 @@ func (s *Set) New(path string, typ Type, value any, rights Rights) {
} }
// sets new driver typ with bus and address // sets new driver typ with bus and address
func (s *Set) NewDriver(typ, bus string, address uint) { func (s *Set) NewDriverAddress(typ, bus string, address uint) {
s.Driver = &Driver{ if s.Drivers == nil {
Type: typ, s.Drivers = &Drivers{}
Bus: bus,
Address: address,
} }
drv := s.Drivers.AddNewDriver(typ)
b := drv.AddNewBus(bus)
b.AddAddress(address)
}
// sets new driver typ with bus and subscription
func (s *Set) NewDriverSubscribe(typ, bus string, sub string) {
if s.Drivers == nil {
s.Drivers = &Drivers{}
}
drv := s.Drivers.AddNewDriver(typ)
b := drv.AddNewBus(bus)
b.AddSubscription(sub)
}
// sets new driver typ with bus and publish
func (s *Set) NewDriverPublish(typ, bus string, pub string) {
if s.Drivers == nil {
s.Drivers = &Drivers{}
}
drv := s.Drivers.AddNewDriver(typ)
b := drv.AddNewBus(bus)
b.AddPublish(pub)
} }

29
models/topic.go Normal file
View File

@@ -0,0 +1,29 @@
package models
import "slices"
// topic model
type Topic struct {
Publish []string `json:"Publish,omitempty"`
Subscribe []string `json:"subscribe,omitempty"`
}
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(pubs ...string) {
for _, pub := range pubs {
if slices.Contains(t.Publish, pub) {
continue
}
t.Subscribe = append(t.Publish, pub)
slices.Sort(t.Publish)
}
}