package cache
|
|
import (
|
"basic.com/pubsub/cache.git/shardmap"
|
"basic.com/pubsub/protomsg.git"
|
"basic.com/valib/bhomeclient.git"
|
"basic.com/valib/bhomedbapi.git"
|
"basic.com/valib/c_bhomebus.git/proto/source/bhome_msg"
|
"basic.com/valib/logger.git"
|
"encoding/json"
|
"errors"
|
)
|
|
var cMap *shardmap.ShardMap
|
|
const (
|
SERVER_KEY = "SERVERINFO" //服务器信息
|
LOCALCAMERAS = "LOCALCAMERAS" //本地摄像机列表
|
GB28181CAMERAS = "GB28181CAMERAS" //国标摄像机列表
|
SCENERULES = "SCENERULES" //摄像机场景列表
|
)
|
|
func UpdateBySub(msg *bhome_msg.MsgPublish) {
|
switch string(msg.Topic) {
|
case bhomeclient.Proc_System_Service:
|
initServerInfo()
|
case bhomeclient.Proc_Camera_Service, bhomeclient.Proc_Gb28181_Service, bhomeclient.Proc_Scene_Service:
|
var protoMsg protomsg.DbChangeMessage
|
if err := json.Unmarshal(msg.Data, &protoMsg); err == nil {
|
if protoMsg.Table == protomsg.TableChanged_T_Camera {
|
// initLocalCameras()
|
} else if protoMsg.Table == protomsg.TableChanged_T_Camera {
|
// initGb28181Cameras()
|
} else if protoMsg.Table == protomsg.TableChanged_T_CameraRule {
|
initCameraRules()
|
}
|
}
|
}
|
}
|
|
//缓存一些其他数据库的数据,并通过订阅消息实时更新
|
func InitDataCache() {
|
cMap = shardmap.New(uint8(32))
|
|
initServerInfo() //初始化服务器配置信息
|
|
// 代码中不需要摄像机信息
|
// initLocalCameras()
|
// initGb28181Cameras()
|
|
initCameraRules()
|
}
|
|
func initServerInfo() {
|
var api bhomedbapi.SysSetApi
|
b, s := api.GetServerInfo()
|
logger.Debug("GetServerInfo b:", b, "s:", s)
|
if b && s.ServerId != "" {
|
cMap.Set(SERVER_KEY, s)
|
}
|
}
|
|
func GetServerInfo() (conf protomsg.LocalConfig, err error) {
|
config, b := cMap.Get(SERVER_KEY)
|
if b {
|
return config.(protomsg.LocalConfig), nil
|
} else {
|
initServerInfo()
|
nc, nb := cMap.Get(SERVER_KEY)
|
if nb {
|
return nc.(protomsg.LocalConfig), nil
|
} else {
|
return conf, errors.New("conf not found")
|
}
|
}
|
}
|
|
func initLocalCameras() {
|
var camApi bhomedbapi.CameraApi
|
localCams := camApi.FindAll("", "", "", "") //获取所有本地摄像机
|
cMap.Set(LOCALCAMERAS, localCams)
|
}
|
|
func GetLocalCameras() ([]protomsg.Camera, error) {
|
cams, b := cMap.Get(LOCALCAMERAS)
|
if b && cams != nil {
|
return cams.([]protomsg.Camera), nil
|
} else {
|
initLocalCameras()
|
nc, nb := cMap.Get(LOCALCAMERAS)
|
if nb {
|
return nc.([]protomsg.Camera), nil
|
} else {
|
return nil, errors.New("no cached localCameras")
|
}
|
}
|
}
|
|
func initGb28181Cameras() {
|
var camApi bhomedbapi.Gb28181Api
|
gbCams := camApi.FindAll("", "", "", "") //获取所有本地摄像机
|
cMap.Set(GB28181CAMERAS, gbCams)
|
}
|
|
func GetGb28181Cameras() ([]protomsg.Camera, error) {
|
cams, b := cMap.Get(GB28181CAMERAS)
|
if b && cams != nil {
|
return cams.([]protomsg.Camera), nil
|
} else {
|
initGb28181Cameras()
|
nc, nb := cMap.Get(GB28181CAMERAS)
|
if nb {
|
return nc.([]protomsg.Camera), nil
|
} else {
|
return nil, errors.New("no cached gb28181Cameras")
|
}
|
}
|
}
|
|
func initCameraRules() {
|
var api bhomedbapi.CameraRuleApi
|
b, allCamRules := api.FindAll()
|
logger.Debug("initCameraRules b:", b, " len(allCamRules):", len(allCamRules))
|
if b {
|
cMap.Set(SCENERULES, allCamRules)
|
}
|
}
|
|
func GetCameraRules() ([]protomsg.CameraAndRules, error) {
|
allRules, b := cMap.Get(SCENERULES)
|
if b {
|
return allRules.([]protomsg.CameraAndRules), nil
|
} else {
|
initCameraRules()
|
nr, nb := cMap.Get(SCENERULES)
|
if nb {
|
return nr.([]protomsg.CameraAndRules), nil
|
} else {
|
return nil, errors.New("no CameraAndRules cached")
|
}
|
}
|
}
|