From 5a9e2e97e78a05209a57a75a75678d67c32c58d5 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期六, 21 十月 2023 11:25:20 +0800
Subject: [PATCH] debug over

---
 service/cache_store.go |   66 ++++++++++++++++++--------------
 1 files changed, 37 insertions(+), 29 deletions(-)

diff --git a/service/cache_store.go b/service/cache_store.go
index 9b7b74d..743e4a7 100644
--- a/service/cache_store.go
+++ b/service/cache_store.go
@@ -1,10 +1,11 @@
 package service
 
 import (
+	"apsClient/conf"
 	"apsClient/constvar"
 	"apsClient/model"
-	"apsClient/model/response"
 	"fmt"
+	"github.com/jinzhu/gorm"
 	"github.com/spf13/cast"
 	"sync"
 	"time"
@@ -47,64 +48,71 @@
 }
 
 const (
-	PlcCacheKey             = "plc:%v:%v" //plc:position:key
-	CurrentTaskCacheKey     = "current_task"
-	CurrentProgressCacheKey = "current_progress:%v"          //current_progress:position
-	PlcCacheKeyUpdateTime   = "finish_number_update_time:%v" //finish_number_update_time:position
+	PlcCacheKey             = "plc:%v:%v"                    //plc:channel:key
+	CurrentTaskCacheKey     = "current_task:%v"              //current_task:channel
+	CurrentProgressCacheKey = "current_progress:%v"          //current_progress:channel
+	PlcCacheKeyUpdateTime   = "finish_number_update_time:%v" //finish_number_update_time:channel
 )
 
-func PlcCacheGet(position int, key string) (interface{}, bool) {
-	return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, position, key))
+func PlcCacheGet(channel int32, key string) (interface{}, bool) {
+	return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, channel, key))
 }
 
-func PlcCacheSet(position int, key string, value interface{}) {
+func PlcCacheSet(channel int32, key string, value interface{}) {
 	if key == constvar.PlcCacheKeyFinishNumber {
-		oldFinishNumber, exists := PlcCacheGet(position, key)
+		oldFinishNumber, exists := PlcCacheGet(channel, key)
 		if !exists || cast.ToInt(oldFinishNumber) != cast.ToInt(value) { //finishNumber鏈変簡鍙樺寲锛岃缃洿鏂版椂闂寸紦瀛�
-			FinishUpdateTimeSet(position, time.Now().Unix())
+			FinishUpdateTimeSet(channel, time.Now().Unix())
 		}
 	}
-	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, position, key), value)
+	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, channel, key), value)
 }
 
-func FinishUpdateTimeGet(position int) interface{} {
-	val, ok := defaultCacheStore.Get(fmt.Sprintf(PlcCacheKeyUpdateTime, position))
+func FinishUpdateTimeGet(channel int32) interface{} {
+	val, ok := defaultCacheStore.Get(fmt.Sprintf(PlcCacheKeyUpdateTime, channel))
 	if ok {
 		return val
 	}
 	return 0
 }
 
-func FinishUpdateTimeSet(position int, value interface{}) {
-	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKeyUpdateTime, position), value)
+func FinishUpdateTimeSet(channel int32, value interface{}) {
+	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKeyUpdateTime, channel), value)
 }
 
-func TaskCacheSet(value *response.TaskData) {
-	defaultCacheStore.Add(CurrentTaskCacheKey, value)
+func TaskFlagSet(channel int32) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), true)
 }
 
-func TaskCacheUnset() {
-	defaultCacheStore.Remove(CurrentTaskCacheKey)
+func TaskFlagUnset(channel int32) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), false)
 }
 
-func TaskCacheGet() (*response.TaskData, bool) {
-	if v, ok := defaultCacheStore.Get(CurrentTaskCacheKey); ok {
-		return v.(*response.TaskData), ok
+func TaskFlagGet(channel int32) bool {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentTaskCacheKey, channel)); ok {
+		return v.(bool)
 	}
-	return nil, false
+	_, err := model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.System.DeviceId).SetStatus(model.ProcedureStatusProcessing).SetChannels([]int32{channel}).First()
+	if err == gorm.ErrRecordNotFound {
+		defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), false)
+		return false
+	} else {
+		defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, channel), true)
+		return true
+	}
 }
 
-func ProgressCacheGet(position int) (*model.ProductionProgress, bool) {
-	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentProgressCacheKey, position)); ok {
+func ProgressCacheGet(channel int32) (*model.ProductionProgress, bool) {
+	if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentProgressCacheKey, channel)); ok {
 		return v.(*model.ProductionProgress), ok
 	}
 	return nil, false
 }
 
-func ProgressCacheSet(position int, value *model.ProductionProgress) {
-	defaultCacheStore.Add(fmt.Sprintf(CurrentProgressCacheKey, position), value)
+func ProgressCacheSet(channel int32, value *model.ProductionProgress) {
+	defaultCacheStore.Add(fmt.Sprintf(CurrentProgressCacheKey, channel), value)
 }
 
-func ProgressCacheUnset(position int) {
-	defaultCacheStore.Remove(fmt.Sprintf(CurrentProgressCacheKey, position))
+func ProgressCacheUnset(channel int32) {
+	defaultCacheStore.Remove(fmt.Sprintf(CurrentProgressCacheKey, channel))
 }

--
Gitblit v1.8.0