From e6c81b3916494ef51edc21774a27402d0f16c49c Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 24 八月 2023 17:21:04 +0800
Subject: [PATCH] plc配置支持serial和modbusTCP两种方式

---
 constvar/const.go     |   11 +++
 model/device_plc.go   |   17 +++--
 service/device_plc.go |   19 +++---
 docs/swagger.yaml     |   36 +++++++++--
 api/v1/config.go      |   15 +++++
 docs/docs.go          |   39 +++++++++---
 docs/swagger.json     |   39 +++++++++---
 7 files changed, 132 insertions(+), 44 deletions(-)

diff --git a/api/v1/config.go b/api/v1/config.go
index 4cf65b0..4765bcf 100644
--- a/api/v1/config.go
+++ b/api/v1/config.go
@@ -2,6 +2,7 @@
 
 import (
 	"apsClient/conf"
+	"apsClient/constvar"
 	"apsClient/model"
 	"apsClient/model/request"
 	_ "apsClient/model/response"
@@ -119,6 +120,20 @@
 		return
 	}
 
+	if !params.Method.Valid() {
+		ctx.FailWithMsg(ecode.ParamsErr, "鎺ュ彛鏂瑰紡涓嶆纭�")
+		return
+	}
+
+	if params.Method == constvar.PlcMethodModbusTCP && (params.Address == "" || params.Port == 0) {
+		ctx.FailWithMsg(ecode.ParamsErr, "褰撴帴鍙f柟寮忎负modbusTCP鏃讹紝address鍜宲ort涓嶈兘涓虹┖")
+		return
+	}
+	if params.Method == constvar.PlcMethodSerial && (params.BaudRate == 0 || params.SerialName == "") {
+		ctx.FailWithMsg(ecode.ParamsErr, "褰撴帴鍙f柟寮忎负serial鏃讹紝baudRate鍜宻erialName涓嶈兘涓虹┖")
+		return
+	}
+
 	errCode := service.NewDevicePlcService().UpdateDevicePlc(&params)
 	if errCode != ecode.OK {
 		ctx.Fail(errCode)
diff --git a/constvar/const.go b/constvar/const.go
index c64e44b..087eeb5 100644
--- a/constvar/const.go
+++ b/constvar/const.go
@@ -20,3 +20,14 @@
 	PlcAddressDataKeyFileName   = "plc_address_key"
 	PlcAddressDataValueFileName = "plc_address_value"
 )
+
+type PlcMethod string
+
+const (
+	PlcMethodModbusTCP PlcMethod = "modbusTCP"
+	PlcMethodSerial    PlcMethod = "serial"
+)
+
+func (slf PlcMethod) Valid() bool {
+	return slf == PlcMethodModbusTCP || slf == PlcMethodSerial
+}
diff --git a/docs/docs.go b/docs/docs.go
index 13c6d5b..1f4a9ab 100644
--- a/docs/docs.go
+++ b/docs/docs.go
@@ -524,6 +524,17 @@
                 }
             }
         },
+        "constvar.PlcMethod": {
+            "type": "string",
+            "enum": [
+                "modbusTCP",
+                "serial"
+            ],
+            "x-enum-varnames": [
+                "PlcMethodModbusTCP",
+                "PlcMethodSerial"
+            ]
+        },
         "contextx.Response": {
             "type": "object",
             "properties": {
@@ -540,7 +551,12 @@
             "type": "object",
             "properties": {
                 "address": {
+                    "description": "PortName   string ` + "`" + `gorm:\"type:varchar(191);comment:绔彛鍚嶇О\" json:\"portName\"` + "`" + `\nFrequency  int    ` + "`" + `gorm:\"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉抃" json:\"frequency\"` + "`" + `",
                     "type": "string"
+                },
+                "baudRate": {
+                    "description": "涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�",
+                    "type": "integer"
                 },
                 "brand": {
                     "type": "string"
@@ -551,9 +567,6 @@
                         "$ref": "#/definitions/model.DevicePlcAddress"
                     }
                 },
-                "frequency": {
-                    "type": "integer"
-                },
                 "id": {
                     "type": "integer"
                 },
@@ -561,12 +574,14 @@
                     "type": "boolean"
                 },
                 "method": {
-                    "type": "string"
+                    "$ref": "#/definitions/constvar.PlcMethod"
                 },
                 "port": {
+                    "description": "plc 绔彛鍙凤紝  method =  modbusTCP鐢�",
                     "type": "integer"
                 },
-                "portName": {
+                "serialName": {
+                    "description": "涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�",
                     "type": "string"
                 }
             }
@@ -767,7 +782,12 @@
             "type": "object",
             "properties": {
                 "address": {
+                    "description": "PortName   string ` + "`" + `gorm:\"type:varchar(191);comment:绔彛鍚嶇О\" json:\"portName\"` + "`" + `\nFrequency  int    ` + "`" + `gorm:\"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉抃" json:\"frequency\"` + "`" + `",
                     "type": "string"
+                },
+                "baudRate": {
+                    "description": "涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�",
+                    "type": "integer"
                 },
                 "brand": {
                     "type": "string"
@@ -778,9 +798,6 @@
                         "$ref": "#/definitions/model.DevicePlcAddress"
                     }
                 },
-                "frequency": {
-                    "type": "integer"
-                },
                 "id": {
                     "type": "integer"
                 },
@@ -788,12 +805,14 @@
                     "type": "boolean"
                 },
                 "method": {
-                    "type": "string"
+                    "$ref": "#/definitions/constvar.PlcMethod"
                 },
                 "port": {
+                    "description": "plc 绔彛鍙凤紝  method =  modbusTCP鐢�",
                     "type": "integer"
                 },
