Compare commits
17 Commits
8de01c90c2
...
69f81addeb
| Author | SHA1 | Date |
|---|---|---|
|
|
69f81addeb | |
|
|
1d48452f23 | |
|
|
1e8b72e9b2 | |
|
|
5bd2707ffd | |
|
|
74d04602d5 | |
|
|
a202a2ef4c | |
|
|
318907eeac | |
|
|
a965a9dd7d | |
|
|
66e341e938 | |
|
|
b8b6cb838c | |
|
|
6aeb4dd4f8 | |
|
|
87c3522374 | |
|
|
59ae9d7f6f | |
|
|
4c1a397775 | |
|
|
8fbc54c2af | |
|
|
749eb5c2ee | |
|
|
f83e720224 |
1
go.mod
1
go.mod
|
|
@ -11,6 +11,7 @@ require (
|
|||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6
|
||||
gocv.io/x/gocv v0.39.0
|
||||
gorm.io/driver/mysql v1.5.7
|
||||
gorm.io/driver/postgres v1.5.11
|
||||
|
|
|
|||
4
go.sum
4
go.sum
|
|
@ -106,10 +106,10 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
|
|||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=
|
||||
github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6 h1:TtyC78WMafNW8QFfv3TeP3yWNDG+uxNkk9vOrnDu6JA=
|
||||
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6/go.mod h1:h8272+G2omSmi30fBXiZDMkmHuOgonplfKIKjQWzlfs=
|
||||
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.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.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||
|
|
|
|||
|
|
@ -164,19 +164,18 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
|||
t_count := 0
|
||||
for {
|
||||
//从service获取当前帧
|
||||
var img gocv.Mat
|
||||
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
||||
img, c := service.GetDeviceCurrentFrameV3(device_id)
|
||||
if c != count {
|
||||
//将img转[]byte
|
||||
if img.Empty() {
|
||||
log.Printf("device:%d img is empty!", device_id)
|
||||
log.Printf("device:%d img is empty! count = %d \n", device_id, c)
|
||||
} 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
|
||||
break
|
||||
}
|
||||
buf1 := buf.GetBytes()
|
||||
|
||||
|
|
@ -184,7 +183,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
|||
if err2 != nil {
|
||||
log.Printf("send message to client err:%v", err2)
|
||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||
goto end
|
||||
break
|
||||
}
|
||||
c = count
|
||||
err4 := img.Close()
|
||||
|
|
@ -199,7 +198,9 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
|||
if err != nil {
|
||||
log.Printf("Connection check failed:%v", err)
|
||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||
goto end
|
||||
break
|
||||
} else {
|
||||
log.Printf("Connection check success")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -207,7 +208,6 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
|||
t_count++
|
||||
}
|
||||
|
||||
end:
|
||||
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
||||
if worker.IsContainKey(online_conn_key) == false {
|
||||
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5)
|
||||
|
|
|
|||
6
main.go
6
main.go
|
|
@ -104,6 +104,12 @@ func ReadConfigAndSetSystem() {
|
|||
service.DeviceIsGettingFrame.Store(device.ID, false)
|
||||
}
|
||||
if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
|
||||
switch device.ID {
|
||||
case 1:
|
||||
service.Device1CurrentFrame = gocv.NewMat()
|
||||
case 50:
|
||||
service.Device50CurrentFrame = gocv.NewMat()
|
||||
}
|
||||
go service.GetVideoStream(device.ID)
|
||||
log.Printf("device:%d has started!\n", device.ID)
|
||||
}
|
||||
|
|
|
|||
109
service/tool.go
109
service/tool.go
|
|
@ -18,6 +18,8 @@ var DeviceRWMap = &sync.Map{}
|
|||
var DeviceCurrentFrameMap = &sync.Map{}
|
||||
var DeviceFrameCount = &sync.Map{}
|
||||
var DeviceIsGettingFrame = &sync.Map{}
|
||||
var Device1CurrentFrame gocv.Mat
|
||||
var Device50CurrentFrame gocv.Mat
|
||||
|
||||
func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error {
|
||||
//获取读写锁
|
||||
|
|
@ -51,6 +53,49 @@ func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func SetDeviceCurrentFrameV2(frame *gocv.Mat, device_id int) error {
|
||||
//获取读写锁
|
||||
mutex_, ok := DeviceRWMap.Load(device_id)
|
||||
if !ok {
|
||||
return fmt.Errorf("设备:%s 读写锁不存在", device_id)
|
||||
}
|
||||
mutex := mutex_.(*sync.RWMutex)
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
//设置当前帧
|
||||
switch device_id {
|
||||
case 1:
|
||||
//err := Device1CurrentFrame.Close()
|
||||
//if err != nil {
|
||||
// log.Printf("设备:%d, 错误: 无法关闭帧\n", device_id)
|
||||
//}
|
||||
if Device1CurrentFrame.Empty() {
|
||||
Device1CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
||||
}
|
||||
(*frame).CopyTo(&Device1CurrentFrame)
|
||||
//err := deepcopier.Copy(*frame).To(&Device1CurrentFrame)
|
||||
//if err != nil {
|
||||
// log.Printf("设备:%d, 错误: 无法设置帧,err: %s \n", device_id, err.Error())
|
||||
//}
|
||||
//查看帧状态
|
||||
//log.Printf("frame:%v,Device1CurrentFrame:%v \n", (*frame).Empty(), Device1CurrentFrame.Empty())
|
||||
//Device1CurrentFrame = *frame
|
||||
case 50:
|
||||
if Device50CurrentFrame.Empty() {
|
||||
Device50CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
||||
}
|
||||
(*frame).CopyTo(&Device50CurrentFrame)
|
||||
}
|
||||
frame_count, ok := DeviceFrameCount.Load(device_id)
|
||||
if !ok {
|
||||
return fmt.Errorf("设备:%s 当前帧计数不存在", device_id)
|
||||
}
|
||||
frame_count_ := frame_count.(int)
|
||||
frame_count_++
|
||||
DeviceFrameCount.Store(device_id, frame_count_)
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
|
|
@ -92,6 +137,55 @@ func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int {
|
|||
return frame_count
|
||||
}
|
||||
|
||||
func GetDeviceCurrentFrameV3(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
|
||||
}
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
//获取当前帧
|
||||
var frame gocv.Mat
|
||||
switch deviceId {
|
||||
case 1:
|
||||
frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
|
||||
Device1CurrentFrame.CopyTo(&frame)
|
||||
//查看帧状态
|
||||
//log.Printf("frame:%v,Device1CurrentFrame:%v \n", frame.Empty(), Device1CurrentFrame.Empty())
|
||||
|
||||
case 50:
|
||||
frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
|
||||
Device50CurrentFrame.CopyTo(&frame)
|
||||
//查看帧状态
|
||||
//log.Printf("frame:%v,Device50CurrentFrame:%v\n", frame.Empty(), Device50CurrentFrame.Empty())
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
//查看地址
|
||||
//log.Printf("frame:%p,Device1CurrentFrame:%p,Device50CurrentFrame:%p\n", &frame, &Device1CurrentFrame, &Device50CurrentFrame)
|
||||
return frame, frame_count
|
||||
}
|
||||
func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
|
|
@ -157,6 +251,7 @@ func getVideoFrame(device proto.DeviceInfo) {
|
|||
fontColor := color.RGBA{G: 255}
|
||||
lineType := 2
|
||||
z := 0
|
||||
var frame gocv.Mat
|
||||
for {
|
||||
if device.LogFrame > 0 && z%device.LogFrame == 0 {
|
||||
log.Printf("设备:%d 当前帧: %d\n", device.ID, z)
|
||||
|
|
@ -164,7 +259,7 @@ func getVideoFrame(device proto.DeviceInfo) {
|
|||
if device.NextStop {
|
||||
break
|
||||
}
|
||||
frame := gocv.NewMat()
|
||||
frame = gocv.NewMat()
|
||||
ok := webcam.Read(&frame)
|
||||
if !ok {
|
||||
log.Printf("设备:%v 错误: 无法从视频流中读取帧\n", device)
|
||||
|
|
@ -185,11 +280,21 @@ func getVideoFrame(device proto.DeviceInfo) {
|
|||
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
||||
gocv.PutText(&frame, currentTime, image.Point{10, 20}, font, fontScale, fontColor, lineType)
|
||||
//需要将帧付给全局变量
|
||||
err3 := SetDeviceCurrentFrame(frame, device.ID)
|
||||
err3 := SetDeviceCurrentFrameV2(&frame, device.ID)
|
||||
if err3 != nil {
|
||||
log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
|
||||
}
|
||||
z++
|
||||
//关闭帧
|
||||
err4 := frame.Close()
|
||||
if err4 != nil {
|
||||
log.Printf("设备:%d 错误: 无法关闭帧,err:%s \n", device.ID, err4.Error())
|
||||
}
|
||||
}
|
||||
//关闭帧
|
||||
err4 := frame.Close()
|
||||
if err4 != nil {
|
||||
log.Printf("设备:%d 错误: 无法关闭帧,err:%s \n", device.ID, err4.Error())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue