添加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=
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=

18
main.go
View File

@ -6,17 +6,22 @@ import (
"VideoStream/proto"
"VideoStream/service"
"VideoStream/worker"
"fmt"
"github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt"
"github.com/robfig/cron/v3"
"gocv.io/x/gocv"
"log"
"runtime"
"strconv"
"strings"
"sync"
)
var configPath string
func main() {
fmt.Println("start server")
gin.SetMode(gin.DebugMode) //设置为debug模式
r := gin.Default()
//数据库初始
@ -45,7 +50,7 @@ func main() {
log.Fatal("添加定时任务失败: ", err2)
}
c.Start()
fmt.Println("定时任务已启动")
err3 := r.Run(":" + proto.Config.SERVER_PORT)
if err3 != nil {
panic("failed to run server:" + err3.Error())
@ -55,7 +60,12 @@ func main() {
func init() {
//读取配置文件
//文件地址/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)
if err != nil {
@ -73,12 +83,11 @@ func init() {
}
func myTask() {
log.Println("每10秒执行一次")
ReadConfigAndSetSystem()
}
func ReadConfigAndSetSystem() {
configPath := "/home/videoplayer/vp_stream.conf"
//configPath := "/home/videoplayer/vp_stream.conf"
//读取配置文件
err := proto.ReadConfig(configPath)
if err != nil {
@ -99,6 +108,7 @@ func ReadConfigAndSetSystem() {
log.Printf("device:%d has started!\n", device.ID)
}
}
log.Println("每10秒执行一次,当前设备:", proto.Config.DeviceInfo)
}
func JWTAuthMiddleware() gin.HandlerFunc {

View File

@ -3,6 +3,7 @@ package proto
import (
"encoding/json"
"fmt"
"io"
"log"
"os"
)
@ -40,50 +41,40 @@ type DeviceInfo struct {
// 读取配置文件
func ReadConfig(path string) error {
//查看配置文件是否存在,不存在则创建
_, err := os.Stat(path)
// 查看配置文件是否存在,不存在则创建
file, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
log.Fatalf("Config file not found!")
//写入json文件
file, err := os.Create(path)
if err != nil {
log.Fatalf("Error creating config file")
log.Printf("Error opening or creating config file: %v", err)
return err
}
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)
if err := encoder.Encode(&Config); err != nil {
log.Printf("Error encoding config: %v", err)
return err
}
defer file.Close()
encoder := json.NewEncoder(file)
err = encoder.Encode(&Config)
if err != nil {
log.Fatalf("Error encoding config")
}
return err
}
//读json文件
file, err := os.Open(path)
// 将配置写入 redis
jsonData, err := json.Marshal(Config)
if err != nil {
log.Fatalf("Error opening config file")
log.Printf("ReadConfigToSetSystem Error encoding config,err :%v\n", err)
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
jsonData, err2 := json.Marshal(Config)
if err2 != nil {
log.Fatalf("ReadConfigToSetSystem Error encoding config,err :", err2)
}
SigningKey = []byte(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)
if err != nil {
log.Printf("设备:%s 错误: 无法打开视频流err: %v\n", device.ID, err)
log.Printf("设备:%d 错误: 无法打开视频流err: %v\n", device.ID, err)
return
}
defer func(webcam *gocv.VideoCapture) {
@ -167,7 +167,7 @@ func getVideoFrame(device proto.DeviceInfo) {
frame := gocv.NewMat()
ok := webcam.Read(&frame)
if !ok {
log.Printf("设备 错误: 无法从视频流中读取帧:", device, "\n")
log.Printf("设备:%v 错误: 无法从视频流中读取帧\n", device)
break
}
if frame.Empty() {

View File

@ -6,6 +6,7 @@ import (
"encoding/json"
"fmt"
"github.com/go-redis/redis/v8"
"log"
"strconv"
"time"
)
@ -42,6 +43,7 @@ func CloseRedis() {
if err := RedisClient.Close(); err != nil {
fmt.Println("Error closing Redis client: %v", err)
}
log.Printf("Redis has closed")
}
func IsContainKey(key string) bool {