Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2924d95f4b | ||
|
|
a39dcc06f4 | ||
|
|
e720f0d2d3 | ||
|
|
ce3ea4746b | ||
|
|
0150a0d120 | ||
|
|
b9c6aa9d02 | ||
|
|
4221815def |
@@ -4,28 +4,53 @@ import "slices"
|
|||||||
|
|
||||||
// bus model
|
// bus model
|
||||||
type Bus struct {
|
type Bus struct {
|
||||||
Name string `json:"name,omitemtpy"`
|
Name string `json:"name"`
|
||||||
Address []uint `json:"address,omitemtpy"` // address of bus
|
Address []uint `json:"address,omitempty"` // address of bus
|
||||||
Topic *Topic `json:"topic,omitemtpy"` // address of bus
|
Topic *Topic `json:"topic,omitempty"` // address of bus
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bus) AddAddress(address uint) {
|
func (b *Bus) AddAddress(address ...uint) {
|
||||||
if !slices.Contains(b.Address, address) {
|
for _, a := range address {
|
||||||
b.Address = append(b.Address, address)
|
if slices.Contains(b.Address, a) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
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) {
|
||||||
if b.Topic == nil {
|
if b.Topic == nil {
|
||||||
b.Topic = &Topic{}
|
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 {
|
if b.Topic == nil {
|
||||||
b.Topic = &Topic{}
|
b.Topic = &Topic{}
|
||||||
}
|
}
|
||||||
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...)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +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) AddBus(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 []*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) RemoveBuses(buses []*Bus) {
|
||||||
|
for _, bus := range buses {
|
||||||
|
for _, currentBus := range d.Buses {
|
||||||
|
if currentBus.Name != bus.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,10 +3,27 @@ package models
|
|||||||
// collection of drivers ordered in map
|
// collection of drivers ordered in map
|
||||||
type Drivers map[string]*Driver
|
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 {
|
if drv, ok := (*d)[typ]; ok {
|
||||||
return drv
|
return drv
|
||||||
}
|
}
|
||||||
(*d)[typ] = &Driver{Type: typ}
|
(*d)[typ] = &Driver{Type: typ}
|
||||||
return (*d)[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
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -41,8 +41,8 @@ func (s *Set) NewDriverAddress(typ, bus string, address uint) {
|
|||||||
if s.Drivers == nil {
|
if s.Drivers == nil {
|
||||||
s.Drivers = &Drivers{}
|
s.Drivers = &Drivers{}
|
||||||
}
|
}
|
||||||
drv := s.Drivers.AddDriver(typ)
|
drv := s.Drivers.AddNewDriver(typ)
|
||||||
b := drv.AddBus(bus)
|
b := drv.AddNewBus(bus)
|
||||||
b.AddAddress(address)
|
b.AddAddress(address)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,8 +51,8 @@ func (s *Set) NewDriverSubscribe(typ, bus string, sub string) {
|
|||||||
if s.Drivers == nil {
|
if s.Drivers == nil {
|
||||||
s.Drivers = &Drivers{}
|
s.Drivers = &Drivers{}
|
||||||
}
|
}
|
||||||
drv := s.Drivers.AddDriver(typ)
|
drv := s.Drivers.AddNewDriver(typ)
|
||||||
b := drv.AddBus(bus)
|
b := drv.AddNewBus(bus)
|
||||||
b.AddSubscription(sub)
|
b.AddSubscription(sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ func (s *Set) NewDriverPublish(typ, bus string, pub string) {
|
|||||||
if s.Drivers == nil {
|
if s.Drivers == nil {
|
||||||
s.Drivers = &Drivers{}
|
s.Drivers = &Drivers{}
|
||||||
}
|
}
|
||||||
drv := s.Drivers.AddDriver(typ)
|
drv := s.Drivers.AddNewDriver(typ)
|
||||||
b := drv.AddBus(bus)
|
b := drv.AddNewBus(bus)
|
||||||
b.AddPublish(pub)
|
b.AddPublish(pub)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,54 @@ import "slices"
|
|||||||
|
|
||||||
// topic model
|
// topic model
|
||||||
type Topic struct {
|
type Topic struct {
|
||||||
Publish []string `json:"Publish,omitemtpy"`
|
Publish []string `json:"publish,omitempty"`
|
||||||
Subscribe []string `json:"subscribe,omitemtpy"`
|
Subscribe []string `json:"subscribe,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Topic) AddSubscription(sub string) {
|
func (t *Topic) AddSubscription(subs ...string) {
|
||||||
if !slices.Contains(t.Subscribe, sub) {
|
for _, sub := range subs {
|
||||||
|
if slices.Contains(t.Subscribe, sub) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
t.Subscribe = append(t.Subscribe, sub)
|
t.Subscribe = append(t.Subscribe, sub)
|
||||||
slices.Sort(t.Subscribe)
|
slices.Sort(t.Subscribe)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Topic) AddPublish(pub string) {
|
func (t *Topic) AddPublish(pubs ...string) {
|
||||||
if !slices.Contains(t.Publish, pub) {
|
for _, pub := range pubs {
|
||||||
t.Publish = append(t.Publish, pub)
|
if slices.Contains(t.Publish, pub) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
t.Subscribe = append(t.Publish, pub)
|
||||||
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user