日志输出,空指针导致程序出错
This commit is contained in:
parent
7c8b0843a0
commit
10c7a6fb18
|
|
@ -144,11 +144,11 @@ func GetRealTimeImage(c *gin.Context) {
|
||||||
}
|
}
|
||||||
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.Fatalf("connect wss err:", err)
|
log.Printf("connect wss err:%v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5)
|
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5)
|
||||||
log.Fatalf("device_id:", device_id_int, " has set is_play to 1")
|
log.Printf("device_id:%d has set is_play to 1", device_id_int)
|
||||||
go subscribeAndHandleMessagesV3(ws, device_id_int)
|
go subscribeAndHandleMessagesV3(ws, device_id_int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -168,12 +168,12 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
if c != count {
|
if c != count {
|
||||||
//将img转[]byte
|
//将img转[]byte
|
||||||
if img.Empty() {
|
if img.Empty() {
|
||||||
log.Fatalf("device:%d img is empty!", device_id)
|
log.Printf("device:%d img is empty!", device_id)
|
||||||
} else {
|
} else {
|
||||||
//gocv.Matrix转为jpeg
|
//gocv.Matrix转为jpeg
|
||||||
buf, err := gocv.IMEncode(".jpg", img)
|
buf, err := gocv.IMEncode(".jpg", img)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("img encode err:", err)
|
log.Printf("img encode err:%v", err)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
@ -181,7 +181,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
|
|
||||||
err2 := ws.WriteMessage(websocket.BinaryMessage, buf1)
|
err2 := ws.WriteMessage(websocket.BinaryMessage, buf1)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Fatalf("send message to client err:", err2)
|
log.Printf("send message to client err:%v", err2)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
@ -192,7 +192,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
if t_count%10 == 0 {
|
if t_count%10 == 0 {
|
||||||
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
|
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Connection check failed:", err)
|
log.Printf("Connection check failed:%v", err)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
@ -202,7 +202,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
t_count++
|
t_count++
|
||||||
err := img.Close()
|
err := img.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("close img err:", err)
|
log.Printf("close img err:%v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,6 +210,6 @@ end:
|
||||||
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
||||||
if worker.IsContainKey(online_conn_key) == false {
|
if worker.IsContainKey(online_conn_key) == false {
|
||||||
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5)
|
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5)
|
||||||
log.Fatalf("device_id:", device_id, " has set is_play to 0")
|
log.Printf("device_id: %d has set is_play to 0", device_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -96,7 +96,7 @@ func ReadConfigAndSetSystem() {
|
||||||
}
|
}
|
||||||
if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
|
if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
|
||||||
go service.GetVideoStream(device.ID)
|
go service.GetVideoStream(device.ID)
|
||||||
log.Fatalf("device:%d has started!\n", device.ID)
|
log.Printf("device:%d has started!\n", device.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,8 @@ func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
|
||||||
}
|
}
|
||||||
mutex, ok := mutex_.(*sync.RWMutex)
|
mutex, ok := mutex_.(*sync.RWMutex)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatalf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d", device_id)
|
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d", device_id)
|
||||||
|
return gocv.NewMat(), -1
|
||||||
}
|
}
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
defer mutex.RUnlock()
|
defer mutex.RUnlock()
|
||||||
|
|
@ -59,7 +60,7 @@ func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
|
||||||
}
|
}
|
||||||
frame, ok := frameIface.(gocv.Mat)
|
frame, ok := frameIface.(gocv.Mat)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatalf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d", device_id)
|
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d", device_id)
|
||||||
}
|
}
|
||||||
frame_countIface, ok := DeviceFrameCount.Load(device_id)
|
frame_countIface, ok := DeviceFrameCount.Load(device_id)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|
@ -67,7 +68,7 @@ func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
|
||||||
}
|
}
|
||||||
frame_count, ok := frame_countIface.(int)
|
frame_count, ok := frame_countIface.(int)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatalf("DeviceFrameCount 存储的不是 int 类型,device_id: %d", device_id)
|
log.Printf("DeviceFrameCount 存储的不是 int 类型,device_id: %d", device_id)
|
||||||
}
|
}
|
||||||
return frame, frame_count
|
return frame, frame_count
|
||||||
}
|
}
|
||||||
|
|
@ -76,19 +77,19 @@ func getVideoFrame(device proto.DeviceInfo) {
|
||||||
//捕获异常
|
//捕获异常
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
log.Fatalf("设备:%d 错误: %v\n", device.ID, err)
|
log.Printf("设备:%d 错误: %v\n", device.ID, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
webcam, err := gocv.OpenVideoCapture(device.Stream)
|
webcam, err := gocv.OpenVideoCapture(device.Stream)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("设备:%s 错误: 无法打开视频流,err: %v\n", device.ID, err)
|
log.Printf("设备:%s 错误: 无法打开视频流,err: %v\n", device.ID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func(webcam *gocv.VideoCapture) {
|
defer func(webcam *gocv.VideoCapture) {
|
||||||
err2 := webcam.Close()
|
err2 := webcam.Close()
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Fatalf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error())
|
log.Printf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error())
|
||||||
}
|
}
|
||||||
}(webcam)
|
}(webcam)
|
||||||
// 字体相关设置,对应OpenCV默认字体等,这里简化处理,实际可按需求调整
|
// 字体相关设置,对应OpenCV默认字体等,这里简化处理,实际可按需求调整
|
||||||
|
|
@ -99,7 +100,7 @@ func getVideoFrame(device proto.DeviceInfo) {
|
||||||
z := 0
|
z := 0
|
||||||
for {
|
for {
|
||||||
if device.LogFrame > 0 && z%device.LogFrame == 0 {
|
if device.LogFrame > 0 && z%device.LogFrame == 0 {
|
||||||
log.Fatalf("设备:%d 当前帧: %d\n", device.ID, z)
|
log.Printf("设备:%d 当前帧: %d\n", device.ID, z)
|
||||||
}
|
}
|
||||||
if device.NextStop {
|
if device.NextStop {
|
||||||
break
|
break
|
||||||
|
|
@ -107,11 +108,11 @@ func getVideoFrame(device proto.DeviceInfo) {
|
||||||
frame := gocv.NewMat()
|
frame := gocv.NewMat()
|
||||||
ok := webcam.Read(&frame)
|
ok := webcam.Read(&frame)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Fatalf("设备 错误: 无法从视频流中读取帧:", device, "\n")
|
log.Printf("设备 错误: 无法从视频流中读取帧:", device, "\n")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if frame.Empty() {
|
if frame.Empty() {
|
||||||
log.Fatalf("设备:%s 错误: 无法从视频流中读取帧\n", device)
|
log.Printf("设备:%s 错误: 无法从视频流中读取帧\n", device)
|
||||||
//等待50ms
|
//等待50ms
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
continue
|
continue
|
||||||
|
|
@ -119,7 +120,7 @@ func getVideoFrame(device proto.DeviceInfo) {
|
||||||
height := frame.Rows()
|
height := frame.Rows()
|
||||||
width := frame.Cols()
|
width := frame.Cols()
|
||||||
if height < device.CheckFrameHeight || width < device.CheckFrameWidth {
|
if height < device.CheckFrameHeight || width < device.CheckFrameWidth {
|
||||||
log.Fatalf("设备:%s 帧尺寸已改变\n", device)
|
log.Printf("设备:%s 帧尺寸已改变\n", device)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
||||||
|
|
@ -127,12 +128,12 @@ func getVideoFrame(device proto.DeviceInfo) {
|
||||||
//需要将帧付给全局变量
|
//需要将帧付给全局变量
|
||||||
err3 := SetDeviceCurrentFrame(frame, device.ID)
|
err3 := SetDeviceCurrentFrame(frame, device.ID)
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
log.Fatalf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
|
log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
|
||||||
}
|
}
|
||||||
z++
|
z++
|
||||||
err2 := frame.Close()
|
err2 := frame.Close()
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Fatalf("设备:%d,计数z=%d, 错误: 无法关闭帧\n", device.ID, z)
|
log.Printf("设备:%d,计数z=%d, 错误: 无法关闭帧\n", device.ID, z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -154,11 +155,11 @@ func Get(url string) int {
|
||||||
func GetVideoStream(id int) {
|
func GetVideoStream(id int) {
|
||||||
is_get, ok := DeviceIsGettingFrame.Load(id)
|
is_get, ok := DeviceIsGettingFrame.Load(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Println("device:", id, " not found")
|
log.Printf("device: %d not found", id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if is_get == true {
|
if is_get == true {
|
||||||
fmt.Println("device:", id, " is running!")
|
log.Printf("device: %d is running!", id)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
|
@ -174,12 +175,12 @@ func GetVideoStream(id int) {
|
||||||
}
|
}
|
||||||
if index == len(proto.Config.DeviceInfo) {
|
if index == len(proto.Config.DeviceInfo) {
|
||||||
//设备不存在
|
//设备不存在
|
||||||
log.Println("device:", id, " not found")
|
log.Printf("device: %d not found", id)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
is_get, ok = DeviceIsGettingFrame.Load(id)
|
is_get, ok = DeviceIsGettingFrame.Load(id)
|
||||||
if is_get == true {
|
if is_get == true {
|
||||||
log.Println("for device:", id, " is running!")
|
log.Printf("for device:%d is running!", id)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -190,7 +191,7 @@ func GetVideoStream(id int) {
|
||||||
//设置设备控制信息
|
//设置设备控制信息
|
||||||
status := Get(device.Control)
|
status := Get(device.Control)
|
||||||
DeviceIsGettingFrame.Store(id, true)
|
DeviceIsGettingFrame.Store(id, true)
|
||||||
log.Println("device:", device.ID, " set control info status:", 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)
|
||||||
//等待1s
|
//等待1s
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue