72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"VideoStream/handler"
|
|
"VideoStream/proto"
|
|
"VideoStream/service"
|
|
"VideoStream/worker"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/robfig/cron/v3"
|
|
"gocv.io/x/gocv"
|
|
"log"
|
|
"sync"
|
|
)
|
|
|
|
func main() {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
r := gin.Default()
|
|
handler.SetUpToolGroup(r) // Tool
|
|
err := worker.InitRedis()
|
|
if err != nil {
|
|
panic("failed to connect redis:" + err.Error())
|
|
}
|
|
//定时任务
|
|
c := cron.New(cron.WithSeconds())
|
|
// 添加每 10 秒执行一次的任务
|
|
_, err2 := c.AddFunc("@every 10s", myTask)
|
|
if err2 != nil {
|
|
log.Fatal("添加定时任务失败: ", err2)
|
|
}
|
|
c.Start()
|
|
|
|
err3 := r.Run(":" + proto.Config.SERVER_PORT)
|
|
if err3 != nil {
|
|
panic("failed to run server:" + err3.Error())
|
|
}
|
|
}
|
|
|
|
func init() {
|
|
//读取配置文件
|
|
//文件地址/home/videoplayer/vp.conf
|
|
configPath := "/home/videoplayer/vp_stream.conf"
|
|
//读取配置文件
|
|
err := proto.ReadConfig(configPath)
|
|
if err != nil {
|
|
panic("failed to read config file:" + err.Error())
|
|
}
|
|
for _, device := range proto.Config.DeviceInfo {
|
|
service.DeviceRWMap[device.ID] = sync.RWMutex{}
|
|
service.DeviceCurrentFrameMap[device.ID] = gocv.NewMat()
|
|
}
|
|
}
|
|
|
|
func myTask() {
|
|
log.Println("每10秒执行一次")
|
|
ReadConfigAndSetSystem()
|
|
}
|
|
|
|
func ReadConfigAndSetSystem() {
|
|
configPath := "/home/videoplayer/vp_stream.conf"
|
|
//读取配置文件
|
|
err := proto.ReadConfig(configPath)
|
|
if err != nil {
|
|
panic("failed to read config file:" + err.Error())
|
|
}
|
|
//检测是否需要获取设备流,如果需要则开启
|
|
for _, device := range proto.Config.DeviceInfo {
|
|
if service.DeviceIsStop[device.ID] == false {
|
|
go service.GetVideoStream(device.ID)
|
|
}
|
|
}
|
|
}
|