From 2b2193e255e55fe55dedf7dce0e6bdc22f4860ee Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 19 八月 2023 19:39:11 +0800
Subject: [PATCH] 修改网络设调用禁用或停用脚本

---
 api/v1/plc.go |   27 ++++++++++++++-------------
 1 files changed, 14 insertions(+), 13 deletions(-)

diff --git a/api/v1/plc.go b/api/v1/plc.go
index bec602d..0d91e0e 100644
--- a/api/v1/plc.go
+++ b/api/v1/plc.go
@@ -47,34 +47,35 @@
 		ipAddr = plcConfig.Address
 	}
 
+	resp := new(response.ProductProgress)
 	if startAddress == 0 || valueType == "" {
-		ctx.FailWithMsg(ecode.UnknownErr, "璇峰厛閰嶇疆PLC")
+		ctx.OkWithDetailed(resp)
+		logx.Warnf("璇峰厛閰嶇疆PLC")
 		return
 	}
-
 	conn, err := plc.NewModbusConnection(ipAddr)
-	defer conn.Close()
+	if conn != nil {
+		defer conn.Close()
+	}
+
 	if err != nil {
-		ctx.FailWithMsg(ecode.UnknownErr, "杩炴帴plc澶辫触")
+		ctx.OkWithDetailed(resp)
 		logx.Errorf("GetProductProgress 杩炴帴plc澶辫触: %v", err.Error())
 		return
 	}
 
-	raw, err := plc.ReadHoldingRegister(conn, startAddress, dataLength)
+	rawData, err := plc.ReadHoldingRegister(conn, startAddress, dataLength)
 	if err != nil {
-		ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇plc鏁版嵁澶辫触")
+		ctx.OkWithDetailed(resp)
 		logx.Errorf("GetProductProgress 鑾峰彇plc鏁版嵁澶辫触: %v", err.Error())
 		return
 	}
-	var finishNumber int
-	err = json.Unmarshal(raw, &finishNumber)
+	var finishNumber interface{}
+	err = json.Unmarshal(rawData, &finishNumber)
 	if err != nil {
-		ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇鐢熶骇杩涘害鏁版嵁瑙f瀽澶辫触")
-		logx.Errorf("GetProductProgress 鑾峰彇鐢熶骇杩涘害鏁版嵁瑙f瀽澶辫触: %v, data: %v, valueType:%v", err.Error(), raw, valueType)
+		ctx.OkWithDetailed(resp)
+		logx.Errorf("GetProductProgress 鑾峰彇鐢熶骇杩涘害鏁版嵁瑙f瀽澶辫触: %v, data: %v, valueType:%v", err.Error(), rawData, valueType)
 		return
-	}
-	resp := response.ProductProgress{
-		FinishNumber: finishNumber,
 	}
 	ctx.OkWithDetailed(resp)
 }

--
Gitblit v1.8.0