ota服务层

This commit is contained in:
2025-12-05 16:31:12 +08:00
parent 2bb187071f
commit b4ecee6626
2 changed files with 71 additions and 16 deletions

View File

@@ -4,11 +4,16 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/file"
"github.com/google/uuid"
)
// AreaControllerService 定义了应用层的区域主控服务接口。
@@ -32,6 +37,7 @@ type areaControllerService struct {
ctx context.Context
areaControllerRepo repository.AreaControllerRepository
thresholdAlarmService ThresholdAlarmService
otaService device.OtaService
}
// NewAreaControllerService 创建一个新的 AreaControllerService 实例。
@@ -39,11 +45,13 @@ func NewAreaControllerService(
ctx context.Context,
areaControllerRepo repository.AreaControllerRepository,
thresholdAlarmService ThresholdAlarmService,
otaService device.OtaService,
) AreaControllerService {
return &areaControllerService{
ctx: ctx,
areaControllerRepo: areaControllerRepo,
thresholdAlarmService: thresholdAlarmService,
otaService: otaService,
}
}
@@ -148,18 +156,65 @@ func (s *areaControllerService) DeleteAreaController(ctx context.Context, id uin
// StartUpgrade 用于启动一个 OTA 升级任务。
func (s *areaControllerService) StartUpgrade(ctx context.Context, areaControllerID uint32, firmware *dto.OtaUpgradeRequest) (*dto.OtaUpgradeResponse, error) {
//TODO implement me
panic("implement me")
serviceCtx, logger := logs.Trace(ctx, s.ctx, "StartUpgrade")
src, err := firmware.FirmwareFile.Open()
if err != nil {
return nil, fmt.Errorf("打开固件文件失败: %w", err)
}
defer src.Close()
data, err := io.ReadAll(src)
if err != nil {
return nil, fmt.Errorf("读取固件文件内容失败: %w", err)
}
subDir := uuid.New().String()
var filePath string
var actionErr error
err = file.ExecuteWithLock(func() error {
filePath, actionErr = file.WriteTempFile(subDir, firmware.FirmwareFile.Filename, data)
return actionErr
}, func(err error) {
removeErr := file.RemoveTempDir(subDir)
if removeErr != nil {
logger.Errorf("回滚失败, 删除临时目录失败: %v, 目标地址: %v", removeErr, filePath)
}
})
if err != nil {
return nil, fmt.Errorf("保存固件文件失败: %w", err)
}
taskID, err := s.otaService.StartUpgrade(serviceCtx, areaControllerID, filePath)
if err != nil {
return nil, fmt.Errorf("启动升级任务失败: %w", err)
}
return &dto.OtaUpgradeResponse{TaskID: taskID}, nil
}
// GetUpgradeProgress 用于查询指定 OTA 任务的进度。
func (s *areaControllerService) GetUpgradeProgress(ctx context.Context, taskID uint32) (*dto.OtaUpgradeProgressResponse, error) {
//TODO implement me
panic("implement me")
executed, total, currentStage, err := s.otaService.GetUpgradeProgress(ctx, taskID)
if err != nil {
return nil, fmt.Errorf("获取升级进度失败: %w", err)
}
return &dto.OtaUpgradeProgressResponse{
TaskID: taskID,
CurrentStage: currentStage,
ExecutedNum: executed,
TotalNum: total,
Message: fmt.Sprintf("当前状态: %s", currentStage),
}, nil
}
// StopUpgrade 用于请求停止一个正在进行的 OTA 升级任务。
func (s *areaControllerService) StopUpgrade(ctx context.Context, taskID uint32) error {
//TODO implement me
panic("implement me")
err := s.otaService.StopUpgrade(ctx, taskID)
if err != nil {
return fmt.Errorf("停止升级任务失败: %w", err)
}
return nil
}