Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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
|
- name: Build binary
|
||||||
run: |
|
run: |
|
||||||
mkdir -p build
|
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
|
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }} main.go
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Upload artifacts
|
- name: Upload artifacts
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
|
|||||||
@@ -13,14 +13,15 @@ func (d *DBMHandler) SaveData(c *gin.Context) {
|
|||||||
s := time.Now()
|
s := time.Now()
|
||||||
if err := d.SaveDb(); err != nil {
|
if err := d.SaveDb(); err != nil {
|
||||||
r := json_dataModels.NewResponse()
|
r := json_dataModels.NewResponse()
|
||||||
r.SendError(err.Error())
|
r.SetError()
|
||||||
|
r.SetMessage(err.Error())
|
||||||
c.JSON(http.StatusBadRequest, r)
|
c.JSON(http.StatusBadRequest, r)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r := json_dataModels.NewResponse()
|
r := json_dataModels.NewResponse()
|
||||||
r.SendMessage(fmt.Sprintf("DBM %d datapoints saved in: %v", d.GetNumbersOfDatapoints(), time.Since(s)))
|
r.SetMessage(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)))
|
d.Log.Info("db.SaveData", fmt.Sprintf("DBM %d datapoints saved in: %v", d.GetNumbersOfDatapoints(), time.Since(s)))
|
||||||
c.JSON(http.StatusOK, r)
|
c.JSON(http.StatusOK, r)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -131,8 +131,8 @@ func (d *DBMHandler) ImportDatapoints(dps ...models.Datapoint) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
dp := d.QueryDatapoints(1, "System:Datapoints")
|
dps := d.QueryDatapoints(1, "System:Datapoints")
|
||||||
d.UpdateDatapointValue("System:Datapoints", dp[0].GetValueUint64()+1)
|
d.UpdateDatapointValue("System:Datapoints", dps[0].GetValueUint64()+1)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
39
dbm/get.go
Normal file
39
dbm/get.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
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.DB.QueryDatapoints(depth, get.Path) {
|
||||||
|
resp.AddGet(json_dataModels.Get{
|
||||||
|
Uuid: dp.Uuid,
|
||||||
|
Path: dp.Path,
|
||||||
|
Type: dp.Type,
|
||||||
|
Value: dp.Value,
|
||||||
|
Rights: dp.ReadWrite,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := d.Conns.SendResponse(id, resp); err != nil {
|
||||||
|
d.Log.Error("get.Get", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,8 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
|
|||||||
payload, err := json_data.ParseRequest(c.Request.Body)
|
payload, err := json_data.ParseRequest(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r := json_data.NewResponse()
|
r := json_data.NewResponse()
|
||||||
r.SendError(err.Error())
|
r.SetError()
|
||||||
|
r.SetMessage(err.Error())
|
||||||
c.JSON(http.StatusBadRequest, r)
|
c.JSON(http.StatusBadRequest, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -44,14 +45,14 @@ func (d *DBMHandler) Json_Data(c *gin.Context) {
|
|||||||
respond.Set, err = d.CreateDatapoints(payload.Set...)
|
respond.Set, err = d.CreateDatapoints(payload.Set...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r := json_data.NewResponse()
|
r := json_data.NewResponse()
|
||||||
r.SendError(err.Error())
|
r.SetError()
|
||||||
|
r.SetMessage(err.Error())
|
||||||
c.JSON(http.StatusBadRequest, r)
|
c.JSON(http.StatusBadRequest, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, respond)
|
c.JSON(200, respond)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DBMHandler) Delete(c *gin.Context) {
|
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)
|
payload, err := json_data.ParseRequest(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r := json_data.NewResponse()
|
r := json_data.NewResponse()
|
||||||
r.SendError(err.Error())
|
r.SetError()
|
||||||
|
r.SetMessage(err.Error())
|
||||||
c.JSON(http.StatusBadRequest, r)
|
c.JSON(http.StatusBadRequest, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -71,12 +73,12 @@ func (d *DBMHandler) Delete(c *gin.Context) {
|
|||||||
response.Set, err = d.RemoveDatapoint(payload.Set...)
|
response.Set, err = d.RemoveDatapoint(payload.Set...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
r := json_data.NewResponse()
|
r := json_data.NewResponse()
|
||||||
r.SendError(err.Error())
|
r.SetError()
|
||||||
|
r.SetMessage(err.Error())
|
||||||
c.JSON(http.StatusBadRequest, r)
|
c.JSON(http.StatusBadRequest, r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(200, response)
|
c.JSON(200, response)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,14 +4,16 @@ import (
|
|||||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *DBMHandler) Set(sets []json_dataModels.Set) {
|
func (d *DBMHandler) Set(req *json_dataModels.Request) {
|
||||||
if sets == nil {
|
if req == nil {
|
||||||
|
return
|
||||||
|
} else if len(req.Set) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d.RLock()
|
d.RLock()
|
||||||
defer d.RUnlock()
|
defer d.RUnlock()
|
||||||
|
|
||||||
for _, set := range sets {
|
for _, set := range req.Set {
|
||||||
for _, dp := range d.DB.QueryDatapoints(1, set.Path) {
|
for _, dp := range d.DB.QueryDatapoints(1, set.Path) {
|
||||||
dp.UpdateValue(d.Conns, set.Value)
|
dp.UpdateValue(d.Conns, set.Value)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,23 @@
|
|||||||
package dbm
|
package dbm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/coder/websocket/wsjson"
|
|
||||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id string) {
|
func (d *DBMHandler) Subscribe(req *json_dataModels.Request, id string) {
|
||||||
if subs == nil {
|
if req == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(req.Subscribe) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d.RLock()
|
d.RLock()
|
||||||
defer d.RUnlock()
|
defer d.RUnlock()
|
||||||
|
|
||||||
client, ok := d.Conns.Clients[id]
|
resp := json_dataModels.NewResponse()
|
||||||
if !ok {
|
resp.Id = req.Id
|
||||||
d.Log.Error("subscribe.Subscribe", "client not found for id "+id)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response := json_dataModels.NewResponse()
|
for _, sub := range req.Subscribe {
|
||||||
|
|
||||||
for _, sub := range subs {
|
|
||||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||||
if sub.Driver != "" {
|
if sub.Driver != "" {
|
||||||
if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil {
|
if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil {
|
||||||
@@ -28,7 +25,7 @@ func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
dp.AddSubscribtion(id, sub)
|
dp.AddSubscribtion(id, sub)
|
||||||
response.AddSubscription(json_dataModels.Subscribe{
|
resp.AddSubscription(json_dataModels.Subscription{
|
||||||
Uuid: dp.Uuid,
|
Uuid: dp.Uuid,
|
||||||
Path: dp.Path,
|
Path: dp.Path,
|
||||||
Value: dp.Value,
|
Value: dp.Value,
|
||||||
@@ -37,39 +34,38 @@ func (d *DBMHandler) Subscribe(subs []json_dataModels.Subscribe, id string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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())
|
d.Log.Error("subscribe.Subscribe", err.Error())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DBMHandler) Unsubscribe(subs []json_dataModels.Subscribe, id string) {
|
func (d *DBMHandler) Unsubscribe(req *json_dataModels.Request, id string) {
|
||||||
if subs == nil {
|
if req == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(req.Unsubscribe) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d.RLock()
|
d.RLock()
|
||||||
defer d.RUnlock()
|
defer d.RUnlock()
|
||||||
|
|
||||||
client, ok := d.Conns.Clients[id]
|
resp := json_dataModels.NewResponse()
|
||||||
if !ok {
|
|
||||||
d.Log.Error("subscribe.Subscribe", "client not found for id "+id)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
response := json_dataModels.NewResponse()
|
for _, sub := range req.Unsubscribe {
|
||||||
|
|
||||||
for _, sub := range subs {
|
|
||||||
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
for _, dp := range d.DB.QueryDatapoints(sub.Depth, sub.Path) {
|
||||||
if _, ok := dp.Subscriptions[id]; !ok {
|
if _, ok := dp.Subscriptions[id]; !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
dp.RemoveSubscribtion(id)
|
dp.RemoveSubscribtion(id)
|
||||||
response.AddUnsubscription(json_dataModels.Subscribe{
|
resp.AddUnsubscription(json_dataModels.Subscription{
|
||||||
Uuid: dp.Uuid,
|
Uuid: dp.Uuid,
|
||||||
Path: dp.Path,
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func (d *DBMHandler) GoSystemTime() error {
|
|||||||
for {
|
for {
|
||||||
t := time.Now().UnixMilli()
|
t := time.Now().UnixMilli()
|
||||||
if tOld != t {
|
if tOld != t {
|
||||||
if er := d.DB.UpdateDatapointValue(d.Conns, t, path); er != nil {
|
if er := d.DB.UpdateDatapointValue(d.Conns, time.UnixMilli(t).Format("2006-01-02 15:04:05"), path); er != nil {
|
||||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||||
}
|
}
|
||||||
tOld = t
|
tOld = t
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package dbm
|
package dbm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
"github.com/coder/websocket/wsjson"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/tecamino/tecamino-dbm/auth"
|
"github.com/tecamino/tecamino-dbm/auth"
|
||||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||||
@@ -35,31 +36,46 @@ func (d *DBMHandler) WebSocket(c *gin.Context) {
|
|||||||
|
|
||||||
//Read loop
|
//Read loop
|
||||||
for {
|
for {
|
||||||
|
|
||||||
request, err := d.readJsonData(id)
|
request, err := d.readJsonData(id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
d.Log.Error("websocket.WebSocket", err.Error())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sets
|
// Sets
|
||||||
go d.Set(request.Set)
|
|
||||||
|
d.Get(request, id)
|
||||||
|
// Sets
|
||||||
|
d.Set(request)
|
||||||
|
|
||||||
// Subscribe
|
// Subscribe
|
||||||
go d.Subscribe(request.Subscribe, id)
|
d.Subscribe(request, id)
|
||||||
|
|
||||||
// Unsubscribe
|
// 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]
|
client, ok := d.Conns.Clients[id]
|
||||||
if !ok {
|
if !ok {
|
||||||
return request, errors.New("client id not found")
|
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 {
|
||||||
|
return request, err
|
||||||
|
}
|
||||||
|
|
||||||
|
b, err := io.ReadAll(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
code := websocket.CloseStatus(err)
|
code := websocket.CloseStatus(err)
|
||||||
|
|
||||||
@@ -74,5 +90,10 @@ func (d *DBMHandler) readJsonData(id string) (request json_dataModels.Request, e
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := json.Unmarshal(b, &request); err != nil {
|
||||||
|
return request, err
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -6,7 +6,7 @@ require (
|
|||||||
github.com/coder/websocket v1.8.13
|
github.com/coder/websocket v1.8.13
|
||||||
github.com/gin-gonic/gin v1.10.0
|
github.com/gin-gonic/gin v1.10.0
|
||||||
github.com/google/uuid v1.6.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.12
|
||||||
github.com/tecamino/tecamino-logger v0.2.0
|
github.com/tecamino/tecamino-logger v0.2.0
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
4
go.sum
4
go.sum
@@ -63,8 +63,8 @@ github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
|
|||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
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 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
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.12 h1:S4Y+WcfQNrin7P73ZI+4eJWh62IwJVhriRsPGGM8N34=
|
||||||
github.com/tecamino/tecamino-json_data v0.0.10/go.mod h1:LLlyD7Wwqplb2BP4PeO86EokEcTRidlW5MwgPd1T2JY=
|
github.com/tecamino/tecamino-json_data v0.0.12/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 h1:NPH/Gg9qRhmVoW8b39i1eXu/LEftHc74nyISpcRG+XU=
|
||||||
github.com/tecamino/tecamino-logger v0.2.0/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8=
|
github.com/tecamino/tecamino-logger v0.2.0/go.mod h1:0M1E9Uei/qw3e3WA1x3lBo1eP3H5oeYE7GjYrMahnj8=
|
||||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||||
|
|||||||
6
main.go
6
main.go
@@ -13,10 +13,12 @@ func main() {
|
|||||||
// cli arguments
|
// cli arguments
|
||||||
a := args.Init()
|
a := args.Init()
|
||||||
|
|
||||||
|
// initiate new database manger
|
||||||
dbmHandler, err := dbm.NewDbmHandler(a)
|
dbmHandler, err := dbm.NewDbmHandler(a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
//save database after exeutabe ends
|
||||||
defer dbmHandler.SaveDb()
|
defer dbmHandler.SaveDb()
|
||||||
|
|
||||||
//initialize new server
|
//initialize new server
|
||||||
@@ -29,20 +31,20 @@ func main() {
|
|||||||
s.Routes.GET("/saveData", dbmHandler.SaveData)
|
s.Routes.GET("/saveData", dbmHandler.SaveData)
|
||||||
s.Routes.POST("/json_data", dbmHandler.Json_Data)
|
s.Routes.POST("/json_data", dbmHandler.Json_Data)
|
||||||
s.Routes.DELETE("/json_data", dbmHandler.Delete)
|
s.Routes.DELETE("/json_data", dbmHandler.Delete)
|
||||||
|
|
||||||
s.Routes.GET("/", func(c *gin.Context) {
|
s.Routes.GET("/", func(c *gin.Context) {
|
||||||
c.String(200, "DBM WebSocket Server is running!")
|
c.String(200, "DBM WebSocket Server is running!")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// start http server
|
||||||
go func() {
|
go func() {
|
||||||
dbmHandler.Log.Info("main", fmt.Sprintf("http listen on %d", a.Port.Http))
|
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 {
|
if err := s.ServeHttp(a.Port.Http); err != nil {
|
||||||
dbmHandler.Log.Error("main", "error http server "+err.Error())
|
dbmHandler.Log.Error("main", "error http server "+err.Error())
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
// start https server
|
||||||
dbmHandler.Log.Info("main", fmt.Sprintf("https listen on %d", a.Port.Https))
|
dbmHandler.Log.Info("main", fmt.Sprintf("https listen on %d", a.Port.Https))
|
||||||
if err := s.ServeHttps(a.Port.Https, a.Cert); err != nil {
|
if err := s.ServeHttps(a.Port.Https, a.Cert); err != nil {
|
||||||
dbmHandler.Log.Error("main", "error http server "+err.Error())
|
dbmHandler.Log.Error("main", "error http server "+err.Error())
|
||||||
|
|||||||
393
models/datapoint.go
Normal file
393
models/datapoint.go
Normal file
@@ -0,0 +1,393 @@
|
|||||||
|
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, 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,
|
||||||
|
})
|
||||||
|
|
||||||
|
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 = 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
|
||||||
|
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 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) {
|
||||||
|
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
|
package models
|
||||||
|
|
||||||
import (
|
import "sort"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/coder/websocket/wsjson"
|
type Datapoints []*Datapoint
|
||||||
"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 (
|
func (d *Datapoints) SortSlice() {
|
||||||
OnCreate = "onCreate"
|
// Sort by Path before processing
|
||||||
OnChange = "onChange"
|
sort.Slice(*d, func(i, j int) bool {
|
||||||
OnDelete = "onDelete"
|
return (*d)[i].Path < (*d)[j].Path
|
||||||
)
|
|
||||||
|
|
||||||
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,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,24 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/coder/websocket"
|
"github.com/coder/websocket"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// serves as connection handler of websocket
|
||||||
type Connections struct {
|
type Connections struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Clients Clients
|
Clients Clients
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initaiates new conections with client map
|
||||||
func NewConnections() *Connections {
|
func NewConnections() *Connections {
|
||||||
return &Connections{
|
return &Connections{
|
||||||
Clients: NewClients(),
|
Clients: NewClients(),
|
||||||
@@ -30,3 +37,31 @@ func (c *Connections) RemoveClient(id string) {
|
|||||||
func (c *Connections) DisconnectWsConnection(id string, code websocket.StatusCode, reason string) {
|
func (c *Connections) DisconnectWsConnection(id string, code websocket.StatusCode, reason string) {
|
||||||
c.Clients.DisconnectWsConnection(id, code, reason)
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,23 +9,28 @@ import (
|
|||||||
"github.com/tecamino/tecamino-logger/logging"
|
"github.com/tecamino/tecamino-logger/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// server model for database manager websocket
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Routes *gin.Engine
|
Routes *gin.Engine
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Logger *logging.Logger
|
Logger *logging.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// initalizes new dbm server
|
||||||
func NewServer() *Server {
|
func NewServer() *Server {
|
||||||
return &Server{
|
return &Server{
|
||||||
Routes: gin.Default(),
|
Routes: gin.Default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// serve dbm as http
|
||||||
func (s *Server) ServeHttp(port uint) error {
|
func (s *Server) ServeHttp(port uint) error {
|
||||||
return s.Routes.Run(fmt.Sprintf(":%d", port))
|
return s.Routes.Run(fmt.Sprintf(":%d", port))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// serve dbm as http
|
||||||
func (s *Server) ServeHttps(port uint, cert cert.Cert) error {
|
func (s *Server) ServeHttps(port uint, cert cert.Cert) error {
|
||||||
|
// generate self signed tls certificate
|
||||||
if err := cert.GenerateSelfSignedCert(); err != nil {
|
if err := cert.GenerateSelfSignedCert(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// return any input type to float32
|
||||||
func Float32From(v any) float32 {
|
func Float32From(v any) float32 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -44,6 +45,7 @@ func Float32From(v any) float32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to float64
|
||||||
func Float64From(v any) float64 {
|
func Float64From(v any) float64 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -83,6 +85,7 @@ func Float64From(v any) float64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to int8
|
||||||
func Int8From(v any) int8 {
|
func Int8From(v any) int8 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -122,6 +125,7 @@ func Int8From(v any) int8 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to int16
|
||||||
func Int16From(v any) int16 {
|
func Int16From(v any) int16 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -161,6 +165,7 @@ func Int16From(v any) int16 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to int32
|
||||||
func Int32From(v any) int32 {
|
func Int32From(v any) int32 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -200,6 +205,7 @@ func Int32From(v any) int32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to int64
|
||||||
func Int64From(v any) int64 {
|
func Int64From(v any) int64 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -239,6 +245,7 @@ func Int64From(v any) int64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to int
|
||||||
func Uint8From(v any) uint8 {
|
func Uint8From(v any) uint8 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -278,6 +285,7 @@ func Uint8From(v any) uint8 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to uint16
|
||||||
func Uint16From(v any) uint16 {
|
func Uint16From(v any) uint16 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -317,6 +325,7 @@ func Uint16From(v any) uint16 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to uint32
|
||||||
func Uint32From(v any) uint32 {
|
func Uint32From(v any) uint32 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -356,6 +365,7 @@ func Uint32From(v any) uint32 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to uint64
|
||||||
func Uint64From(v any) uint64 {
|
func Uint64From(v any) uint64 {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
@@ -395,6 +405,7 @@ func Uint64From(v any) uint64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// return any input type to bool
|
||||||
func BoolFrom(v any) bool {
|
func BoolFrom(v any) bool {
|
||||||
switch val := v.(type) {
|
switch val := v.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user