From 3f1ef5cdf9212f912e9cad544387947d158f5d08 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期四, 14 九月 2023 19:26:49 +0800 Subject: [PATCH] 增加plc连接状态 --- api/v1/plc.go | 50 +++++++++++++++++++++++++------------------------- 1 files changed, 25 insertions(+), 25 deletions(-) diff --git a/api/v1/plc.go b/api/v1/plc.go index 254845f..3ed79a8 100644 --- a/api/v1/plc.go +++ b/api/v1/plc.go @@ -2,11 +2,14 @@ import ( "apsClient/constvar" + "apsClient/model/request" "apsClient/model/response" _ "apsClient/model/response" "apsClient/pkg/contextx" "apsClient/pkg/ecode" + "apsClient/pkg/plc" "apsClient/service" + "fmt" "github.com/gin-gonic/gin" "github.com/spf13/cast" ) @@ -17,10 +20,12 @@ // @Tags 鐢熶骇鏁伴噺 // @Summary 鑾峰彇鐢熶骇杩涘害 // @Produce application/json +// @Param object body request.SendProcessParams true "鏌ヨ鍙傛暟" // @Success 200 {object} contextx.Response{data=response.ProductProgress} "鎴愬姛" -// @Router /v1/plc/productProgress [get] +// @Router /v1/plc/productProgress [post] func (slf *PlcApi) GetProductProgress(c *gin.Context) { - ctx, ok := contextx.NewContext(c, nil) + var params request.GetProductProgress + ctx, ok := contextx.NewContext(c, ¶ms) if !ok { return } @@ -29,6 +34,24 @@ resp := new(response.ProductProgress) resp.FinishNumber = cast.ToInt(finishNumber) resp.TotalNumber = cast.ToInt(totalNumber) + + plcConfig, code := service.NewDevicePlcService().GetDevicePlc() + if code != ecode.OK { + return + } + plcStatus := 1 //鏂紑杩炴帴 + ipAddr := fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port) + + conn, err := plc.GetModbusConnection(ipAddr) + if err == nil && conn.IsConnected() { + if resp.FinishNumber > 0 { //鐢熶骇 + plcStatus = 2 + } else { //寰呮満 + plcStatus = 3 + } + } + resp.PlcStatus = plcStatus + ctx.OkWithDetailed(resp) } @@ -41,29 +64,6 @@ func (slf *PlcApi) SetProductNumber(c *gin.Context) { ctx, ok := contextx.NewContext(c, nil) if !ok { - return - } - - taskData, code := service.NewTaskService().GetTask() - if code != ecode.OK { - ctx.Fail(code) - return - } - - if taskData.Order == nil { - ctx.FailWithMsg(ecode.UnknownErr, "褰撳墠娌℃湁寰呯敓浜у伐鍗�") - return - } - - plcConfig, code := service.NewDevicePlcService().GetDevicePlc() - if code != ecode.OK { - ctx.FailWithMsg(ecode.UnknownErr, "璇峰厛閰嶇疆PLC") - return - } - - err := service.PlcWrite(plcConfig, constvar.PlcStartAddressTypeTotalNumber, taskData.Order.Amount.IntPart()) - if err != nil { - ctx.FailWithMsg(ecode.UnknownErr, "璁剧疆澶辫触锛岃妫�鏌lc閰嶇疆") return } ctx.Ok() -- Gitblit v1.8.0