From f82cb11a0ac571dc7afd3ec339324000a25207c0 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期一, 28 八月 2023 19:32:23 +0800 Subject: [PATCH] 修改提示语 --- api/v1/task.go | 88 +++++++++++++++++++++---------------------- 1 files changed, 43 insertions(+), 45 deletions(-) diff --git a/api/v1/task.go b/api/v1/task.go index 88a677d..d8ebcc7 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" @@ -66,14 +65,14 @@ ctx.OkWithDetailed(taskData) } -// TaskStart +// GetProcessParams // @Tags Task -// @Summary 浠诲姟寮�濮� +// @Summary 浠诲姟寮�濮嬶紙鑾峰彇宸ヨ壓鍙傛暟锛� // @Produce application/json // @Param id path int true "宸ュ簭id" // @Success 200 {object} contextx.Response{data=response.ProcessParamsResponse} "鎴愬姛" // @Router /v1/task/start/{id} [get] -func (slf *TaskApi) TaskStart(c *gin.Context) { +func (slf *TaskApi) GetProcessParams(c *gin.Context) { ctx, ok := contextx.NewContext(c, nil) if !ok { return @@ -163,14 +162,14 @@ ctx.Ok() } -// SendProcessParams +// TaskStart // @Tags Task -// @Summary 涓嬪彂宸ヨ壓鍙傛暟 +// @Summary 涓嬪彂宸ヨ壓鍙傛暟锛堝紑濮嬩换鍔★級 // @Produce application/json // @Param id path int true "宸ュ簭id" // @Success 200 {object} contextx.Response{service.GetProcessModel} "鎴愬姛" // @Router /v1/task/sendProcessParams/{id} [post] -func (slf *TaskApi) SendProcessParams(c *gin.Context) { +func (slf *TaskApi) TaskStart(c *gin.Context) { ctx, ok := contextx.NewContext(c, nil) if !ok { return @@ -230,68 +229,67 @@ }) 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("----------------寮�濮嬩笅鍙戝伐鑹哄弬鏁�-----------------") - var missNumbers int + 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 { - logx.Errorf("WriteHoldingRegister err:%v, address: %v, key: %v value: %v", err.Error(), address, k, v) + plcConfig.CurrentErr = err + failedNumbers++ + logx.Errorf("plc write err:%v, address: %v, key: %v value: %v", err.Error(), address, k, v) } else { delete(paramsMap, k) - logx.Infof("WriteHoldingRegister ok: key: %v, value: %v, result: %v", k, v, result) + logx.Infof("plc write ok: key: %v, value: %v, result: %v", k, v, result) } } else { logx.Errorf("miss param address, k:%v, v:%v", k, v) - missNumbers++ } } - if missNumbers >= 1 { - caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicGetPlcAddress, conf.Conf.NsqConf.NodeId), fmt.Sprintf(constvar.NsqTopicSendPlcAddress, conf.Conf.NsqConf.NodeId)) - var addressResult common.ResponsePlcAddress - err := caller.Call(common.RequestPlcAddress{DeviceId: conf.Conf.System.DeviceId}, &addressResult, time.Second*2) - if err != nil { - logx.Infof("SendParams2 err: %v", err.Error()) - return err - } - tryTimes++ - return SendParams(paramsMap, tryTimes) + if failedNumbers >= 1 { //鍐欏叆plc澶辫触, 閲嶈瘯 + plcConfig.CurrentTryTimes++ + return SendParams(paramsMap, plcConfig) } logx.Info("----------------涓嬪彂宸ヨ壓鍙傛暟瀹屾瘯-----------------") return nil -- Gitblit v1.8.0