From 8324f872ef3a4d0c978a9b1d062800c6a1701c12 Mon Sep 17 00:00:00 2001 From: zhangqian <zhangqian@123.com> Date: 星期五, 01 十二月 2023 09:58:17 +0800 Subject: [PATCH] fix --- api/v1/config.go | 96 +++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 90 insertions(+), 6 deletions(-) diff --git a/api/v1/config.go b/api/v1/config.go index d14c0a6..ee22af0 100644 --- a/api/v1/config.go +++ b/api/v1/config.go @@ -1,12 +1,17 @@ package v1 import ( + "apsClient/conf" + "apsClient/constvar" "apsClient/model" + "apsClient/model/request" _ "apsClient/model/response" "apsClient/pkg/contextx" "apsClient/pkg/ecode" "apsClient/pkg/logx" "apsClient/service" + "apsClient/utils" + "fmt" "github.com/gin-gonic/gin" ) @@ -27,7 +32,7 @@ ConfigData, err := service.NewConfigService().GetNetConfigList() if err != nil { logx.Errorf("get net config error: %v", err.Error()) - ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇缃戠粶閰嶇疆澶辫触") + ctx.FailWithMsg(ecode.DBErr, "鑾峰彇缃戠粶閰嶇疆澶辫触") return } @@ -40,7 +45,7 @@ // @Produce application/json // @Param object body model.NetConfig true "鍙傛暟" // @Success 200 {object} contextx.Response{data=[]response.ProcessParams} "鎴愬姛" -// @Router /v1/config/net [put] +// @Router /v1/config/net [post] func (slf *ConfigApi) SetNetConfig(c *gin.Context) { var params model.NetConfig ctx, ok := contextx.NewContext(c, ¶ms) @@ -48,19 +53,98 @@ return } - _, err := service.NewConfigService().GetNetConfig(params.Id) + _, err := service.NewConfigService().GetNetConfig(params.ID) if err != nil { logx.Errorf("get net config error: %v", err.Error()) - ctx.FailWithMsg(ecode.UnknownErr, "鑾峰彇缃戠粶閰嶇疆澶辫触") + ctx.FailWithMsg(ecode.DBErr, "鑾峰彇缃戠粶閰嶇疆澶辫触") return } - - err = service.NewConfigService().SetNetConfig(params.Id, ¶ms) + err = service.NewConfigService().SetNetConfig(params.ID, ¶ms) if err != nil { logx.Errorf("SetNetConfig error: %v", err.Error()) ctx.FailWithMsg(ecode.DBErr, "璁剧疆澶辫触") return } + if conf.Conf.System.NetSetShellPath == "" || conf.Conf.System.NetUpShellName == "" || conf.Conf.System.NetDownShellName == "" { + ctx.FailWithMsg(ecode.DBErr, "璇峰厛閰嶇疆缃戠粶璁剧疆璺緞鍜岃剼鏈悕") + return + } + var shName string + if params.Status == model.NetConfigStatusEnabled { + shName = conf.Conf.System.NetUpShellName + } else { + shName = conf.Conf.System.NetDownShellName + } + sh := fmt.Sprintf("%s %v %v %v %v", shName, params.NetworkCard, params.IP, params.MASK, params.Gateway) + err = utils.Cmd(conf.Conf.System.NetSetShellPath + sh) + if err != nil { + logx.Errorf("network update failed: %v", err.Error()) + ctx.FailWithMsg(ecode.UnknownErr, "缃戠粶璁剧疆澶辫触") + return + } + ctx.Ok() +} + +// PlcGet +// @Tags Config +// @Summary 鑾峰彇plc閰嶇疆 +// @Produce application/json +// @Success 200 {object} contextx.Response{data=model.DevicePlc} "鎴愬姛" +// @Router /v1/config/plc [get] +func (slf *ConfigApi) PlcGet(c *gin.Context) { + ctx, ok := contextx.NewContext(c, nil) + if !ok { + return + } + + plcData, code := service.NewDevicePlcService().GetDevicePlc() + if code != ecode.OK { + ctx.Fail(code) + return + } + ctx.OkWithDetailed(plcData) +} + +// PlcUpdate +// @Tags Config +// @Summary 鏇存柊plc閰嶇疆 +// @Produce application/json +// @Param object body request.UpdatePlc true "鏌ヨ鍙傛暟" +// @Success 200 {object} contextx.Response{} +// @Router /v1/config/plc [post] +func (slf *ConfigApi) PlcUpdate(c *gin.Context) { + var params request.UpdatePlc + ctx, ok := contextx.NewContext(c, ¶ms) + if !ok { + return + } + + if !params.Method.Valid() { + ctx.FailWithMsg(ecode.ParamsErr, "鎺ュ彛鏂瑰紡涓嶆纭�") + return + } + + params.DeviceID = conf.Conf.CurrentDeviceID + if params.Method == constvar.PlcMethodModbusTCP && (params.Address == "" || params.Port == 0) { + ctx.FailWithMsg(ecode.ParamsErr, "褰撴帴鍙f柟寮忎负modbusTCP鏃讹紝鍦板潃鍜岀鍙e彿涓嶈兘涓虹┖") + return + } + if params.Method == constvar.PlcMethodSerial && (params.BaudRate == 0 || params.SerialName == "") { + ctx.FailWithMsg(ecode.ParamsErr, "褰撴帴鍙f柟寮忎负serial鏃讹紝娉㈢壒鐜囧拰涓插彛鍚嶇О涓嶈兘涓虹┖") + return + } + + if params.Method == constvar.PlcMethodModbusRTU && (params.DataBit == 0 || params.StopBit == 0 || params.Parity == 0) { + ctx.FailWithMsg(ecode.ParamsErr, "褰撴帴鍙f柟寮忎负modbusRTU鏃讹紝鏁版嵁浣嶏紝鍋滄浣嶏紝鏍¢獙浣嶄笉鑳戒负绌�") + return + } + + errCode := service.NewDevicePlcService().UpdateDevicePlc(¶ms) + if errCode != ecode.OK { + ctx.Fail(errCode) + return + } + ctx.Ok() } -- Gitblit v1.8.0