Compare commits
47 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bc2e582203 | ||
|
|
21e4231e24 | ||
|
|
b21101958d | ||
|
|
c3a3060129 | ||
|
|
a23f82e9fe | ||
|
|
c37dd87a37 | ||
|
|
8be5c80a22 | ||
|
|
47a065aaf9 | ||
|
|
5e1e4b9daf | ||
|
|
0f06128ce8 | ||
|
|
be07dc8749 | ||
|
|
e75d7c8b03 | ||
|
|
59c7705ca1 | ||
|
|
91ea59ed6e | ||
|
|
659cbe4072 | ||
|
|
9605b50198 | ||
|
|
c57c22f93a | ||
|
|
60b3f77e29 | ||
|
|
1c4b8a5995 | ||
|
|
836a69f914 | ||
|
|
5ee97416dd | ||
|
|
ecb1f3b2cf | ||
|
|
5203fb8543 | ||
|
|
7d6b09cf11 | ||
|
|
0fe813acd9 | ||
|
|
75eb31abf6 | ||
|
|
4b36598bd7 | ||
|
|
8e6b39c8bf | ||
|
|
2f88525257 | ||
|
|
3551cb035b | ||
|
|
e5c08f69a9 | ||
|
|
f5890c1563 | ||
|
|
c310414b7e | ||
|
|
9e2cd260d5 | ||
|
|
f37a69e75b | ||
|
|
c623d89f94 | ||
|
|
45b3b258c1 | ||
|
|
a0fe455bce | ||
|
|
7b9641f753 | ||
|
|
3d3dab91b9 | ||
|
|
408700367b | ||
|
|
c1d822b296 | ||
|
|
fd835b67dc | ||
|
|
49d8d03d8a | ||
|
|
0a137c9d86 | ||
|
|
a1f947e24a | ||
|
|
3d1dee25f1 |
48
.github/workflows/build.yml
vendored
Normal file
48
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,48 @@
|
||||
name: Build Go Binaries
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
goos: [linux, windows]
|
||||
goarch: [amd64, arm, arm64]
|
||||
exclude:
|
||||
- goos: windows
|
||||
goarch: arm
|
||||
- goos: windows
|
||||
goarch: arm64
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.24.0'
|
||||
|
||||
- name: Set up Git credentials for private modules
|
||||
run: |
|
||||
git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf "https://github.com/"
|
||||
echo "GOPRIVATE=github.com/tecamino/*" >> $GITHUB_ENV
|
||||
|
||||
- name: Build binary
|
||||
run: |
|
||||
mkdir -p build
|
||||
if [ "${{ matrix.goos }}" == "windows" ]; then
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags="-s -w" -trimpath -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }}.exe main.go
|
||||
else
|
||||
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags="-s -w" -trimpath -o build/tecamino-dbm-${{ matrix.goos }}-${{ matrix.goarch }} main.go
|
||||
fi
|
||||
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: binaries-${{ matrix.goos }}-${{ matrix.goarch }}
|
||||
path: build/
|
||||
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
.DS_Store
|
||||
*.dbm
|
||||
*.log
|
||||
|
||||
# local executables
|
||||
dbm-arm64
|
||||
63
args/args.go
Normal file
63
args/args.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package args
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"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
|
||||
}
|
||||
|
||||
// initialte cli arguments
|
||||
func Init() *Args {
|
||||
|
||||
var allowOrigins models.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")
|
||||
remotePort := flag.Uint("remotePort", 9500, "remote Port of gui user interface")
|
||||
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: *organization,
|
||||
CertFile: *certFile,
|
||||
KeyFile: *keyFile,
|
||||
},
|
||||
Port: models.Port{
|
||||
Http: *portHttp,
|
||||
Https: *portHttps,
|
||||
Remote: *remotePort,
|
||||
},
|
||||
RootDir: *rootDir,
|
||||
DBMFile: *dbmFile,
|
||||
Debug: *debug,
|
||||
}
|
||||
|
||||
if len(allowOrigins) == 0 {
|
||||
allowOrigins = models.StringSlice{"http://localhost:9500"}
|
||||
}
|
||||
|
||||
a.AllowOrigins = allowOrigins
|
||||
return &a
|
||||
}
|
||||
23
auth/auth.go
Normal file
23
auth/auth.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func GetIDFromAuth(c *gin.Context) (string, error) {
|
||||
auth := c.GetHeader("Authorization")
|
||||
if len(auth) > 7 && auth[:7] == "Bearer " {
|
||||
return auth[7:], nil
|
||||
}
|
||||
return "", errors.New("authorization token missing")
|
||||
}
|
||||
|
||||
func GetIDFromQuery(c *gin.Context) (string, error) {
|
||||
auth, exists := c.GetQuery("id")
|
||||
if !exists {
|
||||
return "", errors.New("id missing")
|
||||
}
|
||||
return auth, nil
|
||||
}
|
||||
99
cert/cert.go
Normal file
99
cert/cert.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package cert
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
"encoding/pem"
|
||||
"math/big"
|
||||
"os"
|
||||
"path"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Cert struct {
|
||||
Organization string
|
||||
CertFile string
|
||||
KeyFile string
|
||||
}
|
||||
|
||||
// Initialize a new ssl certificate handler with organization name
|
||||
func NewCertHandler(org string) *Cert {
|
||||
return &Cert{
|
||||
Organization: org,
|
||||
}
|
||||
}
|
||||
|
||||
// generates a new self signed ssl certificate foe localhost and development use
|
||||
func (c *Cert) GenerateSelfSignedCert() error {
|
||||
|
||||
// do not generate certs if they exist
|
||||
_, err := os.Stat(c.CertFile)
|
||||
_, err2 := os.Stat(c.KeyFile)
|
||||
if !os.IsNotExist(err) && !os.IsNotExist(err2) {
|
||||
return nil
|
||||
}
|
||||
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
serialNumber, _ := rand.Int(rand.Reader, big.NewInt(1<<62))
|
||||
|
||||
template := x509.Certificate{
|
||||
SerialNumber: serialNumber,
|
||||
Subject: pkix.Name{
|
||||
CommonName: "localhost",
|
||||
Organization: []string{c.Organization},
|
||||
},
|
||||
NotBefore: time.Now(),
|
||||
NotAfter: time.Now().Add(365 * 24 * time.Hour),
|
||||
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
BasicConstraintsValid: true,
|
||||
DNSNames: []string{"localhost"},
|
||||
}
|
||||
|
||||
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := os.Stat(path.Dir(c.CertFile)); os.IsNotExist(err) {
|
||||
os.MkdirAll(path.Dir(c.CertFile), 0700)
|
||||
}
|
||||
|
||||
certOut, err := os.Create(c.CertFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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), 0700)
|
||||
}
|
||||
|
||||
keyOut, err := os.Create(c.KeyFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer keyOut.Close()
|
||||
|
||||
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
|
||||
}
|
||||
19
cert/cert.pem
Normal file
19
cert/cert.pem
Normal file
@@ -0,0 +1,19 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDGzCCAgOgAwIBAgIIB2N9YzgUYFAwDQYJKoZIhvcNAQELBQAwJzERMA8GA1UE
|
||||
ChMIdGVjYW1pbm8xEjAQBgNVBAMTCWxvY2FsaG9zdDAeFw0yNTA0MTYxNjE2NDJa
|
||||
Fw0yNjA0MTYxNjE2NDJaMCcxETAPBgNVBAoTCHRlY2FtaW5vMRIwEAYDVQQDEwls
|
||||
b2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDDFOsJDNRd
|
||||
wX25lB1QGCFhvjK+yN/pZHNtjQvHOcajON+Fhm56RWKbuKR4BQdNWF/uWe/wH6kC
|
||||
xrYXzuAqJIzB/trFZ14whNblxjjxSmGhkMNFFTIIIdTICcoQu3+zzXxUc4s9ni4R
|
||||
uGXudFB7uSZBx5x2TWrdFzBIfAuWfQfCwMWqiDoTH09T7DxJJyuvKf4yNPyDq+oe
|
||||
4WGEXCpk3VBjggqYDGknMUzreEEa8JaIuDMFhQz4J4A5QGZOHOEyaP839cDblY31
|
||||
ot5Pd6PUAs5yvmvIZUCscW7bJH2vUqDC2tJ4WjkVkykULLIDIbe4Thi4//9oKzKV
|
||||
fhP2TM9t+OeVAgMBAAGjSzBJMA4GA1UdDwEB/wQEAwIFoDATBgNVHSUEDDAKBggr
|
||||
BgEFBQcDATAMBgNVHRMBAf8EAjAAMBQGA1UdEQQNMAuCCWxvY2FsaG9zdDANBgkq
|
||||
hkiG9w0BAQsFAAOCAQEANfjOOkU/fl7Y2pJ6V+qKv9vdBb4nEpiNOnSl8sSgZP4r
|
||||
wa1ArfALCPY1Gu+XDrwqcVLii511xT4cFuegxaOdu2+5j4+WjIR9ke/AeEuyNU1X
|
||||
mm/xgBOSibxqSWVHTGhLLY4jwyU3GYx+4ODNmLoQ2eNQ7NDsCDAQq+OAbR+f5486
|
||||
j8AcrjEjWI5Nh9p4DiqEA1DwNCKnpYcw8QBiawNFli3mvFSu1KSTG5UGM8vwzCOu
|
||||
nk2GtBvhDODVhDuM3BjqAmT7xbIJGXdW25+FG9++Vc+36LVSJVxMeOWx4u07Ggqk
|
||||
4y39spP+xOzXegFCJXu+OkxjvZ7mRGaPp1zKeaoVVQ==
|
||||
-----END CERTIFICATE-----
|
||||
27
cert/key.pem
Normal file
27
cert/key.pem
Normal file
@@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEAwxTrCQzUXcF9uZQdUBghYb4yvsjf6WRzbY0LxznGozjfhYZu
|
||||
ekVim7ikeAUHTVhf7lnv8B+pAsa2F87gKiSMwf7axWdeMITW5cY48UphoZDDRRUy
|
||||
CCHUyAnKELt/s818VHOLPZ4uEbhl7nRQe7kmQcecdk1q3RcwSHwLln0HwsDFqog6
|
||||
Ex9PU+w8SScrryn+MjT8g6vqHuFhhFwqZN1QY4IKmAxpJzFM63hBGvCWiLgzBYUM
|
||||
+CeAOUBmThzhMmj/N/XA25WN9aLeT3ej1ALOcr5ryGVArHFu2yR9r1KgwtrSeFo5
|
||||
FZMpFCyyAyG3uE4YuP//aCsylX4T9kzPbfjnlQIDAQABAoIBAAfekaahTyj8puMA
|
||||
39489ifFVOW2U7X/nw2Y+Xpb0P2azAV//HfpOAbN5kopjCrC35SaKZNdq5ZllRXi
|
||||
LgL1MeQBF5ElnNQtn6YDa4U5E2j0xOb5NmkpxQKDk2E+aj92EOEu2G78l24pxDVG
|
||||
Zhkl/GGXheIna4hBdbZCCFoI1b9o4w1iFLsc1MJxDszRqxD5M9uZ4YMtI6rGrUtx
|
||||
xAeYqSvEnMfYHRXbQlFhZF2F9suW+rsUpL264bQ7OPMNsQAtsu5Q9x1dEMuuj+NX
|
||||
4Tavu78CyVGjBrBrCDJ0P/r/EyK72n8noZBto+Xzk4S3PjxWfb0HVsHYVaeq2O+I
|
||||
3et0C+cCgYEA8fd9NB31KU2sjJtNWBq7Vs/6F6IPlZat6FCHFDjt0wCm9LK0gMii
|
||||
l79mJyKwI3i2K3HKVDrpuplYBJ9Z7XQCQin6844Bcp0OizIHX4OYf1CSWsQbJZFm
|
||||
SdrUwKyijYR9nHKmjbFa+PWFh/HD630kkkLEFt+Ti+CsdNe0+Hmfc7cCgYEAzmVS
|
||||
JX9DZVZMvEUPanPrHI1vAkFTd9YSGPx088yadomF8cL6QbYXUFq+ZMq3st70S+qr
|
||||
XxBXjjmDTuE8tLepZemIIM9UC6AQ+2IE/RiThLyKrhk9b/7myqWSz9cY8/SqMOsW
|
||||
015U4Jc+KRS1/Tse8Y7tyzHFlthACoKX5HrxNxMCgYBuOQ1B1nu9ivKVQpGjFtpM
|
||||
G4WTinGK9Q7XiwddgOlleyCSy21KVRssATZpkXWnUu+5Lqa6Y/Pg2sWrpWNztarp
|
||||
tPHqTMAAE+dyJSISsoGfTXa9/iNXo7py3kqYUovh537I67lPRoFoc3+Wg915wpIM
|
||||
RnnI6aPuzjQBLdn0boLiVQKBgQC/cv6y54ylmFqPnOPC1Am3r33UMrJxC3I4GR2G
|
||||
9DgnUkOb0Ud/4p9XmwTWy6+ATQ2AygnyoV8F/1VMuuMrot2QOgJapNaJ/g0ikXad
|
||||
KsnTq2xcN+9kTqbYPKOlBRoRWNbxj2/Z2ruSpNg1FRAG+GsomHL9M4rb9HXbCe5J
|
||||
Mr1DXwKBgQDmsYhXErxHiQ0jwDgqFmcKRjm/UvEGzuJ1wMZzCBU1GNAOid25QHvI
|
||||
VIsJjYT6idRNHNxFr7AoW5bpTRMsfh8nLD4VfK9k2HbiRbZmDhC03zxXpgbzRWrp
|
||||
bWTuoTIidPr5pt6XFlPU9NYPfgpGJvcCSbE15UQBd4DLts+UcZZ3Zw==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
27
dbm/db.go
Normal file
27
dbm/db.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) SaveData(c *gin.Context) {
|
||||
s := time.Now()
|
||||
if err := d.SaveDb(); err != nil {
|
||||
r := json_dataModels.NewResponse()
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
r := json_dataModels.NewResponse()
|
||||
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)
|
||||
}
|
||||
120
dbm/dbmHandler.go
Normal file
120
dbm/dbmHandler.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/tecamino/tecamino-dbm/args"
|
||||
"github.com/tecamino/tecamino-dbm/models"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
type DBMHandler struct {
|
||||
DBM *models.DBM
|
||||
Conns *ws.ClientHandler
|
||||
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,
|
||||
MaxAge: 28,
|
||||
Debug: a.Debug,
|
||||
TerminalOut: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
logger.Info("main", "start dma handler")
|
||||
|
||||
//initialize connection map
|
||||
conns := ws.NewConnectionHandler()
|
||||
|
||||
// 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: conns,
|
||||
}
|
||||
|
||||
// initialize system datapoint and periodically update it
|
||||
if err := dmaHandler.AddSystemDps(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// check if dtabase file exists to load data
|
||||
s := time.Now()
|
||||
if _, err := os.Stat(dmaHandler.filePath); err == nil {
|
||||
|
||||
f, err := os.Open(dmaHandler.filePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// 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.DBM.ImportDatapoints(dp)
|
||||
}
|
||||
}
|
||||
dmaHandler.Log.Info("dmbHandler.NewDmbHandler", fmt.Sprintf("%d datapoint imported in %v", dmaHandler.DBM.GetNumbersOfDatapoints(), time.Since(s)))
|
||||
return &dmaHandler, nil
|
||||
}
|
||||
|
||||
func (d *DBMHandler) SaveDb() (err error) {
|
||||
f, err := os.OpenFile(d.filePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
_, err = f.Write(b)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = f.Write([]byte("\n"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
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())
|
||||
}
|
||||
}
|
||||
84
dbm/json_data.go
Normal file
84
dbm/json_data.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
json_data "github.com/tecamino/tecamino-json_data"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
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.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
|
||||
resp := json_dataModels.NewResponse()
|
||||
|
||||
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.DBM.QueryDatapoints(depth, get.Uuid, get.Path) {
|
||||
resp.AddGet(json_dataModels.Get{
|
||||
Uuid: res.Uuid,
|
||||
Path: res.Path,
|
||||
Type: res.Type,
|
||||
Value: res.Value,
|
||||
Drivers: &res.Drivers,
|
||||
HasChild: res.Datapoints != nil,
|
||||
Rights: res.ReadWrite,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(payload.Set) > 0 {
|
||||
resp.Set, err = d.DBM.CreateDatapoints(payload.Set...)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, resp)
|
||||
}
|
||||
|
||||
func (d *DBMHandler) Delete(c *gin.Context) {
|
||||
var err error
|
||||
payload, err := json_data.ParseRequest(c.Request.Body)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
|
||||
response := json_data.NewResponse()
|
||||
if payload.Set != nil {
|
||||
response.Set, err = d.DBM.RemoveDatapoint(payload.Set...)
|
||||
if err != nil {
|
||||
r := json_data.NewResponse()
|
||||
r.SetError()
|
||||
r.SetMessage(err.Error())
|
||||
c.JSON(http.StatusBadRequest, r)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(200, response)
|
||||
}
|
||||
46
dbm/set.go
Normal file
46
dbm/set.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
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(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())
|
||||
}
|
||||
}
|
||||
98
dbm/subscribe.go
Normal file
98
dbm/subscribe.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
for _, dp := range dps {
|
||||
if sub.Driver != "" {
|
||||
if dp.Drivers == nil || dp.Drivers[sub.Driver] == nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
client := d.DBM.Conns.GetClient(id)
|
||||
if client == nil {
|
||||
d.Log.Warning("subscribe", "id "+id+" not found")
|
||||
continue
|
||||
}
|
||||
|
||||
dp.AddSubscribtion(client, sub)
|
||||
resp.AddSubscription(json_dataModels.Subscription{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Value: dp.Value,
|
||||
HasChild: dp.Datapoints != nil,
|
||||
Rights: dp.ReadWrite.GetRights(),
|
||||
Driver: sub.Driver,
|
||||
Drivers: &dp.Drivers,
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := d.Conns.SendResponse(id, resp); err != nil {
|
||||
d.Log.Error("subscribe.Subscribe", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
resp := json_dataModels.NewResponse()
|
||||
resp.Id = req.Id
|
||||
|
||||
for _, sub := range req.Unsubscribe {
|
||||
for _, dp := range d.DBM.QueryDatapoints(sub.Depth, sub.Uuid, sub.Path) {
|
||||
|
||||
client := d.DBM.Conns.GetClient(id)
|
||||
if client == nil {
|
||||
d.Log.Warning("subscribe", "id "+id+" not found")
|
||||
continue
|
||||
}
|
||||
|
||||
if _, ok := dp.Subscriptions[client]; !ok {
|
||||
continue
|
||||
}
|
||||
dp.RemoveSubscribtion(client)
|
||||
resp.AddUnsubscription(json_dataModels.Subscription{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := d.Conns.SendResponse(id, resp); err != nil {
|
||||
d.Log.Error("subscribe.Unsubscribe", err.Error())
|
||||
}
|
||||
}
|
||||
29
dbm/system.go
Normal file
29
dbm/system.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"github.com/tecamino/tecamino-dbm/models"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) AddSystemDps() (err error) {
|
||||
path := "System:Datapoints"
|
||||
|
||||
typ := json_dataModels.LOU
|
||||
rights := json_dataModels.Read
|
||||
_, 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
|
||||
}
|
||||
models.SystemDatapoints = d.DBM.QueryDatapoints(1, uuid.Nil, path)[0].Uuid
|
||||
|
||||
if err = d.DBM.GoSystemTime(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = d.DBM.GoSystemMemory(); err != nil {
|
||||
return err
|
||||
}
|
||||
return
|
||||
}
|
||||
61
dbm/webSocket.go
Normal file
61
dbm/webSocket.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package dbm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tecamino/tecamino-dbm/auth"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
const (
|
||||
OnCreate = "onCreate"
|
||||
OnChange = "onChange"
|
||||
OnDelete = "onDelete"
|
||||
)
|
||||
|
||||
func (d *DBMHandler) WebSocket(c *gin.Context) {
|
||||
id, err := auth.GetIDFromQuery(c)
|
||||
if err != nil {
|
||||
d.Log.Error("dbmHandler.webSocket.Websocket", "error GetIDFromQuery: "+err.Error())
|
||||
return
|
||||
}
|
||||
d.Log.Debug("dbmHandler.webSocket.Websocket", "authorization id token: "+id)
|
||||
|
||||
client, err := d.Conns.ConnectNewClient(id, c)
|
||||
if err != nil {
|
||||
d.Log.Error("dbmHandler.webSocket.Websocket", err)
|
||||
return
|
||||
}
|
||||
|
||||
client.OnMessage = func(data []byte) {
|
||||
request, err := d.readJsonData(data)
|
||||
if err != nil {
|
||||
d.Log.Error("dbmHandler.webSocket.Websocket", "read json: "+err.Error())
|
||||
}
|
||||
|
||||
// Sets
|
||||
d.Get(request, id)
|
||||
// Sets
|
||||
d.Set(request, id)
|
||||
|
||||
// Subscribe
|
||||
d.Subscribe(request, id)
|
||||
|
||||
// Unsubscribe
|
||||
d.Unsubscribe(request, id)
|
||||
}
|
||||
|
||||
client.OnWarning = func(s string) {
|
||||
d.Log.Warning("dbmHandler.webSocket.Websocket", "warning on websocket connection: "+s)
|
||||
}
|
||||
|
||||
client.OnError = func(err error) {
|
||||
d.Log.Error("dbmHandler.webSocket.Websocket", "error on websocket connection: "+err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DBMHandler) readJsonData(data []byte) (request *json_dataModels.Request, err error) {
|
||||
err = json.Unmarshal(data, &request)
|
||||
return
|
||||
}
|
||||
16
drivers/artNet.go
Normal file
16
drivers/artNet.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package drivers
|
||||
|
||||
type ArtNetDriver struct {
|
||||
Bus string
|
||||
Addresses []uint
|
||||
}
|
||||
|
||||
func NewArtNetDriver(bus string) *ArtNetDriver {
|
||||
return &ArtNetDriver{
|
||||
Bus: bus,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ArtNetDriver) AddAddress(adr uint) {
|
||||
a.Addresses = append(a.Addresses, adr)
|
||||
}
|
||||
10
drivers/drivers.go
Normal file
10
drivers/drivers.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package drivers
|
||||
|
||||
type Drivers []Driver
|
||||
|
||||
type Driver interface {
|
||||
NewArtNetDriver(string)
|
||||
AddAddress()
|
||||
}
|
||||
|
||||
//func Add
|
||||
43
go.mod
Normal file
43
go.mod
Normal file
@@ -0,0 +1,43 @@
|
||||
module github.com/tecamino/tecamino-dbm
|
||||
|
||||
go 1.24.0
|
||||
|
||||
require (
|
||||
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/gorilla/websocket v1.5.3
|
||||
github.com/tecamino/tecamino-json_data v0.0.31
|
||||
github.com/tecamino/tecamino-logger v0.2.0
|
||||
)
|
||||
|
||||
require (
|
||||
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.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.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.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.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
|
||||
)
|
||||
111
go.sum
Normal file
111
go.sum
Normal file
@@ -0,0 +1,111 @@
|
||||
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/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/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.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=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
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.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=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
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.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=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
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.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=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
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.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.31 h1:7zFbANj1Lihr64CCCh3O+9hxxsMcEPniRJZ5NgPrS5Y=
|
||||
github.com/tecamino/tecamino-json_data v0.0.31/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=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
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.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.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.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=
|
||||
54
main.go
Normal file
54
main.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tecamino/tecamino-dbm/args"
|
||||
"github.com/tecamino/tecamino-dbm/dbm"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// cli arguments
|
||||
a := args.Init()
|
||||
|
||||
// initiate new database manger
|
||||
dbmHandler, err := dbm.NewDbmHandler(a)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
//save database after executabe ends
|
||||
defer dbmHandler.SaveDb()
|
||||
|
||||
//initialize new server
|
||||
dbmHandler.Log.Debug("main", "initialize new server instance")
|
||||
s := ws.NewServer(a.AllowOrigins, a.Port.Remote)
|
||||
|
||||
//set routes
|
||||
dbmHandler.Log.Debug("main", "setting routes")
|
||||
s.Routes.GET("/ws", dbmHandler.WebSocket)
|
||||
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!")
|
||||
})
|
||||
|
||||
// start http server
|
||||
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)
|
||||
}
|
||||
}()
|
||||
|
||||
// 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)
|
||||
}
|
||||
|
||||
}
|
||||
562
models/datapoint.go
Normal file
562
models/datapoint.go
Normal file
@@ -0,0 +1,562 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
wsModels "github.com/tecamino/tecamino-dbm/websocket/models"
|
||||
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 {
|
||||
Uuid uuid.UUID `json:"uuid"`
|
||||
Value any `json:"value,omitempty"`
|
||||
Path string `json:"path"`
|
||||
CreateDateTime int64 `json:"createDateTime,omitempty"`
|
||||
UpdateDateTime int64 `json:"updateDateTime,omitempty"`
|
||||
Datapoints map[string]*Datapoint `json:"-"`
|
||||
Drivers json_dataModels.Drivers `json:"drivers,omitempty"`
|
||||
Subscriptions Subscriptions `json:"-"`
|
||||
Type json_dataModels.Type `json:"type"`
|
||||
ReadWrite json_dataModels.Rights `json:"readWrite"`
|
||||
HasChild bool `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 d.Drivers == nil {
|
||||
d.Drivers = json_dataModels.Drivers{}
|
||||
}
|
||||
d.Drivers.AddDriver(set.Driver)
|
||||
|
||||
changed = true
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
|
||||
return changed, nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetValueUint64() uint64 {
|
||||
return utils.Uint64From(d.Value)
|
||||
}
|
||||
|
||||
func (d *Datapoint) CreateDatapoints(uuids *Uuids, sets ...json_dataModels.Set) (created []json_dataModels.Set, err error) {
|
||||
if len(sets) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
publishes := []json_dataModels.Publish{}
|
||||
|
||||
for _, dp := range sets {
|
||||
|
||||
if dp.Path == "" && dp.Uuid != uuid.Nil {
|
||||
existing := uuids.GetDatapoint(dp.Uuid)
|
||||
_, 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,
|
||||
HasChild: existing.HasChild,
|
||||
})
|
||||
|
||||
existing.Publish(OnChange)
|
||||
continue
|
||||
}
|
||||
|
||||
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 {
|
||||
|
||||
_, 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,
|
||||
HasChild: existing.HasChild,
|
||||
})
|
||||
|
||||
existing.Publish(OnChange)
|
||||
} else {
|
||||
var uid uuid.UUID = uuid.New()
|
||||
if dp.Uuid != uuid.Nil {
|
||||
uid = dp.Uuid
|
||||
}
|
||||
ndp := &Datapoint{
|
||||
Uuid: uid,
|
||||
CreateDateTime: time.Now().UnixMilli(),
|
||||
Subscriptions: InitSubscribtion(),
|
||||
}
|
||||
|
||||
// Create new
|
||||
current.Datapoints[part] = ndp
|
||||
|
||||
_, err := ndp.Set(strings.Join(parts, ":"), dp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
//add uuid to flat map for faster lookup
|
||||
renamedDps := uuids.AddDatapoint(current, ndp)
|
||||
created = append(created, renamedDps...)
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
Rights: ndp.ReadWrite,
|
||||
Driver: dp.Driver,
|
||||
HasChild: ndp.HasChild,
|
||||
})
|
||||
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnCreate,
|
||||
Uuid: ndp.Uuid,
|
||||
Path: ndp.Path,
|
||||
Type: ndp.Type,
|
||||
Value: ndp.Value,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 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(),
|
||||
}
|
||||
|
||||
//add uuid to flat map for faster lookup
|
||||
renamedDps := uuids.AddDatapoint(current, newDp)
|
||||
created = append(created, renamedDps...)
|
||||
created = append(created, json_dataModels.Set{
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
Rights: newDp.ReadWrite,
|
||||
HasChild: newDp.HasChild,
|
||||
})
|
||||
|
||||
if dp.Rights != "" {
|
||||
newDp.ReadWrite = dp.Rights.GetRights()
|
||||
}
|
||||
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnCreate,
|
||||
Uuid: newDp.Uuid,
|
||||
Path: newDp.Path,
|
||||
Type: newDp.Type,
|
||||
Value: newDp.Value,
|
||||
})
|
||||
|
||||
current.Datapoints[part] = newDp
|
||||
current = newDp
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r := json_data.NewResponse()
|
||||
r.Publish = append(r.Publish, publishes...)
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return created, err
|
||||
}
|
||||
ws.SendBroadcast(b)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) ImportDatapoint(uuids *Uuids, dp *Datapoint, path string) (err 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(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 lookup
|
||||
uuids.AddDatapoint(current, dp)
|
||||
|
||||
dp.Publish(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
|
||||
//add uuid to flat map for faster lookup
|
||||
uuids.AddDatapoint(current, newDp)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateDatapointValue(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(OnChange)
|
||||
return nil
|
||||
}
|
||||
current = dp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Datapoint) UpdateValue(value any) error {
|
||||
d.Value = d.Type.ConvertValue(value)
|
||||
d.UpdateDateTime = time.Now().UnixMilli()
|
||||
d.Publish(OnChange)
|
||||
return nil
|
||||
}
|
||||
|
||||
// removes datapoint and its children if path is given
|
||||
// if path and
|
||||
func (d *Datapoint) RemoveDatapoint(set json_dataModels.Set) (sets []json_dataModels.Set, err error) {
|
||||
parts := strings.Split(set.Path, ":")
|
||||
|
||||
if len(parts) < 1 {
|
||||
return sets, fmt.Errorf("invalid path: '%s'", set.Path)
|
||||
}
|
||||
|
||||
publishes := []json_dataModels.Publish{}
|
||||
|
||||
current := d
|
||||
for i := 0; i < len(parts)-1; i++ {
|
||||
next, ok := current.Datapoints[parts[i]]
|
||||
if !ok {
|
||||
return sets, 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 {
|
||||
//if driver information found, remoove only driver information
|
||||
if set.Driver != nil {
|
||||
dp.RemoveDriver(set.Driver)
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnChange,
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Value: dp.Value,
|
||||
Drivers: &dp.Drivers,
|
||||
})
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Drivers: &dp.Drivers,
|
||||
Value: dp.Value,
|
||||
})
|
||||
} else {
|
||||
s, pubs := removeChildren(dp)
|
||||
sets = append(sets, s...)
|
||||
publishes = append(publishes, pubs...)
|
||||
publishes = append(publishes, json_dataModels.Publish{
|
||||
Event: OnDelete,
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
})
|
||||
delete(current.Datapoints, toDelete)
|
||||
}
|
||||
r := json_data.NewResponse()
|
||||
r.Publish = append(r.Publish, publishes...)
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return sets, err
|
||||
}
|
||||
ws.SendBroadcast(b)
|
||||
|
||||
return sets, nil
|
||||
}
|
||||
return sets, fmt.Errorf("datapoint '%s' not found", set.Path)
|
||||
}
|
||||
|
||||
// removes all children and grandchlidren of datapoint
|
||||
func removeChildren(dp *Datapoint) (sets []json_dataModels.Set, pubs []json_dataModels.Publish) {
|
||||
for name, d := range dp.Datapoints {
|
||||
s, p := removeChildren(d)
|
||||
sets = append(sets, s...)
|
||||
pubs = append(pubs, p...)
|
||||
|
||||
sets = append(sets, json_dataModels.Set{
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
})
|
||||
delete(d.Datapoints, name)
|
||||
}
|
||||
return sets, pubs
|
||||
}
|
||||
|
||||
func (d *Datapoint) GetAllDatapoints(depth uint) (dps Datapoints) {
|
||||
|
||||
dps = append(dps, d)
|
||||
if depth == 1 {
|
||||
return
|
||||
} else if depth > 0 {
|
||||
depth--
|
||||
}
|
||||
var dfs func(dp *Datapoint, currentDepth uint)
|
||||
dfs = func(dp *Datapoint, currentDepth uint) {
|
||||
switch depth {
|
||||
case 0:
|
||||
// Return all descendants
|
||||
case currentDepth:
|
||||
return
|
||||
}
|
||||
|
||||
for _, child := range dp.Datapoints {
|
||||
dps = append(dps, child)
|
||||
dfs(child, currentDepth+1)
|
||||
}
|
||||
}
|
||||
|
||||
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(conn *wsModels.Client, sub json_dataModels.Subscription) {
|
||||
if d.Subscriptions == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if s, ok := d.Subscriptions[conn]; ok {
|
||||
s.OnCreate = sub.OnCreate
|
||||
s.OnChange = sub.OnChange
|
||||
s.OnDelete = sub.OnDelete
|
||||
} else {
|
||||
d.Subscriptions[conn] = &Subscription{
|
||||
OnCreate: sub.OnCreate,
|
||||
OnChange: sub.OnChange,
|
||||
OnDelete: sub.OnDelete,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Datapoint) RenamePaths(oldPath string) (renamed []json_dataModels.Set) {
|
||||
visited := make(map[*Datapoint]bool)
|
||||
|
||||
if len(d.Datapoints) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dp := range d.Datapoints {
|
||||
dp.Path = strings.Replace(dp.Path, oldPath, d.Path, 1)
|
||||
renamedDps := dp.renameSubPaths(oldPath, d.Path, visited)
|
||||
renamed = append(renamed, renamedDps...)
|
||||
renamed = append(renamed, json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Type: dp.Type,
|
||||
Value: dp.Value,
|
||||
Rights: dp.ReadWrite,
|
||||
Drivers: &dp.Drivers,
|
||||
HasChild: dp.HasChild,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) renameSubPaths(oldPath, newPath string, visited map[*Datapoint]bool) (renamed []json_dataModels.Set) {
|
||||
if visited[d] {
|
||||
return
|
||||
}
|
||||
visited[d] = true
|
||||
|
||||
if len(d.Datapoints) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
for _, dp := range d.Datapoints {
|
||||
dp.Path = strings.Replace(dp.Path, oldPath, newPath, 1)
|
||||
renamedDps := dp.renameSubPaths(oldPath, newPath, visited)
|
||||
renamed = append(renamed, renamedDps...)
|
||||
renamed = append(renamed, json_dataModels.Set{
|
||||
Uuid: dp.Uuid,
|
||||
Path: dp.Path,
|
||||
Type: dp.Type,
|
||||
Value: dp.Value,
|
||||
Rights: dp.ReadWrite,
|
||||
Drivers: &dp.Drivers,
|
||||
HasChild: dp.HasChild,
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveSubscribtion(client *wsModels.Client) {
|
||||
delete(d.Subscriptions, client)
|
||||
}
|
||||
|
||||
func (d *Datapoint) RemoveDriver(driver *json_dataModels.Driver) {
|
||||
d.Drivers.RemoveDriver(driver)
|
||||
}
|
||||
|
||||
func (d *Datapoint) Publish(eventType string) error {
|
||||
r := json_data.NewResponse()
|
||||
r.AddPublish(json_dataModels.Publish{
|
||||
Event: eventType,
|
||||
Uuid: d.Uuid,
|
||||
Path: d.Path,
|
||||
Type: d.Type,
|
||||
Value: d.Value,
|
||||
})
|
||||
|
||||
b, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch eventType {
|
||||
case OnCreate, OnDelete:
|
||||
ws.SendBroadcast(b)
|
||||
default:
|
||||
for client := range d.Subscriptions {
|
||||
client.SendResponse(b)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
12
models/datapoints.go
Normal file
12
models/datapoints.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import "sort"
|
||||
|
||||
type Datapoints []*Datapoint
|
||||
|
||||
func (d *Datapoints) SortSlice() {
|
||||
// Sort by Path before processing
|
||||
sort.Slice(*d, func(i, j int) bool {
|
||||
return (*d)[i].Path < (*d)[j].Path
|
||||
})
|
||||
}
|
||||
178
models/dbm.go
Normal file
178
models/dbm.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
type DBM struct {
|
||||
Datapoints Datapoint
|
||||
Uuids *Uuids
|
||||
Conns *ws.ClientHandler
|
||||
Log *logging.Logger
|
||||
}
|
||||
|
||||
var SystemDatapoints uuid.UUID
|
||||
|
||||
func NewDBM(conns *ws.ClientHandler, log *logging.Logger) *DBM {
|
||||
return &DBM{
|
||||
Uuids: &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, err := d.Datapoints.CreateDatapoints(d.Uuids, sets...)
|
||||
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 {
|
||||
err := d.Datapoints.ImportDatapoint(d.Uuids, dp, dp.Path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.ModifyCountedDatapoints(1, false)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DBM) UpdateDatapointValue(value any, uid uuid.UUID, path ...string) error {
|
||||
if uid != uuid.Nil {
|
||||
dp := d.Uuids.GetDatapoint(uid)
|
||||
if dp == nil {
|
||||
return fmt.Errorf("uuid %s not found", uid.String())
|
||||
}
|
||||
dp.Value = dp.Type.ConvertValue(value)
|
||||
dp.UpdateDateTime = time.Now().UnixMilli()
|
||||
dp.Publish(OnChange)
|
||||
}
|
||||
|
||||
if len(path) > 1 {
|
||||
return fmt.Errorf("only one path allowed")
|
||||
}
|
||||
|
||||
return d.Datapoints.UpdateDatapointValue(value, path[0])
|
||||
}
|
||||
|
||||
func (d *DBM) RemoveDatapoint(sets ...json_dataModels.Set) (lsRemoved []json_dataModels.Set, err error) {
|
||||
for _, set := range sets {
|
||||
if set.Path == "" {
|
||||
if dp := d.Uuids.GetDatapoint(set.Uuid); dp != nil {
|
||||
set.Path = dp.Path
|
||||
}
|
||||
}
|
||||
|
||||
lsRemoved, err = d.Datapoints.RemoveDatapoint(set)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
d.ModifyCountedDatapoints(uint64(len(lsRemoved)), true)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (d *DBM) QueryDatapoints(depth uint, uid uuid.UUID, key ...string) []*Datapoint {
|
||||
if uid != uuid.Nil {
|
||||
dp := d.Uuids.GetDatapoint(uid)
|
||||
if dp == nil {
|
||||
return nil
|
||||
} else 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"
|
||||
|
||||
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() {
|
||||
ticker := time.NewTicker(time.Second)
|
||||
for range ticker.C {
|
||||
if er := d.UpdateDatapointValue(time.Now().Format("2006-01-02 15:04:05"), uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *DBM) GoSystemMemory() error {
|
||||
path := "System:UsedMemory"
|
||||
|
||||
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 {
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
mem := fmt.Sprintf("%.2f MB", float64(m.Alloc)/1024/1024)
|
||||
if er := d.UpdateDatapointValue(mem, uuid.Nil, path); er != nil {
|
||||
d.Log.Error("dmb.Handler.AddSystemDps.UpdateDatapointValue", er.Error())
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
7
models/port.go
Normal file
7
models/port.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package models
|
||||
|
||||
type Port struct {
|
||||
Http uint
|
||||
Https uint
|
||||
Remote uint
|
||||
}
|
||||
14
models/stringSlices.go
Normal file
14
models/stringSlices.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package models
|
||||
|
||||
import "strings"
|
||||
|
||||
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
|
||||
}
|
||||
15
models/subscribtion.go
Normal file
15
models/subscribtion.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package models
|
||||
|
||||
import wsModels "github.com/tecamino/tecamino-dbm/websocket/models"
|
||||
|
||||
type Subscriptions map[*wsModels.Client]*Subscription
|
||||
|
||||
type Subscription struct {
|
||||
OnCreate bool
|
||||
OnDelete bool
|
||||
OnChange bool
|
||||
}
|
||||
|
||||
func InitSubscribtion() Subscriptions {
|
||||
return make(Subscriptions)
|
||||
}
|
||||
50
models/uuids.go
Normal file
50
models/uuids.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
type Uuids map[uuid.UUID]*Datapoint
|
||||
|
||||
func NewUuids() *Uuids {
|
||||
return &Uuids{}
|
||||
}
|
||||
|
||||
func (u *Uuids) AddDatapoint(parentDp, newDp *Datapoint) (renamed []json_dataModels.Set) {
|
||||
if odp, ok := (*u)[newDp.Uuid]; ok {
|
||||
if odp.Path == newDp.Path {
|
||||
return
|
||||
}
|
||||
newDp.Datapoints = odp.Datapoints
|
||||
newDp.HasChild = len(odp.Datapoints) > 0
|
||||
odp.Datapoints = map[string]*Datapoint{}
|
||||
renamed = newDp.RenamePaths(odp.Path)
|
||||
rmDps, _ := parentDp.RemoveDatapoint(json_dataModels.Set{Path: odp.Path})
|
||||
datapoints := u.GetDatapointByPath("System:Datapoints")
|
||||
datapoints.UpdateValue(datapoints.Value.(uint64) - uint64(len(rmDps)))
|
||||
}
|
||||
|
||||
(*u)[newDp.Uuid] = newDp
|
||||
return
|
||||
}
|
||||
|
||||
func (u *Uuids) GetDatapoint(uuid uuid.UUID) *Datapoint {
|
||||
if dp, ok := (*u)[uuid]; ok {
|
||||
return dp
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Uuids) GetDatapointByPath(path string) *Datapoint {
|
||||
for _, dp := range *u {
|
||||
if dp.Path == path {
|
||||
return dp
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Uuids) RemoveDatapoint(uuid uuid.UUID) {
|
||||
delete(*u, uuid)
|
||||
}
|
||||
148
test/dbm_test.go
Normal file
148
test/dbm_test.go
Normal file
@@ -0,0 +1,148 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"runtime"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/tecamino/tecamino-dbm/args"
|
||||
"github.com/tecamino/tecamino-dbm/cert"
|
||||
"github.com/tecamino/tecamino-dbm/dbm"
|
||||
"github.com/tecamino/tecamino-dbm/models"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
ws "github.com/tecamino/tecamino-dbm/websocket"
|
||||
)
|
||||
|
||||
func TestCreateDps(t *testing.T) {
|
||||
dmaHandler, err := dbm.NewDbmHandler(&args.Args{
|
||||
Port: models.Port{
|
||||
Http: 8100,
|
||||
Https: 8101,
|
||||
},
|
||||
Cert: cert.Cert{
|
||||
Organization: "tecamino",
|
||||
CertFile: "./cert/cert.pem",
|
||||
KeyFile: "./cert/key.pem",
|
||||
},
|
||||
RootDir: ".",
|
||||
DBMFile: "Test",
|
||||
Debug: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rand.NewSource(time.Now().UnixNano())
|
||||
|
||||
ndps := utils.ListofA2ZZ()
|
||||
l := len(ndps)
|
||||
s := time.Now()
|
||||
// for _, dp := range ndps {
|
||||
// for i := 0; i < 100; i++ {
|
||||
// err = dmaHandler.ImportDatapoints(&models.Datapoint{
|
||||
// Path: fmt.Sprintf("Test:%s:%03d", dp, i),
|
||||
// Type: models.RandomType(),
|
||||
// Value: rand.Int31(),
|
||||
// })
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
fmt.Printf("time used to create %d datapoints: %v\n", l*100, time.Since(s))
|
||||
|
||||
var m runtime.MemStats
|
||||
runtime.ReadMemStats(&m)
|
||||
|
||||
fmt.Printf("Allocated: %.2f MB\n", float64(m.Alloc)/1024/1024)
|
||||
fmt.Printf("Total Allocated (ever): %.2f MB\n", float64(m.TotalAlloc)/1024/1024)
|
||||
fmt.Printf("System Memory Obtained: %.2f MB\n", float64(m.Sys)/1024/1024)
|
||||
fmt.Printf("Heap In Use: %.2f MB\n", float64(m.HeapInuse)/1024/1024)
|
||||
fmt.Printf("GC Runs: %d\n", m.NumGC)
|
||||
|
||||
err = dmaHandler.SaveDb()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestQuery(t *testing.T) {
|
||||
dmaHandler, err := dbm.NewDbmHandler(&args.Args{
|
||||
Port: models.Port{
|
||||
Http: 8100,
|
||||
Https: 8101,
|
||||
},
|
||||
Cert: cert.Cert{
|
||||
Organization: "tecamino",
|
||||
CertFile: "./cert/cert.pem",
|
||||
KeyFile: "./cert/key.pem",
|
||||
},
|
||||
RootDir: ".",
|
||||
DBMFile: "Test",
|
||||
Debug: false,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for i, o := range dmaHandler.DBM.QueryDatapoints(1, uuid.Nil, "Test:A:000") {
|
||||
fmt.Println(600, i, o)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestUpdateDps(t *testing.T) {
|
||||
dmaHandler, err := dbm.NewDbmHandler(&args.Args{
|
||||
Port: models.Port{
|
||||
Http: 8100,
|
||||
Https: 8101,
|
||||
},
|
||||
Cert: cert.Cert{
|
||||
Organization: "tecamino",
|
||||
CertFile: "./cert/cert.pem",
|
||||
KeyFile: "./cert/key.pem",
|
||||
},
|
||||
RootDir: ".",
|
||||
DBMFile: "Test",
|
||||
Debug: false,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rand.NewSource(time.Now().UnixNano())
|
||||
|
||||
ndps := utils.ListofA2ZZ()
|
||||
l := len(ndps)
|
||||
s := time.Now()
|
||||
// for j, dp := range ndps {
|
||||
// if j > 2 {
|
||||
// break
|
||||
// }
|
||||
// for i := 0; i < 100; i++ {
|
||||
// err = dmaHandler.UpdateDatapointValue(fmt.Sprintf("Test:%s:%03d", dp, i), rand.Int31())
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
fmt.Printf("time used to update %d datapoints: %v\n", l*100, time.Since(s))
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
fmt.Println("save data")
|
||||
err = dmaHandler.SaveDb()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServer(t *testing.T) {
|
||||
fmt.Println("start")
|
||||
server := ws.NewServer([]string{".*"}, 9500)
|
||||
|
||||
t.Fatal(server.ServeHttp("http://localhost", 8100))
|
||||
}
|
||||
441
utils/convert.go
Normal file
441
utils/convert.go
Normal file
@@ -0,0 +1,441 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// return any input type to float32
|
||||
func Float32From(v any) float32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case float32:
|
||||
return val
|
||||
case float64:
|
||||
return float32(val)
|
||||
case int:
|
||||
return float32(val)
|
||||
case int8:
|
||||
return float32(val)
|
||||
case int16:
|
||||
return float32(val)
|
||||
case int32:
|
||||
return float32(val)
|
||||
case int64:
|
||||
return float32(val)
|
||||
case uint8:
|
||||
return float32(val)
|
||||
case uint16:
|
||||
return float32(val)
|
||||
case uint32:
|
||||
return float32(val)
|
||||
case uint64:
|
||||
return float32(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return float32(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to float64
|
||||
func Float64From(v any) float64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case float32:
|
||||
return float64(val)
|
||||
case float64:
|
||||
return val
|
||||
case int:
|
||||
return float64(val)
|
||||
case int8:
|
||||
return float64(val)
|
||||
case int16:
|
||||
return float64(val)
|
||||
case int32:
|
||||
return float64(val)
|
||||
case int64:
|
||||
return float64(val)
|
||||
case uint8:
|
||||
return float64(val)
|
||||
case uint16:
|
||||
return float64(val)
|
||||
case uint32:
|
||||
return float64(val)
|
||||
case uint64:
|
||||
return float64(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return float64(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int8
|
||||
func Int8From(v any) int8 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int8(val)
|
||||
case int8:
|
||||
return val
|
||||
case int16:
|
||||
return int8(val)
|
||||
case int32:
|
||||
return int8(val)
|
||||
case int64:
|
||||
return int8(val)
|
||||
case uint8:
|
||||
return int8(val)
|
||||
case uint16:
|
||||
return int8(val)
|
||||
case uint32:
|
||||
return int8(val)
|
||||
case uint64:
|
||||
return int8(val)
|
||||
case float32:
|
||||
return int8(val)
|
||||
case float64:
|
||||
return int8(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int8(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int16
|
||||
func Int16From(v any) int16 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int16(val)
|
||||
case int8:
|
||||
return int16(val)
|
||||
case int16:
|
||||
return val
|
||||
case int32:
|
||||
return int16(val)
|
||||
case int64:
|
||||
return int16(val)
|
||||
case uint8:
|
||||
return int16(val)
|
||||
case uint16:
|
||||
return int16(val)
|
||||
case uint32:
|
||||
return int16(val)
|
||||
case uint64:
|
||||
return int16(val)
|
||||
case float32:
|
||||
return int16(val)
|
||||
case float64:
|
||||
return int16(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int16(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int32
|
||||
func Int32From(v any) int32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int32(val)
|
||||
case int8:
|
||||
return int32(val)
|
||||
case int16:
|
||||
return int32(val)
|
||||
case int32:
|
||||
return val
|
||||
case int64:
|
||||
return int32(val)
|
||||
case uint8:
|
||||
return int32(val)
|
||||
case uint16:
|
||||
return int32(val)
|
||||
case uint32:
|
||||
return int32(val)
|
||||
case uint64:
|
||||
return int32(val)
|
||||
case float32:
|
||||
return int32(val)
|
||||
case float64:
|
||||
return int32(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int32(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int64
|
||||
func Int64From(v any) int64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return int64(val)
|
||||
case int8:
|
||||
return int64(val)
|
||||
case int16:
|
||||
return int64(val)
|
||||
case int32:
|
||||
return int64(val)
|
||||
case int64:
|
||||
return val
|
||||
case uint8:
|
||||
return int64(val)
|
||||
case uint16:
|
||||
return int64(val)
|
||||
case uint32:
|
||||
return int64(val)
|
||||
case uint64:
|
||||
return int64(val)
|
||||
case float32:
|
||||
return int64(val)
|
||||
case float64:
|
||||
return int64(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return int64(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to int
|
||||
func Uint8From(v any) uint8 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint8(val)
|
||||
case int8:
|
||||
return uint8(val)
|
||||
case int16:
|
||||
return uint8(val)
|
||||
case int32:
|
||||
return uint8(val)
|
||||
case int64:
|
||||
return uint8(val)
|
||||
case uint8:
|
||||
return val
|
||||
case uint16:
|
||||
return uint8(val)
|
||||
case uint32:
|
||||
return uint8(val)
|
||||
case uint64:
|
||||
return uint8(val)
|
||||
case float32:
|
||||
return uint8(val)
|
||||
case float64:
|
||||
return uint8(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint8(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to uint16
|
||||
func Uint16From(v any) uint16 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint16(val)
|
||||
case int8:
|
||||
return uint16(val)
|
||||
case int16:
|
||||
return uint16(val)
|
||||
case int32:
|
||||
return uint16(val)
|
||||
case int64:
|
||||
return uint16(val)
|
||||
case uint8:
|
||||
return uint16(val)
|
||||
case uint16:
|
||||
return val
|
||||
case uint32:
|
||||
return uint16(val)
|
||||
case uint64:
|
||||
return uint16(val)
|
||||
case float32:
|
||||
return uint16(val)
|
||||
case float64:
|
||||
return uint16(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint16(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to uint32
|
||||
func Uint32From(v any) uint32 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint32(val)
|
||||
case int8:
|
||||
return uint32(val)
|
||||
case int16:
|
||||
return uint32(val)
|
||||
case int32:
|
||||
return uint32(val)
|
||||
case int64:
|
||||
return uint32(val)
|
||||
case uint8:
|
||||
return uint32(val)
|
||||
case uint16:
|
||||
return uint32(val)
|
||||
case uint32:
|
||||
return val
|
||||
case uint64:
|
||||
return uint32(val)
|
||||
case float32:
|
||||
return uint32(val)
|
||||
case float64:
|
||||
return uint32(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint32(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to uint64
|
||||
func Uint64From(v any) uint64 {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
if val {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
case int:
|
||||
return uint64(val)
|
||||
case int8:
|
||||
return uint64(val)
|
||||
case int16:
|
||||
return uint64(val)
|
||||
case int32:
|
||||
return uint64(val)
|
||||
case int64:
|
||||
return uint64(val)
|
||||
case uint8:
|
||||
return uint64(val)
|
||||
case uint16:
|
||||
return uint64(val)
|
||||
case uint32:
|
||||
return uint64(val)
|
||||
case uint64:
|
||||
return val
|
||||
case float32:
|
||||
return uint64(val)
|
||||
case float64:
|
||||
return uint64(val)
|
||||
case string:
|
||||
if i, err := strconv.Atoi(val); err == nil {
|
||||
return uint64(i)
|
||||
}
|
||||
return 0
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// return any input type to bool
|
||||
func BoolFrom(v any) bool {
|
||||
switch val := v.(type) {
|
||||
case bool:
|
||||
return val
|
||||
case int:
|
||||
return val > 0
|
||||
case int8:
|
||||
return val > 0
|
||||
case int16:
|
||||
return val > 0
|
||||
case int32:
|
||||
return val > 0
|
||||
case int64:
|
||||
return val > 0
|
||||
case uint8:
|
||||
return val > 0
|
||||
case uint16:
|
||||
return val > 0
|
||||
case uint32:
|
||||
return val > 0
|
||||
case uint64:
|
||||
return val > 0
|
||||
case float32:
|
||||
return val >= 1
|
||||
case float64:
|
||||
return val >= 1
|
||||
case string:
|
||||
return strings.ToLower(val) == "false" || v == "0"
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
}
|
||||
34
utils/utils.go
Normal file
34
utils/utils.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
func ListofA2ZZ() (list []string) {
|
||||
for i := 'A'; i <= 'Z'; i++ {
|
||||
list = append(list, string(i))
|
||||
}
|
||||
for i := 'A'; i <= 'Z'; i++ {
|
||||
for j := 'A'; j <= 'Z'; j++ {
|
||||
list = append(list, string(i)+string(j))
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func GetLocalIP() (string, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, addr := range addrs {
|
||||
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
||||
if ipNet.IP.To4() != nil {
|
||||
return ipNet.IP.String(), nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("no local IP address found")
|
||||
}
|
||||
76
websocket/clientHandler.go
Normal file
76
websocket/clientHandler.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tecamino/tecamino-dbm/websocket/models"
|
||||
wsModels "github.com/tecamino/tecamino-dbm/websocket/models"
|
||||
json_dataModels "github.com/tecamino/tecamino-json_data/models"
|
||||
)
|
||||
|
||||
// serves as connection handler of websocket
|
||||
type ClientHandler struct {
|
||||
sync.RWMutex
|
||||
Clients models.Clients
|
||||
}
|
||||
|
||||
func SendBroadcast(msg []byte) {
|
||||
for _, c := range models.Broadcast {
|
||||
c.SendResponse(msg)
|
||||
}
|
||||
}
|
||||
|
||||
// initaiates new conections with client map
|
||||
func NewConnectionHandler() *ClientHandler {
|
||||
return &ClientHandler{
|
||||
Clients: make(models.Clients),
|
||||
}
|
||||
}
|
||||
|
||||
// Connect a recieving websocket connection
|
||||
func (cH *ClientHandler) ConnectNewClient(id string, c *gin.Context) (client *models.Client, err error) {
|
||||
if _, exists := cH.Clients[id]; exists {
|
||||
return cH.Clients[id], nil
|
||||
}
|
||||
|
||||
client, err = models.ConnectNewClient(id, c)
|
||||
client.OnClose = func(code int, reason string) {
|
||||
delete(cH.Clients, id)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cH.Lock()
|
||||
cH.Clients[id] = client
|
||||
cH.Unlock()
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// get client client
|
||||
func (c *ClientHandler) GetClient(id string) *wsModels.Client {
|
||||
if client, ok := c.Clients[id]; ok {
|
||||
return client
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sends json response to client
|
||||
func (c *ClientHandler) 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
|
||||
}
|
||||
client.SendResponse(b)
|
||||
|
||||
return nil
|
||||
}
|
||||
226
websocket/models/client.go
Normal file
226
websocket/models/client.go
Normal file
@@ -0,0 +1,226 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var Origins []string = []string{"*"}
|
||||
|
||||
var Broadcast Clients = make(Clients)
|
||||
|
||||
const (
|
||||
// Time allowed to write a message to the peer.
|
||||
writeWait = 10 * time.Second
|
||||
|
||||
// Time allowed to read the next pong message from the peer.
|
||||
pongWait = 30 * time.Second
|
||||
|
||||
// Send pings to peer with this period. Must be less than pongWait.
|
||||
pingPeriod = (pongWait * 9) / 10
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
Id string
|
||||
Connected bool `json:"connected"`
|
||||
conn *websocket.Conn `json:"-"`
|
||||
OnOpen func()
|
||||
OnMessage func(data []byte)
|
||||
OnClose func(code int, reason string)
|
||||
OnError func(err error)
|
||||
OnWarning func(warn string)
|
||||
OnPing func()
|
||||
OnPong func()
|
||||
send chan []byte
|
||||
unregister chan []byte
|
||||
}
|
||||
|
||||
var upgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
if len(Origins) == 0 {
|
||||
return false
|
||||
} else if Origins[0] == "*" {
|
||||
return true
|
||||
}
|
||||
return slices.Contains(Origins, r.Header.Get("Origin"))
|
||||
},
|
||||
EnableCompression: false,
|
||||
}
|
||||
|
||||
func ConnectNewClient(id string, c *gin.Context) (*Client, error) {
|
||||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("websocket upgrade error: %w", err)
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
Id: id,
|
||||
Connected: true,
|
||||
conn: conn,
|
||||
send: make(chan []byte, 512),
|
||||
unregister: make(chan []byte, 256),
|
||||
}
|
||||
|
||||
Broadcast[client.Id] = client
|
||||
|
||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
|
||||
conn.SetPingHandler(func(appData string) error {
|
||||
if client.OnPing != nil {
|
||||
client.OnPing()
|
||||
}
|
||||
|
||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if err := client.conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(pongWait)); err != nil {
|
||||
client.OnError(err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
conn.SetPongHandler(func(appData string) error {
|
||||
conn.SetReadDeadline(time.Now().Add(pongWait))
|
||||
if client.OnPong != nil {
|
||||
client.OnPong()
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
// Start reading messages from client
|
||||
go client.Read()
|
||||
go client.Write()
|
||||
go client.PingInterval(pingPeriod)
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *Client) Read() {
|
||||
if c.OnOpen != nil {
|
||||
c.OnOpen()
|
||||
}
|
||||
|
||||
c.conn.SetReadDeadline(time.Now().Add(writeWait))
|
||||
for c.Connected {
|
||||
msgType, msg, err := c.conn.ReadMessage()
|
||||
|
||||
if err != nil {
|
||||
c.handleError(fmt.Errorf("read error (id:%s): %w", c.Id, err))
|
||||
return
|
||||
}
|
||||
switch msgType {
|
||||
case websocket.CloseMessage:
|
||||
c.Close(websocket.CloseNormalClosure, "Client closed")
|
||||
return
|
||||
case websocket.TextMessage:
|
||||
if c.OnMessage != nil {
|
||||
c.OnMessage(msg)
|
||||
} else {
|
||||
log.Printf("Received message but no handler set (id:%s): %s", c.Id, string(msg))
|
||||
}
|
||||
default:
|
||||
log.Printf("Unhandled message type %d (id:%s)", msgType, c.Id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Write() {
|
||||
defer c.conn.Close()
|
||||
|
||||
for {
|
||||
select {
|
||||
case message, ok := <-c.send:
|
||||
c.conn.SetWriteDeadline(time.Now().Add(writeWait))
|
||||
if !ok {
|
||||
// The hub closed the channel.
|
||||
if err := c.conn.WriteMessage(websocket.CloseMessage, []byte("ping")); err != nil {
|
||||
c.handleError(err)
|
||||
return
|
||||
}
|
||||
c.handleError(fmt.Errorf("server %s closed channel", c.Id))
|
||||
return
|
||||
} else {
|
||||
if err := c.conn.WriteMessage(websocket.TextMessage, message); err != nil {
|
||||
c.handleError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
case message := <-c.unregister:
|
||||
c.conn.WriteMessage(websocket.CloseMessage, message)
|
||||
c.Connected = false
|
||||
close(c.send)
|
||||
delete(Broadcast, c.Id)
|
||||
close(c.unregister)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) PingInterval(interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
if err := c.conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(pongWait)); err != nil {
|
||||
c.OnError(err)
|
||||
return
|
||||
}
|
||||
|
||||
for range ticker.C {
|
||||
if c.OnPing != nil {
|
||||
c.OnPing()
|
||||
}
|
||||
|
||||
if err := c.conn.WriteControl(websocket.PingMessage, []byte("ping"), time.Now().Add(pongWait)); err != nil {
|
||||
c.OnError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) SendResponse(data []byte) {
|
||||
if !c.Connected {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case c.send <- data:
|
||||
// sent successfully
|
||||
default:
|
||||
// channel full, drop or log
|
||||
if c.OnWarning != nil {
|
||||
c.OnWarning("Dropping message: channel full")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Close(code int, reason string) error {
|
||||
closeMsg := websocket.FormatCloseMessage(code, reason)
|
||||
|
||||
select {
|
||||
case c.unregister <- closeMsg: // Attempt to send
|
||||
default: // If the channel is full, this runs
|
||||
return fmt.Errorf("attempt close client socket failed")
|
||||
}
|
||||
if c.OnClose != nil {
|
||||
c.OnClose(code, reason)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) handleError(err error) {
|
||||
if c.OnError != nil {
|
||||
c.OnError(err)
|
||||
}
|
||||
|
||||
if err := c.Close(websocket.CloseInternalServerErr, err.Error()); err != nil {
|
||||
if c.OnError != nil {
|
||||
c.OnError(err)
|
||||
} else {
|
||||
fmt.Println("error: ", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
3
websocket/models/clients.go
Normal file
3
websocket/models/clients.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package models
|
||||
|
||||
type Clients map[string]*Client
|
||||
21
websocket/models/wsMessage.go
Normal file
21
websocket/models/wsMessage.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type WSMessage struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
func GetPongByteSlice() []byte {
|
||||
b, err := json.Marshal(WSMessage{
|
||||
Type: "pong",
|
||||
})
|
||||
if err != nil {
|
||||
return []byte{}
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (w WSMessage) IsPing() bool {
|
||||
return w.Type == "ping"
|
||||
}
|
||||
60
websocket/server.go
Normal file
60
websocket/server.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package websocket
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/tecamino/tecamino-dbm/cert"
|
||||
"github.com/tecamino/tecamino-dbm/utils"
|
||||
"github.com/tecamino/tecamino-logger/logging"
|
||||
)
|
||||
|
||||
// server model for database manager websocket
|
||||
type Server struct {
|
||||
Routes *gin.Engine
|
||||
sync.RWMutex
|
||||
Logger *logging.Logger
|
||||
}
|
||||
|
||||
// initalizes new dbm server
|
||||
func NewServer(allowOrigins []string, port uint) *Server {
|
||||
r := gin.Default()
|
||||
|
||||
allowOrigins = append(allowOrigins, fmt.Sprintf("http://localhost:%d", port))
|
||||
|
||||
localIP, err := utils.GetLocalIP()
|
||||
if err != nil {
|
||||
log.Printf("get local ip : %s", err.Error())
|
||||
} else {
|
||||
allowOrigins = append(allowOrigins, fmt.Sprintf("http://%s:%d", localIP, port))
|
||||
}
|
||||
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: r,
|
||||
}
|
||||
}
|
||||
|
||||
// serve dbm as http
|
||||
func (s *Server) ServeHttp(ip string, port uint) error {
|
||||
return s.Routes.Run(fmt.Sprintf("%s:%d", ip, port))
|
||||
}
|
||||
|
||||
// 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("%s:%d", ip, port), cert.CertFile, cert.KeyFile)
|
||||
}
|
||||
Reference in New Issue
Block a user