Compare commits
No commits in common. "15a78b10c531cca633c2fab8e68c5ab33af1073c" and "788c40131f57e4d8281bafbb0c003abe37d37f2f" have entirely different histories.
15a78b10c5
...
788c40131f
2
go.sum
2
go.sum
|
|
@ -108,8 +108,6 @@ github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65E
|
||||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||||
gocv.io/x/gocv v0.39.0 h1:vWHupDE22LebZW6id2mVeT767j1YS8WqGt+ZiV7XJXE=
|
gocv.io/x/gocv v0.39.0 h1:vWHupDE22LebZW6id2mVeT767j1YS8WqGt+ZiV7XJXE=
|
||||||
gocv.io/x/gocv v0.39.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU=
|
gocv.io/x/gocv v0.39.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU=
|
||||||
gocv.io/x/gocv v0.40.0 h1:kGBu/UVj+dO6A9dhQmGOnCICSL7ke7b5YtX3R3azdXI=
|
|
||||||
gocv.io/x/gocv v0.40.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU=
|
|
||||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"gocv.io/x/gocv"
|
"gocv.io/x/gocv"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -144,11 +143,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.Printf("connect wss err:%v", err)
|
fmt.Println("connect wss err:", 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.Printf("device_id:%d has set is_play to 1", device_id_int)
|
fmt.Println("device_id:", device_id_int, " has set is_play to 1")
|
||||||
go subscribeAndHandleMessagesV3(ws, device_id_int)
|
go subscribeAndHandleMessagesV3(ws, device_id_int)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -164,40 +163,25 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
t_count := 0
|
t_count := 0
|
||||||
for {
|
for {
|
||||||
//从service获取当前帧
|
//从service获取当前帧
|
||||||
var img gocv.Mat
|
img, c := service.GetDeviceCurrentFrame(device_id)
|
||||||
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
|
||||||
if c != count {
|
if c != count {
|
||||||
//将img转[]byte
|
//将img转[]byte
|
||||||
if img.Empty() {
|
buf, _ := gocv.IMEncode(".jpg", img)
|
||||||
log.Printf("device:%d img is empty!", device_id)
|
|
||||||
} else {
|
|
||||||
//gocv.Matrix转为jpeg
|
|
||||||
buf, err := gocv.IMEncode(".jpg", img)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("img encode err:%v", err)
|
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
|
||||||
goto end
|
|
||||||
}
|
|
||||||
buf1 := buf.GetBytes()
|
buf1 := buf.GetBytes()
|
||||||
|
|
||||||
err2 := ws.WriteMessage(websocket.BinaryMessage, buf1)
|
err2 := ws.WriteMessage(websocket.BinaryMessage, buf1)
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Printf("send message to client err:%v", err2)
|
fmt.Println("send message to client err:", err2)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
c = count
|
c = count
|
||||||
err4 := img.Close()
|
|
||||||
if err4 != nil {
|
|
||||||
log.Printf("close img err:%v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
//每秒发送一次心跳检测
|
//每秒发送一次心跳检测
|
||||||
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.Printf("Connection check failed:%v", err)
|
fmt.Println("Connection check failed:", err)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
goto end
|
goto end
|
||||||
}
|
}
|
||||||
|
|
@ -211,6 +195,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.Printf("device_id: %d has set is_play to 0", device_id)
|
fmt.Println("device_id:", device_id, " has set is_play to 0")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
19
main.go
19
main.go
|
|
@ -12,16 +12,12 @@ import (
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
"gocv.io/x/gocv"
|
"gocv.io/x/gocv"
|
||||||
"log"
|
"log"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var configPath string
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("start server")
|
|
||||||
gin.SetMode(gin.DebugMode) //设置为debug模式
|
gin.SetMode(gin.DebugMode) //设置为debug模式
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
//数据库初始
|
//数据库初始
|
||||||
|
|
@ -50,7 +46,7 @@ func main() {
|
||||||
log.Fatal("添加定时任务失败: ", err2)
|
log.Fatal("添加定时任务失败: ", err2)
|
||||||
}
|
}
|
||||||
c.Start()
|
c.Start()
|
||||||
fmt.Println("定时任务已启动")
|
|
||||||
err3 := r.Run(":" + proto.Config.SERVER_PORT)
|
err3 := r.Run(":" + proto.Config.SERVER_PORT)
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
panic("failed to run server:" + err3.Error())
|
panic("failed to run server:" + err3.Error())
|
||||||
|
|
@ -60,12 +56,7 @@ func main() {
|
||||||
func init() {
|
func init() {
|
||||||
//读取配置文件
|
//读取配置文件
|
||||||
//文件地址/home/videoplayer/vp.conf
|
//文件地址/home/videoplayer/vp.conf
|
||||||
os := runtime.GOOS
|
configPath := "/home/videoplayer/vp_stream.conf"
|
||||||
if os == "windows" {
|
|
||||||
configPath = "D:/File/vp_stream.conf"
|
|
||||||
} else {
|
|
||||||
configPath = "/home/videoplayer/vp_stream.conf"
|
|
||||||
}
|
|
||||||
//读取配置文件
|
//读取配置文件
|
||||||
err := proto.ReadConfig(configPath)
|
err := proto.ReadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -83,11 +74,12 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func myTask() {
|
func myTask() {
|
||||||
|
log.Println("每10秒执行一次")
|
||||||
ReadConfigAndSetSystem()
|
ReadConfigAndSetSystem()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadConfigAndSetSystem() {
|
func ReadConfigAndSetSystem() {
|
||||||
//configPath := "/home/videoplayer/vp_stream.conf"
|
configPath := "/home/videoplayer/vp_stream.conf"
|
||||||
//读取配置文件
|
//读取配置文件
|
||||||
err := proto.ReadConfig(configPath)
|
err := proto.ReadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -105,10 +97,9 @@ 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.Printf("device:%d has started!\n", device.ID)
|
fmt.Println("device:", device.ID, " has started!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("每10秒执行一次,当前设备:", proto.Config.DeviceInfo)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func JWTAuthMiddleware() gin.HandlerFunc {
|
func JWTAuthMiddleware() gin.HandlerFunc {
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,6 @@ package proto
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -41,40 +39,50 @@ type DeviceInfo struct {
|
||||||
|
|
||||||
// 读取配置文件
|
// 读取配置文件
|
||||||
func ReadConfig(path string) error {
|
func ReadConfig(path string) error {
|
||||||
// 查看配置文件是否存在,不存在则创建
|
//查看配置文件是否存在,不存在则创建
|
||||||
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
|
_, err := os.Stat(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error opening or creating config file: %v", err)
|
fmt.Println("Config file not found!")
|
||||||
|
//写入json文件
|
||||||
|
file, err := os.Create(path)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error creating config file")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer func() {
|
defer file.Close()
|
||||||
if err := file.Close(); err != nil {
|
|
||||||
log.Printf("Error closing config file: %v\n", err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// 尝试读取文件内容
|
|
||||||
decoder := json.NewDecoder(file)
|
|
||||||
if err := decoder.Decode(&Config); err != nil && err != io.EOF {
|
|
||||||
log.Printf("Error decoding config: %v \n", err)
|
|
||||||
// 如果文件为空或解码错误,写入默认配置
|
|
||||||
Config.SERVER_PORT = "5002"
|
|
||||||
encoder := json.NewEncoder(file)
|
encoder := json.NewEncoder(file)
|
||||||
if err := encoder.Encode(&Config); err != nil {
|
err = encoder.Encode(&Config)
|
||||||
log.Printf("Error encoding config: %v", err)
|
if err != nil {
|
||||||
return err
|
fmt.Println("Error encoding config")
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将配置写入 redis
|
//读json文件
|
||||||
jsonData, err := json.Marshal(Config)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ReadConfigToSetSystem Error encoding config,err :%v\n", err)
|
fmt.Println("Error opening config file")
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
defer file.Close()
|
||||||
|
decoder := json.NewDecoder(file)
|
||||||
|
err = decoder.Decode(&Config)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error decoding config:", err)
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
if Config.SERVER_PORT == "" {
|
||||||
|
Config.SERVER_PORT = "5002" // 默认端口
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//将配置写入redis
|
||||||
|
jsonData, err2 := json.Marshal(Config)
|
||||||
|
if err2 != nil {
|
||||||
|
fmt.Println("ReadConfigToSetSystem Error encoding config,err :", err2)
|
||||||
|
}
|
||||||
SigningKey = []byte(Config.TOKEN_SECRET)
|
SigningKey = []byte(Config.TOKEN_SECRET)
|
||||||
if Config.TOKEN_SECRET == "" {
|
if Config.TOKEN_SECRET == "" {
|
||||||
return fmt.Errorf("token secret is empty: %s", string(jsonData))
|
err = fmt.Errorf("token secret is empty: %s", string(jsonData))
|
||||||
}
|
}
|
||||||
return nil
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
134
service/tool.go
134
service/tool.go
|
|
@ -28,17 +28,6 @@ func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error {
|
||||||
mutex := mutex_.(*sync.RWMutex)
|
mutex := mutex_.(*sync.RWMutex)
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
//获取前一帧,将前一帧释放
|
|
||||||
framePrev, ok := DeviceCurrentFrameMap.Load(device_id)
|
|
||||||
if ok {
|
|
||||||
frame_, ok2 := framePrev.(gocv.Mat)
|
|
||||||
if ok2 {
|
|
||||||
err2 := frame_.Close()
|
|
||||||
if err2 != nil {
|
|
||||||
log.Printf("设备:%d, 错误: 无法关闭帧\n", device_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//设置当前帧
|
//设置当前帧
|
||||||
DeviceCurrentFrameMap.Store(device_id, frame)
|
DeviceCurrentFrameMap.Store(device_id, frame)
|
||||||
frame_count, ok := DeviceFrameCount.Load(device_id)
|
frame_count, ok := DeviceFrameCount.Load(device_id)
|
||||||
|
|
@ -51,106 +40,28 @@ func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int {
|
func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
|
||||||
defer func() {
|
|
||||||
if err := recover(); err != nil {
|
|
||||||
log.Printf("设备:%d 错误: %v\n", deviceId, err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
//获取读写锁
|
//获取读写锁
|
||||||
mutex_, ok := DeviceRWMap.Load(deviceId)
|
mutex_, ok := DeviceRWMap.Load(device_id)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Printf("DeviceRWMap 读写锁不存在,device_id: %d \n", deviceId)
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
mutex, ok := mutex_.(*sync.RWMutex)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId)
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
mutex.RLock()
|
|
||||||
defer mutex.RUnlock()
|
|
||||||
var frame_ gocv.Mat
|
|
||||||
//获取当前帧
|
|
||||||
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId)
|
|
||||||
if !ok {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
frame_, ok = frameIface.(gocv.Mat)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId)
|
|
||||||
}
|
|
||||||
*frame = frame_
|
|
||||||
frame_countIface, ok := DeviceFrameCount.Load(deviceId)
|
|
||||||
if !ok {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
frame_count, ok := frame_countIface.(int)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("DeviceFrameCount 存储的不是 int 类型,device_id: %d", deviceId)
|
|
||||||
}
|
|
||||||
return frame_count
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
|
|
||||||
defer func() {
|
|
||||||
if err := recover(); err != nil {
|
|
||||||
log.Printf("设备:%d 错误: %v\n", deviceId, err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
//获取读写锁
|
|
||||||
mutex_, ok := DeviceRWMap.Load(deviceId)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("DeviceRWMap 读写锁不存在,device_id: %d \n", deviceId)
|
|
||||||
return gocv.NewMat(), -1
|
|
||||||
}
|
|
||||||
mutex, ok := mutex_.(*sync.RWMutex)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId)
|
|
||||||
return gocv.NewMat(), -1
|
return gocv.NewMat(), -1
|
||||||
}
|
}
|
||||||
|
mutex := mutex_.(*sync.RWMutex)
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
defer mutex.RUnlock()
|
defer mutex.RUnlock()
|
||||||
//获取当前帧
|
//获取当前帧
|
||||||
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId)
|
frame, ok := DeviceCurrentFrameMap.Load(device_id)
|
||||||
if !ok {
|
frame_count, ok := DeviceFrameCount.Load(device_id)
|
||||||
return gocv.NewMat(), -1
|
return frame.(gocv.Mat), frame_count.(int)
|
||||||
}
|
|
||||||
frame, ok := frameIface.(gocv.Mat)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId)
|
|
||||||
}
|
|
||||||
frame_countIface, ok := DeviceFrameCount.Load(deviceId)
|
|
||||||
if !ok {
|
|
||||||
return gocv.NewMat(), -1
|
|
||||||
}
|
|
||||||
frame_count, ok := frame_countIface.(int)
|
|
||||||
if !ok {
|
|
||||||
log.Printf("DeviceFrameCount 存储的不是 int 类型,device_id: %d", deviceId)
|
|
||||||
}
|
|
||||||
return frame, frame_count
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVideoFrame(device proto.DeviceInfo) {
|
func getVideoFrame(device proto.DeviceInfo) {
|
||||||
//捕获异常
|
|
||||||
defer func() {
|
|
||||||
if err := recover(); err != nil {
|
|
||||||
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.Printf("设备:%d 错误: 无法打开视频流,err: %v\n", device.ID, err)
|
fmt.Printf("设备:%s 错误: 无法打开视频流,err: %v\n", device.ID, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func(webcam *gocv.VideoCapture) {
|
defer webcam.Close()
|
||||||
err2 := webcam.Close()
|
|
||||||
if err2 != nil {
|
|
||||||
log.Printf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error())
|
|
||||||
}
|
|
||||||
}(webcam)
|
|
||||||
// 字体相关设置,对应OpenCV默认字体等,这里简化处理,实际可按需求调整
|
// 字体相关设置,对应OpenCV默认字体等,这里简化处理,实际可按需求调整
|
||||||
font := gocv.FontHersheySimplex
|
font := gocv.FontHersheySimplex
|
||||||
fontScale := 0.5
|
fontScale := 0.5
|
||||||
|
|
@ -159,7 +70,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.Printf("设备:%d 当前帧: %d\n", device.ID, z)
|
fmt.Printf("设备:%d 当前帧: %d\n", device.ID, z)
|
||||||
}
|
}
|
||||||
if device.NextStop {
|
if device.NextStop {
|
||||||
break
|
break
|
||||||
|
|
@ -167,11 +78,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.Printf("设备:%v 错误: 无法从视频流中读取帧\n", device)
|
fmt.Printf("设备 错误: 无法从视频流中读取帧:", device, "\n")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if frame.Empty() {
|
if frame.Empty() {
|
||||||
log.Printf("设备:%s 错误: 无法从视频流中读取帧\n", device)
|
fmt.Printf("设备:%s 错误: 无法从视频流中读取帧\n", device)
|
||||||
//等待50ms
|
//等待50ms
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
continue
|
continue
|
||||||
|
|
@ -179,7 +90,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.Printf("设备:%s 帧尺寸已改变\n", device)
|
fmt.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")
|
||||||
|
|
@ -187,9 +98,13 @@ func getVideoFrame(device proto.DeviceInfo) {
|
||||||
//需要将帧付给全局变量
|
//需要将帧付给全局变量
|
||||||
err3 := SetDeviceCurrentFrame(frame, device.ID)
|
err3 := SetDeviceCurrentFrame(frame, device.ID)
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
|
fmt.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
|
||||||
}
|
}
|
||||||
z++
|
z++
|
||||||
|
err2 := frame.Close()
|
||||||
|
if err2 != nil {
|
||||||
|
fmt.Println("设备:%d,计数z=%d, 错误: 无法关闭帧", device.ID, z)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -208,18 +123,13 @@ func Get(url string) int {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVideoStream(id int) {
|
func GetVideoStream(id int) {
|
||||||
defer func() {
|
|
||||||
if err := recover(); err != nil {
|
|
||||||
log.Printf("设备:%d 错误: %v\n", id, err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
is_get, ok := DeviceIsGettingFrame.Load(id)
|
is_get, ok := DeviceIsGettingFrame.Load(id)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Printf("device: %d not found", id)
|
fmt.Println("device:", id, " not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if is_get == true {
|
if is_get == true {
|
||||||
log.Printf("device: %d is running!", id)
|
fmt.Println("device:", id, " is running!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
|
@ -235,12 +145,12 @@ func GetVideoStream(id int) {
|
||||||
}
|
}
|
||||||
if index == len(proto.Config.DeviceInfo) {
|
if index == len(proto.Config.DeviceInfo) {
|
||||||
//设备不存在
|
//设备不存在
|
||||||
log.Printf("device: %d not found", id)
|
log.Println("device:", id, " not found")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
is_get, ok = DeviceIsGettingFrame.Load(id)
|
is_get, ok = DeviceIsGettingFrame.Load(id)
|
||||||
if is_get == true {
|
if is_get == true {
|
||||||
log.Printf("for device:%d is running!", id)
|
log.Println("for device:", id, " is running!")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -251,7 +161,7 @@ func GetVideoStream(id int) {
|
||||||
//设置设备控制信息
|
//设置设备控制信息
|
||||||
status := Get(device.Control)
|
status := Get(device.Control)
|
||||||
DeviceIsGettingFrame.Store(id, true)
|
DeviceIsGettingFrame.Store(id, true)
|
||||||
log.Printf("device: %d set control info status: %d", device.ID, status)
|
log.Println("device:", device.ID, " set control info status:", status)
|
||||||
getVideoFrame(device)
|
getVideoFrame(device)
|
||||||
DeviceIsGettingFrame.Store(id, false)
|
DeviceIsGettingFrame.Store(id, false)
|
||||||
//等待1s
|
//等待1s
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-redis/redis/v8"
|
"github.com/go-redis/redis/v8"
|
||||||
"log"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -43,7 +42,6 @@ func CloseRedis() {
|
||||||
if err := RedisClient.Close(); err != nil {
|
if err := RedisClient.Close(); err != nil {
|
||||||
fmt.Println("Error closing Redis client: %v", err)
|
fmt.Println("Error closing Redis client: %v", err)
|
||||||
}
|
}
|
||||||
log.Printf("Redis has closed")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsContainKey(key string) bool {
|
func IsContainKey(key string) bool {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue