使用分离方式,修改视频流返回方式,使用redis存是否在获取

This commit is contained in:
lijun 2025-01-18 20:21:38 +08:00
parent de9b8d23ac
commit 5552548a35
3 changed files with 20 additions and 30 deletions

View File

@ -143,19 +143,13 @@ func GetRealTimeImage(c *gin.Context) {
return
}
//查看设备是否在获取
isGet, ok := service.DeviceIsGettingFrame.Load(device.ID)
if !ok {
c.JSON(http.StatusOK, gin.H{"code": 4, "message": "device not getting frame or not exist"})
log.Printf("device:%d not found,isGet:%v , ok = %v", device.ID, isGet, ok)
return
}
isGet_ := isGet.(bool)
if isGet_ == false {
//直接返回
c.JSON(http.StatusOK, gin.H{"code": 4, "message": "device not getting frame or not exist"})
log.Printf("device:%d not found,isGet:%v , ok = %v", device.ID, isGet, ok)
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
if isGetting != "true" {
c.JSON(http.StatusOK, gin.H{"code": 4, "message": "device is not getting or not exist"})
log.Printf("device_id:%d is not getting or not exist", deviceIdInt)
return
}
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
log.Printf("connect wss err:%v", err)

11
main.go
View File

@ -80,6 +80,7 @@ func init() {
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat())
service.DeviceFrameCount[device.ID] = 0
service.DeviceIsGettingFrame.Store(device.ID, false)
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", device.ID), "false")
}
}
@ -96,16 +97,18 @@ func ReadConfigAndSetSystem() {
}
//检测是否需要获取设备流,如果需要则开启
for _, device := range proto.Config.DeviceInfo {
isGet, ok := service.DeviceIsGettingFrame.Load(device.ID)
isGet_ := isGet.(bool)
if !ok {
//isGet, ok := service.DeviceIsGettingFrame.Load(device.ID)
//isGet_ := isGet.(bool)
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
if isGetting == "" {
//说明没有这个设备,需初始化添加
service.DeviceRWMap.Store(device.ID, &sync.RWMutex{})
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat())
service.DeviceFrameCount[device.ID] = 0
service.DeviceIsGettingFrame.Store(device.ID, false)
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", device.ID), "false")
}
if isGet_ == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
if isGetting == "false" && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
switch device.ID {
case 1:
service.Device1CurrentFrame = gocv.NewMat()

View File

@ -303,13 +303,9 @@ func GetVideoStream(id int) {
log.Printf("设备:%d 错误: %v\n", id, err)
}
}()
is_get, ok := DeviceIsGettingFrame.Load(id)
if !ok {
log.Printf("device: %d not found", id)
return
}
if is_get == true {
log.Printf("device: %d is running!", id)
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
if isGetting == "true" || isGetting == "" {
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
return
}
for {
@ -328,14 +324,9 @@ func GetVideoStream(id int) {
log.Printf("device: %d not found", id)
break
}
is_get, ok = DeviceIsGettingFrame.Load(id)
if !ok {
log.Printf("device: %d not found", id)
break
}
isGet := is_get.(bool)
if isGet == true {
log.Printf("for device:%d is running!", id)
isGetting = worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
if isGetting == "true" {
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
break
}
@ -346,9 +337,11 @@ func GetVideoStream(id int) {
//设置设备控制信息
status := Get(device.Control)
DeviceIsGettingFrame.Store(id, true)
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", id), "true")
log.Printf("device: %d set control info status: %d", device.ID, status)
getVideoFrame(device)
DeviceIsGettingFrame.Store(id, false)
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", id), "false")
//等待1s
time.Sleep(1 * time.Second)
}