From a35a9a103873cb7518c2548fd031fc9579090c5b Mon Sep 17 00:00:00 2001 From: huang <1724659546@qq.com> Date: Sat, 8 Nov 2025 21:12:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0DeviceThresholdCheckTask(?= =?UTF-8?q?=E9=99=A4Execute=E5=A4=96)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- design/exceeding-threshold-alarm/index.md | 4 +- .../task/device_threshold_check_task.go | 86 +++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 internal/domain/task/device_threshold_check_task.go diff --git a/design/exceeding-threshold-alarm/index.md b/design/exceeding-threshold-alarm/index.md index 06c7319..232453d 100644 --- a/design/exceeding-threshold-alarm/index.md +++ b/design/exceeding-threshold-alarm/index.md @@ -67,7 +67,7 @@ 1. **任务类型**: 提供两种可供用户配置的阈值告警任务类型,分别对应 **区域主控** 和 **普通设备** 告警。 2. **参数结构**: - * **通用参数**: 任务参数将包含 `Value` (阈值) 和 `Operator` (操作符,如 `>` 或 `<`) 字段。 + * **通用参数**: 任务参数将包含 `Thresholds` (阈值) 和 `Operator` (操作符,如 `>` 或 `<`) 字段。 * **普通设备任务**: 配置包含 `DeviceID`。 * **区域主控任务**: 配置包含 `AreaControllerID`, `SensorType`, 以及一个 `ExcludeDeviceIDs` (需要排除的设备ID列表)。 @@ -132,4 +132,4 @@ 2. 重构部分枚举, 让models包不依赖其他项目中的包 3. 创建仓库层对象(不包含方法) 4. 实现告警发送任务 -5. 实现告警通知发送计划 \ No newline at end of file +5. 实现告警通知发送计划/全量采集计划改名 \ No newline at end of file diff --git a/internal/domain/task/device_threshold_check_task.go b/internal/domain/task/device_threshold_check_task.go new file mode 100644 index 0000000..53b4f46 --- /dev/null +++ b/internal/domain/task/device_threshold_check_task.go @@ -0,0 +1,86 @@ +package task + +import ( + "context" + "fmt" + "sync" + + "git.huangwc.com/pig/pig-farm-controller/internal/domain/plan" + "git.huangwc.com/pig/pig-farm-controller/internal/infra/logs" + "git.huangwc.com/pig/pig-farm-controller/internal/infra/models" +) + +type Operator string + +const ( + OperatorLessThan Operator = "<" + OperatorLessThanOrEqualTo Operator = "<=" + OperatorGreaterThan Operator = ">" + OperatorGreaterThanOrEqualTo Operator = ">=" + OperatorEqualTo Operator = "=" + OperatorNotEqualTo Operator = "!=" +) + +type DeviceThresholdCheckParams struct { + DeviceID uint `json:"device_id"` // 设备ID + Thresholds float64 `json:"thresholds"` // 阈值 + Operator Operator `json:"operator"` // 操作符 +} + +type DeviceThresholdCheckTask struct { + ctx context.Context + onceParse sync.Once + + taskLog *models.TaskExecutionLog + params DeviceThresholdCheckParams +} + +func NewDeviceThresholdCheckTask(ctx context.Context, taskLog *models.TaskExecutionLog) plan.Task { + return &DeviceThresholdCheckTask{ + ctx: ctx, + taskLog: taskLog, + } +} + +func (d *DeviceThresholdCheckTask) Execute(ctx context.Context) error { + //TODO implement me + panic("implement me") +} + +// parseParameters 解析任务参数 +func (d *DeviceThresholdCheckTask) parseParameters(ctx context.Context) error { + logger := logs.TraceLogger(ctx, d.ctx, "parseParameters") + var err error + d.onceParse.Do(func() { + if d.taskLog.Task.Parameters == nil { + logger.Errorf("任务 %v: 缺少参数", d.taskLog.TaskID) + err = fmt.Errorf("任务 %v: 参数不全", d.taskLog.TaskID) + return + } + + var params DeviceThresholdCheckParams + err = d.taskLog.Task.ParseParameters(¶ms) + if err != nil { + logger.Errorf("任务 %v: 解析参数失败: %v", d.taskLog.TaskID, err) + err = fmt.Errorf("任务 %v: 解析参数失败: %v", d.taskLog.TaskID, err) + return + } + + d.params = params + + }) + return err +} + +func (d *DeviceThresholdCheckTask) OnFailure(ctx context.Context, executeErr error) { + logger := logs.TraceLogger(ctx, d.ctx, "OnFailure") + logger.Errorf("设备阈值检测任务执行失败, 任务ID: %v: 执行失败: %v", d.taskLog.TaskID, executeErr) +} + +func (d *DeviceThresholdCheckTask) ResolveDeviceIDs(ctx context.Context) ([]uint, error) { + taskCtx := logs.AddFuncName(ctx, d.ctx, "ResolveDeviceIDs") + if err := d.parseParameters(taskCtx); err != nil { + return nil, err + } + return []uint{d.params.DeviceID}, nil +}