zhangqian
2023-09-21 290f2a28f42ef340db651901fa513c6beae8d899
待机逻辑更改,超过配置的时间数据不变认为待机
5个文件已修改
45 ■■■■ 已修改文件
api/v1/plc.go 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/apsClient.json 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
conf/config.go 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
service/cache_store.go 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/plc_test.go 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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
conf/apsClient.json
@@ -34,7 +34,7 @@
    "modbusIntType": "DINT",
    "slaveId": 0,
    "package": "goborrow",
    "keepAlive": "false"
    "standbyTime": 300
  }
}
conf/config.go
@@ -73,7 +73,7 @@
        ModbusIntType            string
        SlaveId                  int
        Package                  string
        KeepAlive                bool
        StandbyTime              int64
    }
    config struct {
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)
}
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)