panlei
2019-12-05 4452794b46d66af2f83878855af7571857d946fa
cache/cache.go
@@ -6,6 +6,7 @@
   "basic.com/pubsub/protomsg.git"
   "basic.com/valib/gopherdiscovery.git"
   "basic.com/valib/logger.git"
   "encoding/json"
   "errors"
   "fmt"
   "github.com/gogo/protobuf/proto"
@@ -49,7 +50,9 @@
   initTimeRules() //初始化时间规则缓存
   initCameraTaskRules() //初始化摄像机任务规则缓存
   initCameraTaskRules() //初始化摄像机任务规则缓存按摄像机
   initCameraTaskRulesAll() // 初始化摄像机任务规则缓存全部
   initCamera() //初始化摄像机信息
@@ -84,6 +87,7 @@
      initCameraTaskRules()
   case protomsg.TableChanged_T_CameraTaskArgs: //更新摄像机规则配置参数
      initCameraTaskRules()
      initCameraTaskRulesAll()
   case protomsg.TableChanged_T_Sdk: //更新sdk配置
      initSdks()
   default:
@@ -126,6 +130,16 @@
      cMap.Set(PREFIX_POLYGON+k, v)
   }
}
// 缓存区域并不根据id
func initPolygonsById() {
   var api dbapi.CameraApi
   data := api.FindAllPolygons()
   for _, item := range data {
      if item.Type != "line" {
         cMap.Set(PREFIX_POLYGON+item.Id, item)
      }
   }
}
func initTimeRules() {
   var api dbapi.CameraApi
@@ -141,9 +155,13 @@
   var api dbapi.DicApi
   flag, dics := api.FindByType("")
   if flag {
      for key, dics1 := range dics.(map[string]interface{}) {
         for _,dic := range dics1.([]Dic) {
            cMap.Set(PREFIX_DIC+key+dic.value, dic)
      var dicss map[string][]Dic
      b, _ := json.Marshal(dics)
      if err := json.Unmarshal(b, &dicss); err == nil {
         for key, dics1 := range dicss {
            for _, dic := range dics1 {
               cMap.Set(PREFIX_DIC + key + dic.Value, dic)
            }
         }
      }
   }
@@ -158,6 +176,11 @@
   }
}
func initCameraTaskRulesAll() {
   var api dbapi.CameraTaskArgsApi
   all := api.FindAll()
   cMap.Set(PREFIX_RULE, all)
}
func initSdks() {
   var api dbapi.SdkApi
   sdks := api.FindAll("")
@@ -194,6 +217,15 @@
   }
}
func GetPolygonsById(id string) *protomsg.CameraPolygon {
   obj, b := cMap.Get(PREFIX_POLYGON + id)
   if b {
      return obj.(*protomsg.CameraPolygon)
   } else {
      return nil
   }
}
//从缓存中获取时间规则
func GetTimeRuleById(id string) (exist bool, rule protomsg.CameraTimerule) {
   obj, b := cMap.Get(PREFIX_TIME + id)
@@ -213,6 +245,15 @@
      return nil
   }
}
//根据摄像机id从缓存中获取摄像机的任务规则设置
func GetCameraTaskRulesAll() []*protomsg.TaskGroupArgs {
   obj, b := cMap.Get(PREFIX_RULE)
   if b {
      return obj.([]*protomsg.TaskGroupArgs)
   } else {
      return nil
   }
}
func GetSdkById(sdkId string) (sdk protomsg.Sdk, err error) {
   obj, b := cMap.Get(PREFIX_SDK + sdkId)
@@ -222,16 +263,16 @@
      return sdk, errors.New("sdk not found")
   }
}
// 获取字典值
func GetDic(key string) (value string) {
   obj, b := cMap.Get(PREFIX_SDK + key)
func GetDic(key string) (name string) {
   obj, b := cMap.Get(PREFIX_DIC + key)
   if b {
      return obj.(Dic).value
      return obj.(Dic).Name
   } else {
      return ""
   }
}
func initSoData() {
   var api dbapi.SoApi
@@ -253,11 +294,11 @@
}
type Dic struct {
   Id          string
   value       string
   Name        string
   Type        string
   Description string
   Sort        int
   Parent_id   string
   Id          string `json:"id"`
   Value       string `json:"value"`
   Name        string `json:"name"`
   Type        string `json:"type"`
   Description string `json:"description"`
   Sort        int    `json:"sort"`
   Parent_id   string `json:"parent_id"`
}