Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a39dcc06f4 | ||
|
|
e720f0d2d3 | ||
|
|
ce3ea4746b | ||
|
|
0150a0d120 | ||
|
|
b9c6aa9d02 | ||
|
|
4221815def | ||
|
|
eabac1b11b | ||
|
|
2d6db85b8c | ||
|
|
8e742ba1d3 | ||
|
|
2dc3baca3c | ||
|
|
274a80d605 | ||
|
|
3b38be8de5 | ||
|
|
83bf855485 | ||
|
|
9d4d417856 | ||
|
|
0446a160e1 | ||
|
|
640604c06d | ||
|
|
3accf6fc06 | ||
|
|
e3487eb340 | ||
|
|
c7154f0779 | ||
|
|
285a2add53 | ||
|
|
72d0d56868 | ||
|
|
ea4461fd7e | ||
|
|
0f22443b93 | ||
|
|
0d3161337f | ||
|
|
240f4fb3e7 | ||
|
|
bf36b023d3 | ||
|
|
a8127cc381 |
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...)
|
||||||
|
}
|
||||||
80
models/driver.go
Normal file
80
models/driver.go
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
|
// 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) RemoveBus(bus *Bus) {
|
||||||
|
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 {
|
||||||
|
for _, b := range d.Buses {
|
||||||
|
if b.Name == name {
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,20 +1,21 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
// collection of drivers ordered in map
|
||||||
type Drivers map[string]*Driver
|
type Drivers map[string]*Driver
|
||||||
|
|
||||||
type Driver struct {
|
func (d *Drivers) AddNewDriver(typ string) *Driver {
|
||||||
Type string `json:"type,omitempty"`
|
if drv, ok := (*d)[typ]; ok {
|
||||||
Bus string `json:"bus"`
|
return drv
|
||||||
Address uint `json:"address"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewDrivers() Drivers {
|
|
||||||
return make(Drivers)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Drivers) AddDriver(typ, bus string, address uint) {
|
|
||||||
(*d)[typ] = &Driver{
|
|
||||||
Bus: bus,
|
|
||||||
Address: address,
|
|
||||||
}
|
}
|
||||||
|
(*d)[typ] = &Driver{Type: 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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
// Json error response
|
||||||
type ErrorResponse struct {
|
type ErrorResponse struct {
|
||||||
Error bool `json:"error,omitempty"`
|
Error bool `json:"error,omitempty"`
|
||||||
Message string `json:"message,omitempty"`
|
Message string `json:"message,omitempty"`
|
||||||
|
|||||||
@@ -2,16 +2,19 @@ package models
|
|||||||
|
|
||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
|
// Get model
|
||||||
type Get struct {
|
type Get struct {
|
||||||
Uuid uuid.UUID `json:"uuid"`
|
Uuid uuid.UUID `json:"uuid"` // universally unique identifier
|
||||||
Path string `json:"path"`
|
Path string `json:"path"` // dbm path
|
||||||
Query *Query `json:"query,omitempty"`
|
Query *Query `json:"query,omitempty"` // query paramater
|
||||||
Drivers *Drivers `json:"driver,omitempty"`
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Type Type `json:"type,omitempty"`
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
Value any `json:"value,omitempty"`
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
Rights Rights `json:"rights,omitempty"`
|
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||||
|
Rights Rights `json:"rights,omitempty"` // dbm read /write rights
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// search dbm datapoints by path
|
||||||
func (g *Get) ByPath(path string, query *Query) {
|
func (g *Get) ByPath(path string, query *Query) {
|
||||||
g.Path = path
|
g.Path = path
|
||||||
if query != nil {
|
if query != nil {
|
||||||
@@ -19,6 +22,7 @@ func (g *Get) ByPath(path string, query *Query) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// search dbm datapoints by uuid
|
||||||
func (g *Get) ByUuid(uid string, query *Query) {
|
func (g *Get) ByUuid(uid string, query *Query) {
|
||||||
g.Uuid = uuid.MustParse(uid)
|
g.Uuid = uuid.MustParse(uid)
|
||||||
if query != nil {
|
if query != nil {
|
||||||
|
|||||||
@@ -1,10 +1,48 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import "github.com/google/uuid"
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/tecamino/tecamino-json_data/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
// publish model
|
||||||
type Publish struct {
|
type Publish struct {
|
||||||
Event string `json:"event,omitempty"`
|
Event string `json:"event,omitempty"` // publish event onCreate|onChange|onDelete
|
||||||
Uuid uuid.UUID `json:"uuid,omitempty"`
|
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||||
Path string `json:"path,omitempty"`
|
Path string `json:"path,omitempty"` // dbm path
|
||||||
Value any `json:"value,omitempty"`
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
|
}
|
||||||
|
|
||||||
|
// returns input parameter as assigned dbm datatype
|
||||||
|
func (p *Publish) ConvertValue() any {
|
||||||
|
switch p.Type {
|
||||||
|
case BIT:
|
||||||
|
return utils.BoolFrom(p.Value)
|
||||||
|
case BYS:
|
||||||
|
return utils.Int8From(p.Value)
|
||||||
|
case BYU:
|
||||||
|
return utils.Uint8From(p.Value)
|
||||||
|
case WOS:
|
||||||
|
return utils.Int16From(p.Value)
|
||||||
|
case WOU:
|
||||||
|
return utils.Uint16From(p.Value)
|
||||||
|
case DWS:
|
||||||
|
return utils.Int32From(p.Value)
|
||||||
|
case DWU:
|
||||||
|
return utils.Uint32From(p.Value)
|
||||||
|
case LOS:
|
||||||
|
return utils.Int64From(p.Value)
|
||||||
|
case LOU:
|
||||||
|
return utils.Uint64From(p.Value)
|
||||||
|
case F32:
|
||||||
|
return utils.Float32From(p.Value)
|
||||||
|
case F64:
|
||||||
|
return utils.Float64From(p.Value)
|
||||||
|
case STR:
|
||||||
|
return fmt.Sprintf("%v", p.Value)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
// Query model
|
||||||
type Query struct {
|
type Query struct {
|
||||||
Depth int `json:"depth,omitempty"`
|
Depth uint `json:"depth,omitempty"` // depth of query
|
||||||
RegExp string `json:"regExp,omitempty"`
|
RegExp string `json:"regExp,omitempty"` // additional regex paramaters
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,36 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
// Request model
|
||||||
type Request struct {
|
type Request struct {
|
||||||
Get []Get `json:"get,omitempty"`
|
Id string `json:"id,omitempty"` // identification from requesting client
|
||||||
Set []Set `json:"set,omitempty"`
|
Get []Get `json:"get,omitempty"` // collection of requested Gets
|
||||||
Subscribe []Subscribe `json:"subscribe,omitempty"`
|
Set []Set `json:"set,omitempty"` // collection of requested Sets
|
||||||
Unsubscribe []Subscribe `json:"unsubscribe,omitempty"`
|
Subscribe []Subscription `json:"subscribe,omitempty"` // collection of requested Subsciptions
|
||||||
|
Unsubscribe []Subscription `json:"unsubscribe,omitempty"` // collection of requested Unsubsciptions
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a new json_data Request model
|
||||||
|
func NewRequest() *Request {
|
||||||
|
return &Request{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new get to request collection
|
||||||
func (r *Request) AddGet() *Get {
|
func (r *Request) AddGet() *Get {
|
||||||
get := Get{}
|
get := Get{}
|
||||||
r.Get = append(r.Get, get)
|
r.Get = append(r.Get, get)
|
||||||
return &get
|
return &get
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new set to request collection
|
||||||
func (r *Request) AddSet() *Set {
|
func (r *Request) AddSet() *Set {
|
||||||
set := Set{}
|
set := Set{}
|
||||||
r.Set = append(r.Set, set)
|
r.Set = append(r.Set, set)
|
||||||
return &set
|
return &set
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new subsciption to request collection
|
||||||
func (r *Request) AddSubscription(path string, depth uint, onCreate, onChange, onDelete bool) {
|
func (r *Request) AddSubscription(path string, depth uint, onCreate, onChange, onDelete bool) {
|
||||||
r.Subscribe = append(r.Subscribe, Subscribe{
|
r.Subscribe = append(r.Subscribe, Subscription{
|
||||||
Path: path,
|
Path: path,
|
||||||
Depth: depth,
|
Depth: depth,
|
||||||
OnCreate: onCreate,
|
OnCreate: onCreate,
|
||||||
@@ -29,8 +39,9 @@ func (r *Request) AddSubscription(path string, depth uint, onCreate, onChange, o
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new driver specific subsciption to request collection
|
||||||
func (r *Request) AddDriverSubscription(path, driverName string, depth uint, onCreate, onChange, onDelete bool) {
|
func (r *Request) AddDriverSubscription(path, driverName string, depth uint, onCreate, onChange, onDelete bool) {
|
||||||
r.Subscribe = append(r.Subscribe, Subscribe{
|
r.Subscribe = append(r.Subscribe, Subscription{
|
||||||
Path: path,
|
Path: path,
|
||||||
Depth: depth,
|
Depth: depth,
|
||||||
Driver: driverName,
|
Driver: driverName,
|
||||||
@@ -40,15 +51,17 @@ func (r *Request) AddDriverSubscription(path, driverName string, depth uint, onC
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new unsubsciption to request collection
|
||||||
func (r *Request) AddUnsubscribe(path string, depth uint) {
|
func (r *Request) AddUnsubscribe(path string, depth uint) {
|
||||||
r.Unsubscribe = append(r.Unsubscribe, Subscribe{
|
r.Unsubscribe = append(r.Unsubscribe, Subscription{
|
||||||
Path: path,
|
Path: path,
|
||||||
Depth: depth,
|
Depth: depth,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new driver unsubsciption to request collection
|
||||||
func (r *Request) AddUnsubscribeDriver(path, driverName string, depth uint) {
|
func (r *Request) AddUnsubscribeDriver(path, driverName string, depth uint) {
|
||||||
r.Unsubscribe = append(r.Unsubscribe, Subscribe{
|
r.Unsubscribe = append(r.Unsubscribe, Subscription{
|
||||||
Path: path,
|
Path: path,
|
||||||
Depth: depth,
|
Depth: depth,
|
||||||
Driver: driverName,
|
Driver: driverName,
|
||||||
|
|||||||
@@ -1,57 +1,84 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
// Response model
|
||||||
type Response struct {
|
type Response struct {
|
||||||
Error bool `json:"error,omitempty"`
|
Id string `json:"id,omitempty"` // identification for requesting client
|
||||||
Message string `json:"message,omitempty"`
|
Error bool `json:"error,omitempty"` // indicated response is a error
|
||||||
Get []Get `json:"get,omitempty"`
|
Message string `json:"message,omitempty"` // response message
|
||||||
Set []Set `json:"set,omitempty"`
|
Get []Get `json:"get,omitempty"` // collection of requested Gets
|
||||||
Subscribe []Subscribe `json:"subscribe,omitempty"`
|
Set []Set `json:"set,omitempty"` // collection of requested Sets
|
||||||
Unsubscribe []Subscribe `json:"unsubscribe,omitempty"`
|
Subscribe []Subscription `json:"subscribe,omitempty"` // collection of requested Subscriptions
|
||||||
Publish []Publish `json:"publish,omitempty"`
|
Unsubscribe []Subscription `json:"unsubscribe,omitempty"` // collection of requested Unscubscriptions
|
||||||
|
Publish []Publish `json:"publish,omitempty"` // collection of published data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns a new json_data Response model
|
||||||
func NewResponse() *Response {
|
func NewResponse() *Response {
|
||||||
return &Response{}
|
return &Response{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) SendError(msg string) {
|
// Set response as error
|
||||||
|
func (r *Response) SetError() {
|
||||||
r.Error = true
|
r.Error = true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set response message
|
||||||
|
func (r *Response) SetMessage(msg string) {
|
||||||
r.Message = msg
|
r.Message = msg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new get to response collection
|
||||||
func (r *Response) AddGet(get Get) {
|
func (r *Response) AddGet(get Get) {
|
||||||
get.Query = nil
|
get.Query = nil
|
||||||
r.Get = append(r.Get, get)
|
r.Get = append(r.Get, get)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new set to response collection
|
||||||
func (r *Response) AddSet(set Set) {
|
func (r *Response) AddSet(set Set) {
|
||||||
r.Set = append(r.Set, set)
|
r.Set = append(r.Set, set)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) AddSubscription(sub Subscribe) {
|
// Add new subscription to response collection
|
||||||
|
func (r *Response) AddSubscription(sub Subscription) {
|
||||||
r.Subscribe = append(r.Subscribe, sub)
|
r.Subscribe = append(r.Subscribe, sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add new unsubscribe to response collection
|
||||||
|
func (r *Response) AddUnsubscription(sub Subscription) {
|
||||||
|
r.Unsubscribe = append(r.Unsubscribe, sub)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add new data publish to response
|
||||||
|
func (r *Response) AddPublish(pub Publish) {
|
||||||
|
r.Publish = append(r.Publish, pub)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if response has a error
|
||||||
func (r *Response) IsValid() bool {
|
func (r *Response) IsValid() bool {
|
||||||
return !r.Error
|
return !r.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns slice of get collection
|
||||||
func (r *Response) FetchGets() []Get {
|
func (r *Response) FetchGets() []Get {
|
||||||
return r.Get
|
return r.Get
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns slice of set collection
|
||||||
func (r *Response) FetchSets() []Set {
|
func (r *Response) FetchSets() []Set {
|
||||||
return r.Set
|
return r.Set
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) FetchSubscribes() []Subscribe {
|
// Returns slice of subscriptions collection
|
||||||
|
func (r *Response) FetchSubscribes() []Subscription {
|
||||||
return r.Subscribe
|
return r.Subscribe
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Response) FetchUnubscribes() []Subscribe {
|
// Returns slice of unsubscriptions collection
|
||||||
|
func (r *Response) FetchUnubscribes() []Subscription {
|
||||||
return r.Unsubscribe
|
return r.Unsubscribe
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns slice of published data
|
||||||
func (r *Response) FetchPublish() []Publish {
|
func (r *Response) FetchPublish() []Publish {
|
||||||
return r.Publish
|
return r.Publish
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,16 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
type Rights string
|
// all avaiable read/write modes
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Read Rights = "R"
|
Read Rights = "R"
|
||||||
Write Rights = "W"
|
Write Rights = "W"
|
||||||
ReadWrite Rights = "RW"
|
ReadWrite Rights = "RW"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// dbm read/write model
|
||||||
|
type Rights string
|
||||||
|
|
||||||
|
// return current rights
|
||||||
func (r *Rights) GetRights() Rights {
|
func (r *Rights) GetRights() Rights {
|
||||||
if *r == "" {
|
if *r == "" {
|
||||||
return ReadWrite
|
return ReadWrite
|
||||||
|
|||||||
@@ -2,27 +2,33 @@ package models
|
|||||||
|
|
||||||
import "github.com/google/uuid"
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
|
// Set model
|
||||||
type Set struct {
|
type Set struct {
|
||||||
Uuid uuid.UUID `json:"uuid,omitempty"`
|
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||||
Path string `json:"path"`
|
Path string `json:"path"` // dbm path
|
||||||
Driver *Driver `json:"driver,omitempty"`
|
Driver *Driver `json:"driver,omitempty"` // assign new driver
|
||||||
Drivers *Drivers `json:"drivers,omitempty"`
|
Drivers *Drivers `json:"drivers,omitempty"` // assigned drivers
|
||||||
Type Type `json:"type,omitempty"`
|
Type Type `json:"type,omitempty"` // dbm datatype
|
||||||
Value any `json:"value,omitempty"`
|
Value any `json:"value,omitempty"` // dbm value
|
||||||
Rights Rights `json:"rights,omitempty"`
|
Rights Rights `json:"rights,omitempty"` // dbm read /write rights
|
||||||
Updated bool `json:"-"`
|
Create bool `json:"create,omitempty"` // dbm create new datapoint
|
||||||
|
HasChild bool `json:"hasChild,omitempty"` // inidicates path has children
|
||||||
|
Updated bool `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sets value and path
|
||||||
func (s *Set) ValueByPath(path string, value any) {
|
func (s *Set) ValueByPath(path string, value any) {
|
||||||
s.Path = path
|
s.Path = path
|
||||||
s.Value = value
|
s.Value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sets value and uuid
|
||||||
func (s *Set) ValueByUuid(uid string, value any) {
|
func (s *Set) ValueByUuid(uid string, value any) {
|
||||||
s.Uuid = uuid.MustParse(uid)
|
s.Uuid = uuid.MustParse(uid)
|
||||||
s.Value = value
|
s.Value = value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sets new datapoint with given parameters
|
||||||
func (s *Set) New(path string, typ Type, value any, rights Rights) {
|
func (s *Set) New(path string, typ Type, value any, rights Rights) {
|
||||||
s.Path = path
|
s.Path = path
|
||||||
s.Type = typ
|
s.Type = typ
|
||||||
@@ -30,10 +36,32 @@ func (s *Set) New(path string, typ Type, value any, rights Rights) {
|
|||||||
s.Rights = rights.GetRights()
|
s.Rights = rights.GetRights()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Set) NewDriver(typ, bus string, address uint) {
|
// sets new driver typ with bus and address
|
||||||
s.Driver = &Driver{
|
func (s *Set) NewDriverAddress(typ, bus string, address uint) {
|
||||||
Type: typ,
|
if s.Drivers == nil {
|
||||||
Bus: bus,
|
s.Drivers = &Drivers{}
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
type Subscribe struct {
|
|
||||||
Path string `json:"path"`
|
|
||||||
Depth uint `json:"depth,omitempty"`
|
|
||||||
Drivers *Drivers `json:"drivers,omitempty"`
|
|
||||||
Driver string `json:"driver,omitempty"`
|
|
||||||
OnCreate bool `json:"onCreate,omitempty"`
|
|
||||||
OnDelete bool `json:"onDelete,omitempty"`
|
|
||||||
OnChange bool `json:"onChange,omitempty"`
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
package models
|
|
||||||
|
|
||||||
//type Subscriptions map[string]*Subscription
|
|
||||||
|
|
||||||
// type Subscription struct {
|
|
||||||
// OnCreate bool
|
|
||||||
// OnDelete bool
|
|
||||||
// OnChange bool
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func InitSubscribtion() Subscriptions {
|
|
||||||
// return make(Subscriptions)
|
|
||||||
// }
|
|
||||||
18
models/subscription.go
Normal file
18
models/subscription.go
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import "github.com/google/uuid"
|
||||||
|
|
||||||
|
// Subscription model
|
||||||
|
type Subscription struct {
|
||||||
|
Uuid uuid.UUID `json:"uuid,omitempty"` // universally unique identifier
|
||||||
|
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
|
||||||
|
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
|
||||||
|
OnDelete bool `json:"onDelete,omitempty"` // notify on deletion
|
||||||
|
OnChange bool `json:"onChange,omitempty"` // notify change
|
||||||
|
}
|
||||||
57
models/topic.go
Normal file
57
models/topic.go
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,24 +6,27 @@ import (
|
|||||||
"github.com/tecamino/tecamino-json_data/utils"
|
"github.com/tecamino/tecamino-json_data/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// all avaiable datatypes of dbm
|
||||||
const (
|
const (
|
||||||
NONE Type = "NONE"
|
NONE Type = "NONE"
|
||||||
BIT Type = "BIT"
|
BIT Type = "BIT" // BOOL
|
||||||
BYU Type = "BYU" // UINT8
|
BYU Type = "BYU" // UINT8
|
||||||
BYS Type = "BYS" // INT8
|
BYS Type = "BYS" // INT8
|
||||||
WOS Type = "WOS" // INT16
|
WOS Type = "WOS" // INT16
|
||||||
WOU Type = "WOU" // UINT16
|
WOU Type = "WOU" // UINT16
|
||||||
DWS Type = "DWS" // INT32
|
DWS Type = "DWS" // INT32
|
||||||
DWU Type = "DWU" // UINT32
|
DWU Type = "DWU" // UINT32
|
||||||
LOS Type = "LOS" // INT64
|
LOS Type = "LOS" // INT64
|
||||||
LOU Type = "LOU" // UINT64
|
LOU Type = "LOU" // UINT64
|
||||||
F32 Type = "F32" // FLOAT32
|
F32 Type = "F32" // FLOAT32
|
||||||
F64 Type = "F64" // FLOAT64
|
F64 Type = "F64" // FLOAT64
|
||||||
STR Type = "STRING" // STRING
|
STR Type = "STR" // STRING
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// dbm datatype model
|
||||||
type Type string
|
type Type string
|
||||||
|
|
||||||
|
// return default value of assigned datatype
|
||||||
func (t *Type) DefaultValue() any {
|
func (t *Type) DefaultValue() any {
|
||||||
switch *t {
|
switch *t {
|
||||||
case BIT:
|
case BIT:
|
||||||
@@ -36,6 +39,7 @@ func (t *Type) DefaultValue() any {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns input parameter as assigned dbm datatype
|
||||||
func (t *Type) ConvertValue(v any) any {
|
func (t *Type) ConvertValue(v any) any {
|
||||||
switch *t {
|
switch *t {
|
||||||
case BIT:
|
case BIT:
|
||||||
|
|||||||
23
request.go
23
request.go
@@ -1,7 +1,28 @@
|
|||||||
package tecaminodbmjson_data
|
package tecaminodbmjson_data
|
||||||
|
|
||||||
import "github.com/tecamino/tecamino-json_data/models"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/tecamino/tecamino-json_data/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Returns a new json_data Request model
|
||||||
func NewRequest() *models.Request {
|
func NewRequest() *models.Request {
|
||||||
return &models.Request{}
|
return &models.Request{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Parses the request body of a http or web socket request
|
||||||
|
func ParseRequest(body io.ReadCloser) (*models.Request, error) {
|
||||||
|
b, err := io.ReadAll(body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
r := models.Request{}
|
||||||
|
|
||||||
|
err = json.Unmarshal(b, &r)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &r, nil
|
||||||
|
}
|
||||||
|
|||||||
15
response.go
15
response.go
@@ -2,14 +2,25 @@ package tecaminodbmjson_data
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/tecamino/tecamino-json_data/models"
|
"github.com/tecamino/tecamino-json_data/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParseResponse(b []byte) (*models.Response, error) {
|
// Returns a new json_data Response model
|
||||||
|
func NewResponse() *models.Response {
|
||||||
|
return &models.Response{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parses the response body of a http or web socket request
|
||||||
|
func ParseResponse(body io.ReadCloser) (*models.Response, error) {
|
||||||
|
b, err := io.ReadAll(body)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
r := models.Response{}
|
r := models.Response{}
|
||||||
|
|
||||||
err := json.Unmarshal(b, &r)
|
err = json.Unmarshal(b, &r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Returns float32 type from type interface
|
||||||
func Float32From(v any) float32 {
|
func Float32From(v any) float32 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -35,8 +36,8 @@ func Float32From(v any) float32 {
|
|||||||
case uint64:
|
case uint64:
|
||||||
return float32(val)
|
return float32(val)
|
||||||
case string:
|
case string:
|
||||||
if i, err := strconv.Atoi(val); err == nil {
|
if f64, err := strconv.ParseFloat(val, 32); err == nil {
|
||||||
return float32(i)
|
return float32(f64)
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
default:
|
default:
|
||||||
@@ -44,6 +45,7 @@ func Float32From(v any) float32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns float64 type from type interface
|
||||||
func Float64From(v any) float64 {
|
func Float64From(v any) float64 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -74,8 +76,8 @@ func Float64From(v any) float64 {
|
|||||||
case uint64:
|
case uint64:
|
||||||
return float64(val)
|
return float64(val)
|
||||||
case string:
|
case string:
|
||||||
if i, err := strconv.Atoi(val); err == nil {
|
if f64, err := strconv.ParseFloat(val, 32); err == nil {
|
||||||
return float64(i)
|
return f64
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
default:
|
default:
|
||||||
@@ -83,6 +85,7 @@ func Float64From(v any) float64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Int8 type from type interface
|
||||||
func Int8From(v any) int8 {
|
func Int8From(v any) int8 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -122,6 +125,7 @@ func Int8From(v any) int8 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Int16 type from type interface
|
||||||
func Int16From(v any) int16 {
|
func Int16From(v any) int16 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -161,6 +165,7 @@ func Int16From(v any) int16 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Int32 type from type interface
|
||||||
func Int32From(v any) int32 {
|
func Int32From(v any) int32 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -200,6 +205,7 @@ func Int32From(v any) int32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Int64 type from type interface
|
||||||
func Int64From(v any) int64 {
|
func Int64From(v any) int64 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -239,6 +245,7 @@ func Int64From(v any) int64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Uint8 type from type interface
|
||||||
func Uint8From(v any) uint8 {
|
func Uint8From(v any) uint8 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -278,6 +285,7 @@ func Uint8From(v any) uint8 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Uint16 type from type interface
|
||||||
func Uint16From(v any) uint16 {
|
func Uint16From(v any) uint16 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -317,6 +325,7 @@ func Uint16From(v any) uint16 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Uint32 type from type interface
|
||||||
func Uint32From(v any) uint32 {
|
func Uint32From(v any) uint32 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -356,6 +365,7 @@ func Uint32From(v any) uint32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns Uint64 type from type interface
|
||||||
func Uint64From(v any) uint64 {
|
func Uint64From(v any) uint64 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -395,6 +405,7 @@ func Uint64From(v any) uint64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns bool type from type interface
|
||||||
func BoolFrom(v any) bool {
|
func BoolFrom(v any) bool {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -422,7 +433,14 @@ func BoolFrom(v any) bool {
|
|||||||
case float64:
|
case float64:
|
||||||
return val >= 1
|
return val >= 1
|
||||||
case string:
|
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:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user