2025-04-13 11:33:55 +08:00

111 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"ZhenTuLocalPassiveAdapter/api"
"ZhenTuLocalPassiveAdapter/config"
"ZhenTuLocalPassiveAdapter/core"
"ZhenTuLocalPassiveAdapter/dto"
"ZhenTuLocalPassiveAdapter/telemetry"
"context"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"log"
"os"
"time"
)
var tracer = otel.Tracer("vpt")
func startTask(device config.DeviceMapping, task dto.Task) {
ctx, span := tracer.Start(context.Background(), "startTask")
span.SetAttributes(attribute.String("deviceNo", device.DeviceNo))
span.SetAttributes(attribute.String("taskId", task.TaskID))
span.SetAttributes(attribute.String("scenicId", task.ScenicID))
span.SetAttributes(attribute.String("startTime", task.StartTime.Format("2006-01-02 15:04:05")))
span.SetAttributes(attribute.String("endTime", task.EndTime.Format("2006-01-02 15:04:05")))
fo, err := core.HandleTask(ctx, device, task)
if err != nil {
span.SetStatus(codes.Error, "处理任务失败")
log.Printf("处理任务失败, TaskID【%s】, DeviceNo: %s, 错误: %v\n", task.TaskID, task.DeviceNo, err)
api.ReportTaskFailure(ctx, task.TaskID)
return
}
span.SetAttributes(attribute.String("fileUrl", fo.URL))
log.Printf("处理任务成功, TaskID【%s】, DeviceNo: %s\n", task.TaskID, task.DeviceNo)
err = api.UploadTaskFile(ctx, task, *fo)
if err != nil {
span.SetStatus(codes.Error, "上传文件失败")
log.Printf("上传文件失败, TaskID【%s】, DeviceNo: %s, 错误: %v\n", task.TaskID, task.DeviceNo, err)
api.ReportTaskFailure(ctx, task.TaskID)
return
}
result := api.ReportTaskSuccess(ctx, task.TaskID, fo)
if result {
span.SetStatus(codes.Error, "上报任务成功失败")
log.Printf("上报任务成功失败, TaskID【%s】, DeviceNo: %s, 错误: %v\n", task.TaskID, task.DeviceNo, err)
return
}
span.SetStatus(codes.Ok, "上传文件成功")
log.Printf("上传文件成功, TaskID【%s】, DeviceNo: %s\n", task.TaskID, task.DeviceNo)
}
func main() {
err := config.LoadConfig()
if err != nil {
log.Println("加载配置文件失败:", err)
return
}
// 日志文件路径
logFilePath := "app.log"
ctx := context.Background()
shutdown, err := telemetry.InitTelemetry(ctx)
if err != nil {
log.Fatalf("Failed to initialize telemetry: %v", err)
return
}
defer shutdown(ctx)
// 创建或打开日志文件
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
defer logFile.Close()
// 设置日志输出到文件
log.SetOutput(logFile)
if config.Config.Record.Storage.Type == "local" {
_, err = os.Stat(config.Config.Record.Storage.Path)
if err != nil {
log.Println("录像文件夹配置失败", err)
return
} else {
log.Println("录像文件夹配置有效")
}
} else {
log.Println("录像文件夹配置为OSS")
}
// 每两秒定时执行
for {
// 执行任务
tasks, err := api.SyncTask()
if err == nil {
for _, task := range tasks {
log.Printf("开始处理任务, TaskID【%s】DeviceNo: %s开始时间: %s结束时间: %s\n", task.TaskID, task.DeviceNo, task.StartTime, task.EndTime)
// 处理任务
for _, device := range config.Config.Devices {
if device.DeviceNo == task.DeviceNo {
// 处理任务
go startTask(device, task)
break // 提前返回,避免不必要的循环
}
}
}
} else {
log.Println("同步任务失败:", err)
}
// 等待两秒
<-time.After(2 * time.Second)
}
}