-                "portName": {
+                "serialName": {
+                    "description": "涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�",
                     "type": "string"
                 }
             }
diff --git a/docs/swagger.json b/docs/swagger.json
index 8f746a2..fec7e88 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -512,6 +512,17 @@
                 }
             }
         },
+        "constvar.PlcMethod": {
+            "type": "string",
+            "enum": [
+                "modbusTCP",
+                "serial"
+            ],
+            "x-enum-varnames": [
+                "PlcMethodModbusTCP",
+                "PlcMethodSerial"
+            ]
+        },
         "contextx.Response": {
             "type": "object",
             "properties": {
@@ -528,7 +539,12 @@
             "type": "object",
             "properties": {
                 "address": {
+                    "description": "PortName   string `gorm:\"type:varchar(191);comment:绔彛鍚嶇О\" json:\"portName\"`\nFrequency  int    `gorm:\"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉抃" json:\"frequency\"`",
                     "type": "string"
+                },
+                "baudRate": {
+                    "description": "涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�",
+                    "type": "integer"
                 },
                 "brand": {
                     "type": "string"
@@ -539,9 +555,6 @@
                         "$ref": "#/definitions/model.DevicePlcAddress"
                     }
                 },
-                "frequency": {
-                    "type": "integer"
-                },
                 "id": {
                     "type": "integer"
                 },
@@ -549,12 +562,14 @@
                     "type": "boolean"
                 },
                 "method": {
-                    "type": "string"
+                    "$ref": "#/definitions/constvar.PlcMethod"
                 },
                 "port": {
+                    "description": "plc 绔彛鍙凤紝  method =  modbusTCP鐢�",
                     "type": "integer"
                 },
-                "portName": {
+                "serialName": {
+                    "description": "涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�",
                     "type": "string"
                 }
             }
@@ -755,7 +770,12 @@
             "type": "object",
             "properties": {
                 "address": {
+                    "description": "PortName   string `gorm:\"type:varchar(191);comment:绔彛鍚嶇О\" json:\"portName\"`\nFrequency  int    `gorm:\"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉抃" json:\"frequency\"`",
                     "type": "string"
+                },
+                "baudRate": {
+                    "description": "涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�",
+                    "type": "integer"
                 },
                 "brand": {
                     "type": "string"
@@ -766,9 +786,6 @@
                         "$ref": "#/definitions/model.DevicePlcAddress"
                     }
                 },
-                "frequency": {
-                    "type": "integer"
-                },
                 "id": {
                     "type": "integer"
                 },
@@ -776,12 +793,14 @@
                     "type": "boolean"
                 },
                 "method": {
-                    "type": "string"
+                    "$ref": "#/definitions/constvar.PlcMethod"
                 },
                 "port": {
+                    "description": "plc 绔彛鍙凤紝  method =  modbusTCP鐢�",
                     "type": "integer"
                 },
-                "portName": {
+                "serialName": {
+                    "description": "涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�",
                     "type": "string"
                 }
             }
diff --git a/docs/swagger.yaml b/docs/swagger.yaml
index ab9ad60..260f74c 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -59,6 +59,14 @@
           $ref: '#/definitions/common.ProcedureWorker'
         type: array
     type: object
+  constvar.PlcMethod:
+    enum:
+    - modbusTCP
+    - serial
+    type: string
+    x-enum-varnames:
+    - PlcMethodModbusTCP
+    - PlcMethodSerial
   contextx.Response:
     properties:
       code:
@@ -70,24 +78,30 @@
   model.DevicePlc:
     properties:
       address:
+        description: |-
+          PortName   string `gorm:"type:varchar(191);comment:绔彛鍚嶇О" json:"portName"`
+          Frequency  int    `gorm:"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉�" json:"frequency"`
         type: string
+      baudRate:
+        description: 涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�
+        type: integer
       brand:
         type: string
       details:
         items:
           $ref: '#/definitions/model.DevicePlcAddress'
         type: array
-      frequency:
-        type: integer
       id:
         type: integer
       isOpen:
         type: boolean
       method:
