qixiaoning
2025-07-30 1882999b4ee899b79fa5e064238796f1e315d963
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package service
 
import (
    "encoding/json"
    "errors"
    "time"
    "vamicro/camera-common/vo"
    "vamicro/chanmanage-service/cache"
    "vamicro/chanmanage-service/models"
 
    "basic.com/pubsub/protomsg.git"
    "basic.com/valib/bhomeclient.git"
    "basic.com/valib/bhomedbapi.git"
    "basic.com/valib/logger.git"
)
 
const (
    ProcName = "chanmanage-service"
)
 
type PollSetService struct {
    Bk bhomeclient.Broker
}
 
func NewPollSetService(broker bhomeclient.Broker) *PollSetService {
    ps := &PollSetService{
        Bk: broker,
    }
 
    return ps
}
 
func (sv *PollSetService) GetPollConfig() (models.PollConfig, error) {
    var pcE models.PollConfig
    return pcE.GetOne()
}
 
func (sv *PollSetService) SavePollConfig(pc models.PollConfig) bool {
    if pc.Save() {
        dbMsg := protomsg.DbChangeMessage{
            Table:  protomsg.TableChanged_T_PollConfig,
            Action: protomsg.DbAction_Insert,
        }
        pb, _ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}
 
func (sv *PollSetService) UpdatePollConfig(pc models.PollConfig) bool {
    if pc.Update() {
        dbMsg := protomsg.DbChangeMessage{
            Table:  protomsg.TableChanged_T_PollConfig,
            Action: protomsg.DbAction_Update,
        }
        pb, _ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}
 
func (sv *PollSetService) UpdateEnable(enable bool) bool {
    var pc models.PollConfig
    if pc.UpdateEnable(enable) {
        dbMsg := protomsg.DbChangeMessage{
            Table:  protomsg.TableChanged_T_PollConfig,
            Action: protomsg.DbAction_Update,
        }
        pb, _ := json.Marshal(dbMsg)
        sv.Bk.Publish(ProcName, pb)
        return true
    }
    return false
}
 
// 触发条件
// 1.实时摄像机切换为轮询或者监控,或者实时由监控切换为实时
// 2.实时摄像机规则Enable状态改变
func (sv *PollSetService) ResetChannelCount() {
    //1.计算做实时任务的路数
    //2.本地文件占的路数
    //3.总路数减去实时占的路数以及本地文件占的路数,即为轮询占的路数
    runInfo := sv.StatisticRunInfo()
    realCount := runInfo.RealValidCount
    videoCount := 0
    pollCount := 0
    if realCount < runInfo.ChannelTotal {
        allocI := runInfo.ChannelTotal - realCount //实时剩余算力
        logger.Debug("allocI:", allocI)
        videoCount = runInfo.StackChannelCount
        if videoCount > allocI {
            videoCount = allocI
            pollCount = 0
        } else {
            pollCount = runInfo.ChannelTotal - realCount - videoCount
            if pollCount < 0 {
                pollCount = 0
            }
        }
    }
 
    sv.UpdateChannelCount(pollCount, videoCount)
}
 
// 拖动改变轮询和数据栈的数量
func (sv *PollSetService) UpdateChannelCount(pollChannelCount int, videoChannelCount int) bool {
    var fasApi bhomedbapi.FileStackApi
    var err error
    tx := models.GetDB().Begin()
    defer func() {
        if err != nil && tx != nil {
            tx.Rollback()
        }
    }()
    err = tx.Exec("update poll_config set pollChannelCount=?", pollChannelCount).Error
    if err != nil {
        return false
    }
    //改变数据栈的算力
    if !fasApi.UpdateChannelCount(videoChannelCount) {
        err = errors.New("fileSetting UpdateChannelCount ret false")
        return false
    }
 
    tx.Commit()
 
    dbMsg := protomsg.DbChangeMessage{
        Table:  protomsg.TableChanged_T_PollConfig,
        Action: protomsg.DbAction_Update,
    }
 
    pb, _ := json.Marshal(dbMsg)
    sv.Bk.Publish(ProcName, pb)
 
    return true
}
 
func (sv *PollSetService) StatisticRunInfo() vo.CameraRunStatistic {
    var v vo.CameraRunStatistic
    var camApi bhomedbapi.CameraApi
    var gbApi bhomedbapi.Gb28181Api
 
    timeStart := time.Now()
    sysconf, err1 := cache.GetServerInfo()
    if err1 == nil {
        v.ChannelTotal = int(sysconf.RealMax)
    }
    logger.Debug("statisticRunInfo 取完ServerInfo耗时:", time.Since(timeStart))
    timeStart = time.Now()
    camMap := make(map[string]protomsg.Camera)
    if cams := camApi.FindAll("", "", "", ""); cams != nil {
        for _, c := range cams {
            camMap[c.Id] = c
        }
    }
    logger.Debug("statisticRunInfo 取完本地摄像机耗时:", time.Since(timeStart))
    timeStart = time.Now()
 
    if cams := gbApi.FindAll("", "", "", ""); cams != nil {
        for _, c := range cams {
            camMap[c.Id] = c
        }
    }
    logger.Debug("statisticRunInfo 取完国标摄像机耗时:", time.Since(timeStart))
    timeStart = time.Now()
 
    allRules, e := cache.GetCameraRules()
    ruleM := make(map[string]protomsg.CameraAndRules)
    if e == nil && allRules != nil {
        for _, ar := range allRules {
            if ar.CameraInfo != nil {
                ruleM[ar.CameraInfo.Id] = ar
            }
        }
    }
 
    //获取督查任务
    tasks := models.GetTasks()
 
    rTotal := 0
    pTotal := 0
    for _, c := range camMap {
        if c.RunType == bhomeclient.TYPE_RUNTYPE_REALTIME {
            rTotal++
            if taskInfo, ok := tasks[c.Id]; ok && taskInfo != nil && len(taskInfo) > 0 {
                v.RealValidCount++
            } else {
                v.RealInvalidCount++
            }
 
            if c.IsRunning {
                v.RealRunningCount++
            }
        } else if c.RunType == bhomeclient.TYPE_RUNTYPE_POLL {
            // pTotal++
            // if crInfo, ok := ruleM[c.Id]; ok && crInfo.Rules != nil && len(crInfo.Rules) > 0 {
            //     v.PollValidCount++
            // } else {
            //     v.PollInvalidCount++
            // }
            // if c.IsRunning {
            //     v.PollRunningCount++
            // }
        }
    }
 
    //总算里暂时只统计实时算力
    v.ChannelTotal = rTotal
 
    v.RealTotal = rTotal
    v.PollTotal = pTotal
    logger.Debug("statisticRunInfo 获取完ExistRunningTask耗时:", time.Since(timeStart))
    timeStart = time.Now()
 
    //轮询通道数量从轮询配置中获取20250724移除
    // var pollConf models.PollConfig
    // pcE, err2 := pollConf.GetOne()
    // if err2 == nil {
    //     v.PollChannelCount = pcE.PollChannelCount
    // }
 
    //tmpPC := v.RealTotal - v.StackTotal
    //if tmpPC < 0  {
    //    v.PollChannelCount = 0
    //} else {
    //    v.PollChannelCount = tmpPC
    //}
    var fasApi bhomedbapi.FileAnalysisApi
    if set, err := fasApi.GetFileAnalysisSet(); err == nil {
        v.StackChannelCount = int(set.VideoChannelCount)
    }
    logger.Debug("statisticRunInfo 取完FileAnalysisSet耗时:", time.Since(timeStart))
    timeStart = time.Now()
 
    var sckApi bhomedbapi.FileStackApi
    sb, scks := sckApi.FindAllDoingStacks()
    if sb && scks != nil {
        for _, sck := range scks {
            if sck.Enable {
                //打开分析开关,纳入统计
                v.StackTotal++
                if crInfo, ok := ruleM[sck.Id]; ok && crInfo.Rules != nil && len(crInfo.Rules) > 0 {
                    v.StackValidCount++
                } else {
                    v.StackInvalidCount++
                }
                if sck.Status == bhomeclient.Stack_Status_Doing {
                    v.StackRunningCount++
                }
            }
        }
    }
    logger.Debug("statisticRunInfo 取完数据栈状态耗时:", time.Since(timeStart))
 
    // 山东断流监控测试
    //for i := 1; i < 10; i++ {
    //    v.DuanliuID = append(v.DuanliuID, strconv.Itoa(i)+"->"+strconv.Itoa(i)+"->"+"test-duanliu-"+strconv.Itoa(i))
    //}
 
    // 山东断流监控
    for _, k := range GetDuanliuIds() {
        // 解析摄像机信息
        var cameraInfo string
        if c, ok := camMap[k]; ok {
            cameraInfo = c.Id + "->" + c.Name + "->" + c.Rtsp
        } else {
            continue
        }
 
        v.DuanliuID = append(v.DuanliuID, cameraInfo)
    }
    logger.Debug("statisticRunInfo 断流数据:", v.DuanliuID)
 
    return v
}