Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6dffd1fad4 | ||
|
|
c15710ddec | ||
|
|
2924d95f4b | ||
|
|
a39dcc06f4 | ||
|
|
e720f0d2d3 | ||
|
|
ce3ea4746b | ||
|
|
0150a0d120 | ||
|
|
b9c6aa9d02 | ||
|
|
4221815def | ||
|
|
eabac1b11b | ||
|
|
2d6db85b8c | ||
|
|
8e742ba1d3 | ||
|
|
2dc3baca3c |
56
models/bus.go
Normal file
56
models/bus.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
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...)
|
||||||
|
}
|
||||||
|
|
||||||
|
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...)
|
||||||
|
}
|
||||||
71
models/driver.go
Normal file
71
models/driver.go
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
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) RemoveBuses(buses []*Bus) {
|
||||||
|
for _, bus := range buses {
|
||||||
|
for i := 0; i < len(d.Buses); i++ {
|
||||||
|
currentBus := d.Buses[i]
|
||||||
|
if currentBus.Name != bus.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
currentBus.RemoveAddress(bus.Address...)
|
||||||
|
|
||||||
|
shouldRemove := true
|
||||||
|
if bus.Topic != nil {
|
||||||
|
currentBus.RemoveSubscription(bus.Topic.Subscribe...)
|
||||||
|
currentBus.RemovePublish(bus.Topic.Publish...)
|
||||||
|
shouldRemove = len(currentBus.Topic.Subscribe) == 0 && len(currentBus.Topic.Publish) == 0
|
||||||
|
}
|
||||||
|
if len(currentBus.Address) == 0 && shouldRemove {
|
||||||
|
d.Buses = append(d.Buses[:i], d.Buses[i+1:]...)
|
||||||
|
i-- // adjust index after removal
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Driver) GetBus(name string) *Bus {
|
||||||
|
for _, b := range d.Buses {
|
||||||
|
if b.Name == name {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -3,23 +3,34 @@ 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
|
drv := &Driver{Type: typ}
|
||||||
|
(*d)[typ] = drv
|
||||||
|
return drv
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
(*d)[drv.Type] = drv
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new driver to driver collection
|
func (d *Drivers) RemoveDriver(drv *Driver) {
|
||||||
// typ is the name of the driver in driver collection
|
if driver, ok := (*d)[drv.Type]; ok {
|
||||||
func (d *Drivers) AddDriver(typ, bus string, address uint) {
|
driver.RemoveBuses(drv.Buses)
|
||||||
(*d)[typ] = &Driver{
|
}
|
||||||
Bus: bus,
|
d.CleanEmptyDrivers()
|
||||||
Address: address,
|
}
|
||||||
|
|
||||||
|
func (d *Drivers) CleanEmptyDrivers() {
|
||||||
|
for key, drv := range *d {
|
||||||
|
if len(drv.Buses) == 0 {
|
||||||
|
delete(*d, key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ type Get struct {
|
|||||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Type Type `json:"type,omitempty"` // dbm datatype
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
Value any `json:"value,omitempty"` // dbm value
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
HasChild bool `json:"hasChild,omitempty"` // inidicates path has child/ren
|
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||||
Rights Rights `json:"rights,omitempty"` // dbm read /write rights
|
Rights Rights `json:"rights,omitempty"` // dbm read /write rights
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,11 +9,12 @@ import (
|
|||||||
|
|
||||||
// publish model
|
// publish model
|
||||||
type Publish struct {
|
type Publish struct {
|
||||||
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||||
Path string `json:"path,omitempty"` // dbm path
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Type Type `json:"type,omitempty"` // dbm datatype
|
Path string `json:"path,omitempty"` // dbm path
|
||||||
Value any `json:"value,omitempty"` // dbm value
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns input parameter as assigned dbm datatype
|
// returns input parameter as assigned dbm datatype
|
||||||
|
|||||||
@@ -4,15 +4,16 @@ import "github.com/google/uuid"
|
|||||||
|
|
||||||
// Set model
|
// Set model
|
||||||
type Set struct {
|
type Set struct {
|
||||||
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||||
Path string `json:"path"` // dbm path
|
Path string `json:"path"` // dbm path
|
||||||
Driver *Driver `json:"driver,omitempty"` // assign new driver
|
Driver *Driver `json:"driver,omitempty"` // assign new driver
|
||||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Type Type `json:"type,omitempty"` // dbm datatype
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
Value any `json:"value,omitempty"` // dbm value
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
Rights Rights `json:"rights,omitempty"` // dbm read /write rights
|
Rights Rights `json:"rights,omitempty"` // dbm read /write rights
|
||||||
Create bool `json:"create,omitempty"` // dbm create new datapoint
|
Create bool `json:"create,omitempty"` // dbm create new datapoint
|
||||||
Updated bool `json:"-"`
|
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||||
|
Updated bool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// sets value and path
|
// sets value and path
|
||||||
@@ -36,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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ type Subscription struct {
|
|||||||
Path string `json:"path,omitempty"` // dbm path
|
Path string `json:"path,omitempty"` // dbm path
|
||||||
Depth uint `json:"depth,omitempty"` // depth of subscriptions from found path or uuid
|
Depth uint `json:"depth,omitempty"` // depth of subscriptions from found path or uuid
|
||||||
Value any `json:"value,omitempty"` // current value
|
Value any `json:"value,omitempty"` // current value
|
||||||
HasChild bool `json:"hasChild,omitempty"` // inidicates path has child/ren
|
Rights Rights `json:"rights,omitempty"` // read /write rights
|
||||||
|
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Driver string `json:"driver,omitempty"` // driver type to assign this subscription
|
Driver string `json:"driver,omitempty"` // driver type to assign this subscription
|
||||||
OnCreate bool `json:"onCreate,omitempty"` // notify at datapoint creation
|
OnCreate bool `json:"onCreate,omitempty"` // notify at datapoint creation
|
||||||
|
|||||||
60
models/topic.go
Normal file
60
models/topic.go
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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