实现区域阈值告警任务

This commit is contained in:
2025-11-10 15:25:33 +08:00
parent 19d55eb09b
commit d4e8aba1fd
5 changed files with 190 additions and 20 deletions

View File

@@ -13,24 +13,14 @@ import (
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
)
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
SensorType models.SensorType `json:"sensor_type"` // 传感器类型
Thresholds float64 `json:"thresholds"` // 阈值
Operator Operator `json:"operator"` // 操作符
Operator models.Operator `json:"operator"` // 操作符
}
// DeviceThresholdCheckTask 是一个任务,用于检查设备传感器数据是否满足阈值条件。
type DeviceThresholdCheckTask struct {
ctx context.Context
onceParse sync.Once
@@ -132,19 +122,19 @@ func (d *DeviceThresholdCheckTask) Execute(ctx context.Context) error {
}
// checkThreshold 校验当前值是否满足阈值条件
func (d *DeviceThresholdCheckTask) checkThreshold(currentValue float64, operator Operator, threshold float64) bool {
func (d *DeviceThresholdCheckTask) checkThreshold(currentValue float64, operator models.Operator, threshold float64) bool {
switch operator {
case OperatorLessThan:
case models.OperatorLessThan:
return currentValue < threshold
case OperatorLessThanOrEqualTo:
case models.OperatorLessThanOrEqualTo:
return currentValue <= threshold
case OperatorGreaterThan:
case models.OperatorGreaterThan:
return currentValue > threshold
case OperatorGreaterThanOrEqualTo:
case models.OperatorGreaterThanOrEqualTo:
return currentValue >= threshold
case OperatorEqualTo:
case models.OperatorEqualTo:
return currentValue == threshold
case OperatorNotEqualTo:
case models.OperatorNotEqualTo:
return currentValue != threshold
default:
return false