package nvcs
|
|
import (
|
"fmt"
|
"time"
|
|
"gat1400Exchange/config"
|
"gat1400Exchange/models"
|
"gat1400Exchange/pkg/logger"
|
)
|
|
type simpleCache struct {
|
data map[int64]elevatorRunData
|
expiration time.Duration
|
}
|
|
func newCache(expiration time.Duration) *simpleCache {
|
return &simpleCache{
|
data: make(map[int64]elevatorRunData),
|
expiration: expiration,
|
}
|
}
|
|
// 存储数据到缓存中
|
func (c *simpleCache) store(data elevatorRunData) {
|
if config.RFIDConf.ReadFloor && gRFIDFloor != data.Floor {
|
if data.RunState == RunStop {
|
logger.Warn("A floor error has occurred rfid epc %s, nvcs floor %s", gRFIDFloor, data.Floor)
|
}
|
|
data.Floor = gRFIDFloor
|
}
|
|
// 如果楼层相同,并且数据在1秒内,则忽略
|
if existingData, ok := c.data[data.Timestamp]; ok {
|
if existingData.Floor == data.Floor {
|
return
|
}
|
}
|
|
// 数据库保存一份
|
go func() {
|
var d = models.Positions{
|
DeviceId: data.Device,
|
Pos: data.Floor,
|
RunDir: data.RunState,
|
CreateTime: time.Now().Unix(),
|
TimeString: time.Now().Format("2006-01-02 15:04:05"),
|
}
|
|
err := d.Save()
|
if err != nil {
|
logger.Warn("Device position update error:%s", err.Error())
|
}
|
}()
|
|
// 写OSD
|
var runStateStr string
|
if config.NVCSConf.RunState {
|
if data.RunState == RunUp {
|
runStateStr = "上"
|
} else if data.RunState == RunDown {
|
runStateStr = "下"
|
}
|
}
|
|
// 设置osd 格式 "1F上 固 枪"
|
if config.NVCSConf.OSD != "" {
|
floorText := fmt.Sprintf("%s%s %s", data.Floor, runStateStr, config.NVCSConf.OSD)
|
|
// 调用hik api 将文字添加到osd的左下角
|
go addFloorToOSD(floorText)
|
}
|
|
c.data[data.Timestamp] = data
|
}
|
|
// 删除过期数据
|
func (c *simpleCache) cleanExpired() {
|
for t := range c.data {
|
if time.Since(time.Unix(t, 0)) > c.expiration {
|
delete(c.data, t)
|
}
|
}
|
}
|