Skip to content

Commit

Permalink
node write refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon-lz committed Apr 6, 2024
1 parent f3a5e6d commit e990cc7
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[opcua]
endpoint = "opc.tcp://opcserver:4840"
endpoint = "opc.tcp://localhost:4840"
interval = 500 # 读取间隔,单位 milliseconds

[openapi]
Expand Down
1 change: 0 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package db

import (
// "github.com/Brandon-lz/myopcua/db/gen/query"
"os"

"gorm.io/driver/postgres"
Expand Down
13 changes: 8 additions & 5 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const docTemplate = `{
"BearerAuth": []
}
],
"description": "WriteNodeValue 路由\n参数定义\n## 请求参数\n| 参数名称 | 类型 | 必填 | 描述 |\n| --- | --- | --- | --- |\n| data | list | 是 | 见下方JSON |\n## data类型 定义\n| 字段 | 类型 | 是否必填 | 描述 |\n| --- | --- | --- | --- |\n| NodeId | string | 是 | 节点ID |\n| Value | any | 是 | 写入值 |\n## 参数示例\n` + "`" + `` + "`" + `` + "`" + `json\n{\n\"data\": [\n{\n\"NodeId\": \"ns=2;s=MyVariable\",\n\"Value\": 123\n},\n{\n\"NodeId\": \"ns=2;s=MyVariable2\",\n\"Value\": \"abc\"\n}\n]\n}\n` + "`" + `` + "`" + `` + "`" + `\n## 返回值定义\n| 字段 | 类型 | 描述 |\n| --- | --- | --- |\n| Code | int | 状态码 |\n| Message | string | 状态信息 |\n## 返回值示例\n` + "`" + `` + "`" + `` + "`" + `json\n{\n\"Code\": 200,\n\"Message\": \"节点值写入完成\"\n}\n` + "`" + `` + "`" + `` + "`" + `",
"description": "WriteNodeValue 路由\n参数定义\n## 请求参数\n| 参数名称 | 类型 | 必填 | 描述 |\n| --- | --- | --- | --- |\n| data | list | 是 | 见下方JSON |\n## data类型 定义\n| 字段 | 类型 | 是否必填 | 描述 |\n| --- | --- | --- | --- |\n| NodeName | string | 是 | 节点名称 |\n| Value | any | 是 | 写入值 |\n## 参数示例\n` + "`" + `` + "`" + `` + "`" + `json\n{\n\"data\": [\n{\n\"NodeName\": \"MyVariable\",\n\"Value\": 123\n},\n{\n\"NodeName\": \"MyVariable2\",\n\"Value\": \"abc\"\n}\n]\n}\n` + "`" + `` + "`" + `` + "`" + `\n## 返回值定义\n| 字段 | 类型 | 描述 |\n| --- | --- | --- |\n| Code | int | 状态码 |\n| Message | string | 状态信息 |\n## 返回值示例\n` + "`" + `` + "`" + `` + "`" + `json\n{\n\"Code\": 200,\n\"Message\": \"节点值写入完成\"\n}\n` + "`" + `` + "`" + `` + "`" + `",
"consumes": [
"application/json"
],
Expand Down Expand Up @@ -548,15 +548,18 @@ const docTemplate = `{
"globaldata.NodeIdWithValueInput": {
"type": "object",
"required": [
"node_id",
"node_name",
"value"
],
"properties": {
"node_id": {
"node_name": {
"description": "节点名称",
"type": "string",
"example": "ns=2;i=2"
"example": "MyVariable"
},
"value": {}
"value": {
"description": "要写入的值"
}
}
},
"globaldata.Rule": {
Expand Down
13 changes: 8 additions & 5 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
"BearerAuth": []
}
],
"description": "WriteNodeValue 路由\n参数定义\n## 请求参数\n| 参数名称 | 类型 | 必填 | 描述 |\n| --- | --- | --- | --- |\n| data | list | 是 | 见下方JSON |\n## data类型 定义\n| 字段 | 类型 | 是否必填 | 描述 |\n| --- | --- | --- | --- |\n| NodeId | string | 是 | 节点ID |\n| Value | any | 是 | 写入值 |\n## 参数示例\n```json\n{\n\"data\": [\n{\n\"NodeId\": \"ns=2;s=MyVariable\",\n\"Value\": 123\n},\n{\n\"NodeId\": \"ns=2;s=MyVariable2\",\n\"Value\": \"abc\"\n}\n]\n}\n```\n## 返回值定义\n| 字段 | 类型 | 描述 |\n| --- | --- | --- |\n| Code | int | 状态码 |\n| Message | string | 状态信息 |\n## 返回值示例\n```json\n{\n\"Code\": 200,\n\"Message\": \"节点值写入完成\"\n}\n```",
"description": "WriteNodeValue 路由\n参数定义\n## 请求参数\n| 参数名称 | 类型 | 必填 | 描述 |\n| --- | --- | --- | --- |\n| data | list | 是 | 见下方JSON |\n## data类型 定义\n| 字段 | 类型 | 是否必填 | 描述 |\n| --- | --- | --- | --- |\n| NodeName | string | 是 | 节点名称 |\n| Value | any | 是 | 写入值 |\n## 参数示例\n```json\n{\n\"data\": [\n{\n\"NodeName\": \"MyVariable\",\n\"Value\": 123\n},\n{\n\"NodeName\": \"MyVariable2\",\n\"Value\": \"abc\"\n}\n]\n}\n```\n## 返回值定义\n| 字段 | 类型 | 描述 |\n| --- | --- | --- |\n| Code | int | 状态码 |\n| Message | string | 状态信息 |\n## 返回值示例\n```json\n{\n\"Code\": 200,\n\"Message\": \"节点值写入完成\"\n}\n```",
"consumes": [
"application/json"
],
Expand Down Expand Up @@ -545,15 +545,18 @@
"globaldata.NodeIdWithValueInput": {
"type": "object",
"required": [
"node_id",
"node_name",
"value"
],
"properties": {
"node_id": {
"node_name": {
"description": "节点名称",
"type": "string",
"example": "ns=2;i=2"
"example": "MyVariable"
},
"value": {}
"value": {
"description": "要写入的值"
}
}
},
"globaldata.Rule": {
Expand Down
16 changes: 9 additions & 7 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ definitions:
type: object
globaldata.NodeIdWithValueInput:
properties:
node_id:
example: ns=2;i=2
node_name:
description: 节点名称
example: MyVariable
type: string
value: {}
value:
description: 要写入的值
required:
- node_id
- node_name
- value
type: object
globaldata.Rule:
Expand Down Expand Up @@ -484,18 +486,18 @@ paths:
## data类型 定义
| 字段 | 类型 | 是否必填 | 描述 |
| --- | --- | --- | --- |
| NodeId | string | 是 | 节点ID |
| NodeName | string | 是 | 节点名称 |
| Value | any | 是 | 写入值 |
## 参数示例
```json
{
"data": [
{
"NodeId": "ns=2;s=MyVariable",
"NodeName": "MyVariable",
"Value": 123
},
{
"NodeId": "ns=2;s=MyVariable2",
"NodeName": "MyVariable2",
"Value": "abc"
}
]
Expand Down
4 changes: 2 additions & 2 deletions global/opc_node_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ var NodeWriteResult = make(chan bool)


type NodeIdWithValueInput struct {
NodeID string `json:"node_id" form:"node_id" binding:"required" example:"ns=2;i=2"`
Value interface{} `json:"value" form:"value" binding:"required"`
NodeName string `json:"node_name" form:"node_name" binding:"required" example:"MyVariable"` // 节点名称
Value interface{} `json:"value" form:"value" binding:"required"` // 要写入的值
// DataType string `json:"data_type" form:"data_type" example:"Int"`
}

Expand Down
15 changes: 12 additions & 3 deletions http_service/routers/node_routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,18 @@ func ServiceDeleteNode(nodeID int64) {
// @Description ## data类型 定义
// @Description | 字段 | 类型 | 是否必填 | 描述 |
// @Description | --- | --- | --- | --- |
// @Description | NodeId | string | 是 | 节点ID |
// @Description | NodeName | string | 是 | 节点名称 |
// @Description | Value | any | 是 | 写入值 |
// @Description ## 参数示例
// @Description ```json
// @Description {
// @Description "data": [
// @Description {
// @Description "NodeId": "ns=2;s=MyVariable",
// @Description "NodeName": "MyVariable",
// @Description "Value": 123
// @Description },
// @Description {
// @Description "NodeId": "ns=2;s=MyVariable2",
// @Description "NodeName": "MyVariable2",
// @Description "Value": "abc"
// @Description }
// @Description ]
Expand Down Expand Up @@ -228,6 +228,15 @@ func WriteNodeValue(c *gin.Context) {
panic(core.NewKnownError(core.FailedToWriteNodeValue, "", "data is empty"))
}

// check if node exist
for _, data := range req.Data {
_, err := globaldata.OPCNodeVars.GetNodeByName(data.NodeName)
if err != nil {
panic(core.NewKnownError(core.FailedToWriteNodeValue, data.NodeName, "node not found"))
}
}


globaldata.OpcWriteLock.Lock()
defer globaldata.OpcWriteLock.Unlock()

Expand Down
4 changes: 3 additions & 1 deletion opc_service/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ func writeOpcData(c *opcua.Client) {
nodeIDsWithValue := []map[string]interface{}{}
for _, d := range nodeIdWithValue {
// d.DataType
nodeIDsWithValue = append(nodeIDsWithValue, map[string]interface{}{d.NodeID: d.Value}) // ??? node type
// check previously
n,_ := globaldata.OPCNodeVars.GetNodeByName(d.NodeName)
nodeIDsWithValue = append(nodeIDsWithValue, map[string]interface{}{n.NodeID: d.Value}) // ??? node type
}
err := opcUa.WriteMultiValueByNodeIds(nodeIDsWithValue, ctx, c)
if err != nil {
Expand Down
14 changes: 8 additions & 6 deletions opc_ua/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
version: '3.8'


networks:
opcnetwork:
name: mynetwork # 已经存在的network
external: true
# networks:
# opcnetwork:
# name: mynetwork # 已经存在的network
# external: true


services:
Expand All @@ -14,5 +14,7 @@ services:
build:
context: .
dockerfile: Dockerfile
networks:
- opcnetwork
ports:
- "4840:4840"
# networks:
# - opcnetwork
3 changes: 0 additions & 3 deletions test/routers/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ import (
"github.com/Brandon-lz/myopcua/log"
opcservice "github.com/Brandon-lz/myopcua/opc_service"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"
)

var db *gorm.DB

func TestMain(t *testing.T) {
assert := assert.New(t)
Expand All @@ -44,7 +42,6 @@ func TestMain(t *testing.T) {
}

func cleanDb(assert *assert.Assertions) {
var err error
res := sysdb.DB.Exec("DROP SCHEMA public CASCADE;CREATE SCHEMA public;")
assert.NoError(res.Error)
}
Expand Down

0 comments on commit e990cc7

Please sign in to comment.