更新任务工厂

This commit is contained in:
2025-12-03 17:39:46 +08:00
parent 9d9b5f801f
commit b26b7ee0f3
2 changed files with 25 additions and 11 deletions

View File

@@ -98,6 +98,7 @@ type Repositories struct {
rawMaterialRepo repository.RawMaterialRepository
nutrientRepo repository.NutrientRepository
recipeRepo repository.RecipeRepository
otaRepo repository.OtaRepository
unitOfWork repository.UnitOfWork
}
@@ -130,6 +131,7 @@ func initRepositories(ctx context.Context, db *gorm.DB) *Repositories {
rawMaterialRepo: repository.NewGormRawMaterialRepository(logs.AddCompName(baseCtx, "RawMaterialRepo"), db),
nutrientRepo: repository.NewGormNutrientRepository(logs.AddCompName(baseCtx, "NutrientRepo"), db),
recipeRepo: repository.NewGormRecipeRepository(logs.AddCompName(baseCtx, "RecipeRepo"), db),
otaRepo: repository.NewGormOtaRepository(logs.AddCompName(baseCtx, "OtaRepo"), db),
unitOfWork: repository.NewGormUnitOfWork(logs.AddCompName(baseCtx, "UnitOfWork"), db),
}
}
@@ -200,6 +202,7 @@ func initDomainServices(ctx context.Context, cfg *config.Config, infra *Infrastr
infra.repos.deviceRepo,
infra.repos.alarmRepo,
infra.repos.areaControllerRepo,
infra.repos.otaRepo,
generalDeviceService,
generalDeviceService,
notifyService,

View File

@@ -19,6 +19,10 @@ const (
CompNameFullCollectionTask = "FullCollectionTask"
CompNameAlarmNotification = "AlarmNotificationTask"
CompNameHeartbeatTask = "HeartbeatTask"
CompNameOtaCheck = "OtaCheckTask"
CompNameDeviceThresholdCheck = "DeviceThresholdCheckTask"
CompNameAreaCollectorThresholdCheck = "AreaCollectorThresholdCheckTask"
CompNameNotificationRefresh = "NotificationRefreshTask"
)
type taskFactory struct {
@@ -28,6 +32,7 @@ type taskFactory struct {
deviceRepo repository.DeviceRepository
alarmRepo repository.AlarmRepository
areaControllerRepo repository.AreaControllerRepository
otaRepo repository.OtaRepository
deviceOperator device.DeviceOperator
deviceCommunicator device.DeviceCommunicator
@@ -41,6 +46,7 @@ func NewTaskFactory(
deviceRepo repository.DeviceRepository,
alarmRepo repository.AlarmRepository,
areaControllerRepo repository.AreaControllerRepository,
otaRepo repository.OtaRepository,
deviceOperator device.DeviceOperator,
deviceCommunicator device.DeviceCommunicator,
notifyService notify.Service,
@@ -52,6 +58,7 @@ func NewTaskFactory(
deviceRepo: deviceRepo,
alarmRepo: alarmRepo,
areaControllerRepo: areaControllerRepo,
otaRepo: otaRepo,
deviceOperator: deviceOperator,
deviceCommunicator: deviceCommunicator,
notificationService: notifyService,
@@ -74,11 +81,13 @@ func (t *taskFactory) Production(ctx context.Context, claimedLog *models.TaskExe
case models.TaskTypeAlarmNotification:
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), claimedLog, t.notificationService, t.alarmRepo)
case models.TaskTypeDeviceThresholdCheck:
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, "DeviceThresholdCheckTask"), claimedLog, t.sensorDataRepo, t.alarmService)
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, CompNameDeviceThresholdCheck), claimedLog, t.sensorDataRepo, t.alarmService)
case models.TaskTypeAreaCollectorThresholdCheck:
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, "AreaCollectorThresholdCheckTask"), claimedLog, t.sensorDataRepo, t.deviceRepo, t.alarmService)
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, CompNameAreaCollectorThresholdCheck), claimedLog, t.sensorDataRepo, t.deviceRepo, t.alarmService)
case models.TaskTypeNotificationRefresh:
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, "NotificationRefreshTask"), claimedLog, t.alarmService)
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, CompNameNotificationRefresh), claimedLog, t.alarmService)
case models.TaskTypeOTACheck:
return NewOtaCheckTask(logs.AddCompName(baseCtx, CompNameOtaCheck), claimedLog, t.otaRepo)
default:
logger.Panicf("不支持的任务类型: %s", claimedLog.Task.Type)
panic("不支持的任务类型") // 显式panic防编译器报错
@@ -108,11 +117,13 @@ func (t *taskFactory) CreateTaskFromModel(ctx context.Context, taskModel *models
case models.TaskTypeAlarmNotification:
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), tempLog, t.notificationService, t.alarmRepo), nil
case models.TaskTypeDeviceThresholdCheck:
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, "DeviceThresholdCheckTask"), tempLog, t.sensorDataRepo, t.alarmService), nil
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, CompNameDeviceThresholdCheck), tempLog, t.sensorDataRepo, t.alarmService), nil
case models.TaskTypeAreaCollectorThresholdCheck:
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, "AreaCollectorThresholdCheckTask"), tempLog, t.sensorDataRepo, t.deviceRepo, t.alarmService), nil
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, CompNameAreaCollectorThresholdCheck), tempLog, t.sensorDataRepo, t.deviceRepo, t.alarmService), nil
case models.TaskTypeNotificationRefresh:
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, "NotificationRefreshTask"), tempLog, t.alarmService), nil
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, CompNameNotificationRefresh), tempLog, t.alarmService), nil
case models.TaskTypeOTACheck:
return NewOtaCheckTask(logs.AddCompName(baseCtx, CompNameOtaCheck), tempLog, t.otaRepo), nil
default:
return nil, fmt.Errorf("不支持为类型 '%s' 的任务创建模型实例", taskModel.Type)
}