zhangqian
2023-09-07 46d841d93d20773dd2cf31fa1caa7199c65fe037
定时同步生产进度到云端
3个文件已修改
56 ■■■■ 已修改文件
constvar/const.go 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
crontask/cron_task.go 21 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/progress.go 34 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constvar/const.go
@@ -8,6 +8,7 @@
    NsqTopicProcessParamsResponse     = "aps.%v.processParams.response"
    NsqTopicApsProcessParams          = "aps.%v.aps.processParams"     //有了新的工艺模型
    NsqTopicTaskProcedureStatusUpdate = "aps.%v.task.procedure.status" //工序状态更新
    NsqTopicSyncTaskProgress          = "aps.%v.task.procedure.progress" //工序生产进度
)
type PlcStartAddressType int
crontask/cron_task.go
@@ -3,9 +3,11 @@
import (
    "apsClient/conf"
    "apsClient/constvar"
    "apsClient/nsq"
    "apsClient/pkg/ecode"
    "apsClient/pkg/logx"
    "apsClient/service"
    "fmt"
    "github.com/go-co-op/gocron"
    "github.com/spf13/cast"
    "time"
@@ -39,7 +41,7 @@
        return err
    }
    s.Every(totalNumberTimeInterval).Seconds().StartImmediately().Do(func() {
    s.Every(totalNumberTimeInterval).Seconds().Do(func() {
        plcConfig, code := service.NewDevicePlcService().GetDevicePlc()
        if code != ecode.OK {
            return
@@ -51,6 +53,23 @@
        logx.Infof("plc read total number:%v, err:%v", totalNumber, err)
    })
    s.Every(60).Seconds().StartImmediately().Do(SyncProductionProgress) //同步生产数据
    s.StartAsync()
    return nil
}
func SyncProductionProgress() {
    progress, err := service.NewProgressService().GetCurrentProgress()
    if err != nil {
        return
    }
    if progress == nil {
        return
    }
    caller := nsq.NewCaller(fmt.Sprintf(constvar.NsqTopicSyncTaskProgress, conf.Conf.NsqConf.NodeId), "")
    err = caller.Send(progress)
    if err != nil {
        logx.Errorf("SyncProductionProgress error:%v", err.Error())
    }
}
service/progress.go
@@ -30,19 +30,9 @@
}
func (slf ProgressService) UpdateProgress(finishedQuantity int64) (err error) {
    var progressCache *model.ProductionProgress
    progressCache, ok := ProgressCacheGet()
    if !ok {
        progressCache, err = model.NewProductionProgressSearch(nil).SetOrder("id desc").First()
        if err == gorm.ErrRecordNotFound {
            return errors.New("progress cache not found")
        }
    progressCache, err := slf.GetCurrentProgress()
        if err != nil {
            return err
        }
        if progressCache.FinishedQuantity < progressCache.TotalQuantity {
            ProgressCacheSet(progressCache)
        }
    }
    if progressCache == nil {
        return errors.New("progress cache not found")
@@ -54,3 +44,25 @@
    }
    return nil
}
func (slf ProgressService) GetCurrentProgress() (progressCache *model.ProductionProgress, err error) {
    var ok bool
    progressCache, ok = ProgressCacheGet()
    if !ok {
        progressCache, err = model.NewProductionProgressSearch(nil).SetOrder("id desc").First()
        if err == gorm.ErrRecordNotFound {
            return nil, errors.New("progress not found")
        }
        if err != nil {
            return nil, err
        }
        if progressCache.FinishedQuantity >= progressCache.TotalQuantity { //如果完成量大于等于总量就说明是上一个已完成的任务,不是当前进行中的任务。
            progressCache = nil
        }
        if progressCache != nil {
            ProgressCacheSet(progressCache)
        }
    }
    return
}