diff --git a/handler/tool.go b/handler/tool.go index b2900d3..b38a194 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -10,6 +10,7 @@ import ( "github.com/gorilla/websocket" "gocv.io/x/gocv" "io" + "log" "net/http" "strconv" "time" @@ -143,11 +144,11 @@ func GetRealTimeImage(c *gin.Context) { } ws, err := upgrader.Upgrade(c.Writer, c.Request, nil) if err != nil { - fmt.Println("connect wss err:", err) + log.Fatalf("connect wss err:", err) return } worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5) - fmt.Println("device_id:", device_id_int, " has set is_play to 1") + log.Fatalf("device_id:", device_id_int, " has set is_play to 1") go subscribeAndHandleMessagesV3(ws, device_id_int) } @@ -167,12 +168,12 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) { if c != count { //将img转[]byte if img.Empty() { - fmt.Println("device:%d img is empty!", device_id) + log.Fatalf("device:%d img is empty!", device_id) } else { //gocv.Matrix转为jpeg buf, err := gocv.IMEncode(".jpg", img) if err != nil { - fmt.Println("img encode err:", err) + log.Fatalf("img encode err:", err) worker.SetRedisSetRemove(online_conn_key, con_id) goto end } @@ -180,7 +181,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) { err2 := ws.WriteMessage(websocket.BinaryMessage, buf1) if err2 != nil { - fmt.Println("send message to client err:", err2) + log.Fatalf("send message to client err:", err2) worker.SetRedisSetRemove(online_conn_key, con_id) goto end } @@ -191,7 +192,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) { if t_count%10 == 0 { err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second)) if err != nil { - fmt.Println("Connection check failed:", err) + log.Fatalf("Connection check failed:", err) worker.SetRedisSetRemove(online_conn_key, con_id) goto end } @@ -201,7 +202,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) { t_count++ err := img.Close() if err != nil { - fmt.Println("close img err:", err) + log.Fatalf("close img err:", err) } } @@ -209,6 +210,6 @@ end: // 查看是否还有其他连接,没有则设置 is_play 为 0 if worker.IsContainKey(online_conn_key) == false { worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5) - fmt.Println("device_id:", device_id, " has set is_play to 0") + log.Fatalf("device_id:", device_id, " has set is_play to 0") } } diff --git a/main.go b/main.go index 1fc7aa0..8c61d8e 100644 --- a/main.go +++ b/main.go @@ -6,7 +6,6 @@ import ( "VideoStream/proto" "VideoStream/service" "VideoStream/worker" - "fmt" "github.com/gin-gonic/gin" "github.com/golang-jwt/jwt" "github.com/robfig/cron/v3" @@ -97,7 +96,7 @@ func ReadConfigAndSetSystem() { } if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启 go service.GetVideoStream(device.ID) - fmt.Println("device:", device.ID, " has started!") + log.Fatalf("device:%d has started!\n", device.ID) } } } diff --git a/proto/conf.go b/proto/conf.go index e20fcd6..77686ac 100644 --- a/proto/conf.go +++ b/proto/conf.go @@ -3,6 +3,7 @@ package proto import ( "encoding/json" "fmt" + "log" "os" ) @@ -42,18 +43,18 @@ func ReadConfig(path string) error { //查看配置文件是否存在,不存在则创建 _, err := os.Stat(path) if err != nil { - fmt.Println("Config file not found!") + log.Fatalf("Config file not found!") //写入json文件 file, err := os.Create(path) if err != nil { - fmt.Println("Error creating config file") + log.Fatalf("Error creating config file") return err } defer file.Close() encoder := json.NewEncoder(file) err = encoder.Encode(&Config) if err != nil { - fmt.Println("Error encoding config") + log.Fatalf("Error encoding config") } return err } @@ -61,14 +62,14 @@ func ReadConfig(path string) error { //读json文件 file, err := os.Open(path) if err != nil { - fmt.Println("Error opening config file") + log.Fatalf("Error opening config file") return err } defer file.Close() decoder := json.NewDecoder(file) err = decoder.Decode(&Config) if err != nil { - fmt.Println("Error decoding config:", err) + log.Fatalf("Error decoding config:", err) return err } else { if Config.SERVER_PORT == "" { @@ -78,7 +79,7 @@ func ReadConfig(path string) error { //将配置写入redis jsonData, err2 := json.Marshal(Config) if err2 != nil { - fmt.Println("ReadConfigToSetSystem Error encoding config,err :", err2) + log.Fatalf("ReadConfigToSetSystem Error encoding config,err :", err2) } SigningKey = []byte(Config.TOKEN_SECRET) if Config.TOKEN_SECRET == "" { diff --git a/service/tool.go b/service/tool.go index 78cff2d..56ec981 100644 --- a/service/tool.go +++ b/service/tool.go @@ -73,12 +73,24 @@ func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) { } func getVideoFrame(device proto.DeviceInfo) { + //捕获异常 + defer func() { + if err := recover(); err != nil { + log.Fatalf("设备:%d 错误: %v\n", device.ID, err) + } + }() + webcam, err := gocv.OpenVideoCapture(device.Stream) if err != nil { - fmt.Printf("设备:%s 错误: 无法打开视频流,err: %v\n", device.ID, err) + log.Fatalf("设备:%s 错误: 无法打开视频流,err: %v\n", device.ID, err) return } - defer webcam.Close() + defer func(webcam *gocv.VideoCapture) { + err2 := webcam.Close() + if err2 != nil { + log.Fatalf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error()) + } + }(webcam) // 字体相关设置,对应OpenCV默认字体等,这里简化处理,实际可按需求调整 font := gocv.FontHersheySimplex fontScale := 0.5 @@ -87,7 +99,7 @@ func getVideoFrame(device proto.DeviceInfo) { z := 0 for { if device.LogFrame > 0 && z%device.LogFrame == 0 { - fmt.Printf("设备:%d 当前帧: %d\n", device.ID, z) + log.Fatalf("设备:%d 当前帧: %d\n", device.ID, z) } if device.NextStop { break @@ -95,11 +107,11 @@ func getVideoFrame(device proto.DeviceInfo) { frame := gocv.NewMat() ok := webcam.Read(&frame) if !ok { - fmt.Printf("设备 错误: 无法从视频流中读取帧:", device, "\n") + log.Fatalf("设备 错误: 无法从视频流中读取帧:", device, "\n") break } if frame.Empty() { - fmt.Printf("设备:%s 错误: 无法从视频流中读取帧\n", device) + log.Fatalf("设备:%s 错误: 无法从视频流中读取帧\n", device) //等待50ms time.Sleep(50 * time.Millisecond) continue @@ -107,7 +119,7 @@ func getVideoFrame(device proto.DeviceInfo) { height := frame.Rows() width := frame.Cols() if height < device.CheckFrameHeight || width < device.CheckFrameWidth { - fmt.Printf("设备:%s 帧尺寸已改变\n", device) + log.Fatalf("设备:%s 帧尺寸已改变\n", device) break } currentTime := time.Now().Format("2006-01-02 15:04:05") @@ -115,12 +127,12 @@ func getVideoFrame(device proto.DeviceInfo) { //需要将帧付给全局变量 err3 := SetDeviceCurrentFrame(frame, device.ID) if err3 != nil { - fmt.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error()) + log.Fatalf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error()) } z++ err2 := frame.Close() if err2 != nil { - fmt.Println("设备:%d,计数z=%d, 错误: 无法关闭帧", device.ID, z) + log.Fatalf("设备:%d,计数z=%d, 错误: 无法关闭帧\n", device.ID, z) } } }