From 290f2a28f42ef340db651901fa513c6beae8d899 Mon Sep 17 00:00:00 2001
From: zhangqian <zhangqian@123.com>
Date: 星期四, 21 九月 2023 11:34:21 +0800
Subject: [PATCH] 待机逻辑更改,超过配置的时间数据不变认为待机

---
 api/v1/plc.go          |    9 ++++++---
 test/plc_test.go       |    6 +++---
 service/cache_store.go |   26 ++++++++++++++++++++++++--
 conf/config.go         |    2 +-
 conf/apsClient.json    |    2 +-
 5 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/api/v1/plc.go b/api/v1/plc.go
index 6a6392f..47cf4bc 100644
--- a/api/v1/plc.go
+++ b/api/v1/plc.go
@@ -1,6 +1,7 @@
 package v1
 
 import (
+	"apsClient/conf"
 	"apsClient/constvar"
 	"apsClient/model/request"
 	"apsClient/model/response"
@@ -12,6 +13,7 @@
 	"apsClient/service"
 	"github.com/gin-gonic/gin"
 	"github.com/spf13/cast"
+	"time"
 )
 
 type PlcApi struct{}
@@ -38,10 +40,11 @@
 	plcStatus := 1 //鏂紑杩炴帴
 	isConnect := service.PlcIsConnect()
 	if isConnect {
-		if resp.FinishNumber > 0 { //鐢熶骇
+		lastUpdateTime := service.FinishUpdateTimeGet(params.Position)
+		if time.Now().Unix()-cast.ToInt64(lastUpdateTime) < conf.Conf.PLC.StandbyTime { //鐢熶骇
 			plcStatus = 2
-		} else { //寰呮満
-			plcStatus = 3
+		} else {
+			plcStatus = 3 //寰呮満
 		}
 	}
 	resp.PlcStatus = plcStatus
diff --git a/conf/apsClient.json b/conf/apsClient.json
index 9e45ec5..d7bc4bf 100644
--- a/conf/apsClient.json
+++ b/conf/apsClient.json
@@ -34,7 +34,7 @@
     "modbusIntType": "DINT",
     "slaveId": 0,
     "package": "goborrow",
-    "keepAlive": "false"
+    "standbyTime": 300
   }
 }
 
diff --git a/conf/config.go b/conf/config.go
index d18e32e..9b10476 100644
--- a/conf/config.go
+++ b/conf/config.go
@@ -73,7 +73,7 @@
 		ModbusIntType            string
 		SlaveId                  int
 		Package                  string
-		KeepAlive                bool
+		StandbyTime              int64
 	}
 
 	config struct {
diff --git a/service/cache_store.go b/service/cache_store.go
index 9cbd07b..9b7b74d 100644
--- a/service/cache_store.go
+++ b/service/cache_store.go
@@ -1,10 +1,13 @@
 package service
 
 import (
+	"apsClient/constvar"
 	"apsClient/model"
 	"apsClient/model/response"
 	"fmt"
+	"github.com/spf13/cast"
 	"sync"
+	"time"
 )
 
 type CacheStore struct {
@@ -44,9 +47,10 @@
 }
 
 const (
-	PlcCacheKey             = "plc:%v:%v"
+	PlcCacheKey             = "plc:%v:%v" //plc:position:key
 	CurrentTaskCacheKey     = "current_task"
-	CurrentProgressCacheKey = "current_progress:%v"
+	CurrentProgressCacheKey = "current_progress:%v"          //current_progress:position
+	PlcCacheKeyUpdateTime   = "finish_number_update_time:%v" //finish_number_update_time:position
 )
 
 func PlcCacheGet(position int, key string) (interface{}, bool) {
@@ -54,9 +58,27 @@
 }
 
 func PlcCacheSet(position int, key string, value interface{}) {
+	if key == constvar.PlcCacheKeyFinishNumber {
+		oldFinishNumber, exists := PlcCacheGet(position, key)
+		if !exists || cast.ToInt(oldFinishNumber) != cast.ToInt(value) { //finishNumber鏈変簡鍙樺寲锛岃缃洿鏂版椂闂寸紦瀛�
+			FinishUpdateTimeSet(position, time.Now().Unix())
+		}
+	}
 	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, position, key), value)
 }
 
+func FinishUpdateTimeGet(position int) interface{} {
+	val, ok := defaultCacheStore.Get(fmt.Sprintf(PlcCacheKeyUpdateTime, position))
+	if ok {
+		return val
+	}
+	return 0
+}
+
+func FinishUpdateTimeSet(position int, value interface{}) {
+	defaultCacheStore.Add(fmt.Sprintf(PlcCacheKeyUpdateTime, position), value)
+}
+
 func TaskCacheSet(value *response.TaskData) {
 	defaultCacheStore.Add(CurrentTaskCacheKey, value)
 }
diff --git a/test/plc_test.go b/test/plc_test.go
index 0b5f564..92ea393 100644
--- a/test/plc_test.go
+++ b/test/plc_test.go
@@ -9,9 +9,9 @@
 
 func TestWriteHoldingRegister(t *testing.T) {
 	Init()
-	ipPort := "192.168.20.250:502"
-	address := 1104
-	value := 1104
+	ipPort := "127.0.0.1:502"
+	address := 1001
+	value := 1001
 	err := service.WriteHoldingRegister(ipPort, address, value)
 	if err != nil {
 		log.Fatal(err)

--
Gitblit v1.8.0