From 339ae89408bb2e5ba4428afd9c5f5fb64c04f2ad Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期一, 28 八月 2023 13:32:50 +0800
Subject: [PATCH] 下发任务参数和写入生产数量异步改同步

---
 api/v1/task.go |   59 ++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 32 insertions(+), 27 deletions(-)

diff --git a/api/v1/task.go b/api/v1/task.go
index 83c199e..f856f31 100644
--- a/api/v1/task.go
+++ b/api/v1/task.go
@@ -12,7 +12,6 @@
 	"apsClient/pkg/ecode"
 	"apsClient/pkg/logx"
 	"apsClient/pkg/plc"
-	"apsClient/pkg/safe"
 	"apsClient/service"
 	"apsClient/service/plc_address"
 	"errors"
@@ -230,48 +229,54 @@
 	})
 	if err != nil {
 		logx.Errorf("SendProcessParams update order and procedure status error:%v", err.Error())
-		ctx.FailWithMsg(ecode.DBErr, "鏇存敼宸ュ崟鐘舵�佸け璐�")
+		ctx.FailWithMsg(ecode.NeedConfirmedErr, "鏇存敼宸ュ崟鐘舵�佸け璐�")
 		return
 	}
-
-	safe.Go(func() {
-		err = SendParams(result.ParamsMap, 0)
-		if err != nil {
-			logx.Errorf("SendProcessParams: %v", err.Error())
-			return
-		}
-		plcConfig, code := service.NewDevicePlcService().GetDevicePlc()
-		if code != ecode.OK {
-			logx.Errorf("get plcConfig err: %v", err.Error())
-			return
-		}
-		_ = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, order.Amount.IntPart())
-	})
+	plcConfig, code := service.NewDevicePlcService().GetDevicePlc()
+	if code != ecode.OK || plcConfig.Id == 0 {
+		ctx.FailWithMsg(ecode.NeedConfirmedErr, "璇峰厛閰嶇疆PLC")
+		return
+	}
+	plcConfig.MaxTryTimes = 2
+	err = SendParams(result.ParamsMap, plcConfig)
+	if err != nil {
+		logx.Errorf("SendProcessParams: %v", err.Error())
+		ctx.FailWithMsg(ecode.NeedConfirmedErr, "鍐欏叆宸ヨ壓鍙傛暟澶辫触")
+		return
+	}
+	if code != ecode.OK {
+		logx.Errorf("get plcConfig err: %v", err.Error())
+		return
+	}
+	plcConfig.CurrentTryTimes = 0
+	err = service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, order.Amount.IntPart())
+	if err != nil {
+		ctx.FailWithMsg(ecode.NeedConfirmedErr, "鍐欏叆鐢熶骇鏁伴噺澶辫触")
+		return
+	}
 	ctx.Ok()
 }
 
-func SendParams(paramsMap map[string]interface{}, tryTimes int) error {
+func SendParams(paramsMap map[string]interface{}, plcConfig *model.DevicePlc) error {
 	if len(paramsMap) == 0 {
 		return errors.New("empty params")
 	}
-	if tryTimes > 2 {
-		return errors.New("beyond max try time")
-	}
-	plcConfig, code := service.NewDevicePlcService().GetDevicePlc()
-	if code != ecode.OK {
-		return errors.New("璇峰厛閰嶇疆PLC")
+	if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes {
+		return plcConfig.CurrentErr
 	}
 	conn, err := plc.GetModbusConnection(fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port))
 	if err != nil {
 		return errors.New(fmt.Sprintf("杩炴帴plc澶辫触: %v", err.Error()))
 	}
-
-	logx.Info("----------------寮�濮嬩笅鍙戝伐鑹哄弬鏁�-----------------")
+	if plcConfig.CurrentTryTimes == 0 {
+		logx.Info("----------------寮�濮嬩笅鍙戝伐鑹哄弬鏁�-----------------")
+	}
 	var failedNumbers int
 	for k, v := range paramsMap {
 		if address, ok := plc_address.Get(k); ok {
 			result, err := plc.WriteHoldingRegister(conn, address, v)
 			if err != nil {
+				plcConfig.CurrentErr = err
 				failedNumbers++
 				logx.Errorf("plc write err:%v, address: %v, key: %v value: %v", err.Error(), address, k, v)
 			} else {
@@ -283,8 +288,8 @@
 		}
 	}
 	if failedNumbers >= 1 { //鍐欏叆plc澶辫触, 閲嶈瘯
-		tryTimes++
-		return SendParams(paramsMap, tryTimes)
+		plcConfig.CurrentTryTimes++
+		return SendParams(paramsMap, plcConfig)
 	}
 	logx.Info("----------------涓嬪彂宸ヨ壓鍙傛暟瀹屾瘯-----------------")
 	return nil

--
Gitblit v1.8.0