CreateDeviceThresholdAlarm

UpdateDeviceThresholdAlarm
CreateAreaThresholdAlarm
UpdateAreaThresholdAlarm
This commit is contained in:
2025-11-10 17:35:22 +08:00
parent d4e8aba1fd
commit f2b0c2987f
10 changed files with 375 additions and 38 deletions

View File

@@ -44,8 +44,8 @@ type Service interface {
GetPlanByID(ctx context.Context, id uint) (*models.Plan, error)
// ListPlans 获取计划列表,支持过滤和分页
ListPlans(ctx context.Context, opts repository.ListPlansOptions, page, pageSize int) ([]models.Plan, int64, error)
// UpdatePlan 更新计划
UpdatePlan(ctx context.Context, plan *models.Plan) (*models.Plan, error)
// UpdatePlan 更新计划, wantPlanType 表示期望被修改的计划是什么类型
UpdatePlan(ctx context.Context, plan *models.Plan, wantPlanType models.PlanType) (*models.Plan, error)
// DeletePlan 删除计划(软删除)
DeletePlan(ctx context.Context, id uint) error
// StartPlan 启动计划
@@ -207,8 +207,8 @@ func (s *planServiceImpl) ListPlans(ctx context.Context, opts repository.ListPla
return plans, total, nil
}
// UpdatePlan 更新计划
func (s *planServiceImpl) UpdatePlan(ctx context.Context, planToUpdate *models.Plan) (*models.Plan, error) {
// UpdatePlan 更新计划, wantPlanType 表示期望被修改的计划是什么类型
func (s *planServiceImpl) UpdatePlan(ctx context.Context, planToUpdate *models.Plan, wantPlanType models.PlanType) (*models.Plan, error) {
planCtx, logger := logs.Trace(ctx, s.ctx, "UpdatePlan")
const actionType = "领域层:更新计划"
@@ -222,9 +222,8 @@ func (s *planServiceImpl) UpdatePlan(ctx context.Context, planToUpdate *models.P
return nil, err
}
// 系统计划不允许修改
if existingPlan.PlanType == models.PlanTypeSystem {
logger.Warnf("%s: 尝试修改系统计划, ID: %d", actionType, planToUpdate.ID)
if existingPlan.PlanType != wantPlanType {
logger.Warnf("%s: 禁止修改 %v 类型计划, ID: %d", actionType, wantPlanType, planToUpdate.ID)
return nil, ErrPlanCannotBeModified
}

View File

@@ -14,11 +14,12 @@ import (
// AreaThresholdCheckParams 定义了区域阈值检查任务的参数
type AreaThresholdCheckParams struct {
AreaControllerID uint `json:"area_controller_id"` // 区域主控ID
SensorType models.SensorType `json:"sensor_type"` // 传感器类型
Thresholds float64 `json:"thresholds"` // 阈值
Operator models.Operator `json:"operator"` // 操作符
ExcludeDeviceIDs []uint `json:"exclude_device_ids"` // 排除的传感器ID
AreaControllerID uint `json:"area_controller_id"` // 区域主控ID
SensorType models.SensorType `json:"sensor_type"` // 传感器类型
Thresholds float64 `json:"thresholds"` // 阈值
Operator models.Operator `json:"operator"` // 操作符
Level models.SeverityLevel `json:"level"` // 告警级别
ExcludeDeviceIDs []uint `json:"exclude_device_ids"` // 排除的传感器ID
}
// AreaThresholdCheckTask 是一个任务,用于检查区域阈值并触发告警, 区域主控下的所有没有独立校验任务的设备都会受到此任务的检查
@@ -78,6 +79,7 @@ func (a *AreaThresholdCheckTask) Execute(ctx context.Context) error {
DeviceID: device.ID,
SensorType: a.params.SensorType,
Thresholds: a.params.Thresholds,
Level: a.params.Level,
Operator: a.params.Operator,
})
if err != nil {
@@ -150,6 +152,9 @@ func (a *AreaThresholdCheckTask) parseParameters(ctx context.Context) error {
if params.AreaControllerID == 0 {
err = fmt.Errorf("任务 %v: 未配置区域主控ID", a.taskLog.TaskID)
}
if params.Level == "" {
params.Level = models.WarnLevel
}
if params.ExcludeDeviceIDs == nil {
params.ExcludeDeviceIDs = []uint{}
}

View File

@@ -14,10 +14,11 @@ import (
)
type DeviceThresholdCheckParams struct {
DeviceID uint `json:"device_id"` // 设备ID
SensorType models.SensorType `json:"sensor_type"` // 传感器类型
Thresholds float64 `json:"thresholds"` // 阈值
Operator models.Operator `json:"operator"` // 操作符
DeviceID uint `json:"device_id"` // 设备ID
SensorType models.SensorType `json:"sensor_type"` // 传感器类型
Thresholds float64 `json:"thresholds"` // 阈值
Operator models.Operator `json:"operator"` // 操作符
Level models.SeverityLevel `json:"level"` // 告警等级
}
// DeviceThresholdCheckTask 是一个任务,用于检查设备传感器数据是否满足阈值条件。
@@ -99,7 +100,7 @@ func (d *DeviceThresholdCheckTask) Execute(ctx context.Context) error {
AlarmCode: alarmCode,
AlarmSummary: summary,
AlarmDetails: details,
Level: models.WarnLevel, // 默认告警等级,可后续根据需求调整
Level: d.params.Level,
TriggerTime: time.Now(),
}
@@ -172,6 +173,9 @@ func (d *DeviceThresholdCheckTask) parseParameters(ctx context.Context) error {
if params.DeviceID == 0 {
err = fmt.Errorf("任务 %v: 未配置设备ID", d.taskLog.TaskID)
}
if params.Level == "" {
params.Level = models.WarnLevel
}
d.params = params