From 8f692efa28cb68b9644d473aedc5caeee2496a61 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 26 十月 2023 17:11:17 +0800
Subject: [PATCH] 接收云端下发的modbusRTU配置
---
constvar/const.go | 9 +++++++++
nsq/msg_handler.go | 18 ++++++++++++++----
model/common/common.go | 7 +++++--
model/device_plc.go | 9 +++++++--
4 files changed, 35 insertions(+), 8 deletions(-)
diff --git a/constvar/const.go b/constvar/const.go
index 5f6fa74..6cb9a3e 100644
--- a/constvar/const.go
+++ b/constvar/const.go
@@ -34,6 +34,7 @@
const (
PlcMethodModbusTCP PlcMethod = "modbusTCP"
+ PlcMethodModbusRTU PlcMethod = "modbusRTU"
PlcMethodSerial PlcMethod = "serial"
PlcMethodModbusTCPChinese PlcMethod = "缃戠粶"
PlcMethodSerialChinese PlcMethod = "涓插彛"
@@ -70,3 +71,11 @@
TaskModeCurrent TaskMode = 2 //鏈紑濮嬬殑鍜岃繘琛屼腑鐨�
TaskModeLastFinished TaskMode = 3 //涓婁竴涓粨鏉熺殑
)
+
+type Parity int
+
+const (
+ ParityEven Parity = 1 //濂囨牎楠�
+ ParityOdd Parity = 2 //鍋舵牎楠�
+ ParityNull Parity = 3 //鏃犳牎楠�
+)
diff --git a/model/common/common.go b/model/common/common.go
index f92ae17..3a8781f 100644
--- a/model/common/common.go
+++ b/model/common/common.go
@@ -78,8 +78,11 @@
Method constvar.PlcMethod `gorm:"type:varchar(191)" json:"method"` //comment:鎺ュ彛鏂瑰紡
Address string `gorm:"type:varchar(191)" json:"address"` //plc ip鍦板潃锛� method = modbusTCP鐢�
Port int `gorm:"type:int(11)" json:"port"` //plc 绔彛鍙凤紝 method = modbusTCP鐢�
- BaudRate int `gorm:"type:int(11)" json:"baudRate"` //涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�
- SerialName string `gorm:"type:int(11)" json:"serialName"` //涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�
+ BaudRate int `gorm:"type:int(11)" json:"baudRate"` //涓插彛娉㈢壒鐜囷紝 method = serial鎴杕odbusRTU 鏃剁敤
+ SerialName string `gorm:"type:int(11)" json:"serialName"` //涓插彛鍚嶇О锛宮ethod = serial鎴杕odbusRTU 鏃剁敤
+ DataBit int `gorm:"type:int(11)" json:"dataBit"` //鏁版嵁浣嶏紝method = modbusRTU 鐢�
+ StopBit int `gorm:"type:int(11)" json:"stopBit"` //鍋滄浣嶏紝method = modbusRTU 鐢�
+ Parity constvar.Parity `gorm:"type:int(11)" json:"parity"` //鏍¢獙鏂瑰紡锛宮ethod = modbusRTU 鐢�
IsOpen bool `gorm:"type:tinyint(1)" json:"isOpen"`
Detail string `gorm:"type:varchar(2048)" json:"-"`
Details []*PlcAddress `gorm:"-" json:"details"`
diff --git a/model/device_plc.go b/model/device_plc.go
index 134e48b..a098e81 100644
--- a/model/device_plc.go
+++ b/model/device_plc.go
@@ -20,8 +20,13 @@
Port int `json:"port"` //plc 绔彛鍙凤紝 method = modbusTCP鐢�
BaudRate int `json:"baudRate"` //涓插彛娉㈢壒鐜囷紝 method = serial鏃� 鐢�
SerialName string `json:"serialName"` //涓插彛鍚嶇О锛宮ethod = serial鏃� 鐢�
- IsOpen bool `json:"isOpen"`
- Detail string `gorm:"type:varchar(2048)" json:"-"`
+
+ DataBit int `gorm:"type:int(11)" json:"dataBit"` //鏁版嵁浣嶏紝method = modbusRTU 鐢�
+ StopBit int `gorm:"type:int(11)" json:"stopBit"` //鍋滄浣嶏紝method = modbusRTU 鐢�
+ Parity constvar.Parity `gorm:"type:int(11)" json:"parity"` //鏍¢獙鏂瑰紡锛宮ethod = modbusRTU 鐢�
+
+ IsOpen bool `json:"isOpen"`
+ Detail string `gorm:"type:varchar(2048)" json:"-"`
Details []*DevicePlcAddress `gorm:"-" json:"details"`
diff --git a/nsq/msg_handler.go b/nsq/msg_handler.go
index 0b3d0d5..3933b1f 100644
--- a/nsq/msg_handler.go
+++ b/nsq/msg_handler.go
@@ -301,15 +301,25 @@
oldPlcRecord, err := model.NewDevicePlcSearch().SetDeviceId(msg.ID).First()
if err == gorm.ErrRecordNotFound {
err = model.NewDevicePlcSearch().Create(&record)
- } else if record.Port != oldPlcRecord.Port ||
- record.Method != oldPlcRecord.Method ||
+ } else if record.Port != oldPlcRecord.Port || record.Method != oldPlcRecord.Method ||
record.Address != oldPlcRecord.Address ||
- record.Detail != oldPlcRecord.Detail {
+ record.Detail != oldPlcRecord.Detail ||
+ record.BaudRate != oldPlcRecord.BaudRate ||
+ record.SerialName != oldPlcRecord.SerialName ||
+ record.DataBit != oldPlcRecord.DataBit ||
+ record.StopBit != oldPlcRecord.StopBit ||
+ record.Parity != oldPlcRecord.Parity {
- oldPlcRecord.Port = record.Port
oldPlcRecord.Method = record.Method
oldPlcRecord.Address = record.Address
+ oldPlcRecord.Port = record.Port
oldPlcRecord.Detail = record.Detail
+ oldPlcRecord.BaudRate = record.BaudRate
+ oldPlcRecord.SerialName = record.SerialName
+ oldPlcRecord.DataBit = record.DataBit
+ oldPlcRecord.StopBit = record.StopBit
+ oldPlcRecord.Parity = record.Parity
+
err = model.NewDevicePlcSearch().Save(oldPlcRecord)
}
if err != nil {
--
Gitblit v1.8.0