From 04cea6ec858be6ddef3982dcecabf6b3205a1f8b Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期二, 22 八月 2023 19:23:01 +0800 Subject: [PATCH] fix --- api/v1/plc.go | 24 +++++++++--------------- 1 files changed, 9 insertions(+), 15 deletions(-) diff --git a/api/v1/plc.go b/api/v1/plc.go index 2a93a15..1ae6174 100644 --- a/api/v1/plc.go +++ b/api/v1/plc.go @@ -9,9 +9,8 @@ "apsClient/pkg/logx" "apsClient/pkg/plc" "apsClient/service" - "encoding/json" + "encoding/binary" "github.com/gin-gonic/gin" - "github.com/spf13/cast" ) type PlcApi struct{} @@ -48,36 +47,31 @@ 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) - 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 } + defer conn.Close() 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 interface{} - err = json.Unmarshal(rawData, &finishNumber) + resp.FinishNumber = int(binary.BigEndian.Uint16(rawData)) if err != nil { - ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇鐢熶骇杩涘害鏁版嵁瑙f瀽澶辫触") + ctx.OkWithDetailed(resp) logx.Errorf("GetProductProgress 鑾峰彇鐢熶骇杩涘害鏁版嵁瑙f瀽澶辫触: %v, data: %v, valueType:%v", err.Error(), rawData, valueType) return - } - resp := response.ProductProgress{ - FinishNumber: cast.ToInt(finishNumber), } ctx.OkWithDetailed(resp) } -- Gitblit v1.8.0