添加windows支持

This commit is contained in:
lijun 2025-01-16 23:40:08 +08:00
parent 5b3ef982eb
commit 15a78b10c5
5 changed files with 46 additions and 41 deletions

2
go.sum
View File

@ -108,6 +108,8 @@ 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=

18
main.go
View File

@ -6,17 +6,22 @@ import (
"VideoStream/proto" "VideoStream/proto"
"VideoStream/service" "VideoStream/service"
"VideoStream/worker" "VideoStream/worker"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"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()
//数据库初始 //数据库初始
@ -45,7 +50,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())
@ -55,7 +60,12 @@ func main() {
func init() { func init() {
//读取配置文件 //读取配置文件
//文件地址/home/videoplayer/vp.conf //文件地址/home/videoplayer/vp.conf
configPath := "/home/videoplayer/vp_stream.conf" os := runtime.GOOS
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 {
@ -73,12 +83,11 @@ 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 {
@ -99,6 +108,7 @@ func ReadConfigAndSetSystem() {
log.Printf("device:%d has started!\n", device.ID) log.Printf("device:%d has started!\n", device.ID)
} }
} }
log.Println("每10秒执行一次,当前设备:", proto.Config.DeviceInfo)
} }
func JWTAuthMiddleware() gin.HandlerFunc { func JWTAuthMiddleware() gin.HandlerFunc {

View File

@ -3,6 +3,7 @@ package proto
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"log" "log"
"os" "os"
) )
@ -40,50 +41,40 @@ type DeviceInfo struct {
// 读取配置文件 // 读取配置文件
func ReadConfig(path string) error { func ReadConfig(path string) error {
//查看配置文件是否存在,不存在则创建 // 查看配置文件是否存在,不存在则创建
_, err := os.Stat(path) file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil { if err != nil {
log.Fatalf("Config file not found!") log.Printf("Error opening or creating config file: %v", err)
//写入json文件
file, err := os.Create(path)
if err != nil {
log.Fatalf("Error creating config file")
return err return err
} }
defer file.Close() defer func() {
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)
err = encoder.Encode(&Config) if err := encoder.Encode(&Config); err != nil {
if err != nil { log.Printf("Error encoding config: %v", err)
log.Fatalf("Error encoding config")
}
return err return err
} }
}
//读json文件
file, err := os.Open(path)
if err != nil {
log.Fatalf("Error opening config file")
return err
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&Config)
if err != nil {
log.Fatalf("Error decoding config:", err)
return err
} else {
if Config.SERVER_PORT == "" {
Config.SERVER_PORT = "5002" // 默认端口
}
}
// 将配置写入 redis // 将配置写入 redis
jsonData, err2 := json.Marshal(Config) jsonData, err := json.Marshal(Config)
if err2 != nil { if err != nil {
log.Fatalf("ReadConfigToSetSystem Error encoding config,err :", err2) log.Printf("ReadConfigToSetSystem Error encoding config,err :%v\n", err)
return err
} }
SigningKey = []byte(Config.TOKEN_SECRET) SigningKey = []byte(Config.TOKEN_SECRET)
if Config.TOKEN_SECRET == "" { if Config.TOKEN_SECRET == "" {
err = fmt.Errorf("token secret is empty: %s", string(jsonData)) return fmt.Errorf("token secret is empty: %s", string(jsonData))
} }
return err return nil
} }

View File

@ -142,7 +142,7 @@ func getVideoFrame(device proto.DeviceInfo) {
webcam, err := gocv.OpenVideoCapture(device.Stream) webcam, err := gocv.OpenVideoCapture(device.Stream)
if err != nil { if err != nil {
log.Printf("设备:%s 错误: 无法打开视频流err: %v\n", device.ID, err) log.Printf("设备:%d 错误: 无法打开视频流err: %v\n", device.ID, err)
return return
} }
defer func(webcam *gocv.VideoCapture) { defer func(webcam *gocv.VideoCapture) {
@ -167,7 +167,7 @@ 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("设备 错误: 无法从视频流中读取帧:", device, "\n") log.Printf("设备:%v 错误: 无法从视频流中读取帧\n", device)
break break
} }
if frame.Empty() { if frame.Empty() {

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/go-redis/redis/v8" "github.com/go-redis/redis/v8"
"log"
"strconv" "strconv"
"time" "time"
) )
@ -42,6 +43,7 @@ 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 {