Files
pig-farm-controller/internal/domain/device/ota_service.go
2025-12-05 16:53:52 +08:00

76 lines
2.4 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 device
import (
"context"
"fmt"
"path/filepath"
"time"
"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"
)
// otaServiceImpl 是 OtaService 接口的实现。
type otaServiceImpl struct {
ctx context.Context
otaRepo repository.OtaRepository
deviceRepo repository.DeviceRepository
}
// NewOtaService 创建一个新的 OtaService 实例。
func NewOtaService(ctx context.Context, otaRepo repository.OtaRepository, deviceRepo repository.DeviceRepository) OtaService {
return &otaServiceImpl{
ctx: ctx,
otaRepo: otaRepo,
deviceRepo: deviceRepo,
}
}
func (o *otaServiceImpl) StartUpgrade(ctx context.Context, areaControllerID uint32, firmwarePath string) (uint32, error) {
//TODO implement me
panic("implement me")
}
func (o *otaServiceImpl) GetUpgradeProgress(ctx context.Context, taskID uint32) (executed, total uint32, CurrentStage models.OTATaskStatus, err error) {
//TODO implement me
panic("implement me")
}
func (o *otaServiceImpl) StopUpgrade(ctx context.Context, taskID uint32) error {
serviceCtx, logger := logs.Trace(ctx, o.ctx, "StopUpgrade")
task, err := o.otaRepo.FindByID(serviceCtx, taskID)
if err != nil {
logger.Errorf("查找 OTA 任务失败: %v, 任务ID: %d", err, taskID)
return fmt.Errorf("查找 OTA 任务失败: %w", err)
}
// 幂等性检查:如果任务已处于终态,则直接返回成功
if task.IsOver() {
logger.Infof("OTA 任务 %d 已处于终态 %s无需停止", taskID, task.Status)
return nil
}
now := time.Now()
task.Status = models.OTATaskStatusStopped
task.CompletedAt = &now
task.ErrorMessage = "任务被用户手动停止"
if err := o.otaRepo.Update(serviceCtx, task); err != nil {
logger.Errorf("更新 OTA 任务状态失败: %v, 任务ID: %d", err, taskID)
return fmt.Errorf("更新 OTA 任务状态失败: %w", err)
}
// 清理相关文件目录
dirToRemove := filepath.Join(models.OTADir, fmt.Sprintf("%d", taskID))
if err := file.RemoveTempDir(dirToRemove); err != nil {
// 文件清理失败不应阻塞主流程,但需要记录日志
logger.Warnf("清理 OTA 任务 %d 的文件目录 %s 失败: %v", taskID, dirToRemove, err)
}
logger.Infof("OTA 任务 %d 已被成功标记为手动停止", taskID)
return nil
}