plc配置支持serial和modbusTCP两种方式
| | |
| | | |
| | | import ( |
| | | "apsClient/conf" |
| | | "apsClient/constvar" |
| | | "apsClient/model" |
| | | "apsClient/model/request" |
| | | _ "apsClient/model/response" |
| | |
| | | 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(¶ms) |
| | | if errCode != ecode.OK { |
| | | ctx.Fail(errCode) |
| | |
| | | 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 |
| | | } |
| | |
| | | } |
| | | } |
| | | }, |
| | | "constvar.PlcMethod": { |
| | | "type": "string", |
| | | "enum": [ |
| | | "modbusTCP", |
| | | "serial" |
| | | ], |
| | | "x-enum-varnames": [ |
| | | "PlcMethodModbusTCP", |
| | | "PlcMethodSerial" |
| | | ] |
| | | }, |
| | | "contextx.Response": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | "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" |
| | |
| | | "$ref": "#/definitions/model.DevicePlcAddress" |
| | | } |
| | | }, |
| | | "frequency": { |
| | | "type": "integer" |
| | | }, |
| | | "id": { |
| | | "type": "integer" |
| | | }, |
| | |
| | | "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", |
| | | "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" |
| | |
| | | "$ref": "#/definitions/model.DevicePlcAddress" |
| | | } |
| | | }, |
| | | "frequency": { |
| | | "type": "integer" |
| | | }, |
| | | "id": { |
| | | "type": "integer" |
| | | }, |
| | |
| | | "type": "boolean" |
| | | }, |
| | | "method": { |
| | | "type": "string" |
| | | "$ref": "#/definitions/constvar.PlcMethod" |
| | | }, |
| | | "port": { |
| | | "description": "plc 端口号, method = modbusTCP用", |
| | | "type": "integer" |
| | | }, |
| | | "portName": { |
| | | "serialName": { |
| | | "description": "串口名称,method = serial时 用", |
| | | "type": "string" |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | }, |
| | | "constvar.PlcMethod": { |
| | | "type": "string", |
| | | "enum": [ |
| | | "modbusTCP", |
| | | "serial" |
| | | ], |
| | | "x-enum-varnames": [ |
| | | "PlcMethodModbusTCP", |
| | | "PlcMethodSerial" |
| | | ] |
| | | }, |
| | | "contextx.Response": { |
| | | "type": "object", |
| | | "properties": { |
| | |
| | | "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" |
| | |
| | | "$ref": "#/definitions/model.DevicePlcAddress" |
| | | } |
| | | }, |
| | | "frequency": { |
| | | "type": "integer" |
| | | }, |
| | | "id": { |
| | | "type": "integer" |
| | | }, |
| | |
| | | "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", |
| | | "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" |
| | |
| | | "$ref": "#/definitions/model.DevicePlcAddress" |
| | | } |
| | | }, |
| | | "frequency": { |
| | | "type": "integer" |
| | | }, |
| | | "id": { |
| | | "type": "integer" |
| | | }, |
| | |
| | | "type": "boolean" |
| | | }, |
| | | "method": { |
| | | "type": "string" |
| | | "$ref": "#/definitions/constvar.PlcMethod" |
| | | }, |
| | | "port": { |
| | | "description": "plc 端口号, method = modbusTCP用", |
| | | "type": "integer" |
| | | }, |
| | | "portName": { |
| | | "serialName": { |
| | | "description": "串口名称,method = serial时 用", |
| | | "type": "string" |
| | | } |
| | | } |
| | |
| | | $ref: '#/definitions/common.ProcedureWorker' |
| | | type: array |
| | | type: object |
| | | constvar.PlcMethod: |
| | | enum: |
| | | - modbusTCP |
| | | - serial |
| | | type: string |
| | | x-enum-varnames: |
| | | - PlcMethodModbusTCP |
| | | - PlcMethodSerial |
| | | contextx.Response: |
| | | properties: |
| | | code: |
| | |
| | | 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: |
| | |
| | | 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: |
| | |
| | | package model |
| | | |
| | | import ( |
| | | "apsClient/constvar" |
| | | "apsClient/pkg/sqlitex" |
| | | "encoding/json" |
| | | "fmt" |
| | |
| | | // 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:"-"` |
| | | |
| | |
| | | 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 { |