qixiaoning
2025-07-08 84d2ef9760af0a4a4aa933937294400b3caa291d
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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"
    "strconv"
)
 
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 && 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 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")
        }
    }
}
 
//获取weedfs的访问地址
func GetWeedfsUri() (serverId string,uri string,err error) {
    localConf, err2 := GetServerInfo()
    if err2 !=nil || localConf.WebPicIp == "" {
        logger.Debug("localConfig is wrong!!!")
        return "", "", errors.New("localConfig err")
    }
    weedfsUri := "http://"+localConf.WebPicIp+":"+strconv.Itoa(int(localConf.WebPicPort))+"/submit?collection="+localConf.ServerId+"-persistent"
    return localConf.ServerId, weedfsUri, nil
}
 
//获取ES的访问地址
func GetESConfig() (serverId string, ip string,port int,err error) {
    localConf, err2 := GetServerInfo()
    if err2 !=nil || localConf.WebPicIp == "" {
        logger.Debug("localConfig is wrong!!!")
        return "", "", 0, errors.New("localConfig err")
    }
    return localConf.ServerId, localConf.AlarmIp, int(localConf.AlarmPort), nil
}