add func of circuit diagram

This commit is contained in:
douxu 2024-11-12 16:39:14 +08:00
parent e2a414292d
commit 2ac524fa13
20 changed files with 916 additions and 0 deletions

23
modelRT/.gitignore vendored Normal file
View File

@ -0,0 +1,23 @@
# ---> Go
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work

2
modelRT/README.md Normal file
View File

@ -0,0 +1,2 @@
# ModelRT

52
modelRT/config/config.go Normal file
View File

@ -0,0 +1,52 @@
// Package config define config struct of wave record project
package config
import (
"fmt"
"time"
"modelRT/constant"
"modelRT/log"
"github.com/spf13/viper"
)
// ModelRTConfig define config stuct of model runtime server
type ModelRTConfig struct {
ParseConcurrentQuantity int // parse comtrade file concurrent quantity
PostgresDBURI string
LCfg log.CutLogConfig // log config
}
// ReadAndInitConfig return wave record project config struct
func ReadAndInitConfig(configDir, configName, configType string) (modelRTConfig ModelRTConfig) {
config := viper.New()
config.AddConfigPath(configDir)
config.SetConfigName(configName)
config.SetConfigType(configType)
if err := config.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
panic("can not find conifg file")
}
panic(err)
}
// init postgresdb config from config.yaml
postgresDBHost := config.GetString("postgresdb_host")
postgresDBPort := config.GetInt("postgresdb_port")
postgresDBUser := config.GetString("postgresdb_user")
postgresDBPassword := config.GetString("postgresdb_password")
postgresDBDataBase := config.GetString("postgresdb_database")
modelRTConfig.PostgresDBURI = fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s", postgresDBHost, postgresDBPort, postgresDBUser, postgresDBPassword, postgresDBDataBase)
// init zap log config from config.yaml
modelRTConfig.LCfg.Mode = config.GetString("log_mode")
modelRTConfig.LCfg.Level = config.GetString("log_level")
modelRTConfig.LCfg.FileName = fmt.Sprintf(config.GetString("log_filepath"), time.Now().Format(constant.LogTimeFormate))
modelRTConfig.LCfg.MaxSize = config.GetInt("log_maxsize")
modelRTConfig.LCfg.MaxBackups = config.GetInt("log_maxbackups")
modelRTConfig.LCfg.MaxAge = config.GetInt("log_maxage")
modelRTConfig.ParseConcurrentQuantity = config.GetInt("parse_concurrent_quantity")
return modelRTConfig
}

View File

@ -0,0 +1,20 @@
postgres_host: "localhost"
postgres_port: 5432
postgres_database: "model_rt"
postgres_user: "postgres"
postgres_password: "coslight"
# influxdb_host: "localhost"
# influxdb_port: "8086"
# influxdb_token: "lCuiQ316qlly3iFeoi1EUokPJ0XxW-5lnG-3rXsKaaZSjfuxO5EaZfFdrNGM7Zlrdk1PrN_7TOsM_SCu9Onyew=="
# influxdb_org: "coslight"
# influxdb_bucket: "wave_record"
log_mode: "development"
log_level: "debug"
log_filepath: "/home/douxu/log/wave_record-%s.log"
log_maxsize: 1
log_maxbackups: 5
log_maxage: 30
parse_concurrent_quantity: 10

View File

@ -0,0 +1,19 @@
package constant
const (
// 母线服役属性
// 母线服役运行属性
BusbarServiceRunning = iota
// 母线服役退出属性
BusbarServiceExited
)
const (
// 现役/新建/计划/检修/库存可用/库存报废
BusbarStatusActive = iota
BusbarStatusNewBuild
BusbarStatusPlan
BusbarStatusOverhaul
BusbarStatusInventoryAvailable
BusbarStatusInventoryScrap
)

View File

@ -0,0 +1,9 @@
// Package constant define constant value
package constant
const (
// DevelopmentLogMode define development operator environment for wave record project
DevelopmentLogMode = "development"
// ProductionLogMode define production operator environment for wave record project
ProductionLogMode = "production"
)

7
modelRT/constant/time.go Normal file
View File

