ota服务层

This commit is contained in:
huang
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" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto" "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/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models" "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/repository"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/utils/file"
"github.com/google/uuid"
) )
// AreaControllerService 定义了应用层的区域主控服务接口。 // AreaControllerService 定义了应用层的区域主控服务接口。
@@ -32,6 +37,7 @@ type areaControllerService struct {
ctx context.Context ctx context.Context
areaControllerRepo repository.AreaControllerRepository areaControllerRepo repository.AreaControllerRepository
thresholdAlarmService ThresholdAlarmService thresholdAlarmService ThresholdAlarmService
otaService device.OtaService
} }
// NewAreaControllerService 创建一个新的 AreaControllerService 实例。 // NewAreaControllerService 创建一个新的 AreaControllerService 实例。
@@ -39,11 +45,13 @@ func NewAreaControllerService(
ctx context.Context, ctx context.Context,
areaControllerRepo repository.AreaControllerRepository, areaControllerRepo repository.AreaControllerRepository,
thresholdAlarmService ThresholdAlarmService, thresholdAlarmService ThresholdAlarmService,
otaService device.OtaService,
) AreaControllerService { ) AreaControllerService {
return &areaControllerService{ return &areaControllerService{
ctx: ctx, ctx: ctx,
areaControllerRepo: areaControllerRepo, areaControllerRepo: areaControllerRepo,
thresholdAlarmService: thresholdAlarmService, thresholdAlarmService: thresholdAlarmService,
otaService: otaService,
} }
} }
@@ -148,18 +156,65 @@ func (s *areaControllerService) DeleteAreaController(ctx context.Context, id uin
// StartUpgrade 用于启动一个 OTA 升级任务。 // StartUpgrade 用于启动一个 OTA 升级任务。
func (s *areaControllerService) StartUpgrade(ctx context.Context, areaControllerID uint32, firmware *dto.OtaUpgradeRequest) (*dto.OtaUpgradeResponse, error) { func (s *areaControllerService) StartUpgrade(ctx context.Context, areaControllerID uint32, firmware *dto.OtaUpgradeRequest) (*dto.OtaUpgradeResponse, error) {
//TODO implement me serviceCtx, logger := logs.Trace(ctx, s.ctx, "StartUpgrade")
panic("implement me") 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 任务的进度。 // GetUpgradeProgress 用于查询指定 OTA 任务的进度。
func (s *areaControllerService) GetUpgradeProgress(ctx context.Context, taskID uint32) (*dto.OtaUpgradeProgressResponse, error) { func (s *areaControllerService) GetUpgradeProgress(ctx context.Context, taskID uint32) (*dto.OtaUpgradeProgressResponse, error) {
//TODO implement me executed, total, currentStage, err := s.otaService.GetUpgradeProgress(ctx, taskID)
panic("implement me") 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 升级任务。 // StopUpgrade 用于请求停止一个正在进行的 OTA 升级任务。
func (s *areaControllerService) StopUpgrade(ctx context.Context, taskID uint32) error { func (s *areaControllerService) StopUpgrade(ctx context.Context, taskID uint32) error {
//TODO implement me err := s.otaService.StopUpgrade(ctx, taskID)
panic("implement me") if err != nil {
return fmt.Errorf("停止升级任务失败: %w", err)
}
return nil
} }

View File

@@ -55,8 +55,8 @@ func ExecuteWithLock(action func() error, onRollback func(err error)) error {
// CreateTempDir 在指定的根目录下创建一个子目录。 // CreateTempDir 在指定的根目录下创建一个子目录。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。 // 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func CreateTempDir(tempRoot, subDir string) (string, error) { func CreateTempDir(subDir string) (string, error) {
fullDirPath := filepath.Join(tempRoot, subDir) fullDirPath := filepath.Join(instance.tempRoot, subDir)
if err := os.MkdirAll(fullDirPath, 0755); err != nil { if err := os.MkdirAll(fullDirPath, 0755); err != nil {
return "", fmt.Errorf("创建临时目录 %s 失败: %w", fullDirPath, err) return "", fmt.Errorf("创建临时目录 %s 失败: %w", fullDirPath, err)
} }
@@ -65,8 +65,8 @@ func CreateTempDir(tempRoot, subDir string) (string, error) {
// PrepareTempFilePath 获取文件在指定子目录下的完整路径,并确保父目录存在。 // PrepareTempFilePath 获取文件在指定子目录下的完整路径,并确保父目录存在。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。 // 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func PrepareTempFilePath(tempRoot, subDir, fileName string) (string, error) { func PrepareTempFilePath(subDir, fileName string) (string, error) {
fullDirPath := filepath.Join(tempRoot, subDir) fullDirPath := filepath.Join(instance.tempRoot, subDir)
if err := os.MkdirAll(fullDirPath, 0755); err != nil { if err := os.MkdirAll(fullDirPath, 0755); err != nil {
return "", fmt.Errorf("创建临时文件父目录 %s 失败: %w", fullDirPath, err) return "", fmt.Errorf("创建临时文件父目录 %s 失败: %w", fullDirPath, err)
} }
@@ -75,8 +75,8 @@ func PrepareTempFilePath(tempRoot, subDir, fileName string) (string, error) {
// RemoveTempDir 在指定的根目录下清理并删除一个子目录。 // RemoveTempDir 在指定的根目录下清理并删除一个子目录。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。 // 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func RemoveTempDir(tempRoot, subDir string) error { func RemoveTempDir(subDir string) error {
fullDirPath := filepath.Join(tempRoot, subDir) fullDirPath := filepath.Join(instance.tempRoot, subDir)
return os.RemoveAll(fullDirPath) return os.RemoveAll(fullDirPath)
} }
@@ -107,8 +107,8 @@ func LoadYaml(path string, c interface{}) error {
// WriteTempFile 将数据写入到临时子目录的指定文件中。 // WriteTempFile 将数据写入到临时子目录的指定文件中。
// 它会自动创建所需的子目录。 // 它会自动创建所需的子目录。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。 // 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func WriteTempFile(tempRoot, subDir, fileName string, data []byte) (string, error) { func WriteTempFile(subDir, fileName string, data []byte) (string, error) {
fullDirPath := filepath.Join(tempRoot, subDir) fullDirPath := filepath.Join(instance.tempRoot, subDir)
if err := os.MkdirAll(fullDirPath, 0755); err != nil { if err := os.MkdirAll(fullDirPath, 0755); err != nil {
return "", fmt.Errorf("为写入操作创建临时目录 %s 失败: %w", fullDirPath, err) return "", fmt.Errorf("为写入操作创建临时目录 %s 失败: %w", fullDirPath, err)
} }
@@ -123,8 +123,8 @@ func WriteTempFile(tempRoot, subDir, fileName string, data []byte) (string, erro
// ReadTempFile 从临时子目录的指定文件中读取数据。 // ReadTempFile 从临时子目录的指定文件中读取数据。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。 // 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func ReadTempFile(tempRoot, subDir, fileName string) ([]byte, error) { func ReadTempFile(subDir, fileName string) ([]byte, error) {
filePath := filepath.Join(tempRoot, subDir, fileName) filePath := filepath.Join(instance.tempRoot, subDir, fileName)
data, err := os.ReadFile(filePath) data, err := os.ReadFile(filePath)
if err != nil { if err != nil {
return nil, fmt.Errorf("读取临时文件 %s 失败: %w", filePath, err) return nil, fmt.Errorf("读取临时文件 %s 失败: %w", filePath, err)