Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0150a0d120 | ||
|
|
b9c6aa9d02 | ||
|
|
4221815def | ||
|
|
eabac1b11b | ||
|
|
2d6db85b8c | ||
|
|
8e742ba1d3 | ||
|
|
2dc3baca3c | ||
|
|
274a80d605 | ||
|
|
3b38be8de5 |
36
models/bus.go
Normal file
36
models/bus.go
Normal 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
46
models/driver.go
Normal 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
|
||||
}
|
||||
@@ -3,23 +3,18 @@ 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
|
||||
func (d *Drivers) AddNewDriver(typ string) *Driver {
|
||||
if drv, ok := (*d)[typ]; ok {
|
||||
return drv
|
||||
}
|
||||
(*d)[typ] = &Driver{Type: typ}
|
||||
return (*d)[typ]
|
||||
}
|
||||
|
||||
// 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(drv Driver) {
|
||||
if driver, ok := (*d)[drv.Type]; ok {
|
||||
driver.AddBuses(drv.Buses)
|
||||
(*d)[drv.Type] = driver
|
||||
}
|
||||
(*d)[drv.Type] = &drv
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ type Get struct {
|
||||
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||
Type Type `json:"type,omitempty"` // dbm datatype
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ type Set struct {
|
||||
Value any `json:"value,omitempty"` // dbm value
|
||||
Rights Rights `json:"rights,omitempty"` // dbm read /write rights
|
||||
Create bool `json:"create,omitempty"` // dbm create new datapoint
|
||||
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||
Updated bool `json:"-"`
|
||||
}
|
||||
|
||||
@@ -36,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.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
|
||||
Depth uint `json:"depth,omitempty"` // depth of subscriptions from found path or uuid
|
||||
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
|
||||
Driver string `json:"driver,omitempty"` // driver type to assign this subscription
|
||||
OnCreate bool `json:"onCreate,omitempty"` // notify at datapoint creation
|
||||
|
||||
29
models/topic.go
Normal file
29
models/topic.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ const (
|
||||
LOU Type = "LOU" // UINT64
|
||||
F32 Type = "F32" // FLOAT32
|
||||
F64 Type = "F64" // FLOAT64
|
||||
STR Type = "STRING" // STRING
|
||||
STR Type = "STR" // STRING
|
||||
)
|
||||
|
||||
// dbm datatype model
|
||||
|
||||
@@ -36,8 +36,8 @@ func Float32From(v any) float32 {
|
||||
case uint64:
|
||||
return float32(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return float32(i)
|
||||
if f64, err := strconv.ParseFloat(val, 32); err == nil {
|
||||
return float32(f64)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
@@ -76,8 +76,8 @@ func Float64From(v any) float64 {
|
||||
case uint64:
|
||||
return float64(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return float64(i)
|
||||
if f64, err := strconv.ParseFloat(val, 32); err == nil {
|
||||
return f64
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
@@ -433,7 +433,14 @@ func BoolFrom(v any) bool {
|
||||
case float64:
|
||||
return val >= 1
|
||||
case string:
|
||||
return strings.ToLower(val) == "false" || v == "0"
|
||||
if strings.ToLower(val) == "true" {
|
||||
return true
|
||||
} else if strings.ToLower(val) == "false" {
|
||||
return false
|
||||
} else if i, err := strconv.Atoi(val); err == nil {
|
||||
return i > 0
|
||||
}
|
||||
return false
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user