实现DeviceThresholdCheckTask(除Execute外)
This commit is contained in:
86
internal/domain/task/device_threshold_check_task.go
Normal file
86
internal/domain/task/device_threshold_check_task.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user