@ -0,0 +1,7 @@
// Package constant define constant value
package constant
const (
// LogTimeFormate define time format for log file name
LogTimeFormate = "2006-01-02 15:04:05"
)

View File

@ -0,0 +1,48 @@
// Package database define database operation functions
package database
import (
"context"
"encoding/json"
"strconv"
"time"
"modelRT/orm"
"go.uber.org/zap"
)
// LoadCircuitDiagramFromPostgresDB return the result of query circuit diagram info from postgresDB
func LoadCircuitDiagramFromPostgresDB(ctx context.Context, logger *zap.Logger) ([]orm.CircuitDiagram, error) {
var circuitDiagramOverviews []orm.CircuitDiagramOverview
// ctx超时判断
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
result := _globalPostgresClient.WithContext(cancelCtx).Find(&circuitDiagramOverviews)
if result.Error != nil {
logger.Error("query circuit diagram overview info failed", zap.Error(result.Error))
}
var circuitDiagrams []orm.CircuitDiagram
for _, diagram := range circuitDiagramOverviews {
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var circuitDiagram orm.CircuitDiagram
tableName := "circuit_diagram_" + strconv.FormatInt(diagram.ID, 10)
result := _globalPostgresClient.Table(tableName).WithContext(cancelCtx).Find(&circuitDiagram)
if result.Error != nil {
logger.Error("query circuit diagram info failed", zap.Error(result.Error))
return nil, result.Error
}
circuitDiagrams = append(circuitDiagrams, circuitDiagram)
}
for _, diagram := range circuitDiagrams {
// TODO
// 根据 diagram 的 type 类型选择不同的承接结构
// 换一个解析更快的 json encoder
json.Unmarshal([]byte(diagram.OtherParams), nil)
// unmarshal diagram
}
return circuitDiagrams, nil
}

View File

