remodel json data for compatibility
This commit is contained in:
20
models/drivers.go
Normal file
20
models/drivers.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package models
|
||||
|
||||
type Drivers map[string]*Driver
|
||||
|
||||
type Driver struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Bus string `json:"bus"`
|
||||
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,
|
||||
}
|
||||
}
|
6
models/errorResponse.go
Normal file
6
models/errorResponse.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error bool `json:"error,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
27
models/get.go
Normal file
27
models/get.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Get struct {
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Path string `json:"path"`
|
||||
Query *Query `json:"query,omitempty"`
|
||||
Drivers *Drivers `json:"driver,omitempty"`
|
||||
Type Type `json:"type,omitempty"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Rights Rights `json:"rights,omitempty"`
|
||||
}
|
||||
|
||||
func (g *Get) ByPath(path string, query *Query) {
|
||||
g.Path = path
|
||||
if query != nil {
|
||||
g.Query = query
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Get) ByUuid(uid string, query *Query) {
|
||||
g.Uuid = uuid.MustParse(uid)
|
||||
if query != nil {
|
||||
g.Query = query
|
||||
}
|
||||
}
|
10
models/publish.go
Normal file
10
models/publish.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Publish struct {
|
||||
Event string `json:"event,omitempty"`
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Value any `json:"value,omitempty"`
|
||||
}
|
6
models/query.go
Normal file
6
models/query.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models
|
||||
|
||||
type Query struct {
|
||||
Depth int `json:"depth,omitempty"`
|
||||
RegExp string `json:"regExp,omitempty"`
|
||||
}
|
56
models/request.go
Normal file
56
models/request.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package models
|
||||
|
||||
type Request struct {
|
||||
Get []Get `json:"get,omitempty"`
|
||||
Set []Set `json:"set,omitempty"`
|
||||
Subscribe []Subscribe `json:"subscribe,omitempty"`
|
||||
Unsubscribe []Subscribe `json:"unsubscribe,omitempty"`
|
||||
}
|
||||
|
||||
func (r *Request) AddGet() *Get {
|
||||
get := Get{}
|
||||
r.Get = append(r.Get, get)
|
||||
return &get
|
||||
}
|
||||
|
||||
func (r *Request) AddSet() *Set {
|
||||
set := Set{}
|
||||
r.Set = append(r.Set, set)
|
||||
return &set
|
||||
}
|
||||
|
||||
func (r *Request) AddSubscription(path string, depth uint, onCreate, onChange, onDelete bool) {
|
||||
r.Subscribe = append(r.Subscribe, Subscribe{
|
||||
Path: path,
|
||||
Depth: depth,
|
||||
OnCreate: onCreate,
|
||||
OnDelete: onChange,
|
||||
OnChange: onDelete,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Request) AddDriverSubscription(path, driverName string, depth uint, onCreate, onChange, onDelete bool) {
|
||||
r.Subscribe = append(r.Subscribe, Subscribe{
|
||||
Path: path,
|
||||
Depth: depth,
|
||||
Driver: driverName,
|
||||
OnCreate: onCreate,
|
||||
OnDelete: onChange,
|
||||
OnChange: onDelete,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Request) AddUnsubscribe(path string, depth uint) {
|
||||
r.Unsubscribe = append(r.Unsubscribe, Subscribe{
|
||||
Path: path,
|
||||
Depth: depth,
|
||||
})
|
||||
}
|
||||
|
||||
func (r *Request) AddUnsubscribeDriver(path, driverName string, depth uint) {
|
||||
r.Unsubscribe = append(r.Unsubscribe, Subscribe{
|
||||
Path: path,
|
||||
Depth: depth,
|
||||
Driver: driverName,
|
||||
})
|
||||
}
|
57
models/response.go
Normal file
57
models/response.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package models
|
||||
|
||||
type Response struct {
|
||||
Error bool `json:"error,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Get []Get `json:"get,omitempty"`
|
||||
Set []Set `json:"set,omitempty"`
|
||||
Subscribe []Subscribe `json:"subscribe,omitempty"`
|
||||
Unsubscribe []Subscribe `json:"unsubscribe,omitempty"`
|
||||
Publish []Publish `json:"publish,omitempty"`
|
||||
}
|
||||
|
||||
func NewResponse() *Response {
|
||||
return &Response{}
|
||||
}
|
||||
|
||||
func (r *Response) SendError(msg string) {
|
||||
r.Error = true
|
||||
r.Message = msg
|
||||
}
|
||||
|
||||
func (r *Response) AddGet(get Get) {
|
||||
get.Query = nil
|
||||
r.Get = append(r.Get, get)
|
||||
}
|
||||
|
||||
func (r *Response) AddSet(set Set) {
|
||||
r.Set = append(r.Set, set)
|
||||
}
|
||||
|
||||
func (r *Response) AddSubscription(sub Subscribe) {
|
||||
r.Subscribe = append(r.Subscribe, sub)
|
||||
}
|
||||
|
||||
func (r *Response) IsValid() bool {
|
||||
return !r.Error
|
||||
}
|
||||
|
||||
func (r *Response) FetchGets() []Get {
|
||||
return r.Get
|
||||
}
|
||||
|
||||
func (r *Response) FetchSets() []Set {
|
||||
return r.Set
|
||||
}
|
||||
|
||||
func (r *Response) FetchSubscribes() []Subscribe {
|
||||
return r.Subscribe
|
||||
}
|
||||
|
||||
func (r *Response) FetchUnubscribes() []Subscribe {
|
||||
return r.Unsubscribe
|
||||
}
|
||||
|
||||
func (r *Response) FetchPublish() []Publish {
|
||||
return r.Publish
|
||||
}
|
16
models/rights.go
Normal file
16
models/rights.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package models
|
||||
|
||||
type Rights string
|
||||
|
||||
const (
|
||||
Read Rights = "R"
|
||||
Write Rights = "W"
|
||||
ReadWrite Rights = "RW"
|
||||
)
|
||||
|
||||
func (r *Rights) GetRights() Rights {
|
||||
if *r == "" {
|
||||
return ReadWrite
|
||||
}
|
||||
return *r
|
||||
}
|
39
models/set.go
Normal file
39
models/set.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Set struct {
|
||||
Uuid uuid.UUID `json:"uuid,omitempty"`
|
||||
Path string `json:"path"`
|
||||
Driver *Driver `json:"driver,omitempty"`
|
||||
Drivers *Drivers `json:"drivers,omitempty"`
|
||||
Type Type `json:"type,omitempty"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Rights Rights `json:"rights,omitempty"`
|
||||
Updated bool `json:"-"`
|
||||
}
|
||||
|
||||
func (s *Set) ValueByPath(path string, value any) {
|
||||
s.Path = path
|
||||
s.Value = value
|
||||
}
|
||||
|
||||
func (s *Set) ValueByUuid(uid string, value any) {
|
||||
s.Uuid = uuid.MustParse(uid)
|
||||
s.Value = value
|
||||
}
|
||||
|
||||
func (s *Set) New(path string, typ Type, value any, rights Rights) {
|
||||
s.Path = path
|
||||
s.Type = typ
|
||||
s.Value = value
|
||||
s.Rights = rights.GetRights()
|
||||
}
|
||||
|
||||
func (s *Set) NewDriver(typ, bus string, address uint) {
|
||||
s.Driver = &Driver{
|
||||
Type: typ,
|
||||
Bus: bus,
|
||||
Address: address,
|
||||
}
|
||||
}
|
11
models/subscribe.go
Normal file
11
models/subscribe.go
Normal file
@@ -0,0 +1,11 @@
|
||||
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"`
|
||||
}
|
13
models/subscribtion.go
Normal file
13
models/subscribtion.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
//type Subscriptions map[string]*Subscription
|
||||
|
||||
// type Subscription struct {
|
||||
// OnCreate bool
|
||||
// OnDelete bool
|
||||
// OnChange bool
|
||||
// }
|
||||
|
||||
// func InitSubscribtion() Subscriptions {
|
||||
// return make(Subscriptions)
|
||||
// }
|
67
models/type.go
Normal file
67
models/type.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/tecamino/tecamino-dbm-json_data/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
NONE Type = "NONE"
|
||||
BIT Type = "BIT"
|
||||
BYU Type = "BYU" // UINT8
|
||||
BYS Type = "BYS" // INT8
|
||||
WOS Type = "WOS" // INT16
|
||||
WOU Type = "WOU" // UINT16
|
||||
DWS Type = "DWS" // INT32
|
||||
DWU Type = "DWU" // UINT32
|
||||
LOS Type = "LOS" // INT64
|
||||
LOU Type = "LOU" // UINT64
|
||||
F32 Type = "F32" // FLOAT32
|
||||
F64 Type = "F64" // FLOAT64
|
||||
STR Type = "STRING" // STRING
|
||||
)
|
||||
|
||||
type Type string
|
||||
|
||||
func (t *Type) DefaultValue() any {
|
||||
switch *t {
|
||||
case BIT:
|
||||
return false
|
||||
case BYS, BYU, WOS, WOU, DWS, DWU, LOS, LOU, F32, F64:
|
||||
return 0
|
||||
case STR:
|
||||
return ""
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *Type) ConvertValue(v any) any {
|
||||
switch *t {
|
||||
case BIT:
|
||||
return utils.BoolFrom(v)
|
||||
case BYS:
|
||||
return utils.Int8From(v)
|
||||
case BYU:
|
||||
return utils.Uint8From(v)
|
||||
case WOS:
|
||||
return utils.Int16From(v)
|
||||
case WOU:
|
||||
return utils.Uint16From(v)
|
||||
case DWS:
|
||||
return utils.Int32From(v)
|
||||
case DWU:
|
||||
return utils.Uint32From(v)
|
||||
case LOS:
|
||||
return utils.Int64From(v)
|
||||
case LOU:
|
||||
return utils.Uint64From(v)
|
||||
case F32:
|
||||
return utils.Float32From(v)
|
||||
case F64:
|
||||
return utils.Float64From(v)
|
||||
case STR:
|
||||
return fmt.Sprintf("%v", v)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user