Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9605b50198 | ||
|
|
c57c22f93a | ||
|
|
60b3f77e29 | ||
|
|
1c4b8a5995 | ||
|
|
836a69f914 | ||
|
|
5ee97416dd | ||
|
|
ecb1f3b2cf | ||
|
|
5203fb8543 | ||
|
|
7d6b09cf11 | ||
|
|
0fe813acd9 | ||
|
|
75eb31abf6 | ||
|
|
4b36598bd7 | ||
|
|
8e6b39c8bf | ||
|
|
2f88525257 | ||
|
|
3551cb035b |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -35,7 +35,11 @@ jobs:
|
||||
- name: Build binary
|
||||
run: |
|
||||
mkdir -p build
|
||||
if [ "${{ matrix.goos }}" == "windows" ]; then
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }}.exe main.go
|
||||
else
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }} main.go
|
||||
fi
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
|
||||
55
args/args.go
55
args/args.go
@@ -2,35 +2,72 @@ package args
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"strings"
|
||||
|
||||
"github.com/tecamino/tecamino-dbm/cert"
|
||||
"github.com/tecamino/tecamino-dbm/models"
|
||||
)
|
||||
|
||||
// DBM cli arguments
|
||||
type Args struct {
|
||||
Ip string
|
||||
Port models.Port
|
||||
Cert cert.Cert
|
||||
AllowOrigins []string
|
||||
RootDir string
|
||||
DBMFile string
|
||||
Debug bool
|
||||
}
|
||||
|
||||
type StringSlice []string
|
||||
|
||||
func (s *StringSlice) String() string {
|
||||
return strings.Join(*s, ",")
|
||||
}
|
||||
|
||||
func (s *StringSlice) Set(value string) error {
|
||||
*s = append(*s, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
// initialte cli arguments
|
||||
func Init() *Args {
|
||||
|
||||
var allowOrigins StringSlice
|
||||
|
||||
flag.Var(&allowOrigins, "allowOrigin", "Allowed origin (can repeat this flag)")
|
||||
|
||||
ip := flag.String("ip", "0.0.0.0", "local ip address")
|
||||
organization := flag.String("org", "tecamino", "name of organization for certificate")
|
||||
certFile := flag.String("certFile", "./cert/cert.pem", "path of certfile")
|
||||
keyFile := flag.String("keyFile", "./cert/key.pem", "path of keyfile")
|
||||
portHttp := flag.Uint("http-port", 8100, "json server communication for http/ws")
|
||||
portHttps := flag.Uint("https-port", 8101, "json server communication for http/wss")
|
||||
rootDir := flag.String("workingDir", "./", "working directory")
|
||||
dbmFile := flag.String("dbm", "/test/test", "dbm file name")
|
||||
debug := flag.Bool("debug", false, "debug flag")
|
||||
flag.Parse()
|
||||
|
||||
a := Args{
|
||||
Ip: *ip,
|
||||
Cert: cert.Cert{
|
||||
Organization: *flag.String("org", "tecamino", "name of organization for certificate"),
|
||||
CertFile: *flag.String("certFile", "./cert/cert.pem", "path of certfile"),
|
||||
KeyFile: *flag.String("keyFile", "./cert/key.pem", "path of keyfile"),
|
||||
Organization: *organization,
|
||||
CertFile: *certFile,
|
||||
KeyFile: *keyFile,
|
||||
},
|
||||
Port: models.Port{
|
||||
Http: *flag.Uint("http-port", 8100, "json server communication for http/ws"),
|
||||
Https: *flag.Uint("https-port", 8101, "json server communication for http/wss"),
|
||||
Http: *portHttp,
|
||||
Https: *portHttps,
|
||||
},
|
||||
RootDir: *flag.String("workingDir", "./", "working directory"),
|
||||
DBMFile: *flag.String("dbm", "/test/test", "dbm file name"),
|
||||
Debug: *flag.Bool("debug", false, "debug flag"),
|
||||
RootDir: *rootDir,
|
||||
DBMFile: *dbmFile,
|
||||
Debug: *debug,
|
||||
}
|
||||
flag.Parse()
|
||||
|
||||
if len(allowOrigins) == 0 {
|
||||
allowOrigins = StringSlice{"http://localhost:9500"}
|
||||
}
|
||||
|
||||
a.AllowOrigins = allowOrigins
|
||||
return &a
|
||||
}
|
||||
|
||||
14
cert/cert.go
14
cert/cert.go
@@ -63,7 +63,7 @@ func (c *Cert) GenerateSelfSignedCert() error {
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path.Dir(c.CertFile)); os.IsNotExist(err) {
|
||||
os.MkdirAll(path.Dir(c.CertFile), 0666)
|
||||
os.MkdirAll(path.Dir(c.CertFile), 0700)
|
||||
}
|
||||
|
||||
certOut, err := os.Create(c.CertFile)
|
||||
@@ -73,8 +73,13 @@ func (c *Cert) GenerateSelfSignedCert() error {
|
||||
defer certOut.Close()
|
||||
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certDER})
|
||||
|
||||
// Set permission to 0600 (read/write by owner only)
|
||||
if err := os.Chmod(c.CertFile, 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path.Dir(c.KeyFile)); os.IsNotExist(err) {
|
||||
os.MkdirAll(path.Dir(c.KeyFile), 0666)
|
||||
os.MkdirAll(path.Dir(c.KeyFile), 0700)
|
||||
}
|
||||
|
||||
keyOut, err := os.Create(c.KeyFile)
|
||||
@@ -85,5 +90,10 @@ func (c *Cert) GenerateSelfSignedCert() error {
|
||||
|
||||
pem.Encode(keyOut, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})
|
||||
|
||||
// Set permission to 0600 (read/write by owner only)
|
||||
if err := os.Chmod(c.KeyFile, 0600); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -13,14 +13,15 @@ func (d *DBMHandler) SaveData(c *gin.Context) {
|
||||
s := time.Now()
|
||||
if err := d.SaveDb(); err != nil {
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SendError(err.Error())
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SendMessage(fmt.Sprintf("DBM %d datapoints saved in: %v", d.GetNumbersOfDatapoints(), time.Since(s)))
|
||||
d.Log.Info("db.SaveData", fmt.Sprintf("DBM %d datapoints saved in: %v", d.GetNumbersOfDatapoints(), time.Since(s)))
|
||||
r.SetMessage(fmt.Sprintf("DBM %d datapoints saved in: %v", d.DBM.GetNumbersOfDatapoints(), time.Since(s)))
|
||||
d.Log.Info("db.SaveData", fmt.Sprintf("DBM %d datapoints saved in: %v", d.DBM.GetNumbersOfDatapoints(), time.Since(s)))
|
||||
c.JSON(http.StatusOK, r)
|
||||
}
|
||||
|
||||
@@ -5,29 +5,30 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tecamino/tecamino-dbm/args"
|
||||
"github.com/tecamino/tecamino-dbm/models"
|
||||
serverModels "github.com/tecamino/tecamino-dbm/server/models"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
type DBMHandler struct {
|
||||
filePath string
|
||||
DB models.Datapoint
|
||||
DBM *models.DBM
|
||||
Conns *serverModels.Connections
|
||||
sync.RWMutex
|
||||
Log *logging.Logger
|
||||
arg *args.Args
|
||||
filePath string
|
||||
}
|
||||
|
||||
// initialze new Database Manager
|
||||
// it will call cli arguments
|
||||
func NewDbmHandler(a *args.Args) (*DBMHandler, error) {
|
||||
|
||||
//initialize new logger
|
||||
logger, err := logging.NewLogger("dbmServer.log", &logging.Config{
|
||||
MaxSize: 1,
|
||||
MaxBackup: 3,
|
||||
@@ -41,12 +42,16 @@ func NewDbmHandler(a *args.Args) (*DBMHandler, error) {
|
||||
}
|
||||
logger.Info("main", "start dma handler")
|
||||
|
||||
//initialize connection map
|
||||
conns := serverModels.NewConnections()
|
||||
|
||||
// Initialize dtabase manager handler
|
||||
dmaHandler := DBMHandler{
|
||||
arg: a,
|
||||
filePath: fmt.Sprintf("%s/%s.dbm", a.RootDir, a.DBMFile),
|
||||
DBM: models.NewDBM(conns, logger),
|
||||
Log: logger,
|
||||
Conns: serverModels.NewConnections(),
|
||||
Conns: conns,
|
||||
}
|
||||
|
||||
// initialize system datapoint and periodically update it
|
||||
@@ -66,16 +71,20 @@ func NewDbmHandler(a *args.Args) (*DBMHandler, error) {
|
||||
|
||||
// read in dtaabase file content
|
||||
scanner := bufio.NewScanner(f)
|
||||
|
||||
var line int
|
||||
for scanner.Scan() {
|
||||
line++
|
||||
dp := models.Datapoint{}
|
||||
if err = json.Unmarshal(scanner.Bytes(), &dp); err != nil {
|
||||
dmaHandler.Log.Error("dmbHandler.NewDmbHandler", "error in line "+fmt.Sprint(line)+" "+scanner.Text())
|
||||
dmaHandler.Log.Error("dmbHandler.NewDmbHandler", err.Error())
|
||||
|
||||
return nil, err
|
||||
}
|
||||
dmaHandler.ImportDatapoints(dp)
|
||||
dmaHandler.DBM.ImportDatapoints(dp)
|
||||
}
|
||||
}
|
||||
dmaHandler.Log.Info("dmbHandler.NewDmbHandler", fmt.Sprintf("%d datapoint imported in %v", dmaHandler.GetNumbersOfDatapoints(), time.Since(s)))
|
||||
dmaHandler.Log.Info("dmbHandler.NewDmbHandler", fmt.Sprintf("%d datapoint imported in %v", dmaHandler.DBM.GetNumbersOfDatapoints(), time.Since(s)))
|
||||
return &dmaHandler, nil
|
||||
}
|
||||
|
||||
@@ -86,7 +95,13 @@ func (d *DBMHandler) SaveDb() (err error) {
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
for _, dp := range d.DB.GetAllDatapoints(0) {
|
||||
for _, dp := range d.DBM.GetAllDatapoints(0) {
|
||||
//exclude System datapoints from saving
|
||||
//System datapoints are used for internal purposes and should not be saved in the database
|
||||
if strings.Contains(dp.Path, "System:") {
|
||||
continue
|
||||
}
|
||||
|
||||
b, er := json.Marshal(dp)
|
||||
if er != nil {
|
||||
return er
|
||||
@@ -103,59 +118,3 @@ func (d *DBMHandler) SaveDb() (err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *DBMHandler) CreateDatapoints(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
|
||||
if len(sets) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
dps, err := d.DB.CreateDatapoints(d.Conns, sets...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ndp uint64
|
||||
for _, dp := range dps {
|
||||
if !dp.Updated {
|
||||
ndp++
|
||||
}
|
||||
}
|
||||
dp := d.QueryDatapoints(1, "System:Datapoints")
|
||||
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()+ndp)
|
||||
return dps, nil
|
||||
}
|
||||
|
||||
func (d *DBMHandler) ImportDatapoints(dps ...models.Datapoint) error {
|
||||
for _, dp := range dps {
|
||||
err := d.DB.ImportDatapoint(d.Conns, dp, dp.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dp := d.QueryDatapoints(1, "System:Datapoints")
|
||||
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()+1)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DBMHandler) UpdateDatapointValue(path string, value any) error {
|
||||
return d.DB.UpdateDatapointValue(d.Conns, value, path)
|
||||
}
|
||||
|
||||
func (d *DBMHandler) RemoveDatapoint(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
|
||||
var lsRemoved []json_dataModels.Set
|
||||
for _, set := range sets {
|
||||
removed, err := d.DB.RemoveDatapoint(d.Conns, set)
|
||||
if err != nil {
|
||||
return lsRemoved, err
|
||||
}
|
||||
lsRemoved = append(lsRemoved, removed)
|
||||
dp := d.QueryDatapoints(1, "System:Datapoints")
|
||||
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()-1)
|
||||
}
|
||||
|
||||
return lsRemoved, nil
|
||||
}
|
||||
|
||||
func (d *DBMHandler) QueryDatapoints(depth uint, key string) []*models.Datapoint {
|
||||
return d.DB.QueryDatapoints(depth, key)
|
||||
}
|
||||
|
||||
40
dbm/get.go
Normal file
40
dbm/get.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
json_data "github.com/tecamino/tecamino-json_data"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) Get(req *json_dataModels.Request, id string) {
|
||||
if req == nil {
|
||||
return
|
||||
} else if len(req.Get) == 0 {
|
||||
return
|
||||
}
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
resp := json_data.NewResponse()
|
||||
resp.Id = req.Id
|
||||
for _, get := range req.Get {
|
||||
var depth uint = 1
|
||||
if get.Query != nil {
|
||||
depth = get.Query.Depth
|
||||
}
|
||||
|
||||
for _, dp := range d.DBM.QueryDatapoints(depth, get.Uuid, get.Path) {
|
||||
resp.AddGet(json_dataModels.Get{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Type: dp.Type,
|
||||
Value: dp.Value,
|
||||
HasChild: dp.Datapoints != nil,
|
||||
Rights: dp.ReadWrite,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if err := d.Conns.SendResponse(id, resp); err != nil {
|
||||
d.Log.Error("get.Get", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -9,49 +9,50 @@ import (
|
||||
)
|
||||
|
||||
func (d *DBMHandler) Json_Data(c *gin.Context) {
|
||||
|
||||
var err error
|
||||
payload, err := json_data.ParseRequest(c.Request.Body)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SendError(err.Error())
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
|
||||
respond := json_dataModels.NewResponse()
|
||||
resp := json_dataModels.NewResponse()
|
||||
|
||||
if payload.Get != nil {
|
||||
if len(payload.Get) > 0 {
|
||||
var depth uint = 1
|
||||
|
||||
for _, get := range payload.Get {
|
||||
if get.Query != nil {
|
||||
depth = get.Query.Depth
|
||||
}
|
||||
|
||||
for _, res := range d.QueryDatapoints(depth, get.Path) {
|
||||
respond.AddGet(json_dataModels.Get{
|
||||
for _, res := range d.DBM.QueryDatapoints(depth, get.Uuid, get.Path) {
|
||||
resp.AddGet(json_dataModels.Get{
|
||||
Uuid: res.Uuid,
|
||||
Path: res.Path,
|
||||
Type: res.Type,
|
||||
Value: res.Value,
|
||||
HasChild: res.Datapoints != nil,
|
||||
Rights: res.ReadWrite,
|
||||
Drivers: &res.Drivers,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if payload.Set != nil {
|
||||
respond.Set, err = d.CreateDatapoints(payload.Set...)
|
||||
if len(payload.Set) > 0 {
|
||||
resp.Set, err = d.DBM.CreateDatapoints(payload.Set...)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SendError(err.Error())
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, respond)
|
||||
return
|
||||
c.JSON(200, resp)
|
||||
}
|
||||
|
||||
func (d *DBMHandler) Delete(c *gin.Context) {
|
||||
@@ -59,7 +60,8 @@ func (d *DBMHandler) Delete(c *gin.Context) {
|
||||
payload, err := json_data.ParseRequest(c.Request.Body)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SendError(err.Error())
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
@@ -68,15 +70,15 @@ func (d *DBMHandler) Delete(c *gin.Context) {
|
||||
|
||||
if payload.Set != nil {
|
||||
|
||||
response.Set, err = d.RemoveDatapoint(payload.Set...)
|
||||
response.Set, err = d.DBM.RemoveDatapoint(payload.Set...)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SendError(err.Error())
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, response)
|
||||
return
|
||||
}
|
||||
|
||||
31
dbm/set.go
31
dbm/set.go
@@ -1,19 +1,42 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) Set(sets []json_dataModels.Set) {
|
||||
if sets == nil {
|
||||
func (d *DBMHandler) Set(req *json_dataModels.Request, id string) {
|
||||
if req == nil {
|
||||
return
|
||||
} else if len(req.Set) == 0 {
|
||||
return
|
||||
}
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
for _, set := range sets {
|
||||
for _, dp := range d.DB.QueryDatapoints(1, set.Path) {
|
||||
resp := json_dataModels.NewResponse()
|
||||
resp.Id = req.Id
|
||||
|
||||
for _, set := range req.Set {
|
||||
dps := d.DBM.QueryDatapoints(1, set.Uuid, set.Path)
|
||||
if len(dps) == 0 {
|
||||
resp.SetError()
|
||||
if resp.Message == "" {
|
||||
resp.SetMessage(fmt.Sprintf("datapoint %s not found", set.Path))
|
||||
}
|
||||
continue
|
||||
}
|
||||
for _, dp := range dps {
|
||||
dp.UpdateValue(d.Conns, set.Value)
|
||||
resp.AddSet(json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Value: dp.Value,
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := d.Conns.SendResponse(id, resp); err != nil {
|
||||
d.Log.Error("get.Set", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,75 +1,84 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"fmt"
|
||||
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id string) {
|
||||
if subs == nil {
|
||||
func (d *DBMHandler) Subscribe(req *json_dataModels.Request, id string) {
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
if len(req.Subscribe) == 0 {
|
||||
return
|
||||
}
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
client, ok := d.Conns.Clients[id]
|
||||
if !ok {
|
||||
d.Log.Error("subscribe.Subscribe", "client not found for id "+id)
|
||||
return
|
||||
resp := json_dataModels.NewResponse()
|
||||
resp.Id = req.Id
|
||||
|
||||
for _, sub := range req.Subscribe {
|
||||
dps := d.DBM.QueryDatapoints(sub.Depth, sub.Uuid, sub.Path)
|
||||
|
||||
if len(dps) == 0 {
|
||||
resp.SetError()
|
||||
if resp.Message == "" {
|
||||
resp.SetMessage(fmt.Sprintf("datapoint %s not found", sub.Path))
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
response := json_dataModels.NewResponse()
|
||||
|
||||
for _, sub := range subs {
|
||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||
for _, dp := range dps {
|
||||
if sub.Driver != "" {
|
||||
if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
dp.AddSubscribtion(id, sub)
|
||||
response.AddSubscription(json_dataModels.Subscribe{
|
||||
resp.AddSubscription(json_dataModels.Subscription{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Value: dp.Value,
|
||||
HasChild: dp.Datapoints != nil,
|
||||
Driver: sub.Driver,
|
||||
Drivers: &dp.Drivers,
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := wsjson.Write(client.Ctx, client.Conn, response); err != nil {
|
||||
|
||||
if err := d.Conns.SendResponse(id, resp); err != nil {
|
||||
d.Log.Error("subscribe.Subscribe", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DBMHandler) Unsubscribe(subs []json_dataModels.Subscribe, id string) {
|
||||
if subs == nil {
|
||||
func (d *DBMHandler) Unsubscribe(req *json_dataModels.Request, id string) {
|
||||
if req == nil {
|
||||
return
|
||||
}
|
||||
if len(req.Unsubscribe) == 0 {
|
||||
return
|
||||
}
|
||||
d.RLock()
|
||||
defer d.RUnlock()
|
||||
|
||||
client, ok := d.Conns.Clients[id]
|
||||
if !ok {
|
||||
d.Log.Error("subscribe.Subscribe", "client not found for id "+id)
|
||||
return
|
||||
}
|
||||
resp := json_dataModels.NewResponse()
|
||||
resp.Id = req.Id
|
||||
|
||||
response := json_dataModels.NewResponse()
|
||||
|
||||
for _, sub := range subs {
|
||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||
for _, sub := range req.Unsubscribe {
|
||||
for _, dp := range d.DBM.QueryDatapoints(sub.Depth, sub.Uuid, sub.Path) {
|
||||
if _, ok := dp.Subscriptions[id]; !ok {
|
||||
continue
|
||||
}
|
||||
dp.RemoveSubscribtion(id)
|
||||
response.AddUnsubscription(json_dataModels.Subscribe{
|
||||
resp.AddUnsubscription(json_dataModels.Subscription{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := wsjson.Write(client.Ctx, client.Conn, response); err != nil {
|
||||
d.Log.Error("subscribe.Subscribe", err.Error())
|
||||
if err := d.Conns.SendResponse(id, resp); err != nil {
|
||||
d.Log.Error("subscribe.Unsubscribe", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
"github.com/google/uuid"
|
||||
"github.com/tecamino/tecamino-dbm/models"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
@@ -14,84 +11,19 @@ func (d *DBMHandler) AddSystemDps() (err error) {
|
||||
|
||||
typ := json_dataModels.LOU
|
||||
rights := json_dataModels.Read
|
||||
_, err = d.DB.CreateDatapoints(d.Conns, json_dataModels.Set{Path: path, Value: 0, Type: typ, Rights: rights})
|
||||
_, err = d.DBM.CreateDatapoints(json_dataModels.Set{Path: path, Value: 0, Type: typ, Rights: rights})
|
||||
if err != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps", err.Error())
|
||||
return err
|
||||
}
|
||||
dp := d.QueryDatapoints(1, path)
|
||||
d.UpdateDatapointValue(path, dp[0].GetValueUint64()+1)
|
||||
models.SystemDatapoints = d.DBM.QueryDatapoints(1, uuid.Nil, path)[0].Uuid
|
||||
|
||||
if err = d.GoSystemTime(); err != nil {
|
||||
if err = d.DBM.GoSystemTime(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = d.GoSystemMemory(); err != nil {
|
||||
if err = d.DBM.GoSystemMemory(); err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *DBMHandler) GetNumbersOfDatapoints() uint64 {
|
||||
return utils.Uint64From(d.DB.Datapoints["System"].Datapoints["Datapoints"].Value)
|
||||
}
|
||||
|
||||
func (d *DBMHandler) GoSystemTime() error {
|
||||
path := "System:Time"
|
||||
var tOld int64
|
||||
|
||||
typ := json_dataModels.STR
|
||||
rights := json_dataModels.Read
|
||||
_, err := d.DB.CreateDatapoints(d.Conns, json_dataModels.Set{Path: path, Type: typ, Rights: rights})
|
||||
if err != nil {
|
||||
d.Log.Error("system.GoSystemTime", err.Error())
|
||||
return err
|
||||
}
|
||||
dp := d.QueryDatapoints(1, "System:Datapoints")
|
||||
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()+1)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
t := time.Now().UnixMilli()
|
||||
if tOld != t {
|
||||
if er := d.DB.UpdateDatapointValue(d.Conns, t, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
tOld = t
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DBMHandler) GoSystemMemory() error {
|
||||
path := "System:UsedMemory"
|
||||
var m runtime.MemStats
|
||||
var mOld uint64
|
||||
|
||||
typ := json_dataModels.STR
|
||||
rights := json_dataModels.Read
|
||||
_, err := d.DB.CreateDatapoints(d.Conns, json_dataModels.Set{Path: path, Type: typ, Rights: rights})
|
||||
if err != nil {
|
||||
d.Log.Error("system.GoSystemMemory", err.Error())
|
||||
return err
|
||||
}
|
||||
dp := d.QueryDatapoints(1, "System:Datapoints")
|
||||
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()+1)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
runtime.ReadMemStats(&m)
|
||||
if m.Sys != mOld {
|
||||
mem := fmt.Sprintf("%.2f MB", float64(m.Sys)/1024/1024)
|
||||
if er := d.DB.UpdateDatapointValue(d.Conns, mem, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
mOld = m.Sys
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tecamino/tecamino-dbm/auth"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
@@ -35,31 +36,48 @@ func (d *DBMHandler) WebSocket(c *gin.Context) {
|
||||
|
||||
//Read loop
|
||||
for {
|
||||
|
||||
request, err := d.readJsonData(id)
|
||||
if err != nil {
|
||||
d.Log.Error("websocket.WebSocket", err.Error())
|
||||
break
|
||||
}
|
||||
|
||||
// Sets
|
||||
go d.Set(request.Set)
|
||||
|
||||
d.Get(request, id)
|
||||
// Sets
|
||||
d.Set(request, id)
|
||||
|
||||
// Subscribe
|
||||
go d.Subscribe(request.Subscribe, id)
|
||||
d.Subscribe(request, id)
|
||||
|
||||
// Unsubscribe
|
||||
go d.Unsubscribe(request.Unsubscribe, id)
|
||||
d.Unsubscribe(request, id)
|
||||
|
||||
request.Get = make([]json_dataModels.Get, 0)
|
||||
request.Set = make([]json_dataModels.Set, 0)
|
||||
request.Subscribe = make([]json_dataModels.Subscription, 0)
|
||||
request.Unsubscribe = make([]json_dataModels.Subscription, 0)
|
||||
request = nil
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DBMHandler) readJsonData(id string) (request json_dataModels.Request, err error) {
|
||||
func (d *DBMHandler) readJsonData(id string) (request *json_dataModels.Request, err error) {
|
||||
|
||||
client, ok := d.Conns.Clients[id]
|
||||
if !ok {
|
||||
return request, errors.New("client id not found")
|
||||
}
|
||||
|
||||
err = wsjson.Read(client.Ctx, client.Conn, &request)
|
||||
_, reader, err := client.Conn.Reader(client.Ctx)
|
||||
if err != nil {
|
||||
d.Log.Info("webSocket.readJsonData", fmt.Sprintf("WebSocket reader: %v\n", err))
|
||||
return request, nil
|
||||
}
|
||||
|
||||
b, err := io.ReadAll(reader)
|
||||
|
||||
if err != nil {
|
||||
code := websocket.CloseStatus(err)
|
||||
|
||||
@@ -74,5 +92,10 @@ func (d *DBMHandler) readJsonData(id string) (request json_dataModels.Request, e
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(b, &request); err != nil {
|
||||
return request, err
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
34
go.mod
34
go.mod
@@ -4,40 +4,40 @@ go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/coder/websocket v1.8.13
|
||||
github.com/gin-contrib/cors v1.7.5
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/tecamino/tecamino-json_data v0.0.10
|
||||
github.com/tecamino/tecamino-json_data v0.0.15
|
||||
github.com/tecamino/tecamino-logger v0.2.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/sonic v1.11.6 // indirect
|
||||
github.com/bytedance/sonic/loader v0.1.1 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
github.com/cloudwego/iasm v0.2.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/bytedance/sonic v1.13.2 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.4 // indirect
|
||||
github.com/cloudwego/base64x v0.1.5 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
|
||||
github.com/gin-contrib/sse v1.0.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.20.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/go-playground/validator/v10 v10.26.0 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.12 // indirect
|
||||
go.uber.org/multierr v1.10.0 // indirect
|
||||
go.uber.org/zap v1.27.0 // indirect
|
||||
golang.org/x/arch v0.8.0 // indirect
|
||||
golang.org/x/crypto v0.23.0 // indirect
|
||||
golang.org/x/net v0.25.0 // indirect
|
||||
golang.org/x/sys v0.20.0 // indirect
|
||||
golang.org/x/text v0.15.0 // indirect
|
||||
google.golang.org/protobuf v1.34.1 // indirect
|
||||
golang.org/x/arch v0.15.0 // indirect
|
||||
golang.org/x/crypto v0.36.0 // indirect
|
||||
golang.org/x/net v0.38.0 // indirect
|
||||
golang.org/x/sys v0.31.0 // indirect
|
||||
golang.org/x/text v0.23.0 // indirect
|
||||
google.golang.org/protobuf v1.36.6 // indirect
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
||||
82
go.sum
82
go.sum
@@ -1,20 +1,22 @@
|
||||
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
|
||||
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
|
||||
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
|
||||
github.com/bytedance/sonic v1.13.2 h1:8/H1FempDZqC4VqjptGo14QQlJx8VdZJegxs6wwfqpQ=
|
||||
github.com/bytedance/sonic v1.13.2/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
|
||||
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
|
||||
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
|
||||
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
|
||||
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
|
||||
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
|
||||
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
|
||||
github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE=
|
||||
github.com/coder/websocket v1.8.13/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
|
||||
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8 h1:FfZ3gj38NjllZIeJAmMhr+qKL8Wu+nOoI3GqacKw1NM=
|
||||
github.com/gabriel-vasile/mimetype v1.4.8/go.mod h1:ByKUIKGjh1ODkGM1asKUbQZOLGrPjydw3hYPU2YU9t8=
|
||||
github.com/gin-contrib/cors v1.7.5 h1:cXC9SmofOrRg0w9PigwGlHG3ztswH6bqq4vJVXnvYMk=
|
||||
github.com/gin-contrib/cors v1.7.5/go.mod h1:4q3yi7xBEDDWKapjT2o1V7mScKDDr8k+jZ0fSquGoy0=
|
||||
github.com/gin-contrib/sse v1.0.0 h1:y3bT1mUWUxDpW4JLQg/HnTqV4rozuW4tC9eFKTxYI9E=
|
||||
github.com/gin-contrib/sse v1.0.0/go.mod h1:zNuFdwarAygJBht0NTKiSi3jRf6RbqeILZ9Sp6Slhe0=
|
||||
github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU=
|
||||
github.com/gin-gonic/gin v1.10.0/go.mod h1:4PMNQiOhvDRa013RKVbsiNwoyezlm2rm0uX/T7kzp5Y=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
@@ -23,10 +25,10 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
|
||||
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/go-playground/validator/v10 v10.26.0 h1:SP05Nqhjcvz81uJaRfEV0YBSSSGMc/iMaVtFbr3Sw2k=
|
||||
github.com/go-playground/validator/v10 v10.26.0/go.mod h1:I5QpIEbmr8On7W0TktmJAumgzX4CA1XNl4ZmDuVHKKo=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
@@ -35,9 +37,13 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
|
||||
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
@@ -47,10 +53,12 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
|
||||
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
|
||||
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
@@ -61,10 +69,10 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tecamino/tecamino-json_data v0.0.10 h1:I5xvJ8eRxX0QbMTuSHlAA16FQ8uE49OiCSsQ7Xjircc=
|
||||
github.com/tecamino/tecamino-json_data v0.0.10/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tecamino/tecamino-json_data v0.0.15 h1:r8S6Ls/MMKTdsqUWd3iVDW6Zm5I5H9UCqNAW9wid02E=
|
||||
github.com/tecamino/tecamino-json_data v0.0.15/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
|
||||
github.com/tecamino/tecamino-logger v0.2.0 h1:NPH/Gg9qRhmVoW8b39i1eXu/LEftHc74nyISpcRG+XU=
|
||||
github.com/tecamino/tecamino-logger v0.2.0/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
@@ -77,29 +85,27 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
|
||||
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
|
||||
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
|
||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw=
|
||||
golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=
|
||||
golang.org/x/crypto v0.36.0 h1:AnAEvhDddvBdpY+uR+MyHmuZzzNqXSe/GvuDeob5L34=
|
||||
golang.org/x/crypto v0.36.0/go.mod h1:Y4J0ReaxCR1IMaabaSMugxJES1EpwhBHhv2bDHklZvc=
|
||||
golang.org/x/net v0.38.0 h1:vRMAPTMaeGqVhG5QyLJHqNDwecKTomGeqbnfZyKlBI8=
|
||||
golang.org/x/net v0.38.0/go.mod h1:ivrbrMbzFq5J41QOQh0siUuly180yBYtLp+CKbEaFx8=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/sys v0.31.0 h1:ioabZlmFYtWhL+TRYpcnNlLwhyxaM9kWTDEmfnprqik=
|
||||
golang.org/x/sys v0.31.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/text v0.23.0 h1:D71I7dUrlY+VX0gQShAThNGHFxZ13dGLBHQLVl1mJlY=
|
||||
golang.org/x/text v0.23.0/go.mod h1:/BLNzu4aZCJ1+kcD0DNRotWKage4q2rGVAg4o22unh4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
|
||||
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
google.golang.org/protobuf v1.36.6 h1:z1NpPI8ku2WgiWnf+t9wTPsn6eP1L7ksHUlkfLvd9xY=
|
||||
google.golang.org/protobuf v1.36.6/go.mod h1:jduwjTPXsFjZGTmRluh+L6NjiWu7pchiJ2/5YcXBHnY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
|
||||
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
|
||||
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
|
||||
|
||||
16
main.go
16
main.go
@@ -13,15 +13,17 @@ func main() {
|
||||
// cli arguments
|
||||
a := args.Init()
|
||||
|
||||
// initiate new database manger
|
||||
dbmHandler, err := dbm.NewDbmHandler(a)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//save database after exeutabe ends
|
||||
defer dbmHandler.SaveDb()
|
||||
|
||||
//initialize new server
|
||||
dbmHandler.Log.Debug("main", "initialize new server instance")
|
||||
s := server.NewServer()
|
||||
s := server.NewServer(a.AllowOrigins)
|
||||
|
||||
//set routes
|
||||
dbmHandler.Log.Debug("main", "setting routes")
|
||||
@@ -29,22 +31,22 @@ func main() {
|
||||
s.Routes.GET("/saveData", dbmHandler.SaveData)
|
||||
s.Routes.POST("/json_data", dbmHandler.Json_Data)
|
||||
s.Routes.DELETE("/json_data", dbmHandler.Delete)
|
||||
|
||||
s.Routes.GET("/", func(c *gin.Context) {
|
||||
c.String(200, "DBM WebSocket Server is running!")
|
||||
})
|
||||
|
||||
go func() {
|
||||
dbmHandler.Log.Info("main", fmt.Sprintf("http listen on %d", a.Port.Http))
|
||||
// start http server
|
||||
if err := s.ServeHttp(a.Port.Http); err != nil {
|
||||
go func() {
|
||||
dbmHandler.Log.Info("main", fmt.Sprintf("http listen on ip: %s port: %d", a.Ip, a.Port.Http))
|
||||
if err := s.ServeHttp(a.Ip, a.Port.Http); err != nil {
|
||||
dbmHandler.Log.Error("main", "error http server "+err.Error())
|
||||
panic(err)
|
||||
}
|
||||
}()
|
||||
|
||||
dbmHandler.Log.Info("main", fmt.Sprintf("https listen on %d", a.Port.Https))
|
||||
if err := s.ServeHttps(a.Port.Https, a.Cert); err != nil {
|
||||
// start https server
|
||||
dbmHandler.Log.Info("main", fmt.Sprintf("https listen on ip: %s port: %d", a.Ip, a.Port.Https))
|
||||
if err := s.ServeHttps(a.Ip, a.Port.Https, a.Cert); err != nil {
|
||||
dbmHandler.Log.Error("main", "error http server "+err.Error())
|
||||
panic(err)
|
||||
}
|
||||
|
||||
407
models/datapoint.go
Normal file
407
models/datapoint.go
Normal file
@@ -0,0 +1,407 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
serverModels "github.com/tecamino/tecamino-dbm/server/models"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
json_data "github.com/tecamino/tecamino-json_data"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
const (
|
||||
OnCreate = "onCreate"
|
||||
OnChange = "onChange"
|
||||
OnDelete = "onDelete"
|
||||
)
|
||||
|
||||
type Datapoint struct {
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Path string `json:"path"`
|
||||
Value any `json:"value,omitempty"`
|
||||
CreateDateTime int64 `json:"createDateTime,omitempty"`
|
||||
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
|
||||
Type json_dataModels.Type `json:"type"`
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
Drivers json_dataModels.Drivers `json:"drivers,omitempty"`
|
||||
Subscriptions Subscriptions `json:"-"`
|
||||
}
|
||||
|
||||
func (d *Datapoint) Set(path string, set json_dataModels.Set) (bool, error) {
|
||||
var changed bool
|
||||
if path != "" {
|
||||
changed = true
|
||||
d.Path = path
|
||||
}
|
||||
|
||||
if set.Type != "" {
|
||||
changed = true
|
||||
d.Type = set.Type
|
||||
}
|
||||
|
||||
if d.Type != "" {
|
||||
if d.Value == nil && set.Value == nil {
|
||||
changed = true
|
||||
d.Value = d.Type.DefaultValue()
|
||||
} else if d.Value != d.Type.ConvertValue(set.Value) {
|
||||
changed = true
|
||||
d.Value = d.Type.ConvertValue(set.Value)
|
||||
}
|
||||
|
||||
}
|
||||
if set.Rights != "" {
|
||||
changed = true
|
||||
d.ReadWrite = set.Rights.GetRights()
|
||||
}
|
||||
|
||||
if changed {
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
if set.Driver == nil {
|
||||
return changed, nil
|
||||
}
|
||||
if set.Driver.Type == "" {
|
||||
return changed, errors.New("driver type missing")
|
||||
}
|
||||
if set.Driver.Bus == "" {
|
||||
return changed, errors.New("driver bus name missing")
|
||||
}
|
||||
|
||||
if d.Drivers == nil {
|
||||
d.Drivers = json_dataModels.NewDrivers()
|
||||
}
|
||||
d.Drivers.AddDriver(set.Driver.Type, set.Driver.Bus, set.Driver.Address)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetValueUint64() uint64 {
|
||||
return utils.Uint64From(d.Value)
|
||||
}
|
||||
|
||||
func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...json_dataModels.Set) (created []json_dataModels.Set, uuids Uuids, err error) {
|
||||
if len(sets) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
uuids = make(Uuids, 1)
|
||||
|
||||
for _, dp := range sets {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: create or update datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
publish, err := existing.Set("", dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: existing.Uuid,
|
||||
Path: existing.Path,
|
||||
Type: existing.Type,
|
||||
Value: existing.Value,
|
||||
Rights: existing.ReadWrite,
|
||||
Drivers: &existing.Drivers,
|
||||
Updated: true,
|
||||
})
|
||||
|
||||
if publish {
|
||||
existing.Publish(conns, OnChange)
|
||||
}
|
||||
} else {
|
||||
ndp := Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
// Create new
|
||||
current.Datapoints[part] = &ndp
|
||||
publish, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
})
|
||||
if publish {
|
||||
current.Publish(conns, OnChange)
|
||||
}
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[ndp.Uuid] = &ndp
|
||||
}
|
||||
}
|
||||
|
||||
// Traverse or create intermediate datapoints
|
||||
if next, ok := current.Datapoints[part]; ok {
|
||||
current = next
|
||||
} else {
|
||||
newDp := &Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
Path: strings.Join(parts[:i+1], ":"),
|
||||
Type: json_dataModels.NONE,
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
UpdateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
Rights: newDp.ReadWrite,
|
||||
})
|
||||
|
||||
if dp.Rights != "" {
|
||||
newDp.ReadWrite = dp.Rights.GetRights()
|
||||
}
|
||||
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[newDp.Uuid] = newDp
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoint, path string) (uuids Uuids, err error) {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
uuids = make(Uuids, 1)
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: import the datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
existing.Type = dp.Type
|
||||
existing.Value = dp.Type.ConvertValue(dp.Value)
|
||||
existing.ReadWrite = dp.ReadWrite.GetRights()
|
||||
existing.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
} else {
|
||||
dp.Path = strings.Join(parts, ":")
|
||||
dp.ReadWrite = dp.ReadWrite.GetRights()
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Subscriptions = InitSubscribtion()
|
||||
current.Datapoints[part] = &dp
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[dp.Uuid] = &dp
|
||||
dp.Publish(conns, OnChange)
|
||||
}
|
||||
|
||||
return uuids, nil
|
||||
}
|
||||
|
||||
// Traverse or create intermediate nodes
|
||||
if next, ok := current.Datapoints[part]; ok {
|
||||
current = next
|
||||
} else {
|
||||
newDp := &Datapoint{
|
||||
Path: strings.Join(parts[:i+1], ":"),
|
||||
Type: json_dataModels.NONE,
|
||||
ReadWrite: dp.ReadWrite.GetRights(),
|
||||
UpdateDateTime: time.Now().UnixMilli(),
|
||||
}
|
||||
newDp.ReadWrite = newDp.ReadWrite.GetRights()
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
//add uuid to flat map for faster lookuo
|
||||
uuids[newDp.Uuid] = newDp
|
||||
}
|
||||
}
|
||||
return uuids, nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateDatapointValue(conns *serverModels.Connections, value any, path string) error {
|
||||
|
||||
paths := strings.Split(path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range paths {
|
||||
dp, ok := current.Datapoints[part]
|
||||
if !ok {
|
||||
return fmt.Errorf("datapoint path not found: %s (at %s)", path, part)
|
||||
}
|
||||
if i == len(paths)-1 {
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
return nil
|
||||
}
|
||||
current = dp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateValue(conns *serverModels.Connections, value any) error {
|
||||
d.Value = d.Type.ConvertValue(value)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
d.Publish(conns, OnChange)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDatapoint(conns *serverModels.Connections, set json_dataModels.Set) (json_dataModels.Set, error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
return json_dataModels.Set{}, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
}
|
||||
|
||||
current := d
|
||||
for i := range len(parts) - 1 {
|
||||
next, ok := current.Datapoints[parts[i]]
|
||||
if !ok {
|
||||
return json_dataModels.Set{}, fmt.Errorf("path not found: '%s'", strings.Join(parts[:i+1], ":"))
|
||||
}
|
||||
current = next
|
||||
}
|
||||
|
||||
toDelete := parts[len(parts)-1]
|
||||
if dp, ok := current.Datapoints[toDelete]; ok {
|
||||
dp.Publish(conns, OnDelete)
|
||||
delete(current.Datapoints, toDelete)
|
||||
return json_dataModels.Set{
|
||||
Uuid: set.Uuid,
|
||||
Path: set.Path,
|
||||
}, nil
|
||||
}
|
||||
return json_dataModels.Set{}, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
|
||||
var dfs func(dp *Datapoint, currentDepth uint)
|
||||
dfs = func(dp *Datapoint, currentDepth uint) {
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth == 0 {
|
||||
// Return all descendants
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if currentDepth == depth-1 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
}
|
||||
dps = append(dps, d)
|
||||
dfs(d, 0)
|
||||
dps.SortSlice()
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) QueryDatapoints(depth uint, path string) (dps Datapoints) {
|
||||
parts := strings.Split(path, ":")
|
||||
|
||||
var dfs func(current *Datapoint, index int)
|
||||
dfs = func(current *Datapoint, index int) {
|
||||
if index == len(parts) || path == "" {
|
||||
dps = append(dps, current.GetAllDatapoints(depth)...)
|
||||
return
|
||||
}
|
||||
|
||||
pattern := "^" + parts[index] + "$"
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for name, dp := range current.Datapoints {
|
||||
if re.MatchString(name) {
|
||||
dfs(dp, index+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dfs(d, 0)
|
||||
dps.SortSlice()
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) AddSubscribtion(id string, sub json_dataModels.Subscription) {
|
||||
if d.Subscriptions == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s, ok := d.Subscriptions[id]; ok {
|
||||
s.OnCreate = sub.OnCreate
|
||||
s.OnChange = sub.OnChange
|
||||
s.OnDelete = sub.OnDelete
|
||||
} else {
|
||||
d.Subscriptions[id] = &Subscription{
|
||||
OnCreate: sub.OnCreate,
|
||||
OnChange: sub.OnChange,
|
||||
OnDelete: sub.OnDelete,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveSubscribtion(id string) {
|
||||
if _, ok := d.Subscriptions[id]; !ok {
|
||||
return
|
||||
}
|
||||
delete(d.Subscriptions, id)
|
||||
}
|
||||
|
||||
func (d *Datapoint) Publish(conns *serverModels.Connections, eventType string) error {
|
||||
if conns.Clients == nil {
|
||||
return nil
|
||||
}
|
||||
conns.RLock()
|
||||
defer conns.RUnlock()
|
||||
|
||||
for id := range d.Subscriptions {
|
||||
if _, ok := conns.Clients[id]; !ok {
|
||||
delete(d.Subscriptions, id)
|
||||
} else {
|
||||
r := json_data.NewResponse()
|
||||
r.AddUPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Value: d.Value,
|
||||
})
|
||||
|
||||
if err := conns.SendResponse(id, r); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,392 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
import "sort"
|
||||
|
||||
"github.com/coder/websocket/wsjson"
|
||||
"github.com/google/uuid"
|
||||
serverModels "github.com/tecamino/tecamino-dbm/server/models"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
json_data "github.com/tecamino/tecamino-json_data"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
type Datapoints []*Datapoint
|
||||
|
||||
const (
|
||||
OnCreate = "onCreate"
|
||||
OnChange = "onChange"
|
||||
OnDelete = "onDelete"
|
||||
)
|
||||
|
||||
type Datapoint struct {
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Path string `json:"path"`
|
||||
Value any `json:"value,omitempty"`
|
||||
CreateDateTime int64 `json:"createDateTime,omitempty"`
|
||||
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
|
||||
Type json_dataModels.Type `json:"type"`
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
Drivers json_dataModels.Drivers `json:"drivers,omitempty"`
|
||||
Subscriptions Subscriptions `json:"-"`
|
||||
}
|
||||
|
||||
func (d *Datapoint) Set(path string, set json_dataModels.Set) (bool, error) {
|
||||
var changed bool
|
||||
if path != "" {
|
||||
changed = true
|
||||
d.Path = path
|
||||
}
|
||||
|
||||
if set.Type != "" {
|
||||
changed = true
|
||||
d.Type = set.Type
|
||||
}
|
||||
|
||||
if d.Type != "" {
|
||||
if d.Value == nil && set.Value == nil {
|
||||
changed = true
|
||||
d.Value = d.Type.DefaultValue()
|
||||
} else if d.Value != d.Type.ConvertValue(set.Value) {
|
||||
changed = true
|
||||
d.Value = d.Type.ConvertValue(set.Value)
|
||||
}
|
||||
|
||||
}
|
||||
if set.Rights != "" {
|
||||
changed = true
|
||||
d.ReadWrite = set.Rights.GetRights()
|
||||
}
|
||||
|
||||
if changed {
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
}
|
||||
|
||||
if set.Driver == nil {
|
||||
return changed, nil
|
||||
}
|
||||
if set.Driver.Type == "" {
|
||||
return changed, errors.New("driver type missing")
|
||||
}
|
||||
if set.Driver.Bus == "" {
|
||||
return changed, errors.New("driver bus name missing")
|
||||
}
|
||||
|
||||
if d.Drivers == nil {
|
||||
d.Drivers = json_dataModels.NewDrivers()
|
||||
}
|
||||
d.Drivers.AddDriver(set.Driver.Type, set.Driver.Bus, set.Driver.Address)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetValueUint64() uint64 {
|
||||
return utils.Uint64From(d.Value)
|
||||
}
|
||||
|
||||
func (d *Datapoint) CreateDatapoints(conns *serverModels.Connections, sets ...json_dataModels.Set) (created []json_dataModels.Set, err error) {
|
||||
if len(sets) == 0 {
|
||||
return
|
||||
}
|
||||
for _, dp := range sets {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: create or update datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
publish, err := existing.Set("", dp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: existing.Uuid,
|
||||
Path: existing.Path,
|
||||
Type: existing.Type,
|
||||
Value: existing.Value,
|
||||
Rights: existing.ReadWrite,
|
||||
Drivers: &existing.Drivers,
|
||||
Updated: true,
|
||||
func (d *Datapoints) SortSlice() {
|
||||
// Sort by Path before processing
|
||||
sort.Slice(*d, func(i, j int) bool {
|
||||
return (*d)[i].Path < (*d)[j].Path
|
||||
})
|
||||
|
||||
if publish {
|
||||
existing.Publish(conns, OnChange)
|
||||
}
|
||||
} else {
|
||||
ndp := Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
// Create new
|
||||
current.Datapoints[part] = &ndp
|
||||
publish, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
})
|
||||
if publish {
|
||||
current.Publish(conns, OnChange)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Traverse or create intermediate datapoints
|
||||
if next, ok := current.Datapoints[part]; ok {
|
||||
current = next
|
||||
} else {
|
||||
newDp := &Datapoint{
|
||||
Uuid: uuid.New(),
|
||||
Path: strings.Join(parts[:i+1], ":"),
|
||||
Type: json_dataModels.NONE,
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
UpdateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
Rights: newDp.ReadWrite,
|
||||
})
|
||||
|
||||
if dp.Rights != "" {
|
||||
newDp.ReadWrite = dp.Rights.GetRights()
|
||||
}
|
||||
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) ImportDatapoint(conns *serverModels.Connections, dp Datapoint, path string) error {
|
||||
parts := strings.Split(dp.Path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range parts {
|
||||
if current.Datapoints == nil {
|
||||
current.Datapoints = make(map[string]*Datapoint)
|
||||
}
|
||||
|
||||
if i == len(parts)-1 {
|
||||
// Leaf node: import the datapoint
|
||||
if existing, ok := current.Datapoints[part]; ok {
|
||||
existing.Type = dp.Type
|
||||
existing.Value = current.Type.ConvertValue(dp.Value)
|
||||
existing.ReadWrite = dp.ReadWrite.GetRights()
|
||||
existing.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
} else {
|
||||
dp.Path = strings.Join(parts, ":")
|
||||
dp.ReadWrite = dp.ReadWrite.GetRights()
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Subscriptions = InitSubscribtion()
|
||||
current.Datapoints[part] = &dp
|
||||
dp.Publish(conns, OnChange)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Traverse or create intermediate nodes
|
||||
if next, ok := current.Datapoints[part]; ok {
|
||||
current = next
|
||||
} else {
|
||||
newDp := &Datapoint{
|
||||
Path: strings.Join(parts[:i+1], ":"),
|
||||
Type: json_dataModels.NONE,
|
||||
ReadWrite: dp.ReadWrite.GetRights(),
|
||||
UpdateDateTime: time.Now().UnixMilli(),
|
||||
}
|
||||
newDp.ReadWrite = newDp.ReadWrite.GetRights()
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateDatapointValue(conns *serverModels.Connections, value any, path string) error {
|
||||
|
||||
paths := strings.Split(path, ":")
|
||||
|
||||
current := d
|
||||
for i, part := range paths {
|
||||
dp, ok := current.Datapoints[part]
|
||||
if !ok {
|
||||
return fmt.Errorf("datapoint path not found: %s (at %s)", path, part)
|
||||
}
|
||||
if i == len(paths)-1 {
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(conns, OnChange)
|
||||
return nil
|
||||
}
|
||||
current = dp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateValue(conns *serverModels.Connections, value any) error {
|
||||
d.Value = d.Type.ConvertValue(value)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
d.Publish(conns, OnChange)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDatapoint(conns *serverModels.Connections, set json_dataModels.Set) (json_dataModels.Set, error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
return json_dataModels.Set{}, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
}
|
||||
|
||||
current := d
|
||||
for i := range len(parts) - 1 {
|
||||
next, ok := current.Datapoints[parts[i]]
|
||||
if !ok {
|
||||
return json_dataModels.Set{}, fmt.Errorf("path not found: '%s'", strings.Join(parts[:i+1], ":"))
|
||||
}
|
||||
current = next
|
||||
}
|
||||
|
||||
toDelete := parts[len(parts)-1]
|
||||
if dp, ok := current.Datapoints[toDelete]; ok {
|
||||
dp.Publish(conns, OnDelete)
|
||||
delete(current.Datapoints, toDelete)
|
||||
return json_dataModels.Set{
|
||||
Uuid: set.Uuid,
|
||||
Path: set.Path,
|
||||
}, nil
|
||||
}
|
||||
return json_dataModels.Set{}, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetAllDatapoints(depth uint) (dps []*Datapoint) {
|
||||
|
||||
var dfs func(dp *Datapoint, currentDepth uint)
|
||||
dfs = func(dp *Datapoint, currentDepth uint) {
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth == 0 {
|
||||
// Return all descendants
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if currentDepth == depth-1 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
}
|
||||
dps = append(dps, d)
|
||||
dfs(d, 0)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) QueryDatapoints(depth uint, path string) (dps []*Datapoint) {
|
||||
parts := strings.Split(path, ":")
|
||||
|
||||
var dfs func(current *Datapoint, index int)
|
||||
dfs = func(current *Datapoint, index int) {
|
||||
|
||||
if index == len(parts) {
|
||||
dps = append(dps, current.GetAllDatapoints(depth)...)
|
||||
return
|
||||
}
|
||||
|
||||
pattern := "^" + parts[index] + "$"
|
||||
re, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for name, dp := range current.Datapoints {
|
||||
if re.MatchString(name) {
|
||||
dfs(dp, index+1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dfs(d, 0)
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) AddSubscribtion(id string, sub json_dataModels.Subscribe) {
|
||||
if d.Subscriptions == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s, ok := d.Subscriptions[id]; ok {
|
||||
s.OnCreate = sub.OnCreate
|
||||
s.OnChange = sub.OnChange
|
||||
s.OnDelete = sub.OnDelete
|
||||
} else {
|
||||
d.Subscriptions[id] = &Subscription{
|
||||
OnCreate: sub.OnCreate,
|
||||
OnChange: sub.OnChange,
|
||||
OnDelete: sub.OnDelete,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveSubscribtion(id string) {
|
||||
if _, ok := d.Subscriptions[id]; !ok {
|
||||
return
|
||||
}
|
||||
delete(d.Subscriptions, id)
|
||||
}
|
||||
|
||||
func (d *Datapoint) Publish(conns *serverModels.Connections, eventType string) error {
|
||||
if conns.Clients == nil {
|
||||
return nil
|
||||
}
|
||||
conns.RLock()
|
||||
defer conns.RUnlock()
|
||||
|
||||
for id := range d.Subscriptions {
|
||||
if client, ok := conns.Clients[id]; !ok {
|
||||
delete(d.Subscriptions, id)
|
||||
} else {
|
||||
r := json_data.NewResponse()
|
||||
r.AddUPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Value: d.Value,
|
||||
})
|
||||
err := wsjson.Write(client.Ctx, client.Conn, r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
191
models/dbm.go
Normal file
191
models/dbm.go
Normal file
@@ -0,0 +1,191 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"maps"
|
||||
|
||||
"github.com/google/uuid"
|
||||
serverModels "github.com/tecamino/tecamino-dbm/server/models"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
type DBM struct {
|
||||
Datapoints Datapoint
|
||||
Uuids Uuids
|
||||
Conns *serverModels.Connections
|
||||
Log *logging.Logger
|
||||
}
|
||||
|
||||
var SystemDatapoints uuid.UUID
|
||||
|
||||
func NewDBM(conns *serverModels.Connections, log *logging.Logger) *DBM {
|
||||
return &DBM{
|
||||
Uuids: make(Uuids),
|
||||
Conns: conns,
|
||||
Log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DBM) CreateDatapoints(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
|
||||
if len(sets) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
dps, uuids, err := d.Datapoints.CreateDatapoints(d.Conns, sets...)
|
||||
|
||||
//save uuid in seperate map for fast look up
|
||||
maps.Copy(d.Uuids, uuids)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ndp uint64
|
||||
for _, dp := range dps {
|
||||
if !dp.Updated {
|
||||
ndp++
|
||||
}
|
||||
}
|
||||
|
||||
d.ModifyCountedDatapoints(ndp, false)
|
||||
return dps, nil
|
||||
}
|
||||
|
||||
func (d *DBM) ImportDatapoints(dps ...Datapoint) error {
|
||||
for _, dp := range dps {
|
||||
uuids, err := d.Datapoints.ImportDatapoint(d.Conns, dp, dp.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
maps.Copy(d.Uuids, uuids)
|
||||
|
||||
d.ModifyCountedDatapoints(1, false)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DBM) UpdateDatapointValue(value any, uid uuid.UUID, path ...string) error {
|
||||
if uid != uuid.Nil {
|
||||
if _, ok := d.Uuids[uid]; !ok {
|
||||
return fmt.Errorf("uuid %s not found", uid.String())
|
||||
}
|
||||
dp := d.Uuids[uid]
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(d.Conns, OnChange)
|
||||
}
|
||||
|
||||
if len(path) > 1 {
|
||||
return fmt.Errorf("only one path allowed")
|
||||
}
|
||||
|
||||
return d.Datapoints.UpdateDatapointValue(d.Conns, value, path[0])
|
||||
}
|
||||
|
||||
func (d *DBM) RemoveDatapoint(sets ...json_dataModels.Set) ([]json_dataModels.Set, error) {
|
||||
var lsRemoved []json_dataModels.Set
|
||||
for _, set := range sets {
|
||||
removed, err := d.Datapoints.RemoveDatapoint(d.Conns, set)
|
||||
if err != nil {
|
||||
return lsRemoved, err
|
||||
}
|
||||
lsRemoved = append(lsRemoved, removed)
|
||||
d.ModifyCountedDatapoints(1, true)
|
||||
}
|
||||
|
||||
return lsRemoved, nil
|
||||
}
|
||||
|
||||
func (d *DBM) QueryDatapoints(depth uint, uid uuid.UUID, key ...string) []*Datapoint {
|
||||
if uid != uuid.Nil {
|
||||
if _, ok := d.Uuids[uid]; !ok {
|
||||
return nil
|
||||
}
|
||||
dp := d.Uuids[uid]
|
||||
if depth == 1 {
|
||||
return []*Datapoint{dp}
|
||||
}
|
||||
return append([]*Datapoint{}, dp.QueryDatapoints(depth, key[0])...)
|
||||
}
|
||||
return d.Datapoints.QueryDatapoints(depth, key[0])
|
||||
}
|
||||
|
||||
func (d *DBM) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
return d.Datapoints.GetAllDatapoints(0)
|
||||
}
|
||||
|
||||
func (d *DBM) GetNumbersOfDatapoints() uint64 {
|
||||
return utils.Uint64From(d.Datapoints.Datapoints["System"].Datapoints["Datapoints"].Value)
|
||||
}
|
||||
|
||||
func (d *DBM) ModifyCountedDatapoints(count uint64, countDown bool) {
|
||||
dp := d.QueryDatapoints(1, SystemDatapoints, "System:Datapoints")
|
||||
amount := dp[0].GetValueUint64()
|
||||
if countDown {
|
||||
amount -= count
|
||||
} else {
|
||||
amount += count
|
||||
}
|
||||
d.UpdateDatapointValue(amount, SystemDatapoints, "System:Datapoints")
|
||||
}
|
||||
|
||||
func (d *DBM) GoSystemTime() error {
|
||||
path := "System:Time"
|
||||
var tOld int64
|
||||
|
||||
typ := json_dataModels.STR
|
||||
rights := json_dataModels.Read
|
||||
_, err := d.CreateDatapoints(json_dataModels.Set{Path: path, Type: typ, Rights: rights})
|
||||
if err != nil {
|
||||
d.Log.Error("system.GoSystemTime", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
t := time.Now().UnixMilli()
|
||||
if tOld != t {
|
||||
if er := d.UpdateDatapointValue(time.UnixMilli(t).Format("2006-01-02 15:04:05"), uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
tOld = t
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DBM) GoSystemMemory() error {
|
||||
path := "System:UsedMemory"
|
||||
var m runtime.MemStats
|
||||
var mOld uint64
|
||||
|
||||
typ := json_dataModels.STR
|
||||
rights := json_dataModels.Read
|
||||
_, err := d.CreateDatapoints(json_dataModels.Set{Path: path, Type: typ, Rights: rights})
|
||||
if err != nil {
|
||||
d.Log.Error("system.GoSystemMemory", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
runtime.ReadMemStats(&m)
|
||||
if m.Sys != mOld {
|
||||
mem := fmt.Sprintf("%.2f MB", float64(m.Sys)/1024/1024)
|
||||
if er := d.UpdateDatapointValue(mem, uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
mOld = m.Sys
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
5
models/uuids.go
Normal file
5
models/uuids.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package models
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
type Uuids map[uuid.UUID]*Datapoint
|
||||
@@ -1,17 +1,24 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
"github.com/gin-gonic/gin"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
// serves as connection handler of websocket
|
||||
type Connections struct {
|
||||
sync.RWMutex
|
||||
Clients Clients
|
||||
}
|
||||
|
||||
// initaiates new conections with client map
|
||||
func NewConnections() *Connections {
|
||||
return &Connections{
|
||||
Clients: NewClients(),
|
||||
@@ -30,3 +37,31 @@ func (c *Connections) RemoveClient(id string) {
|
||||
func (c *Connections) DisconnectWsConnection(id string, code websocket.StatusCode, reason string) {
|
||||
c.Clients.DisconnectWsConnection(id, code, reason)
|
||||
}
|
||||
|
||||
// sends json response to client
|
||||
func (c *Connections) SendResponse(id string, r *json_dataModels.Response) error {
|
||||
client, ok := c.Clients[id]
|
||||
if !ok {
|
||||
return fmt.Errorf("client not found for id %s", id)
|
||||
|
||||
}
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
w, err := client.Conn.Writer(ctx, websocket.MessageText)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer w.Close()
|
||||
|
||||
_, err = w.Write(b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,31 +3,47 @@ package server
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tecamino/tecamino-dbm/cert"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
// server model for database manager websocket
|
||||
type Server struct {
|
||||
Routes *gin.Engine
|
||||
sync.RWMutex
|
||||
Logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
// initalizes new dbm server
|
||||
func NewServer(allowOrigins []string) *Server {
|
||||
r := gin.Default()
|
||||
r.Use(cors.New(cors.Config{
|
||||
AllowOrigins: allowOrigins,
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Content-Type", "Accept"},
|
||||
ExposeHeaders: []string{"Content-Length"},
|
||||
AllowCredentials: true,
|
||||
MaxAge: 12 * time.Hour,
|
||||
}))
|
||||
return &Server{
|
||||
Routes: gin.Default(),
|
||||
Routes: r,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) ServeHttp(port uint) error {
|
||||
return s.Routes.Run(fmt.Sprintf(":%d", port))
|
||||
// serve dbm as http
|
||||
func (s *Server) ServeHttp(ip string, port uint) error {
|
||||
return s.Routes.Run(fmt.Sprintf("%s:%d", ip, port))
|
||||
}
|
||||
|
||||
func (s *Server) ServeHttps(port uint, cert cert.Cert) error {
|
||||
// serve dbm as http
|
||||
func (s *Server) ServeHttps(ip string, port uint, cert cert.Cert) error {
|
||||
// generate self signed tls certificate
|
||||
if err := cert.GenerateSelfSignedCert(); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.Routes.RunTLS(fmt.Sprintf(":%d", port), cert.CertFile, cert.KeyFile)
|
||||
return s.Routes.RunTLS(fmt.Sprintf("%s:%d", ip, port), cert.CertFile, cert.KeyFile)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/tecamino/tecamino-dbm/args"
|
||||
"github.com/tecamino/tecamino-dbm/cert"
|
||||
"github.com/tecamino/tecamino-dbm/dbm"
|
||||
@@ -87,11 +88,7 @@ func TestQuery(t *testing.T) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// for i, o := range dmaHandler.QueryDatapoints(".*002.*") {
|
||||
// fmt.Println(600, i, o)
|
||||
// }
|
||||
|
||||
for i, o := range dmaHandler.QueryDatapoints(1, "Test:A:000") {
|
||||
for i, o := range dmaHandler.DBM.QueryDatapoints(1, uuid.Nil, "Test:A:000") {
|
||||
fmt.Println(600, i, o)
|
||||
}
|
||||
|
||||
@@ -145,7 +142,7 @@ func TestUpdateDps(t *testing.T) {
|
||||
|
||||
func TestServer(t *testing.T) {
|
||||
fmt.Println("start")
|
||||
server := server.NewServer()
|
||||
server := server.NewServer([]string{".*"})
|
||||
|
||||
t.Fatal(server.ServeHttp(8100))
|
||||
t.Fatal(server.ServeHttp("http://localhost", 8100))
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// return any input type to float32
|
||||
func Float32From(v any) float32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -44,6 +45,7 @@ func Float32From(v any) float32 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to float64
|
||||
func Float64From(v any) float64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -83,6 +85,7 @@ func Float64From(v any) float64 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int8
|
||||
func Int8From(v any) int8 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -122,6 +125,7 @@ func Int8From(v any) int8 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int16
|
||||
func Int16From(v any) int16 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -161,6 +165,7 @@ func Int16From(v any) int16 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int32
|
||||
func Int32From(v any) int32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -200,6 +205,7 @@ func Int32From(v any) int32 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int64
|
||||
func Int64From(v any) int64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -239,6 +245,7 @@ func Int64From(v any) int64 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int
|
||||
func Uint8From(v any) uint8 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -278,6 +285,7 @@ func Uint8From(v any) uint8 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to uint16
|
||||
func Uint16From(v any) uint16 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -317,6 +325,7 @@ func Uint16From(v any) uint16 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to uint32
|
||||
func Uint32From(v any) uint32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -356,6 +365,7 @@ func Uint32From(v any) uint32 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to uint64
|
||||
func Uint64From(v any) uint64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
@@ -395,6 +405,7 @@ func Uint64From(v any) uint64 {
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to bool
|
||||
func BoolFrom(v any) bool {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
|
||||
Reference in New Issue
Block a user