zhangqian
2023-08-24 e6c81b3916494ef51edc21774a27402d0f16c49c
plc配置支持serial和modbusTCP两种方式
7个文件已修改
176 ■■■■ 已修改文件
api/v1/config.go 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/const.go 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/docs.go 39 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.json 39 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
docs/swagger.yaml 36 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/device_plc.go 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/device_plc.go 19 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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, "当接口方式为modbusTCP时,address和port不能为空")
        return
    }
    if params.Method == constvar.PlcMethodSerial && (params.BaudRate == 0 || params.SerialName == "") {
        ctx.FailWithMsg(ecode.ParamsErr, "当接口方式为serial时,baudRate和serialName不能为空")
        return
    }
    errCode := service.NewDevicePlcService().UpdateDevicePlc(&params)
    if errCode != ecode.OK {
        ctx.Fail(errCode)
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
}
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": "串口名称,method = 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": "串口名称,method = serial时 用",
                    "type": "string"
                }
            }
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": "串口名称,method = 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": "串口名称,method = serial时 用",
                    "type": "string"
                }
            }
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: 串口名称,method = 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: 串口名称,method = serial时 用
        type: string
    type: object
  request.UpdatePlcBrand:
model/device_plc.go
@@ -1,6 +1,7 @@
package model
import (
    "apsClient/constvar"
    "apsClient/pkg/sqlitex"
    "encoding/json"
    "fmt"
@@ -12,13 +13,15 @@
    // DevicePlc 设备的PLC配置
    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"`   //串口名称,method = serial时 用
        IsOpen     bool   `gorm:"type:tinyint(1);comment:是否开启" json:"isOpen"`
        Detail     string `gorm:"type:varchar(2048);comment:数据详情" json:"-"`
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 {