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