first commit

This commit is contained in:
Adrian Zuercher
2025-04-14 18:03:14 +02:00
parent 46c6b8424d
commit 3d1dee25f1
15 changed files with 72205 additions and 0 deletions

183
models/datapoints.go Normal file
View File

@@ -0,0 +1,183 @@
package models
import (
"fmt"
"regexp"
"strings"
"time"
)
type Datapoint struct {
Datapoints map[string]*Datapoint `json:"-"`
Path string `json:"path"`
Value any `json:"value,omitempty"`
CreateDateTime int64 `json:"createDateTime,omitempty"`
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
Type Type `json:"type"`
ReadWrite Rights `json:"readWrite"`
}
var depth1 int
func (d *Datapoint) CreateDatapoint(typ Type, value any, rights Rights, paths ...string) error {
l := len(paths) - 1
if l == 0 {
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
l = len(paths) - 1
}
if d.Datapoints == nil {
d.Datapoints = make(map[string]*Datapoint)
}
if l == depth1 {
if od, ok := d.Datapoints[paths[depth1]]; !ok {
d.Datapoints[paths[depth1]] = &Datapoint{
Path: strings.Join(paths, ":"),
Type: typ,
Value: typ.ConvertValue(value),
ReadWrite: rights.GetRights(),
CreateDateTime: time.Now().UnixMilli(),
UpdateDateTime: time.Now().UnixMilli(),
}
} else {
od.Type = typ
od.ReadWrite = rights.GetRights()
od.Value = typ.ConvertValue(value)
od.UpdateDateTime = time.Now().UnixMilli()
}
depth1 = 0
} else if datapoint, ok := d.Datapoints[paths[depth1]]; ok {
depth1 += 1
datapoint.CreateDatapoint(typ, value, rights, paths...)
} else {
da := Datapoint{
Path: strings.Join(paths[:depth1+1], ":"),
Type: NONE,
ReadWrite: rights.GetRights(),
CreateDateTime: time.Now().UnixMilli(),
UpdateDateTime: time.Now().UnixMilli(),
}
d.Datapoints[paths[depth1]] = &da
depth1 += 1
da.CreateDatapoint(typ, value, rights, paths...)
}
return nil
}
var depth2 int
func (d *Datapoint) ImportDatapoint(dp *Datapoint, paths ...string) error {
l := len(paths) - 1
if l == 0 {
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
l = len(paths) - 1
}
if d.Datapoints == nil {
d.Datapoints = make(map[string]*Datapoint)
}
if l == depth2 {
if od, ok := d.Datapoints[paths[depth2]]; !ok {
d.Datapoints[paths[depth2]] = dp
dp.ReadWrite = dp.ReadWrite.GetRights()
dp.UpdateDateTime = time.Now().UnixMilli()
} else {
od.Type = dp.Type
od.Value = d.Type.ConvertValue(dp.Value)
od.ReadWrite = dp.ReadWrite.GetRights()
od.UpdateDateTime = time.Now().UnixMilli()
}
depth2 = 0
} else if datapoint, ok := d.Datapoints[paths[depth2]]; ok {
depth2 += 1
datapoint.ImportDatapoint(dp, paths...)
} else {
da := Datapoint{
Path: strings.Join(paths[:depth2+1], ":"),
Type: NONE,
UpdateDateTime: time.Now().UnixMilli(),
}
da.ReadWrite = da.ReadWrite.GetRights()
d.Datapoints[paths[depth2]] = &da
depth2 += 1
da.ImportDatapoint(dp, paths...)
}
return nil
}
var depth3 int
func (d *Datapoint) UpdateDatapointValue(value any, paths ...string) error {
l := len(paths) - 1
if l == 0 {
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
l = len(paths) - 1
}
if l == depth3 {
if dp, ok := d.Datapoints[paths[depth3]]; ok {
fmt.Print("update dp:", dp.Path, " old value:", dp.Value)
dp.Value = dp.Type.ConvertValue(value)
fmt.Println(" new value:", dp.Value)
dp.UpdateDateTime = time.Now().UnixMilli()
depth3 = 0
return nil
}
depth3 = 0
return fmt.Errorf("datapoint '%s' not found", strings.Join(paths, ":"))
} else if datapoint, ok := d.Datapoints[paths[depth3]]; ok {
depth3 += 1
if err := datapoint.UpdateDatapointValue(value, paths...); err != nil {
fmt.Println(100, err)
return err
}
}
depth3 = 0
return nil
}
var depth4 int
func (d *Datapoint) RemoveDatapoint(paths ...string) error {
l := len(paths) - 1
if l == 0 {
paths = regexp.MustCompile(`[:]+`).Split(paths[0], -1)
l = len(paths) - 1
}
if l == depth4 {
if _, ok := d.Datapoints[paths[depth4]]; ok {
delete(d.Datapoints, paths[depth4])
fmt.Println("removed dp:", strings.Join(paths, ":"))
}
depth4 = 0
} else if datapoint, ok := d.Datapoints[paths[depth4]]; ok {
depth4 += 1
datapoint.RemoveDatapoint(paths...)
}
return fmt.Errorf("datapoint '%s' not found", strings.Join(paths, ":"))
}
func (d *Datapoint) GetAllDatapoints() (dps []*Datapoint) {
for _, dp := range d.Datapoints {
dps = append(dps, dp.GetAllDatapoints()...)
dps = append(dps, dp)
}
return
}
func (d *Datapoint) QueryDatapoints(key string) (dps []*Datapoint) {
reg := regexp.MustCompile(key)
for _, dp := range d.Datapoints {
if reg.MatchString(dp.Path) {
dps = append(dps, dp)
}
dps = append(dps, dp.QueryDatapoints(key)...)
}
return
}

18
models/rights.go Normal file
View File

@@ -0,0 +1,18 @@
package models
type Rights string
const (
Read Rights = "R"
Write Rights = "W"
ReadWrite Rights = "RW"
)
func (r *Rights) GetRights() Rights {
if r == nil {
return ReadWrite
} else if *r == "" {
return ReadWrite
}
return *r
}

67
models/type.go Normal file
View File

@@ -0,0 +1,67 @@
package models
import (
"fmt"
"github.com/zuadi/tecamino-dbm.git/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(v any) 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
}