package service
|
|
import (
|
"apsClient/conf"
|
"apsClient/constvar"
|
"apsClient/model"
|
"fmt"
|
"github.com/jinzhu/gorm"
|
"github.com/spf13/cast"
|
"sync"
|
"time"
|
)
|
|
type CacheStore struct {
|
cache map[string]interface{}
|
mu sync.Mutex
|
}
|
|
var defaultCacheStore *CacheStore
|
|
func init() {
|
defaultCacheStore = newCacheManager()
|
}
|
func newCacheManager() *CacheStore {
|
return &CacheStore{
|
cache: make(map[string]interface{}),
|
}
|
}
|
|
func (cm *CacheStore) Get(key string) (interface{}, bool) {
|
cm.mu.Lock()
|
defer cm.mu.Unlock()
|
|
conn, ok := cm.cache[key]
|
return conn, ok
|
}
|
|
func (cm *CacheStore) Add(key string, value interface{}) {
|
cm.mu.Lock()
|
defer cm.mu.Unlock()
|
cm.cache[key] = value
|
}
|
|
func (cm *CacheStore) Remove(key string) {
|
cm.mu.Lock()
|
defer cm.mu.Unlock()
|
delete(cm.cache, key)
|
}
|
|
const (
|
PlcCacheKey = "plc:%v:%v:%v" //plc:deviceID:channel:key 缓存加工数或目标数
|
CurrentTaskCacheKey = "current_task:%v:%v" //current_task:deviceID:channel 缓存当前任务id
|
CurrentProgressCacheKey = "current_progress:%v:%v" //current_progress:deviceId:channel
|
PlcCacheKeyUpdateTime = "finish_number_update_time:%v:%v" //finish_number_update_time:deviceID:channel
|
TaskStartTimeCache = "task_start_time:%v:%v" //task_start_time:deviceID:channel
|
TaskEndTimeCache = "task_end_time:%v:%v" //task_end_time:deviceID:channel
|
)
|
|
func PlcCacheGet(deviceId string, channel int32, key string) (interface{}, bool) {
|
return defaultCacheStore.Get(fmt.Sprintf(PlcCacheKey, deviceId, channel, key))
|
}
|
|
func PlcCacheSet(deviceId string, channel int32, key string, value interface{}) {
|
if key == constvar.PlcCacheKeyFinishNumber {
|
oldFinishNumber, exists := PlcCacheGet(deviceId, channel, key)
|
if !exists || cast.ToInt(oldFinishNumber) != cast.ToInt(value) { //finishNumber有了变化,设置更新时间缓存
|
FinishUpdateTimeSet(deviceId, channel, time.Now().Unix())
|
}
|
}
|
defaultCacheStore.Add(fmt.Sprintf(PlcCacheKey, deviceId, channel, key), value)
|
}
|
|
// FinishUpdateTimeGet 用于判断plc状态,超过多少时间未更新视为待机
|
func FinishUpdateTimeGet(deviceId string, channel int32) interface{} {
|
val, ok := defaultCacheStore.Get(fmt.Sprintf(PlcCacheKeyUpdateTime, deviceId, channel))
|
if ok {
|
return val
|
}
|
return 0
|
}
|
|
func FinishUpdateTimeSet(deviceId string, channel int32, value interface{}) {
|
defaultCacheStore.Add(fmt.Sprintf(PlcCacheKeyUpdateTime, deviceId, channel), value)
|
}
|
|
func TaskStartTimeSet(deviceID string, channel int32, ts int64) {
|
defaultCacheStore.Add(fmt.Sprintf(TaskStartTimeCache, deviceID, channel), ts)
|
}
|
|
// TaskStartTimeGet 用于前端展示工序运行时间
|
func TaskStartTimeGet(deviceId string, channel int32) int64 {
|
if v, ok := defaultCacheStore.Get(fmt.Sprintf(TaskStartTimeCache, deviceId, channel)); ok {
|
return v.(int64)
|
}
|
procedure, err := model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.CurrentDeviceID).SetStatus(model.ProcedureStatusProcessing).SetChannels([]int32{channel}).First() //进行中任务
|
if err == gorm.ErrRecordNotFound {
|
procedure, err = model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.CurrentDeviceID).
|
SetStatus(model.ProcedureStatusFinished).SetChannels([]int32{channel}).SetOrder("real_end_time desc").First() //上一个结束的任务
|
if err == gorm.ErrRecordNotFound { //进行中和结束的都没有,开始时间和结束时间都设置0
|
TaskStartTimeSet(deviceId, channel, int64(0))
|
TaskEndTimeSet(deviceId, channel, int64(0))
|
return 0
|
} else {
|
TaskStartTimeSet(deviceId, channel, procedure.RealStartTime)
|
TaskEndTimeSet(deviceId, channel, procedure.RealEndTime)
|
return 0
|
}
|
} else {
|
TaskStartTimeSet(deviceId, channel, procedure.RealStartTime)
|
TaskEndTimeSet(deviceId, channel, int64(0))
|
return procedure.RealStartTime
|
}
|
}
|
|
func TaskEndTimeSet(deviceID string, channel int32, ts int64) {
|
defaultCacheStore.Add(fmt.Sprintf(TaskEndTimeCache, deviceID, channel), ts)
|
}
|
|
func TaskEndTimeGet(deviceID string, channel int32) int64 {
|
if v, ok := defaultCacheStore.Get(fmt.Sprintf(TaskEndTimeCache, deviceID, channel)); ok {
|
return v.(int64)
|
}
|
return 0
|
}
|
|
func TaskFlagSet(deviceID string, channel int32, taskId int) {
|
defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel), taskId)
|
}
|
|
func TaskFlagUnset(deviceID string, channel int32) {
|
defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel), 0)
|
}
|
|
func TaskFlagGet(deviceID string, channel int32) bool {
|
if v, ok := defaultCacheStore.Get(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel)); ok {
|
return v.(int) > 0
|
}
|
procedure, err := model.NewProceduresSearch(nil).SetDeviceId(conf.Conf.CurrentDeviceID).SetStatus(model.ProcedureStatusProcessing).SetChannels([]int32{channel}).First()
|
if err == gorm.ErrRecordNotFound {
|
defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel), 0)
|
return false
|
} else {
|
defaultCacheStore.Add(fmt.Sprintf(CurrentTaskCacheKey, deviceID, channel), int(procedure.ID))
|
return true
|
}
|
}
|
|
func ProgressCacheUnset(deviceID string, channel int32) {
|
defaultCacheStore.Remove(fmt.Sprintf(CurrentProgressCacheKey, deviceID, channel))
|
}
|