modify and add new fields for drivers
This commit is contained in:
31
models/bus.go
Normal file
31
models/bus.go
Normal file
@@ -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)
|
||||
}
|
25
models/driver.go
Normal file
25
models/driver.go
Normal file
@@ -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
|
||||
}
|
@@ -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]
|
||||
}
|
||||
|
@@ -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)
|
||||
}
|
||||
|
23
models/topic.go
Normal file
23
models/topic.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user