zhangqian
2023-08-28 339ae89408bb2e5ba4428afd9c5f5fb64c04f2ad
下发任务参数和写入生产数量异步改同步
4个文件已修改
88 ■■■■■ 已修改文件
api/v1/task.go 59 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
model/device_plc.go 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pkg/ecode/code.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/plc.go 16 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
model/device_plc.go
@@ -26,6 +26,10 @@
        Detail     string `gorm:"type:varchar(2048);comment:数据详情" json:"-"`
        Details []*DevicePlcAddress `gorm:"-" json:"details"`
        MaxTryTimes     int   `gorm:"-" json:"-"` //最大写入重试次数
        CurrentTryTimes int   `gorm:"-" json:"-"` //当前写入重试次数
        CurrentErr      error `gorm:"-" json:"-"` //当前报错
    }
    DevicePlcAddress struct {
pkg/ecode/code.go
@@ -3,8 +3,9 @@
const (
    OK = 200
    UnknownErr = 2001 // 未知错误
    DBErr      = 2002 // db错误
    RedisErr   = 2003 // redis错误
    ParamsErr  = 2004 // 请求参数错误
    UnknownErr       = 2001 // 未知错误
    DBErr            = 2002 // db错误
    RedisErr         = 2003 // redis错误
    ParamsErr        = 2004 // 请求参数错误
    NeedConfirmedErr = 2005 // 需要弹窗确认的错误
)
service/plc.go
@@ -59,23 +59,29 @@
        ipAddr       string
    )
    if plcConfig.CurrentTryTimes > plcConfig.MaxTryTimes {
        return plcConfig.CurrentErr
    }
    plcConfig.CurrentTryTimes++
    for _, pc := range plcConfig.Details {
        if pc.FieldName == fieldType {
            startAddress = pc.StartAddress
        }
    }
    ipAddr = fmt.Sprintf("%s:%v", plcConfig.Address, plcConfig.Port)
    conn, err := plc.GetModbusConnection(ipAddr)
    if err != nil {
        logx.Errorf("PlcWrite 连接plc失败: %v", err.Error())
        return
        logx.Errorf("plc write failed, 连接plc失败: %v", err.Error())
        plcConfig.CurrentErr = err
        return PlcWrite(plcConfig, fieldType, value)
    }
    result, err := plc.WriteHoldingRegister(conn, startAddress, value)
    if err != nil {
        logx.Infof("plc write failed, address: %v, value: %v, err: %v", startAddress, value, err.Error())
        return
        logx.Errorf("plc write failed, address: %v, value: %v, err: %v", startAddress, value, err.Error())
        plcConfig.CurrentErr = err
        return PlcWrite(plcConfig, fieldType, value)
    }
    logx.Infof("plc write ok, address: %v, value: %v, result: %v", startAddress, value, result)
    return