From f4ab4e4ea444f2558e89f2b0bffb74292532d33a Mon Sep 17 00:00:00 2001 From: douxu Date: Fri, 15 Aug 2025 16:25:48 +0800 Subject: [PATCH] refactor(orm/circuit_diagram_component): fix compilation issues caused by structure field changes http://server.baseware.net:9000/project/datart/task/47 --- alert/init.go | 10 +- config/anchor_param_config.go | 2 +- database/create_component.go | 27 ++-- database/query_component.go | 45 +++--- database/query_topologic.go | 58 ++----- database/update_component.go | 31 ++-- diagram/anchor_set.go | 24 +-- diagram/component_set.go | 30 ++-- diagram/multi_branch_tree.go | 16 +- handler/anchor_point_replace.go | 37 +---- handler/circuit_diagram_create.go | 96 +++++------ handler/circuit_diagram_delete.go | 28 +--- handler/circuit_diagram_load.go | 4 +- handler/circuit_diagram_update.go | 89 +++++------ handler/real_time_data_receive.go | 4 +- main.go | 12 +- network/circuit_diagram_create_request.go | 22 ++- network/circuit_diagram_update_request.go | 21 ++- network/real_time_data_request.go | 6 +- orm/circuit_diagram_bay.go | 1 + orm/circuit_diagram_component.go | 4 - pool/component_chan_set.go | 49 +++--- pool/concurrency_anchor_parse.go | 16 +- pool/concurrency_model_parse.go | 39 +---- real-time-data/real_time_data_receive.go | 37 ++--- sharememory/share_memeory.go | 1 - test/handler/circuit_diagram_query_test.go | 178 +++++++++++++++++++++ 27 files changed, 442 insertions(+), 445 deletions(-) create mode 100644 test/handler/circuit_diagram_query_test.go diff --git a/alert/init.go b/alert/init.go index 96e7da0..b5d64d3 100644 --- a/alert/init.go +++ b/alert/init.go @@ -16,11 +16,11 @@ var ( // Event define alert event struct type Event struct { - ComponentID int64 - AnchorName string - Level constants.AlertLevel - Message string - StartTime int64 + ComponentUUID string + AnchorName string + Level constants.AlertLevel + Message string + StartTime int64 } // EventManager define store and manager alert event struct diff --git a/config/anchor_param_config.go b/config/anchor_param_config.go index 7921518..018aca9 100644 --- a/config/anchor_param_config.go +++ b/config/anchor_param_config.go @@ -15,7 +15,7 @@ type AnchorParamListConfig struct { // AnchorParamBaseConfig define anchor params base config struct type AnchorParamBaseConfig struct { - ComponentID int64 // component表 ID + ComponentUUID string // componentUUID AnchorName string // 锚定参量名称 CompareValUpperLimit float64 // 比较值上限 CompareValLowerLimit float64 // 比较值下限 diff --git a/database/create_component.go b/database/create_component.go index 9c3a907..c97730f 100644 --- a/database/create_component.go +++ b/database/create_component.go @@ -16,26 +16,25 @@ import ( ) // CreateComponentIntoDB define create component info of the circuit diagram into DB -func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentCreateInfo) (int64, error) { +func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentCreateInfo) (string, error) { cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() globalUUID, err := uuid.FromString(componentInfo.UUID) if err != nil { - return -1, fmt.Errorf("format uuid from string type failed:%w", err) + return "", fmt.Errorf("format uuid from string type failed:%w", err) } component := orm.Component{ - GlobalUUID: globalUUID, - GridID: strconv.FormatInt(componentInfo.GridID, 10), - ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10), - StationID: strconv.FormatInt(componentInfo.StationID, 10), - Tag: componentInfo.Tag, - ComponentType: componentInfo.ComponentType, - Name: componentInfo.Name, - Context: componentInfo.Context, - Op: componentInfo.Op, - Ts: time.Now(), + GlobalUUID: globalUUID, + GridID: strconv.FormatInt(componentInfo.GridID, 10), + ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10), + StationID: strconv.FormatInt(componentInfo.StationID, 10), + Tag: componentInfo.Tag, + Name: componentInfo.Name, + Context: componentInfo.Context, + Op: componentInfo.Op, + Ts: time.Now(), } result := tx.WithContext(cancelCtx).Create(&component) @@ -44,7 +43,7 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo if result.RowsAffected == 0 { err = fmt.Errorf("%w:please check insert component slice", errcode.ErrInsertRowUnexpected) } - return -1, fmt.Errorf("insert component info failed:%w", err) + return "", fmt.Errorf("insert component info failed:%w", err) } - return component.ID, nil + return component.GlobalUUID.String(), nil } diff --git a/database/query_component.go b/database/query_component.go index 785b8c6..dca464a 100644 --- a/database/query_component.go +++ b/database/query_component.go @@ -5,42 +5,37 @@ import ( "context" "time" - "modelRT/config" - "modelRT/logger" "modelRT/orm" "github.com/gofrs/uuid" - "github.com/panjf2000/ants/v2" "gorm.io/gorm" "gorm.io/gorm/clause" ) // QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB -func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc) (map[uuid.UUID]int, error) { - var components []orm.Component - // ctx超时判断 - cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) - defer cancel() +// func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc) (map[uuid.UUID]string, error) { +// var components []orm.Component +// // ctx超时判断 +// cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) +// defer cancel() - result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components) - if result.Error != nil { - logger.Error(ctx, "query circuit diagram component info failed", "error", result.Error) - return nil, result.Error - } +// result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components) +// if result.Error != nil { +// logger.Error(ctx, "query circuit diagram component info failed", "error", result.Error) +// return nil, result.Error +// } - // TODO 优化componentTypeMap输出 - componentTypeMap := make(map[uuid.UUID]int, len(components)) +// componentTypeMap := make(map[uuid.UUID]string, len(components)) +// for _, component := range components { +// pool.Invoke(config.ModelParseConfig{ +// ComponentInfo: component, +// Ctx: ctx, +// }) - for _, component := range components { - pool.Invoke(config.ModelParseConfig{ - ComponentInfo: component, - Ctx: ctx, - }) - - componentTypeMap[component.GlobalUUID] = component.ComponentType - } - return componentTypeMap, nil -} +// componentTypeMap[component.GlobalUUID] = component.GlobalUUID.String() +// } +// return componentTypeMap, nil +// } // QueryComponentByUUID return the result of query circuit diagram component info by uuid from postgresDB func QueryComponentByUUID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm.Component, error) { diff --git a/database/query_topologic.go b/database/query_topologic.go index 35bb92d..eea21b1 100644 --- a/database/query_topologic.go +++ b/database/query_topologic.go @@ -33,14 +33,14 @@ func QueryTopologic(ctx context.Context, tx *gorm.DB) ([]orm.Topologic, error) { } // QueryTopologicFromDB return the result of query topologic info from DB -func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, componentTypeMap map[uuid.UUID]int) (*diagram.MultiBranchTreeNode, error) { +func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB) (*diagram.MultiBranchTreeNode, error) { topologicInfos, err := QueryTopologic(ctx, tx) if err != nil { logger.Error(ctx, "query topologic info failed", "error", err) return nil, err } - tree, err := BuildMultiBranchTree(topologicInfos, componentTypeMap) + tree, err := BuildMultiBranchTree(topologicInfos) if err != nil { logger.Error(ctx, "init topologic failed", "error", err) return nil, err @@ -49,17 +49,11 @@ func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, componentTypeMap map } // InitCircuitDiagramTopologic return circuit diagram topologic info from postgres -func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMap map[uuid.UUID]int) error { +func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic) error { var rootVertex *diagram.MultiBranchTreeNode for _, node := range topologicNodes { if node.UUIDFrom == constants.UUIDNil { - // rootVertex = node.UUIDTo - var componentType int - componentType, ok := componentTypeMap[node.UUIDFrom] - if !ok { - return fmt.Errorf("can not get component type by uuid: %s", node.UUIDFrom) - } - rootVertex = diagram.NewMultiBranchTree(node.UUIDFrom, componentType) + rootVertex = diagram.NewMultiBranchTree(node.UUIDFrom) break } } @@ -70,13 +64,7 @@ func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMa for _, node := range topologicNodes { if node.UUIDFrom == constants.UUIDNil { - var componentType int - componentType, ok := componentTypeMap[node.UUIDTo] - if !ok { - return fmt.Errorf("can not get component type by uuid: %s", node.UUIDTo) - } - nodeVertex := diagram.NewMultiBranchTree(node.UUIDTo, componentType) - + nodeVertex := diagram.NewMultiBranchTree(node.UUIDTo) rootVertex.AddChild(nodeVertex) } } @@ -91,9 +79,7 @@ func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMa // TODO 电流互感器不单独划分间隔,以母线、浇筑母线、变压器为间隔原件 func IntervalBoundaryDetermine(uuid uuid.UUID) bool { - fmt.Println(uuid) - var componentID int64 - diagram.GetComponentMap(componentID) + diagram.GetComponentMap(uuid.String()) // TODO 判断 component 的类型是否为间隔 // TODO 0xA1B2C3D4,高四位表示可以成为间隔的compoent类型的值为FFFF,普通 component 类型的值为 0000。低四位中前二位表示component的一级类型,例如母线 PT、母联/母分、进线等,低四位中后二位表示一级类型中包含的具体类型,例如母线 PT中包含的电压互感器、隔离开关、接地开关、避雷器、带电显示器等。 num := uint32(0xA1B2C3D4) // 八位16进制数 @@ -104,23 +90,16 @@ func IntervalBoundaryDetermine(uuid uuid.UUID) bool { } // BuildMultiBranchTree return the multi branch tree by topologic info and component type map -func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.UUID]int) (*diagram.MultiBranchTreeNode, error) { +func BuildMultiBranchTree(topologics []orm.Topologic) (*diagram.MultiBranchTreeNode, error) { nodeMap := make(map[uuid.UUID]*diagram.MultiBranchTreeNode, len(topologics)*2) for _, topo := range topologics { if _, exists := nodeMap[topo.UUIDFrom]; !exists { // skip special uuid if topo.UUIDTo != constants.UUIDNil { - var ok bool - componentType, ok := componentTypeMap[topo.UUIDFrom] - if !ok { - return nil, fmt.Errorf("can not get component type by uuid: %s", topo.UUIDFrom) - } - nodeMap[topo.UUIDFrom] = &diagram.MultiBranchTreeNode{ - ID: topo.UUIDFrom, - NodeComponentType: componentType, - Children: make([]*diagram.MultiBranchTreeNode, 0), + ID: topo.UUIDFrom, + Children: make([]*diagram.MultiBranchTreeNode, 0), } } } @@ -128,16 +107,9 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid. if _, exists := nodeMap[topo.UUIDTo]; !exists { // skip special uuid if topo.UUIDTo != constants.UUIDNil { - var ok bool - componentType, ok := componentTypeMap[topo.UUIDTo] - if !ok { - return nil, fmt.Errorf("can not get component type by uuid: %s", topo.UUIDTo) - } - nodeMap[topo.UUIDTo] = &diagram.MultiBranchTreeNode{ - ID: topo.UUIDTo, - NodeComponentType: componentType, - Children: make([]*diagram.MultiBranchTreeNode, 0), + ID: topo.UUIDTo, + Children: make([]*diagram.MultiBranchTreeNode, 0), } } } @@ -146,10 +118,8 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid. for _, topo := range topologics { var parent *diagram.MultiBranchTreeNode if topo.UUIDFrom == constants.UUIDNil { - var componentType int parent = &diagram.MultiBranchTreeNode{ - ID: constants.UUIDNil, - NodeComponentType: componentType, + ID: constants.UUIDNil, } nodeMap[constants.UUIDNil] = parent } else { @@ -158,10 +128,8 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid. var child *diagram.MultiBranchTreeNode if topo.UUIDTo == constants.UUIDNil { - var componentType int child = &diagram.MultiBranchTreeNode{ - ID: topo.UUIDTo, - NodeComponentType: componentType, + ID: topo.UUIDTo, } } else { child = nodeMap[topo.UUIDTo] diff --git a/database/update_component.go b/database/update_component.go index 3ee38d6..3c7ed78 100644 --- a/database/update_component.go +++ b/database/update_component.go @@ -16,13 +16,13 @@ import ( ) // UpdateComponentIntoDB define update component info of the circuit diagram into DB -func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentUpdateInfo) (int64, error) { +func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentUpdateInfo) (string, error) { cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() globalUUID, err := uuid.FromString(componentInfo.UUID) if err != nil { - return -1, fmt.Errorf("format uuid from string type failed:%w", err) + return "", fmt.Errorf("format uuid from string type failed:%w", err) } var component orm.Component @@ -32,30 +32,29 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo if result.RowsAffected == 0 { err = fmt.Errorf("%w:please check update component conditions", errcode.ErrUpdateRowZero) } - return -1, fmt.Errorf("query component info failed:%w", err) + return "", fmt.Errorf("query component info failed:%w", err) } updateParams := orm.Component{ - GlobalUUID: globalUUID, - GridID: strconv.FormatInt(componentInfo.GridID, 10), - ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10), - StationID: strconv.FormatInt(componentInfo.StationID, 10), - Tag: componentInfo.Tag, - ComponentType: componentInfo.ComponentType, - Name: componentInfo.Name, - Context: componentInfo.Context, - Op: componentInfo.Op, - Ts: time.Now(), + GlobalUUID: globalUUID, + GridID: strconv.FormatInt(componentInfo.GridID, 10), + ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10), + StationID: strconv.FormatInt(componentInfo.StationID, 10), + Tag: componentInfo.Tag, + Name: componentInfo.Name, + Context: componentInfo.Context, + Op: componentInfo.Op, + Ts: time.Now(), } - result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("id = ?", component.ID).Updates(&updateParams) + result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("GLOBAL_UUID = ?", component.GlobalUUID).Updates(&updateParams) if result.Error != nil || result.RowsAffected == 0 { err := result.Error if result.RowsAffected == 0 { err = fmt.Errorf("%w:please check update component conditions", errcode.ErrUpdateRowZero) } - return -1, fmt.Errorf("update component info failed:%w", err) + return "", fmt.Errorf("update component info failed:%w", err) } - return component.ID, nil + return component.GlobalUUID.String(), nil } diff --git a/diagram/anchor_set.go b/diagram/anchor_set.go index 6b52b23..94a83f4 100644 --- a/diagram/anchor_set.go +++ b/diagram/anchor_set.go @@ -10,10 +10,10 @@ import ( var anchorValueOverview sync.Map // GetAnchorValue define func of get circuit diagram data by componentID -func GetAnchorValue(componentID int64) (string, error) { - value, ok := diagramsOverview.Load(componentID) +func GetAnchorValue(componentUUID string) (string, error) { + value, ok := diagramsOverview.Load(componentUUID) if !ok { - return "", fmt.Errorf("can not find anchor value by componentID:%d", componentID) + return "", fmt.Errorf("can not find anchor value by componentUUID:%s", componentUUID) } anchorValue, ok := value.(string) if !ok { @@ -22,20 +22,20 @@ func GetAnchorValue(componentID int64) (string, error) { return anchorValue, nil } -// UpdateAnchorValue define func of update anchor value by componentID and anchor name -func UpdateAnchorValue(componentID int64, anchorValue string) bool { - _, result := anchorValueOverview.Swap(componentID, anchorValue) +// UpdateAnchorValue define func of update anchor value by componentUUID and anchor name +func UpdateAnchorValue(componentUUID string, anchorValue string) bool { + _, result := anchorValueOverview.Swap(componentUUID, anchorValue) return result } -// StoreAnchorValue define func of store anchor value with componentID and anchor name -func StoreAnchorValue(componentID int64, anchorValue string) { - anchorValueOverview.Store(componentID, anchorValue) +// StoreAnchorValue define func of store anchor value with componentUUID and anchor name +func StoreAnchorValue(componentUUID string, anchorValue string) { + anchorValueOverview.Store(componentUUID, anchorValue) return } -// DeleteAnchorValue define func of delete anchor value with componentID -func DeleteAnchorValue(componentID int64) { - anchorValueOverview.Delete(componentID) +// DeleteAnchorValue define func of delete anchor value with componentUUID +func DeleteAnchorValue(componentUUID string) { + anchorValueOverview.Delete(componentUUID) return } diff --git a/diagram/component_set.go b/diagram/component_set.go index adf497f..7a7b6c5 100644 --- a/diagram/component_set.go +++ b/diagram/component_set.go @@ -4,38 +4,40 @@ import ( "errors" "fmt" "sync" + + "modelRT/orm" ) // diagramsOverview define struct of storage all circuit diagram data var diagramsOverview sync.Map -// GetComponentMap define func of get circuit diagram data by component id -func GetComponentMap(componentID int64) (map[string]interface{}, error) { - value, ok := diagramsOverview.Load(componentID) +// GetComponentMap define func of get circuit diagram data by component uuid +func GetComponentMap(componentUUID string) (*orm.Component, error) { + value, ok := diagramsOverview.Load(componentUUID) if !ok { - return nil, fmt.Errorf("can not find graph by global uuid:%d", componentID) + return nil, fmt.Errorf("can not find graph by global uuid:%s", componentUUID) } - paramsMap, ok := value.(map[string]interface{}) + componentInfo, ok := value.(*orm.Component) if !ok { return nil, errors.New("convert to component map struct failed") } - return paramsMap, nil + return componentInfo, nil } -// UpdateComponentMap define func of update circuit diagram data by component id and component info -func UpdateComponentMap(componentID int64, componentInfo map[string]interface{}) bool { +// UpdateComponentMap define func of update circuit diagram data by component uuid and component info +func UpdateComponentMap(componentID int64, componentInfo *orm.Component) bool { _, result := diagramsOverview.Swap(componentID, componentInfo) return result } -// StoreComponentMap define func of store circuit diagram data with component id and component info -func StoreComponentMap(componentID int64, componentInfo map[string]interface{}) { - diagramsOverview.Store(componentID, componentInfo) +// StoreComponentMap define func of store circuit diagram data with component uuid and component info +func StoreComponentMap(componentUUID string, componentInfo *orm.Component) { + diagramsOverview.Store(componentUUID, componentInfo) return } -// DeleteComponentMap define func of delete circuit diagram data with component id -func DeleteComponentMap(componentID int64) { - diagramsOverview.Delete(componentID) +// DeleteComponentMap define func of delete circuit diagram data with component uuid +func DeleteComponentMap(componentUUID string) { + diagramsOverview.Delete(componentUUID) return } diff --git a/diagram/multi_branch_tree.go b/diagram/multi_branch_tree.go index 04337e3..ceb6cfa 100644 --- a/diagram/multi_branch_tree.go +++ b/diagram/multi_branch_tree.go @@ -10,17 +10,15 @@ var GlobalTree *MultiBranchTreeNode // MultiBranchTreeNode represents a topological structure using an multi branch tree type MultiBranchTreeNode struct { - ID uuid.UUID // 节点唯一标识 - NodeComponentType int // 节点组件类型 - Parent *MultiBranchTreeNode // 指向父节点的指针 - Children []*MultiBranchTreeNode // 指向所有子节点的指针切片 + ID uuid.UUID // 节点唯一标识 + Parent *MultiBranchTreeNode // 指向父节点的指针 + Children []*MultiBranchTreeNode // 指向所有子节点的指针切片 } -func NewMultiBranchTree(id uuid.UUID, componentType int) *MultiBranchTreeNode { +func NewMultiBranchTree(id uuid.UUID) *MultiBranchTreeNode { return &MultiBranchTreeNode{ - ID: id, - NodeComponentType: componentType, - Children: make([]*MultiBranchTreeNode, 0), + ID: id, + Children: make([]*MultiBranchTreeNode, 0), } } @@ -58,7 +56,7 @@ func (n *MultiBranchTreeNode) PrintTree(level int) { fmt.Print(" ") } - fmt.Printf("- ComponentType:%d,(ID: %s)\n", n.NodeComponentType, n.ID) + fmt.Printf("-ID: %s\n", n.ID) for _, child := range n.Children { child.PrintTree(level + 1) diff --git a/handler/anchor_point_replace.go b/handler/anchor_point_replace.go index 7fd4e94..272a697 100644 --- a/handler/anchor_point_replace.go +++ b/handler/anchor_point_replace.go @@ -8,11 +8,9 @@ import ( "time" "modelRT/common/errcode" - "modelRT/constants" "modelRT/database" "modelRT/diagram" "modelRT/logger" - "modelRT/model" "modelRT/network" "modelRT/orm" @@ -65,40 +63,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) { c.JSON(http.StatusOK, resp) return } - - cancelCtx, cancel = context.WithTimeout(c, 5*time.Second) - defer cancel() - unmarshalMap := make(map[string]interface{}) - tableName := model.SelectModelNameByType(componentInfo.ComponentType) - result = pgClient.WithContext(cancelCtx).Table(tableName).Where("global_uuid = ?", uuid).Find(&unmarshalMap) - if result.Error != nil { - logger.Error(c, "query model detail info failed", "error", result.Error) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: result.Error.Error(), - } - c.JSON(http.StatusOK, resp) - return - } - - if unmarshalMap == nil { - err := fmt.Errorf("query model detail info by uuid failed:%w", errcode.ErrQueryRowZero) - logger.Error(c, "query model detail info from table is empty", "table_name", tableName) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - } - c.JSON(http.StatusOK, resp) - return - } - - componentType := unmarshalMap["component_type"].(int) - if componentType != constants.DemoType { - logger.Error(c, "can not process real time data of component type not equal DemoType", "component_id", componentInfo.ID) - } - diagram.UpdateAnchorValue(componentInfo.ID, anchorName) + diagram.UpdateAnchorValue(componentInfo.GlobalUUID.String(), anchorName) resp := network.SuccessResponse{ Code: http.StatusOK, diff --git a/handler/circuit_diagram_create.go b/handler/circuit_diagram_create.go index d66968f..3563ae0 100644 --- a/handler/circuit_diagram_create.go +++ b/handler/circuit_diagram_create.go @@ -9,8 +9,8 @@ import ( "modelRT/diagram" "modelRT/logger" "modelRT/network" + "modelRT/orm" - "github.com/bitly/go-simplejson" "github.com/gin-gonic/gin" "github.com/gofrs/uuid" ) @@ -102,8 +102,8 @@ func CircuitDiagramCreateHandler(c *gin.Context) { graph.AddEdge(topologicCreateInfo.UUIDFrom, topologicCreateInfo.UUIDTo) } - for index, componentInfo := range request.ComponentInfos { - componentID, err := database.CreateComponentIntoDB(c, tx, componentInfo) + for index, info := range request.ComponentInfos { + componentUUID, err := database.CreateComponentIntoDB(c, tx, info) if err != nil { tx.Rollback() @@ -119,64 +119,48 @@ func CircuitDiagramCreateHandler(c *gin.Context) { c.JSON(http.StatusOK, resp) return } - request.ComponentInfos[index].ID = componentID - - err = database.CreateModelIntoDB(c, tx, componentID, componentInfo.ComponentType, componentInfo.Params) - if err != nil { - tx.Rollback() - - logger.Error(c, "create component model into DB failed", "component_infos", request.ComponentInfos, "error", err) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - PayLoad: map[string]interface{}{ - "uuid": request.PageID, - "component_infos": request.ComponentInfos, - }, - } - c.JSON(http.StatusOK, resp) - return - } + request.ComponentInfos[index].UUID = componentUUID } - for _, componentInfo := range request.ComponentInfos { - paramsJSON, err := simplejson.NewJson([]byte(componentInfo.Params)) - if err != nil { - tx.Rollback() - - logger.Error(c, "unmarshal component params info failed", "component_params", componentInfo.Params, "error", err) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - PayLoad: map[string]interface{}{ - "uuid": componentInfo.UUID, - "component_params": componentInfo.Params, - }, - } - c.JSON(http.StatusOK, resp) - return + for _, info := range request.ComponentInfos { + component := &orm.Component{ + GlobalUUID: uuid.FromStringOrNil(info.UUID), + // NsPath + // Tag string `gorm:"column:TAG"` + Name: info.Name, + // ModelName string `gorm:"column:MODEL_NAME"` + // Description: info.Description, + // GridID: info.GridID, + // ZoneID: info.ZoneID, + // StationID: info.StationID, + // Type int `gorm:"column:TYPE"` + // InService bool `gorm:"column:IN_SERVICE"` + // State int `gorm:"column:STATE"` + // Status int `gorm:"column:STATUS"` + // Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"` + // Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"` + // Context string `gorm:"column:CONTEXT"` + // Op: info.Op, + // Ts: info.Ts, } + // paramsJSON, err := simplejson.NewJson([]byte(info.Params)) + // if err != nil { + // tx.Rollback() - componentMap, err := paramsJSON.Map() - if err != nil { - tx.Rollback() + // logger.Error(c, "unmarshal component params info failed", "component_params", info.Params, "error", err) - logger.Error(c, "format params json info to map failed", "error", err) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - PayLoad: map[string]interface{}{ - "uuid": componentInfo.UUID, - "component_params": componentInfo.Params, - }, - } - c.JSON(http.StatusOK, resp) - return - } - diagram.StoreComponentMap(componentInfo.ID, componentMap) + // resp := network.FailureResponse{ + // Code: http.StatusBadRequest, + // Msg: err.Error(), + // PayLoad: map[string]interface{}{ + // "uuid": info.UUID, + // "component_params": info.Params, + // }, + // } + // c.JSON(http.StatusOK, resp) + // return + // } + diagram.StoreComponentMap(info.UUID, component) } if len(request.FreeVertexs) > 0 { diff --git a/handler/circuit_diagram_delete.go b/handler/circuit_diagram_delete.go index 73c3151..aa60f15 100644 --- a/handler/circuit_diagram_delete.go +++ b/handler/circuit_diagram_delete.go @@ -11,7 +11,6 @@ import ( "modelRT/database" "modelRT/diagram" "modelRT/logger" - "modelRT/model" "modelRT/network" "modelRT/orm" @@ -192,32 +191,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) { c.JSON(http.StatusOK, resp) return } - - modelStruct := model.SelectModelByType(component.ComponentType) - modelStruct.SetComponentID(component.ID) - result = tx.WithContext(cancelCtx).Where("component_id = ?", component.ID).Delete(modelStruct) - if result.Error != nil || result.RowsAffected == 0 { - tx.Rollback() - - err := result.Error - if result.RowsAffected == 0 { - err = fmt.Errorf("%w:please check uuid conditions", errcode.ErrDeleteRowZero) - } - - msg := fmt.Sprintf("delete component info from table %s failed", modelStruct.ReturnTableName()) - logger.Error(c, msg, "component_global_uuid", componentInfo.UUID, "error", err) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - PayLoad: map[string]interface{}{ - "uuid": componentInfo.UUID, - }, - } - c.JSON(http.StatusOK, resp) - return - } - diagram.DeleteComponentMap(component.ID) + diagram.DeleteComponentMap(component.GlobalUUID.String()) } if len(request.FreeVertexs) > 0 { diff --git a/handler/circuit_diagram_load.go b/handler/circuit_diagram_load.go index 763acbd..932d792 100644 --- a/handler/circuit_diagram_load.go +++ b/handler/circuit_diagram_load.go @@ -77,7 +77,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) { return } - componentParams, err := diagram.GetComponentMap(component.ID) + componentParams, err := diagram.GetComponentMap(component.GlobalUUID.String()) if err != nil { logger.Error(c, "get component data from set by uuid failed", "error", err) @@ -111,7 +111,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) { return } - rootComponentParam, err := diagram.GetComponentMap(rootComponent.ID) + rootComponentParam, err := diagram.GetComponentMap(rootComponent.GlobalUUID.String()) if err != nil { logger.Error(c, "get component data from set by uuid failed", "error", err) diff --git a/handler/circuit_diagram_update.go b/handler/circuit_diagram_update.go index 2ab945f..7ef69f4 100644 --- a/handler/circuit_diagram_update.go +++ b/handler/circuit_diagram_update.go @@ -8,9 +8,10 @@ import ( "modelRT/diagram" "modelRT/logger" "modelRT/network" + "modelRT/orm" - "github.com/bitly/go-simplejson" "github.com/gin-gonic/gin" + "github.com/gofrs/uuid" ) // CircuitDiagramUpdateHandler define circuit diagram update process API @@ -103,7 +104,7 @@ func CircuitDiagramUpdateHandler(c *gin.Context) { } for index, componentInfo := range request.ComponentInfos { - componentID, err := database.UpdateComponentIntoDB(c, tx, componentInfo) + componentUUID, err := database.UpdateComponentIntoDB(c, tx, componentInfo) if err != nil { logger.Error(c, "udpate component info into DB failed", "error", err) @@ -118,59 +119,47 @@ func CircuitDiagramUpdateHandler(c *gin.Context) { c.JSON(http.StatusOK, resp) return } - - request.ComponentInfos[index].ID = componentID - - err = database.UpdateModelIntoDB(c, tx, componentID, componentInfo.ComponentType, componentInfo.Params) - if err != nil { - logger.Error(c, "udpate component model info into DB failed", "error", err) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - PayLoad: map[string]interface{}{ - "page_id": request.PageID, - "component_info": request.ComponentInfos, - }, - } - c.JSON(http.StatusOK, resp) - return - } + request.ComponentInfos[index].UUID = componentUUID } - for _, componentInfo := range request.ComponentInfos { - paramsJSON, err := simplejson.NewJson([]byte(componentInfo.Params)) - if err != nil { - logger.Error(c, "unmarshal component info by concurrent map failed", "component_params", componentInfo.Params, "error", err) - - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - PayLoad: map[string]interface{}{ - "uuid": componentInfo.UUID, - "component_params": componentInfo.Params, - }, - } - c.JSON(http.StatusOK, resp) - return + for _, info := range request.ComponentInfos { + component := &orm.Component{ + GlobalUUID: uuid.FromStringOrNil(info.UUID), + // NsPath + // Tag string `gorm:"column:TAG"` + Name: info.Name, + // ModelName string `gorm:"column:MODEL_NAME"` + // Description: info.Description, + // GridID: info.GridID, + // ZoneID: info.ZoneID, + // StationID: info.StationID, + // Type int `gorm:"column:TYPE"` + // InService bool `gorm:"column:IN_SERVICE"` + // State int `gorm:"column:STATE"` + // Status int `gorm:"column:STATUS"` + // Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"` + // Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"` + // Context string `gorm:"column:CONTEXT"` + Op: info.Op, + // Ts: info.Ts, } - componentMap, err := paramsJSON.Map() - if err != nil { - logger.Error(c, "format params json info to map failed", "error", err) + // paramsJSON, err := simplejson.NewJson([]byte(info.Params)) + // if err != nil { + // logger.Error(c, "unmarshal component info by concurrent map failed", "component_params", info.Params, "error", err) - resp := network.FailureResponse{ - Code: http.StatusBadRequest, - Msg: err.Error(), - PayLoad: map[string]interface{}{ - "uuid": componentInfo.UUID, - "component_params": componentInfo.Params, - }, - } - c.JSON(http.StatusOK, resp) - return - } - diagram.UpdateComponentMap(componentInfo.ID, componentMap) + // resp := network.FailureResponse{ + // Code: http.StatusBadRequest, + // Msg: err.Error(), + // PayLoad: map[string]interface{}{ + // "uuid": info.UUID, + // "component_params": info.Params, + // }, + // } + // c.JSON(http.StatusOK, resp) + // return + // } + diagram.UpdateComponentMap(info.ID, component) } if len(request.FreeVertexs) > 0 { diff --git a/handler/real_time_data_receive.go b/handler/real_time_data_receive.go index b2412fb..77ab71f 100644 --- a/handler/real_time_data_receive.go +++ b/handler/real_time_data_receive.go @@ -65,8 +65,8 @@ func RealTimeDataReceivehandler(c *gin.Context) { realtimedata.RealTimeDataChan <- request payload := map[string]interface{}{ - "component_id": request.PayLoad.ComponentID, - "point": request.PayLoad.Point, + "component_uuid": request.PayLoad.ComponentUUID, + "point": request.PayLoad.Point, } respByte := processResponse(0, "success", payload) if len(respByte) == 0 { diff --git a/main.go b/main.go index c2c890c..4bd7b4a 100644 --- a/main.go +++ b/main.go @@ -102,14 +102,14 @@ func main() { postgresDBClient.Transaction(func(tx *gorm.DB) error { // load circuit diagram from postgres - componentTypeMap, err := database.QueryCircuitDiagramComponentFromDB(cancelCtx, tx, parsePool) - if err != nil { - logger.Error(ctx, "load circuit diagrams from postgres failed", "error", err) - panic(err) - } + // componentTypeMap, err := database.QueryCircuitDiagramComponentFromDB(cancelCtx, tx, parsePool) + // if err != nil { + // logger.Error(ctx, "load circuit diagrams from postgres failed", "error", err) + // panic(err) + // } // TODO 暂时屏蔽完成 swagger 启动测试 - tree, err := database.QueryTopologicFromDB(ctx, tx, componentTypeMap) + tree, err := database.QueryTopologicFromDB(ctx, tx) if err != nil { logger.Error(ctx, "load topologic info from postgres failed", "error", err) panic(err) diff --git a/network/circuit_diagram_create_request.go b/network/circuit_diagram_create_request.go index fdbabb4..aa2fbfa 100644 --- a/network/circuit_diagram_create_request.go +++ b/network/circuit_diagram_create_request.go @@ -21,18 +21,16 @@ type TopologicUUIDCreateInfo struct { // ComponentCreateInfo defines circuit diagram component create index info type ComponentCreateInfo struct { - ID int64 `json:"id"` - UUID string `json:"uuid"` - Name string `json:"name"` - Context string `json:"context"` - GridID int64 `json:"grid_id"` - ZoneID int64 `json:"zone_id"` - StationID int64 `json:"station_id"` - PageID int64 `json:"page_id"` - Tag string `json:"tag"` - ComponentType int `json:"component_type"` - Params string `json:"params"` - Op int `json:"op"` + UUID string `json:"uuid"` + Name string `json:"name"` + Context string `json:"context"` + GridID int64 `json:"grid_id"` + ZoneID int64 `json:"zone_id"` + StationID int64 `json:"station_id"` + PageID int64 `json:"page_id"` + Tag string `json:"tag"` + Params string `json:"params"` + Op int `json:"op"` } // CircuitDiagramCreateRequest defines request params of circuit diagram create api diff --git a/network/circuit_diagram_update_request.go b/network/circuit_diagram_update_request.go index bf58501..abc1cc6 100644 --- a/network/circuit_diagram_update_request.go +++ b/network/circuit_diagram_update_request.go @@ -34,17 +34,16 @@ type TopologicUUIDChangeInfos struct { // ComponentUpdateInfo defines circuit diagram component params info type ComponentUpdateInfo struct { - ID int64 `json:"id"` - UUID string `json:"uuid"` - Name string `json:"name"` - Context string `json:"context"` - GridID int64 `json:"grid_id"` - ZoneID int64 `json:"zone_id"` - StationID int64 `json:"station_id"` - ComponentType int `json:"component_type"` - Params string `json:"params"` - Op int `json:"op"` - Tag string `json:"tag"` + ID int64 `json:"id"` + UUID string `json:"uuid"` + Name string `json:"name"` + Context string `json:"context"` + GridID int64 `json:"grid_id"` + ZoneID int64 `json:"zone_id"` + StationID int64 `json:"station_id"` + Params string `json:"params"` + Op int `json:"op"` + Tag string `json:"tag"` } // CircuitDiagramUpdateRequest defines request params of circuit diagram update api diff --git a/network/real_time_data_request.go b/network/real_time_data_request.go index 0a8c63a..497ac1b 100644 --- a/network/real_time_data_request.go +++ b/network/real_time_data_request.go @@ -8,9 +8,9 @@ type RealTimeDataReceiveRequest struct { // RealTimeDataReceivePayload defines request payload of real time data receive api type RealTimeDataReceivePayload struct { - ComponentID int64 `json:"component_id"` - Point string `json:"point"` - Values []RealTimeDataReceiveParam `json:"values"` + ComponentUUID string `json:"component_uuid"` + Point string `json:"point"` + Values []RealTimeDataReceiveParam `json:"values"` } // RealTimeDataReceiveParam defines request param of real time data receive api diff --git a/orm/circuit_diagram_bay.go b/orm/circuit_diagram_bay.go index 1ec1f13..f9e22be 100644 --- a/orm/circuit_diagram_bay.go +++ b/orm/circuit_diagram_bay.go @@ -1,3 +1,4 @@ +// Package orm define database data struct package orm import ( diff --git a/orm/circuit_diagram_component.go b/orm/circuit_diagram_component.go index a2c67ee..2d72322 100644 --- a/orm/circuit_diagram_component.go +++ b/orm/circuit_diagram_component.go @@ -27,10 +27,6 @@ type Component struct { Context string `gorm:"column:CONTEXT"` Op int `gorm:"column:OP"` Ts time.Time `gorm:"column:TS"` - - // TODO 解决删除ID、ComponentType 后的报错 - ID int64 `gorm:"column:id;primaryKey"` - ComponentType int `gorm:"column:TYPE"` } // TableName func respresent return table name of Component diff --git a/pool/component_chan_set.go b/pool/component_chan_set.go index d615f4f..f61d7a2 100644 --- a/pool/component_chan_set.go +++ b/pool/component_chan_set.go @@ -2,6 +2,7 @@ package pool import ( "context" + "fmt" "sync" "modelRT/config" @@ -13,7 +14,10 @@ func init() { } } -var globalComponentChanSet *ComponentChanSet +var ( + globalComponentChanMap sync.Map + globalComponentChanSet *ComponentChanSet +) // ComponentChanSet defines component anchor real time data process channel set type ComponentChanSet struct { @@ -21,36 +25,21 @@ type ComponentChanSet struct { AnchorChans []chan config.AnchorParamConfig } -func GetComponentChan(ctx context.Context, componentID int64) chan config.AnchorParamConfig { - globalComponentChanSet.RLock() - componentChan := globalComponentChanSet.AnchorChans[componentID] - if componentChan == nil { - globalComponentChanSet.RUnlock() - globalComponentChanSet.Lock() - defer globalComponentChanSet.Unlock() - return CreateNewComponentChan(ctx, componentID) +func GetAnchorParamChan(ctx context.Context, componentUUID string) (chan config.AnchorParamConfig, error) { + paramChan, ok := globalComponentChanMap.Load(componentUUID) + if !ok { + return CreateNewAnchorParamChan(ctx, componentUUID), nil } - globalComponentChanSet.RUnlock() - return componentChan + + anchorParamChan, ok := paramChan.(chan config.AnchorParamConfig) + if !ok { + return nil, fmt.Errorf("conversion component anchor param chan type failed for componentUUID: %s", componentUUID) + } + return anchorParamChan, nil } -func CreateNewComponentChan(ctx context.Context, componentID int64) chan config.AnchorParamConfig { - componentChan := globalComponentChanSet.AnchorChans[componentID] - if componentChan == nil { - componentChan = make(chan config.AnchorParamConfig, 100) - globalComponentChanSet.AnchorChans[componentID] = componentChan - - readyChan := make(chan struct{}) - chanConfig := config.AnchorChanConfig{ - Ctx: ctx, - AnchorChan: componentChan, - ReadyChan: readyChan, - } - - AnchorRealTimePool.Invoke(chanConfig) - - <-readyChan - return componentChan - } - return componentChan +func CreateNewAnchorParamChan(ctx context.Context, componentUUID string) chan config.AnchorParamConfig { + anchorParamChan := make(chan config.AnchorParamConfig, 100) + globalComponentChanMap.Store(componentUUID, anchorParamChan) + return anchorParamChan } diff --git a/pool/concurrency_anchor_parse.go b/pool/concurrency_anchor_parse.go index a73bb0b..443f1a7 100644 --- a/pool/concurrency_anchor_parse.go +++ b/pool/concurrency_anchor_parse.go @@ -48,13 +48,13 @@ var AnchorFunc = func(poolConfig interface{}) { firstStart = false } - componentID := anchorParaConfig.ComponentID + componentUUID := anchorParaConfig.ComponentUUID anchorRealTimeDatas := anchorParaConfig.AnchorRealTimeData for _, value := range anchorRealTimeDatas { - anchorName, err := diagram.GetAnchorValue(componentID) + anchorName, err := diagram.GetAnchorValue(componentUUID) if err != nil { - logger.Error(anchorChanConfig.Ctx, "can not get anchor value from map by uuid", "component_id", componentID, "error", err) + logger.Error(anchorChanConfig.Ctx, "can not get anchor value from map by uuid", "component_uuid", componentUUID, "error", err) continue } @@ -70,11 +70,11 @@ var AnchorFunc = func(poolConfig interface{}) { message := fmt.Sprintf("anchor param %s value out of range, value:%f, upper limit:%f, lower limit:%f", anchorName, compareValue, upperLimitVal, lowerlimitVal) event := alert.Event{ - ComponentID: componentID, - AnchorName: anchorName, - Level: constants.InfoAlertLevel, - Message: message, - StartTime: time.Now().Unix(), + ComponentUUID: componentUUID, + AnchorName: anchorName, + Level: constants.InfoAlertLevel, + Message: message, + StartTime: time.Now().Unix(), } alertManager.AddEvent(event) } diff --git a/pool/concurrency_model_parse.go b/pool/concurrency_model_parse.go index fe2e356..1fe8718 100644 --- a/pool/concurrency_model_parse.go +++ b/pool/concurrency_model_parse.go @@ -9,7 +9,6 @@ import ( "modelRT/database" "modelRT/diagram" "modelRT/logger" - "modelRT/model" ) // ParseFunc defines func that parses the model data from postgres @@ -23,42 +22,16 @@ var ParseFunc = func(parseConfig interface{}) { cancelCtx, cancel := context.WithTimeout(modelParseConfig.Ctx, 5*time.Second) defer cancel() pgClient := database.GetPostgresDBClient() - - unmarshalMap := make(map[string]interface{}) - tableName := model.SelectModelNameByType(modelParseConfig.ComponentInfo.ComponentType) - - result := pgClient.WithContext(cancelCtx).Table(tableName).Where("component_id = ?", modelParseConfig.ComponentInfo.ID).Find(&unmarshalMap) - if result.Error != nil { - logger.Error(modelParseConfig.Ctx, "query component detail info failed", "error", result.Error) - return - } else if result.RowsAffected == 0 { - logger.Error(modelParseConfig.Ctx, "query component detail info from table is empty", "table_name", tableName) + component, err := database.QueryComponentByUUID(cancelCtx, pgClient, modelParseConfig.ComponentInfo.GlobalUUID) + if err != nil { + logger.Error(modelParseConfig.Ctx, "query component info failed", "error", err) return } - var anchorName string - if anchoringV := unmarshalMap["anchor_v"].(bool); anchoringV { - anchorName = "voltage" - } else { - anchorName = "current" - } - diagram.StoreAnchorValue(modelParseConfig.ComponentInfo.ID, anchorName) + diagram.StoreAnchorValue(modelParseConfig.ComponentInfo.GlobalUUID.String(), "") - GetComponentChan(modelParseConfig.Ctx, modelParseConfig.ComponentInfo.ID) + GetAnchorParamChan(modelParseConfig.Ctx, modelParseConfig.ComponentInfo.GlobalUUID.String()) - uuid := modelParseConfig.ComponentInfo.GlobalUUID.String() - unmarshalMap["id"] = modelParseConfig.ComponentInfo.ID - unmarshalMap["uuid"] = uuid - unmarshalMap["uuid"] = modelParseConfig.ComponentInfo.Tag - unmarshalMap["grid_id"] = modelParseConfig.ComponentInfo.GridID - unmarshalMap["zone_id"] = modelParseConfig.ComponentInfo.ZoneID - unmarshalMap["station_id"] = modelParseConfig.ComponentInfo.StationID - unmarshalMap["component_type"] = modelParseConfig.ComponentInfo.ComponentType - unmarshalMap["name"] = modelParseConfig.ComponentInfo.Name - unmarshalMap["context"] = modelParseConfig.ComponentInfo.Context - unmarshalMap["op"] = modelParseConfig.ComponentInfo.Op - unmarshalMap["ts"] = modelParseConfig.ComponentInfo.Ts - - diagram.StoreComponentMap(modelParseConfig.ComponentInfo.ID, unmarshalMap) + diagram.StoreComponentMap(modelParseConfig.ComponentInfo.GlobalUUID.String(), &component) return } diff --git a/real-time-data/real_time_data_receive.go b/real-time-data/real_time_data_receive.go index 205d63d..5d9f1f3 100644 --- a/real-time-data/real_time_data_receive.go +++ b/real-time-data/real_time_data_receive.go @@ -26,17 +26,16 @@ func ReceiveChan(ctx context.Context) { case <-ctx.Done(): return case realTimeData := <-RealTimeDataChan: - // TODO 根据 componentID 缓存到来的实时数据 - componentID := realTimeData.PayLoad.ComponentID - component, err := diagram.GetComponentMap(componentID) + componentUUID := realTimeData.PayLoad.ComponentUUID + component, err := diagram.GetComponentMap(componentUUID) if err != nil { - logger.Error(ctx, "query component info from diagram map by componet id failed", "component_id", componentID, "error", err) + logger.Error(ctx, "query component info from diagram map by componet id failed", "component_uuid", componentUUID, "error", err) continue } - componentType := component["component_type"].(int) + componentType := component.Type if componentType != constants.DemoType { - logger.Error(ctx, "can not process real time data of component type not equal DemoType", "component_id", componentID) + logger.Error(ctx, "can not process real time data of component type not equal DemoType", "component_uuid", componentUUID) continue } @@ -44,21 +43,8 @@ func ReceiveChan(ctx context.Context) { var compareValUpperLimit, compareValLowerLimit float64 var anchorRealTimeData []float64 var calculateFunc func(archorValue float64, args ...float64) float64 - if anchoringV := component["anchor_v"].(bool); anchoringV { - anchorName = "voltage" - compareValUpperLimit = component["uv_alarm"].(float64) - compareValLowerLimit = component["ov_alarm"].(float64) - } else { - anchorName = "current" - compareValUpperLimit = component["ui_alarm"].(float64) - compareValLowerLimit = component["oi_alarm"].(float64) - } - componentData := map[string]interface{}{ - "resistance": component["resistance"].(float64), - } - - calculateFunc, params := config.SelectAnchorCalculateFuncAndParams(componentType, anchorName, componentData) + // calculateFunc, params := config.SelectAnchorCalculateFuncAndParams(componentType, anchorName, componentData) for _, param := range realTimeData.PayLoad.Values { anchorRealTimeData = append(anchorRealTimeData, param.Value) @@ -66,16 +52,21 @@ func ReceiveChan(ctx context.Context) { anchorConfig := config.AnchorParamConfig{ AnchorParamBaseConfig: config.AnchorParamBaseConfig{ - ComponentID: componentID, + ComponentUUID: componentUUID, AnchorName: anchorName, CompareValUpperLimit: compareValUpperLimit, CompareValLowerLimit: compareValLowerLimit, AnchorRealTimeData: anchorRealTimeData, }, CalculateFunc: calculateFunc, - CalculateParams: params, + CalculateParams: []float64{}, } - pool.GetComponentChan(ctx, componentID) <- anchorConfig + anchorChan, err := pool.GetAnchorParamChan(ctx, componentUUID) + if err != nil { + logger.Error(ctx, "get anchor param chan failed", "component_uuid", componentUUID, "error", err) + continue + } + anchorChan <- anchorConfig default: } } diff --git a/sharememory/share_memeory.go b/sharememory/share_memeory.go index 6667cfe..8248064 100644 --- a/sharememory/share_memeory.go +++ b/sharememory/share_memeory.go @@ -63,7 +63,6 @@ func WriteComponentInShareMemory(key uintptr, componentInfo *orm.Component) erro obj := (*orm.Component)(unsafe.Pointer(shmAddr + unsafe.Sizeof(structSize))) fmt.Println(obj) - obj.ComponentType = componentInfo.ComponentType // id integer NOT NULL DEFAULT nextval('component_id_seq'::regclass), // global_uuid uuid NOT NULL DEFAULT gen_random_uuid(), diff --git a/test/handler/circuit_diagram_query_test.go b/test/handler/circuit_diagram_query_test.go new file mode 100644 index 0000000..880c2ba --- /dev/null +++ b/test/handler/circuit_diagram_query_test.go @@ -0,0 +1,178 @@ +package handler_test + +// TODO 使用 mock测试 handler + +// 一、安装 GoMock +// 首先需要安装 GoMock 和 mockgen 工具: + +// bash +// 复制 +// # 安装 GoMock 包 +// go get github.com/golang/mock/gomock + +// # 安装 mockgen 工具 +// go install github.com/golang/mock/mockgen@latest +// 二、基本使用步骤 +// 1. 定义接口 +// 假设我们有一个简单的接口: + +// go +// 复制 +// // greeter.go +// package main + +// type Greeter interface { +// Greet(name string) string +// } +// 2. 生成 mock 代码 +// 使用 mockgen 为接口生成 mock 实现: + +// bash +// 复制 +// mockgen -source=greeter.go -destination=mock_greeter.go -package=main +// 这会生成一个 mock_greeter.go 文件,包含 MockGreeter 结构体。 + +// 3. 编写测试代码 +// go +// 复制 +// // greeter_test.go +// package main + +// import ( +// "testing" + +// "github.com/golang/mock/gomock" +// ) + +// func TestGreet(t *testing.T) { +// // 创建控制器 +// ctrl := gomock.NewController(t) +// defer ctrl.Finish() // 断言所有期望都被满足 + +// // 创建 mock 对象 +// mockGreeter := NewMockGreeter(ctrl) + +// // 设置期望 +// mockGreeter.EXPECT(). +// Greet("Alice"). +// Return("Hello, Alice!"). +// Times(1) + +// // 使用 mock 对象 +// result := mockGreeter.Greet("Alice") + +// // 验证结果 +// if result != "Hello, Alice!" { +// t.Errorf("Expected 'Hello, Alice!', got '%s'", result) +// } +// } +// 三、高级功能 +// 1. 参数匹配 +// GoMock 提供了多种参数匹配方式: + +// go +// 复制 +// // 精确匹配 +// mockGreeter.EXPECT().Greet("Alice") + +// // 任意值匹配 +// mockGreeter.EXPECT().Greet(gomock.Any()) + +// // 自定义匹配 +// mockGreeter.EXPECT().Greet(gomock.AssignableToTypeOf("")).DoAndReturn( +// func(name string) string { +// return "Hello, " + name + "!" +// }, +// ) +// 2. 调用次数控制 +// go +// 复制 +// // 精确调用次数 +// mockGreeter.EXPECT().Greet("Alice").Times(3) + +// // 至少调用一次 +// mockGreeter.EXPECT().Greet("Alice").MinTimes(1) + +// // 最多调用一次 +// mockGreeter.EXPECT().Greet("Alice").MaxTimes(1) + +// // 任意次数(0或多次) +// mockGreeter.EXPECT().Greet("Alice").AnyTimes() +// 3. 调用顺序控制 +// go +// 复制 +// gomock.InOrder( +// mockGreeter.EXPECT().Greet("Alice"), +// mockGreeter.EXPECT().Greet("Bob"), +// ) +// 4. 模拟副作用 +// go +// 复制 +// // 使用 Do +// mockGreeter.EXPECT().Greet("Alice").Do(func(name string) { +// t.Logf("Greet was called with %s", name) +// }) + +// // 使用 DoAndReturn +// mockGreeter.EXPECT().Greet("Alice").DoAndReturn( +// func(name string) string { +// return "Hi, " + name + "!" +// }, +// ) +// 四、实际应用示例 +// 假设我们有一个服务依赖 Greeter 接口: + +// go +// 复制 +// // greeter_service.go +// package main + +// type GreeterService struct { +// greeter Greeter +// } + +// func (s *GreeterService) WelcomePeople(names []string) []string { +// var greetings []string +// for _, name := range names { +// greetings = append(greetings, s.greeter.Greet(name)) +// } +// return greetings +// } +// 对应的测试: + +// go +// 复制 +// // greeter_service_test.go +// package main + +// import ( +// "testing" + +// "github.com/golang/mock/gomock" +// ) + +// func TestWelcomePeople(t *testing.T) { +// ctrl := gomock.NewController(t) +// defer ctrl.Finish() + +// mockGreeter := NewMockGreeter(ctrl) +// service := &GreeterService{greeter: mockGreeter} + +// // 设置期望 +// mockGreeter.EXPECT().Greet("Alice").Return("Hello, Alice!") +// mockGreeter.EXPECT().Greet("Bob").Return("Hello, Bob!") + +// // 测试 +// result := service.WelcomePeople([]string{"Alice", "Bob"}) + +// // 验证 +// expected := []string{"Hello, Alice!", "Hello, Bob!"} +// if len(result) != len(expected) { +// t.Fatalf("Expected %d greetings, got %d", len(expected), len(result)) +// } +// for i := range expected { +// if result[i] != expected[i] { +// t.Errorf("At index %d: expected %q, got %q", i, expected[i], result[i]) +// } +// } +// }