2025-09-17 16:55:56 +08:00
|
|
|
package task
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-05 21:40:19 +08:00
|
|
|
"context"
|
2025-11-03 16:29:57 +08:00
|
|
|
"fmt"
|
|
|
|
|
|
2025-11-10 21:14:36 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/domain/alarm"
|
2025-10-29 15:30:16 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/domain/device"
|
2025-11-08 17:35:03 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/domain/notify"
|
2025-11-02 18:16:44 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/domain/plan"
|
2025-10-29 15:30:16 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
2025-09-17 16:55:56 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
2025-10-29 15:30:16 +08:00
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
2025-09-17 16:55:56 +08:00
|
|
|
)
|
|
|
|
|
|
2025-11-05 21:40:19 +08:00
|
|
|
const (
|
2025-12-03 17:39:46 +08:00
|
|
|
CompNameDelayTask = "DelayTask"
|
|
|
|
|
CompNameReleaseFeedWeight = "ReleaseFeedWeightTask"
|
|
|
|
|
CompNameFullCollectionTask = "FullCollectionTask"
|
|
|
|
|
CompNameAlarmNotification = "AlarmNotificationTask"
|
|
|
|
|
CompNameHeartbeatTask = "HeartbeatTask"
|
|
|
|
|
CompNameOtaCheck = "OtaCheckTask"
|
|
|
|
|
CompNameDeviceThresholdCheck = "DeviceThresholdCheckTask"
|
|
|
|
|
CompNameAreaCollectorThresholdCheck = "AreaCollectorThresholdCheckTask"
|
|
|
|
|
CompNameNotificationRefresh = "NotificationRefreshTask"
|
2025-11-05 21:40:19 +08:00
|
|
|
)
|
|
|
|
|
|
2025-10-29 15:30:16 +08:00
|
|
|
type taskFactory struct {
|
2025-11-08 17:35:03 +08:00
|
|
|
ctx context.Context
|
|
|
|
|
|
2025-12-01 20:42:21 +08:00
|
|
|
sensorDataRepo repository.SensorDataRepository
|
|
|
|
|
deviceRepo repository.DeviceRepository
|
|
|
|
|
alarmRepo repository.AlarmRepository
|
|
|
|
|
areaControllerRepo repository.AreaControllerRepository
|
2025-12-03 17:39:46 +08:00
|
|
|
otaRepo repository.OtaRepository
|
2025-11-08 17:35:03 +08:00
|
|
|
|
2025-12-03 15:12:43 +08:00
|
|
|
deviceOperator device.DeviceOperator
|
|
|
|
|
deviceCommunicator device.DeviceCommunicator
|
2025-11-08 17:35:03 +08:00
|
|
|
notificationService notify.Service
|
2025-11-10 21:14:36 +08:00
|
|
|
alarmService alarm.AlarmService
|
2025-10-29 15:30:16 +08:00
|
|
|
}
|
2025-09-17 22:43:35 +08:00
|
|
|
|
2025-10-29 15:30:16 +08:00
|
|
|
func NewTaskFactory(
|
2025-11-05 21:40:19 +08:00
|
|
|
ctx context.Context,
|
2025-10-29 15:30:16 +08:00
|
|
|
sensorDataRepo repository.SensorDataRepository,
|
|
|
|
|
deviceRepo repository.DeviceRepository,
|
2025-11-08 17:35:03 +08:00
|
|
|
alarmRepo repository.AlarmRepository,
|
2025-12-01 20:42:21 +08:00
|
|
|
areaControllerRepo repository.AreaControllerRepository,
|
2025-12-03 17:39:46 +08:00
|
|
|
otaRepo repository.OtaRepository,
|
2025-12-03 15:12:43 +08:00
|
|
|
deviceOperator device.DeviceOperator,
|
|
|
|
|
deviceCommunicator device.DeviceCommunicator,
|
2025-11-08 17:35:03 +08:00
|
|
|
notifyService notify.Service,
|
2025-11-10 21:14:36 +08:00
|
|
|
alarmService alarm.AlarmService,
|
2025-11-02 18:16:44 +08:00
|
|
|
) plan.TaskFactory {
|
2025-10-29 15:30:16 +08:00
|
|
|
return &taskFactory{
|
2025-11-08 17:35:03 +08:00
|
|
|
ctx: ctx,
|
|
|
|
|
sensorDataRepo: sensorDataRepo,
|
|
|
|
|
deviceRepo: deviceRepo,
|
|
|
|
|
alarmRepo: alarmRepo,
|
2025-12-01 20:42:21 +08:00
|
|
|
areaControllerRepo: areaControllerRepo,
|
2025-12-03 17:39:46 +08:00
|
|
|
otaRepo: otaRepo,
|
2025-12-03 15:12:43 +08:00
|
|
|
deviceOperator: deviceOperator,
|
|
|
|
|
deviceCommunicator: deviceCommunicator,
|
2025-11-08 17:35:03 +08:00
|
|
|
notificationService: notifyService,
|
2025-11-10 21:14:36 +08:00
|
|
|
alarmService: alarmService,
|
2025-10-29 15:30:16 +08:00
|
|
|
}
|
2025-09-17 22:43:35 +08:00
|
|
|
}
|
|
|
|
|
|
2025-11-05 21:40:19 +08:00
|
|
|
func (t *taskFactory) Production(ctx context.Context, claimedLog *models.TaskExecutionLog) plan.Task {
|
|
|
|
|
logger := logs.TraceLogger(ctx, t.ctx, "Production")
|
|
|
|
|
baseCtx := context.Background()
|
2025-10-29 15:30:16 +08:00
|
|
|
switch claimedLog.Task.Type {
|
2025-09-17 16:55:56 +08:00
|
|
|
case models.TaskTypeWaiting:
|
2025-11-05 21:40:19 +08:00
|
|
|
return NewDelayTask(logs.AddCompName(baseCtx, CompNameDelayTask), claimedLog)
|
2025-10-29 15:30:16 +08:00
|
|
|
case models.TaskTypeReleaseFeedWeight:
|
2025-12-03 15:12:43 +08:00
|
|
|
return NewReleaseFeedWeightTask(logs.AddCompName(baseCtx, CompNameReleaseFeedWeight), claimedLog, t.sensorDataRepo, t.deviceRepo, t.deviceOperator)
|
2025-10-29 16:37:05 +08:00
|
|
|
case models.TaskTypeFullCollection:
|
2025-12-03 15:12:43 +08:00
|
|
|
return NewFullCollectionTask(logs.AddCompName(baseCtx, CompNameFullCollectionTask), claimedLog, t.deviceRepo, t.deviceOperator)
|
2025-12-01 20:42:21 +08:00
|
|
|
case models.TaskTypeHeartbeat:
|
2025-12-03 15:12:43 +08:00
|
|
|
return NewHeartbeatTask(logs.AddCompName(baseCtx, CompNameHeartbeatTask), claimedLog, t.areaControllerRepo, t.deviceCommunicator)
|
2025-11-07 23:42:09 +08:00
|
|
|
case models.TaskTypeAlarmNotification:
|
2025-11-08 17:35:03 +08:00
|
|
|
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), claimedLog, t.notificationService, t.alarmRepo)
|
2025-11-10 21:14:36 +08:00
|
|
|
case models.TaskTypeDeviceThresholdCheck:
|
2025-12-03 17:39:46 +08:00
|
|
|
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, CompNameDeviceThresholdCheck), claimedLog, t.sensorDataRepo, t.alarmService)
|
2025-11-10 21:14:36 +08:00
|
|
|
case models.TaskTypeAreaCollectorThresholdCheck:
|
2025-12-03 17:39:46 +08:00
|
|
|
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, CompNameAreaCollectorThresholdCheck), claimedLog, t.sensorDataRepo, t.deviceRepo, t.alarmService)
|
2025-11-16 23:03:05 +08:00
|
|
|
case models.TaskTypeNotificationRefresh:
|
2025-12-03 17:39:46 +08:00
|
|
|
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, CompNameNotificationRefresh), claimedLog, t.alarmService)
|
|
|
|
|
case models.TaskTypeOTACheck:
|
|
|
|
|
return NewOtaCheckTask(logs.AddCompName(baseCtx, CompNameOtaCheck), claimedLog, t.otaRepo)
|
2025-09-17 16:55:56 +08:00
|
|
|
default:
|
2025-11-05 21:40:19 +08:00
|
|
|
logger.Panicf("不支持的任务类型: %s", claimedLog.Task.Type)
|
2025-10-29 15:30:16 +08:00
|
|
|
panic("不支持的任务类型") // 显式panic防编译器报错
|
2025-09-17 16:55:56 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-11-03 16:29:57 +08:00
|
|
|
|
|
|
|
|
// CreateTaskFromModel 实现了 TaskFactory 接口,用于从模型创建任务实例。
|
2025-11-05 21:40:19 +08:00
|
|
|
func (t *taskFactory) CreateTaskFromModel(ctx context.Context, taskModel *models.Task) (plan.TaskDeviceIDResolver, error) {
|
2025-11-03 16:29:57 +08:00
|
|
|
tempLog := &models.TaskExecutionLog{Task: *taskModel}
|
2025-11-05 21:40:19 +08:00
|
|
|
baseCtx := context.Background()
|
2025-11-03 16:29:57 +08:00
|
|
|
|
|
|
|
|
switch taskModel.Type {
|
|
|
|
|
case models.TaskTypeWaiting:
|
2025-11-05 21:40:19 +08:00
|
|
|
return NewDelayTask(logs.AddCompName(baseCtx, CompNameDelayTask), tempLog), nil
|
2025-11-03 16:29:57 +08:00
|
|
|
case models.TaskTypeReleaseFeedWeight:
|
|
|
|
|
return NewReleaseFeedWeightTask(
|
2025-11-05 21:40:19 +08:00
|
|
|
logs.AddCompName(baseCtx, CompNameReleaseFeedWeight),
|
2025-11-03 16:29:57 +08:00
|
|
|
tempLog,
|
|
|
|
|
t.sensorDataRepo,
|
|
|
|
|
t.deviceRepo,
|
2025-12-03 15:12:43 +08:00
|
|
|
t.deviceOperator,
|
2025-11-03 16:29:57 +08:00
|
|
|
), nil
|
|
|
|
|
case models.TaskTypeFullCollection:
|
2025-12-03 15:12:43 +08:00
|
|
|
return NewFullCollectionTask(logs.AddCompName(baseCtx, CompNameFullCollectionTask), tempLog, t.deviceRepo, t.deviceOperator), nil
|
2025-12-01 20:42:21 +08:00
|
|
|
case models.TaskTypeHeartbeat:
|
2025-12-03 15:12:43 +08:00
|
|
|
return NewHeartbeatTask(logs.AddCompName(baseCtx, CompNameHeartbeatTask), tempLog, t.areaControllerRepo, t.deviceCommunicator), nil
|
2025-11-07 23:42:09 +08:00
|
|
|
case models.TaskTypeAlarmNotification:
|
2025-11-08 17:35:03 +08:00
|
|
|
return NewAlarmNotificationTask(logs.AddCompName(baseCtx, CompNameAlarmNotification), tempLog, t.notificationService, t.alarmRepo), nil
|
2025-11-10 21:14:36 +08:00
|
|
|
case models.TaskTypeDeviceThresholdCheck:
|
2025-12-03 17:39:46 +08:00
|
|
|
return NewDeviceThresholdCheckTask(logs.AddCompName(baseCtx, CompNameDeviceThresholdCheck), tempLog, t.sensorDataRepo, t.alarmService), nil
|
2025-11-10 21:14:36 +08:00
|
|
|
case models.TaskTypeAreaCollectorThresholdCheck:
|
2025-12-03 17:39:46 +08:00
|
|
|
return NewAreaThresholdCheckTask(logs.AddCompName(baseCtx, CompNameAreaCollectorThresholdCheck), tempLog, t.sensorDataRepo, t.deviceRepo, t.alarmService), nil
|
2025-11-16 23:03:05 +08:00
|
|
|
case models.TaskTypeNotificationRefresh:
|
2025-12-03 17:39:46 +08:00
|
|
|
return NewRefreshNotificationTask(logs.AddCompName(baseCtx, CompNameNotificationRefresh), tempLog, t.alarmService), nil
|
|
|
|
|
case models.TaskTypeOTACheck:
|
|
|
|
|
return NewOtaCheckTask(logs.AddCompName(baseCtx, CompNameOtaCheck), tempLog, t.otaRepo), nil
|
2025-11-03 16:29:57 +08:00
|
|
|
default:
|
|
|
|
|
return nil, fmt.Errorf("不支持为类型 '%s' 的任务创建模型实例", taskModel.Type)
|
|
|
|
|
}
|
|
|
|
|
}
|