使用分离方式,修改视频流返回方式,使用redis存是否在获取
This commit is contained in:
parent
de9b8d23ac
commit
5552548a35
|
|
@ -143,19 +143,13 @@ func GetRealTimeImage(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//查看设备是否在获取
|
//查看设备是否在获取
|
||||||
isGet, ok := service.DeviceIsGettingFrame.Load(device.ID)
|
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
|
||||||
if !ok {
|
if isGetting != "true" {
|
||||||
c.JSON(http.StatusOK, gin.H{"code": 4, "message": "device not getting frame or not exist"})
|
c.JSON(http.StatusOK, gin.H{"code": 4, "message": "device is not getting or not exist"})
|
||||||
log.Printf("device:%d not found,isGet:%v , ok = %v", device.ID, isGet, ok)
|
log.Printf("device_id:%d is not getting or not exist", deviceIdInt)
|
||||||
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)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("connect wss err:%v", err)
|
log.Printf("connect wss err:%v", err)
|
||||||
|
|
|
||||||
11
main.go
11
main.go
|
|
@ -80,6 +80,7 @@ func init() {
|
||||||
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat())
|
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat())
|
||||||
service.DeviceFrameCount[device.ID] = 0
|
service.DeviceFrameCount[device.ID] = 0
|
||||||
service.DeviceIsGettingFrame.Store(device.ID, false)
|
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 {
|
for _, device := range proto.Config.DeviceInfo {
|
||||||
isGet, ok := service.DeviceIsGettingFrame.Load(device.ID)
|
//isGet, ok := service.DeviceIsGettingFrame.Load(device.ID)
|
||||||
isGet_ := isGet.(bool)
|
//isGet_ := isGet.(bool)
|
||||||
if !ok {
|
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
|
||||||
|
if isGetting == "" {
|
||||||
//说明没有这个设备,需初始化添加
|
//说明没有这个设备,需初始化添加
|
||||||
service.DeviceRWMap.Store(device.ID, &sync.RWMutex{})
|
service.DeviceRWMap.Store(device.ID, &sync.RWMutex{})
|
||||||
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat())
|
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat())
|
||||||
service.DeviceFrameCount[device.ID] = 0
|
service.DeviceFrameCount[device.ID] = 0
|
||||||
service.DeviceIsGettingFrame.Store(device.ID, false)
|
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 {
|
switch device.ID {
|
||||||
case 1:
|
case 1:
|
||||||
service.Device1CurrentFrame = gocv.NewMat()
|
service.Device1CurrentFrame = gocv.NewMat()
|
||||||
|
|
|
||||||
|
|
@ -303,13 +303,9 @@ func GetVideoStream(id int) {
|
||||||
log.Printf("设备:%d 错误: %v\n", id, err)
|
log.Printf("设备:%d 错误: %v\n", id, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
is_get, ok := DeviceIsGettingFrame.Load(id)
|
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
|
||||||
if !ok {
|
if isGetting == "true" || isGetting == "" {
|
||||||
log.Printf("device: %d not found", id)
|
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
|
||||||
return
|
|
||||||
}
|
|
||||||
if is_get == true {
|
|
||||||
log.Printf("device: %d is running!", id)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
|
@ -328,14 +324,9 @@ func GetVideoStream(id int) {
|
||||||
log.Printf("device: %d not found", id)
|
log.Printf("device: %d not found", id)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
is_get, ok = DeviceIsGettingFrame.Load(id)
|
isGetting = worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
|
||||||
if !ok {
|
if isGetting == "true" {
|
||||||
log.Printf("device: %d not found", id)
|
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
|
||||||
break
|
|
||||||
}
|
|
||||||
isGet := is_get.(bool)
|
|
||||||
if isGet == true {
|
|
||||||
log.Printf("for device:%d is running!", id)
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -346,9 +337,11 @@ func GetVideoStream(id int) {
|
||||||
//设置设备控制信息
|
//设置设备控制信息
|
||||||
status := Get(device.Control)
|
status := Get(device.Control)
|
||||||
DeviceIsGettingFrame.Store(id, true)
|
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)
|
log.Printf("device: %d set control info status: %d", device.ID, status)
|
||||||
getVideoFrame(device)
|
getVideoFrame(device)
|
||||||
DeviceIsGettingFrame.Store(id, false)
|
DeviceIsGettingFrame.Store(id, false)
|
||||||
|
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", id), "false")
|
||||||
//等待1s
|
//等待1s
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue