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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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")
        }
    }
}