qixiaoning
2025-07-18 24f44f6ecefb5e83295bab670533529c6bc81810
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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" //服务器信息
    SCENERULES = "SCENERULES" //摄像机场景列表
)
 
func UpdateBySub(msg *bhome_msg.MsgPublish) {
    switch string(msg.Topic) {
    case bhomeclient.Proc_System_Service:
        initServerInfo()
    case bhomeclient.Proc_Scene_Service:
        var protoMsg protomsg.DbChangeMessage
        if err := json.Unmarshal(msg.Data, &protoMsg); err == nil {
            initCameraRules()
        }
    }
}
 
//缓存一些其他数据库的数据,并通过订阅消息实时更新
func InitDataCache() {
    cMap = shardmap.New(uint8(32))
 
    initServerInfo() //初始化服务器配置信息
 
    initCameraRules()
}
 
func initServerInfo() {
    var api bhomedbapi.SysSetApi
    b, s := api.GetServerInfo()
    logger.Debug("GetServerInfo b:", b, "s:", s)
    if b && len(s.ServerId) != 0 {
        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 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")
        }
    }
}