@ -0,0 +1,44 @@
// Package database define database operation functions
package database
import (
"context"
"sync"
"time"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var (
postgresOnce sync.Once
_globalPostgresClient *gorm.DB
_globalPostgresMu sync.RWMutex
)
// PostgresDBClient returns the global PostgresDB client.It's safe for concurrent use.
func PostgresDBClient() *gorm.DB {
_globalPostgresMu.RLock()
client := _globalPostgresClient
_globalPostgresMu.RUnlock()
return client
}
// GetPostgresDBInstance return instance of PostgresDB client
func GetPostgresDBInstance(ctx context.Context, PostgresDBURI string) *gorm.DB {
postgresOnce.Do(func() {
_globalPostgresClient = initPostgresDBClient(ctx, PostgresDBURI)
})
return _globalPostgresClient
}
// initPostgresDBClient return successfully initialized PostgresDB client
func initPostgresDBClient(ctx context.Context, PostgresDBURI string) *gorm.DB {
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
db, err := gorm.Open(postgres.Open(PostgresDBURI), &gorm.Config{})
if err != nil {
panic(err)
}
return db
}

65
modelRT/go.mod Normal file
View File

@ -0,0 +1,65 @@
module modelRT
go 1.22.5
require (
github.com/gin-gonic/gin v1.10.0
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/spf13/viper v1.19.0
go.uber.org/zap v1.27.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.12
)
require (
github.com/BurntSushi/toml v1.4.0 // indirect
github.com/bytedance/sonic v1.11.6 // indirect
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // 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
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

158
modelRT/go.sum Normal file
View File

@ -0,0 +1,158 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/0Y=
github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg=
github.com/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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/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.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
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/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
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/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
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/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE=
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8=
github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNoBjkY=
github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0=
github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper v1.19.0 h1:RWq5SEjt8o25SROyN3z2OrDB9l7RPd3lwTWU8EcEdcI=
github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+Ntkg=
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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
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.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/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/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
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=
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=

View File

@ -0,0 +1,16 @@
package handler
import (
"fmt"
"modelRT/database"
"github.com/gin-gonic/gin"
)
// ModelLoad define model load process API
func ModelLoad(ctx *gin.Context) {
ctx.Writer.Write([]byte("Hi Boy"))
pgClient := database.GetPostgresDBInstance(ctx, "")
fmt.Println(pgClient)
}

84
modelRT/log/init.go Normal file
View File

@ -0,0 +1,84 @@
// Package log define log struct of wave record project
package log
import (
"os"
"sync"
"modelRT/constant"
"github.com/natefinch/lumberjack"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
var (
logger *zap.Logger
once sync.Once
)
// CutLogConfig define log config of wave record project
type CutLogConfig struct {
Mode string `json:"mode"` // Mode 日志模式 development、production
Level string `json:"level"` // Level 最低日志等级DEBUG<INFO<WARN<ERROR<FATAL INFO-->收集INFO等级以上的日志
FileName string `json:"file_name"` // FileName 日志文件位置
MaxSize int `json:"max_size"` // MaxSize 进行切割之前,日志文件的最大大小(MB为单位)默认为100MB
MaxAge int `json:"max_age"` // MaxAge 是根据文件名中编码的时间戳保留旧日志文件的最大天数。
MaxBackups int `json:"max_backups"` // MaxBackups 是要保留的旧日志文件的最大数量。默认是保留所有旧的日志文件(尽管 MaxAge 可能仍会导致它们被删除)
}
// getEncoder responsible for setting the log format for encoding
func getEncoder() zapcore.Encoder {
encoderConfig := zap.NewProductionEncoderConfig()
// serialization time eg:2006-01-02 15:04:05
encoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05")
encoderConfig.TimeKey = "time"
encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
encoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
return zapcore.NewJSONEncoder(encoderConfig)
}
// getLogWriter responsible for setting the location of log storage
func getLogWriter(mode, filename string, maxsize, maxBackup, maxAge int) zapcore.WriteSyncer {
lumberJackLogger := &lumberjack.Logger{
Filename: filename, // log file position
MaxSize: maxsize, // log file maxsize
MaxAge: maxAge, // maximum number of day files retained
MaxBackups: maxBackup, // maximum number of old files retained
Compress: false, // whether to compress
}
syncConsole := zapcore.AddSync(os.Stderr)
if mode == constant.DevelopmentLogMode {
return syncConsole
}
syncFile := zapcore.AddSync(lumberJackLogger)
return zapcore.NewMultiWriteSyncer(syncFile, syncConsole)
}
// initLogger return successfully initialized zap logger
func initLogger(lCfg CutLogConfig) *zap.Logger {
writeSyncer := getLogWriter(lCfg.Mode, lCfg.FileName, lCfg.MaxSize, lCfg.MaxBackups, lCfg.MaxAge)
encoder := getEncoder()
l := new(zapcore.Level)
err := l.UnmarshalText([]byte(lCfg.Level))
if err != nil {
panic(err)
}
core := zapcore.NewCore(encoder, writeSyncer, l)
logger = zap.New(core, zap.AddCaller())
zap.ReplaceGlobals(logger)
return logger
}
// GetLoggerInstance return instance of zap logger
func GetLoggerInstance(lCfg CutLogConfig) *zap.Logger {
once.Do(func() {
logger = initLogger(lCfg)
})
return logger
}

76
modelRT/main.go Normal file
View File

@ -0,0 +1,76 @@
// entry function
package main
import (
"context"
"flag"
"time"
"modelRT/config"
"modelRT/database"
"modelRT/handler"
"modelRT/log"
"modelRT/middleware"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"gorm.io/gorm"
)
var limiter *middleware.Limiter
func init() {
limiter = middleware.NewLimiter(10, 1*time.Minute) // 设置限流器允许每分钟最多请求10次
}
var (
modelRTConfigDir = flag.String("modelRT_config_dir", "./config", "config file dir of model runtime service")
modelRTConfigName = flag.String("modelRT_config_name", "config", "config file name of model runtime service")
modelRTConfigType = flag.String("modelRT_config_type", "yaml", "config file type of model runtime service")
)
var (
modelRTConfig config.ModelRTConfig
postgresDBClient *gorm.DB
logger *zap.Logger
)
// TODO 使用 wire 依赖注入
func main() {
flag.Parse()
ctx := context.TODO()
modelRTConfig = config.ReadAndInitConfig(*modelRTConfigDir, *modelRTConfigName, *modelRTConfigType)
// TODO 创建 Redis 与 pg client
// init postgresDBClient
postgresDBClient = database.GetPostgresDBInstance(ctx, modelRTConfig.PostgresDBURI)
defer func() {
sqlDB, err := postgresDBClient.DB()
if err != nil {
panic(err)
}
sqlDB.Close()
}()
// init logger
logger = log.GetLoggerInstance(modelRTConfig.LCfg)
defer logger.Sync()
// load circuit diagram from postgres
database.LoadCircuitDiagramFromPostgresDB(ctx, logger)
// TODO 从 pg 加载到 redis
engine := gin.Default()
engine.Use(limiter.Middleware)
engine.GET("/model/model_load", handler.ModelLoad)
engine.POST("/model/model_create", nil)
engine.POST("/model/model_update", nil)
engine.POST("/model/model_delete", nil)
// start route with 8080 port
engine.Run(":8080")
// Redis hashmap 母线模型、异步电动机模型
// kv key name value busx
}

View File

@ -0,0 +1,58 @@
package middleware
import (
"time"
"github.com/gin-gonic/gin"
)
// Limiter 限流器
type Limiter struct {
limit int // 限制的请求数量
duration time.Duration // 时间窗口
timestamps map[string][]int64 // 请求的时间戳
}
// NewLimiter 创建限流器
func NewLimiter(limit int, duration time.Duration) *Limiter {
return &Limiter{
limit: limit,
duration: duration,
timestamps: make(map[string][]int64),
}
}
// Middleware 限流中间件
func (l *Limiter) Middleware(c *gin.Context) {
ip := c.ClientIP() // 获取客户端IP地址
// 检查请求时间戳切片是否存在
if _, ok := l.timestamps[ip]; !ok {
l.timestamps[ip] = make([]int64, 0)
}
now := time.Now().Unix() // 当前时间戳
// 移除过期的请求时间戳
for i := 0; i < len(l.timestamps[ip]); i++ {
if l.timestamps[ip][i] < now-int64(l.duration.Seconds()) {
l.timestamps[ip] = append(l.timestamps[ip][:i], l.timestamps[ip][i+1:]...)
i--
}
}
// 检查请求数量是否超过限制
if len(l.timestamps[ip]) >= l.limit {
c.JSON(429, gin.H{
"message": "Too Many Requests",
})
c.Abort()
return
}
// 添加当前请求时间戳到切片
l.timestamps[ip] = append(l.timestamps[ip], now)
// 继续处理请求
c.Next()
}

View File

@ -0,0 +1,86 @@
package model
type BusbarSection struct {
// 母线基本参数
Name string // 母线端名称,默认值BusX
BusbarNumber int // 母线编号,默认值1
StandardVoltage float32 // 标准电压,单位kV,范围值在000.01~500.00
Desc string // 描述
IsService bool // 是否服役,值为运行/退出
Status string // 状态,值为现役/新建/计划/检修/库存可用/库存报废
PowerGridName string // 当前工程电网的顶层建模时的电网名称
RegionName string // 当前工程电网的顶层建模时的区域电网名称
FactoryStationName string // 当前工程电网的顶层建模时的厂站名称
// 母线模型参数
VoltagePercentValue float32 // 以母线标称电压为基准的百分数,默认值1.00~200.00
VoltageCalculcatedValue float32 // 通过StandardVoltage与VoltagePercentValtage计算得出的电压值,默认值0.01~1000.00
PhaseAngle float32 // 面向三相对称电网的属性值,可按A相电压考虑即可,默认值-180.00~180.00
RatedCurrent float32 // 母线额定电流,范围值0.01~65536
DynamicStableCurrent float32 // 母线动稳定电流,范围值0.01~65536
MinLoadAdjustmentCoefficient int // 最小母线负荷调整系数,范围值0-100
MaxLoadAdjustmentCoefficient int // 最大母线负荷调整系数,范围值0-500
BusbarType int // 母线类型,默认值PQ
ReferenceVoltage float32 // 母线类型,单位kV,默认值37
ReferenceVCurrent float32 // 母线类型,单位MVA,默认值100
MinS3Capacities float32 // 最小三项短路容量,范围值0.00~65536.00
MaxS3Capacities float32 // 最大三项短路容量,范围值0.00~65536.00
MinS3Current float32 // 最小三项短路电流,范围值0.00~65536.00
MaxS3Current float32 // 最大三项短路电流,范围值0.00~65536.00
MinZ3Impedance float32 // 最小三项短路阻抗,默认值 0.1,范围值0.0000~100.0000
MaxZ3Impedance float32 // 最大三项短路阻抗,默认值 0.05,范围值0.0000~100.0000
MinS1Capacity float32 // 最小单项短路容量,范围值0.00~65536.00
MaxS1Capacity float32 // 最大单项短路容量,范围值0.00~65536.00
MinS1Current float32 // 最小单项短路电流,范围值0.00~65536.00
MaxS1Current float32 // 最大单项短路电流,范围值0.00~65536.00
MinS1Impedance float32 // 最小单项短路阻抗,默认值 0.1,范围值0.0000~100.0000
MaxS1Impedance float32 // 最大单项短路阻抗,默认值 0.05,范围值0.0000~100.0000
// 母线稳定参数
UnderVoltageWarningThreshold int // 欠压预警阈值
UnderVoltageWarningRunningTime float32 // 欠压预警运行时间,默认值 10s,默认单位秒默认范围值0s-100s
UnderVoltageAlarmThreshold int // 欠压告警阈值
UnderVoltageAlarmRunningTime float32 // 欠压告警运行时间,默认值 10s,默认单位秒默认范围值0s-100s
OverVoltageWarningThreshold int // 过压预警阈值
OverVoltageWarningRunningTime float32 // 过压预警运行时间,默认值 10s,默认单位秒默认范围值0s-100s
OverVoltageAlarmThreshold int // 过压告警阈值
OverVoltageAlarmRunningTime float32 // 过压告警运行时间,默认值 10s,默认单位秒默认范围值0s-100s
PMax float32 // 有功储备裕度最大值
QMax float32 // 无功储备裕度最大值
Ulim float32 // 电压裕度
Plim float32 // 实时有功安全裕度限值,默认值30%,范围 0-100%
Qlim float32 // 实时无功安全裕度限值,默认值30%,范围 0-100%
// 母线间隔信息
MeasurementLevelCurrent []string // 测量级电流测点
MeasurementLevelVoltage []string // 测量级电压测点
ProtectionLevelCurrent []string // 保护级电流测点
ProtectionLevelVoltaget []string // 保护级电压测点
Trend []string // 潮流测点
Frequency []string // 频率测点
StatusMeasurementPoint []string // 状态测点测点
}
func NewBusbarSection(name string) *BusbarSection {
return &BusbarSection{
Name: name,
}
}
func (b *BusbarSection) BusNameLenCheck() bool {
if len([]rune(b.Name)) > 20 {
return false
}
return true
}
func (b *BusbarSection) BusVoltageCheck() bool {
if b.StandardVoltage > 500.00 || b.StandardVoltage < 000.01 {
return false
}
return true
}
func (b *BusbarSection) BusDescLenCheck() bool {
if len([]rune(b.Desc)) > 100 {
return false
}
return true
}

View File

@ -0,0 +1,13 @@
// Package orm define database data struct
package orm
import "time"
type CircuitDiagram struct {
ID int64 `gorm:""`
ParentID int64
Type string
OtherParams string
CreatedTime time.Time
UpdateTime time.Time
}

View File

@ -0,0 +1,16 @@
// Package orm define database data struct
package orm
import "time"
type CircuitDiagramOverview struct {
ID int64 `gorm:""`
Name string
CreatedTime time.Time
UpdateTime time.Time
}
// TableName func respresent return table name of circuit diagram overview
func (co *CircuitDiagramOverview) TableName() string {
return "circuit_diagram_overview"
}

58
modelRT/pg.sql Normal file
View File

@ -0,0 +1,58 @@
CREATE DATABASE circuit_diagram;
CREATE TABLE circuit_diagram_overview
(
id bigserial not null,
name text not null,
created_at timestamp with time zone default CURRENT_TIMESTAMP not null,
updated_at timestamp with time zone default CURRENT_TIMESTAMP not null
);
CREATE TABLE circuit_diagram_1
(
id bigserial not null,
name text not null,
created_at timestamp with time zone default CURRENT_TIMESTAMP not null,
updated_at timestamp with time zone default CURRENT_TIMESTAMP not null,
parent_id bigint default 0 not null,
other_params text not null
);
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(1,'母线','2024-11-07 09:37:00','2024-11-07 09:37:00',0,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(2,'电流互感器','2024-11-07 09:37:00','2024-11-07 09:37:00',1,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(3,'过继电流器','2024-11-07 09:37:00','2024-11-07 09:37:00',1,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(4,'断路器','2024-11-07 09:37:00','2024-11-07 09:37:00',2,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(5,'变压器','2024-11-07 09:37:00','2024-11-07 09:37:00',4,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(6,'线缆','2024-11-07 09:37:00','2024-11-07 09:37:00',5,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(7,'低压母线','2024-11-07 09:37:00','2024-11-07 09:37:00',6,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(8,' 电动机 1','2024-11-07 09:37:00','2024-11-07 09:37:00',7,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(9,' 电动机 2','2024-11-07 09:37:00','2024-11-07 09:37:00',7,'voltage:35,status:0');
SELECT id,parent_id,name,other_params
FROM circuit_diagram_1
WHERE id = 1
WITH RECURSIVE recursive_tree as (
SELECT id,parent_id,name,other_params
FROM circuit_diagram_1
WHERE id = 1
UNION ALL
SELECT ctd.id,ctd.parent_id,ctd.name,ctd.other_params
FROM circuit_diagram_1 ctd
JOIN recursive_tree rt ON ctd.parent_id = rt.id
)
SELECT * FROM recursive_tree;

62
modelRT/sql/load.sql Normal file
View File

@ -0,0 +1,62 @@
CREATE DATABASE circuit_diagram;
CREATE TABLE circuit_diagram_overview
(
id bigserial not null,
name text not null,
created_at timestamp with time zone default CURRENT_TIMESTAMP not null,
updated_at timestamp with time zone default CURRENT_TIMESTAMP not null
);
CREATE TABLE circuit_diagram_1
(
id bigserial not null,
name text not null,
created_at timestamp with time zone default CURRENT_TIMESTAMP not null,
updated_at timestamp with time zone default CURRENT_TIMESTAMP not null,
parent_id bigint default 0 not null,
other_params text not null
);
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(1,'母线','2024-11-07 09:37:00','2024-11-07 09:37:00',0,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(2,'电流互感器','2024-11-07 09:37:00','2024-11-07 09:37:00',1,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(3,'过继电流器','2024-11-07 09:37:00','2024-11-07 09:37:00',1,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(4,'断路器','2024-11-07 09:37:00','2024-11-07 09:37:00',2,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(5,'变压器','2024-11-07 09:37:00','2024-11-07 09:37:00',4,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(6,'线缆','2024-11-07 09:37:00','2024-11-07 09:37:00',5,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(7,'低压母线','2024-11-07 09:37:00','2024-11-07 09:37:00',6,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(8,' 电动机 1','2024-11-07 09:37:00','2024-11-07 09:37:00',7,'voltage:35,status:0');
INSERT INTO public.circuit_diagram_1(id,name,created_at,updated_at,parent_id,other_params) VALUES(9,' 电动机 2','2024-11-07 09:37:00','2024-11-07 09:37:00',7,'voltage:35,status:0');
SELECT id,parent_id,name,other_params
FROM circuit_diagram_1
WHERE id = 1
WITH RECURSIVE recursive_tree as (
SELECT id,parent_id,name,other_params
FROM circuit_diagram_1
WHERE id = 1
UNION ALL
SELECT ctd.id,ctd.parent_id,ctd.name,ctd.other_params
FROM circuit_diagram_1 ctd
JOIN recursive_tree rt ON ctd.parent_id = rt.id
)
SELECT * FROM recursive_tree;