diff --git a/models/bus.go b/models/bus.go new file mode 100644 index 0000000..53300d7 --- /dev/null +++ b/models/bus.go @@ -0,0 +1,31 @@ +package models + +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 +} + +func (b *Bus) AddAddress(address uint) { + if !slices.Contains(b.Address, address) { + b.Address = append(b.Address, address) + 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) +} diff --git a/models/driver.go b/models/driver.go new file mode 100644 index 0000000..e107d95 --- /dev/null +++ b/models/driver.go @@ -0,0 +1,25 @@ +package models + +// driver model +type Driver struct { + Type string `json:"type"` // driver type name of driver in collection + Buses map[string]*Bus `json:"buses"` // name of driver bus +} + +func (d *Driver) AddBus(name string) *Bus { + if d.Buses == nil { + d.Buses = make(map[string]*Bus) + } + if b, ok := d.Buses[name]; ok { + return b + } + d.Buses[name] = &Bus{Name: name} + return d.Buses[name] +} + +func (d *Driver) GetBus(name string) *Bus { + if b, ok := d.Buses[name]; ok { + return b + } + return nil +} diff --git a/models/drivers.go b/models/drivers.go index b02fd80..0c55f60 100644 --- a/models/drivers.go +++ b/models/drivers.go @@ -3,23 +3,10 @@ package models // collection of drivers ordered in map type Drivers map[string]*Driver -// driver model -type Driver struct { - Type string `json:"type,omitempty"` // driver type name of driver in collection - Bus string `json:"bus"` // name of driver bus - Address uint `json:"address"` // address of bus -} - -// returns a new collection of drivers -func NewDrivers() Drivers { - return make(Drivers) -} - -// 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, +func (d *Drivers) AddDriver(typ string) *Driver { + if drv, ok := (*d)[typ]; ok { + return drv } + (*d)[typ] = &Driver{Type: typ} + return (*d)[typ] } diff --git a/models/set.go b/models/set.go index 7ed92c6..f02ed2a 100644 --- a/models/set.go +++ b/models/set.go @@ -37,10 +37,31 @@ func (s *Set) New(path string, typ Type, value any, rights Rights) { } // sets new driver typ with bus and address -func (s *Set) NewDriver(typ, bus string, address uint) { - s.Driver = &Driver{ - Type: typ, - Bus: bus, - Address: address, +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) + 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.AddDriver(typ) + b := drv.AddBus(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.AddDriver(typ) + b := drv.AddBus(bus) + b.AddPublish(pub) } diff --git a/models/topic.go b/models/topic.go new file mode 100644 index 0000000..1a6b222 --- /dev/null +++ b/models/topic.go @@ -0,0 +1,23 @@ +package models + +import "slices" + +// topic model +type Topic struct { + Publish []string `json:"Publish,omitemtpy"` + Subscribe []string `json:"subscribe,omitemtpy"` +} + +func (t *Topic) AddSubscription(sub string) { + if !slices.Contains(t.Subscribe, sub) { + 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) + slices.Sort(t.Publish) + } +}