-        type: string
+        $ref: '#/definitions/constvar.PlcMethod'
       port:
+        description: plc 绔彛鍙凤紝  method =  modbusTCP鐢�
         type: integer
-      portName:
+      serialName:
+        description: 涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�
         type: string
     type: object
   model.DevicePlcAddress:
@@ -223,24 +237,30 @@
   request.UpdatePlc:
     properties:
       address:
+        description: |-
+          PortName   string `gorm:"type:varchar(191);comment:绔彛鍚嶇О" json:"portName"`
+          Frequency  int    `gorm:"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉�" json:"frequency"`
         type: string
+      baudRate:
+        description: 涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�
+        type: integer
       brand:
         type: string
       details:
         items:
           $ref: '#/definitions/model.DevicePlcAddress'
         type: array
-      frequency:
-        type: integer
       id:
         type: integer
       isOpen:
         type: boolean
       method:
-        type: string
+        $ref: '#/definitions/constvar.PlcMethod'
       port:
+        description: plc 绔彛鍙凤紝  method =  modbusTCP鐢�
         type: integer
-      portName:
+      serialName:
+        description: 涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�
         type: string
     type: object
   request.UpdatePlcBrand:
diff --git a/model/device_plc.go b/model/device_plc.go
index e37a718..785ab8e 100644
--- a/model/device_plc.go
+++ b/model/device_plc.go
@@ -1,6 +1,7 @@
 package model
 
 import (
+	"apsClient/constvar"
 	"apsClient/pkg/sqlitex"
 	"encoding/json"
 	"fmt"
@@ -12,13 +13,15 @@
 	// DevicePlc 璁惧鐨凱LC閰嶇疆
 	DevicePlc struct {
 		gorm.Model `json:"-"`
-		Id         int    `gorm:"primarykey;type:int;" json:"id"`
-		Brand      string `gorm:"type:varchar(191);comment:PLC鍝佺墝" json:"brand"`
-		Method     string `gorm:"type:varchar(191);comment:鎺ュ彛鏂瑰紡" json:"method"`
-		PortName   string `gorm:"type:varchar(191);comment:绔彛鍚嶇О" json:"portName"`
-		Frequency  int    `gorm:"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉�" json:"frequency"`
-		Address    string `gorm:"type:varchar(191);comment:PLC鍦板潃" json:"address"`
-		Port       int    `gorm:"type:int(11);comment:绔彛" json:"port"`
+		Id         int                `gorm:"primarykey;type:int;" json:"id"`
+		Brand      string             `gorm:"type:varchar(191);comment:PLC鍝佺墝" json:"brand"`
+		Method     constvar.PlcMethod `gorm:"type:varchar(191);comment:鎺ュ彛鏂瑰紡" json:"method"`
+		//PortName   string `gorm:"type:varchar(191);comment:绔彛鍚嶇О" json:"portName"`
+		//Frequency  int    `gorm:"type:int(11);comment:鏁版嵁鏇存柊棰戠巼 0-瀹炴椂鏇存柊 1-1娆�/绉�" json:"frequency"`
+		Address    string `gorm:"type:varchar(191);comment:PLC鍦板潃" json:"address"` //plc ip鍦板潃锛� method = modbusTCP鐢�
+		Port       int    `gorm:"type:int(11);comment:绔彛" json:"port"`            //plc 绔彛鍙凤紝  method =  modbusTCP鐢�
+		BaudRate   int    `gorm:"type:int(11);comment:娉㈢壒鐜�"  json:"baudRate"`      //涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�
+		SerialName string `gorm:"type:int(11);comment:涓插彛鍚嶇О"  json:"serialName"`   //涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�
 		IsOpen     bool   `gorm:"type:tinyint(1);comment:鏄惁寮�鍚�" json:"isOpen"`
 		Detail     string `gorm:"type:varchar(2048);comment:鏁版嵁璇︽儏" json:"-"`
 
diff --git a/service/device_plc.go b/service/device_plc.go
index 7ec6fee..db1fd71 100644
--- a/service/device_plc.go
+++ b/service/device_plc.go
@@ -20,15 +20,16 @@
 	DevicePlc, err := model.NewDevicePlcSearch().SetOrder("id desc").First()
 	if err == gorm.ErrRecordNotFound {
 		return &model.DevicePlc{
-			Brand:     "",
-			Method:    "",
-			PortName:  "",
-			Frequency: 0,
-			Address:   "",
-			Port:      0,
-			IsOpen:    false,
-			Detail:    "",
-			Details:   make([]*model.DevicePlcAddress, 0),
+			Id:         0,
+			Brand:      "",
+			Method:     "",
+			Address:    "",
+			Port:       0,
+			BaudRate:   0,
+			SerialName: "",
+			IsOpen:     false,
+			Detail:     "",
+			Details:    make([]*model.DevicePlcAddress, 0),
 		}, ecode.OK
 	}
 	if err != nil {

--
Gitblit v1.8.0