Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
新增告警
Browse files Browse the repository at this point in the history
  • Loading branch information
November-12 committed Sep 28, 2023
1 parent 697cad6 commit e7d82a1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
23 changes: 23 additions & 0 deletions services/tp_scenario_strategy_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"ThingsPanel-Go/models"
"ThingsPanel-Go/utils"
valid "ThingsPanel-Go/validate"
"encoding/json"
"errors"
"time"

Expand Down Expand Up @@ -80,13 +81,35 @@ func (*TpScenarioStrategyService) AddTpScenarioStrategy(tp_scenario_strategy val
return tp_scenario_strategy, result.Error
}
for _, scenarioAction := range tp_scenario_strategy.AddTpScenarioActions {

scenarioAction.Id = utils.GetUuid()
scenarioAction.ScenarioStrategyId = tp_scenario_strategy.Id
// DeviceId外键可以为null,需要用map处理
scenarioActionMap := structs.Map(&scenarioAction)
if scenarioAction.DeviceId == "" {
delete(scenarioActionMap, "DeviceId")
}
// 告警
if scenarioAction.ActionType == "3" {
// 创建策略
delete(scenarioActionMap, "Instruct")
scenarioAction.WarningStrategy.Id = utils.GetUuid()
result := tx.Model(&models.TpWarningStrategy{}).Create(scenarioAction.WarningStrategy)
if result.Error != nil {
tx.Rollback()
logs.Error(result.Error.Error())
return tp_scenario_strategy, result.Error
}
warning_strategy := make(map[string]interface{})
warning_strategy["warning_strategy_id"] = scenarioAction.WarningStrategy.Id
jsonData, err := json.Marshal(warning_strategy)
if err != nil {
return tp_scenario_strategy, err
}
scenarioActionMap["instruct"] = jsonData
}

delete(scenarioActionMap, "WarningStrategy")
result := tx.Model(&models.TpScenarioAction{}).Create(scenarioActionMap)
if result.Error != nil {
tx.Rollback()
Expand Down
15 changes: 8 additions & 7 deletions validate/tp_scenario_action_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ type EditTpScenarioActionValidate struct {
}

type AddTpScenarioActionValidate struct {
Id string `json:"id" gorm:"primaryKey" valid:"MaxSize(36)"`
ScenarioStrategyId string `json:"scenario_strategy_id,omitempty" valid:"MaxSize(36)"`
ActionType string `json:"action_type,omitempty" valid:"MaxSize(2)"`
DeviceId string `json:"device_id,omitempty" valid:"MaxSize(36)"`
DeviceModel string `json:"device_model,omitempty" valid:"MaxSize(2)"` // 模型类型1-设定属性 2-调动服务
Instruct string `json:"instruct,omitempty" valid:"MaxSize(999)"` // 指令
Remark string `json:"remark,omitempty" valid:"MaxSize(255)"`
Id string `json:"id" gorm:"primaryKey" valid:"MaxSize(36)"`
ScenarioStrategyId string `json:"scenario_strategy_id,omitempty" valid:"MaxSize(36)"`
ActionType string `json:"action_type,omitempty" valid:"MaxSize(2)"`
DeviceId string `json:"device_id,omitempty" valid:"MaxSize(36)"`
DeviceModel string `json:"device_model,omitempty" valid:"MaxSize(2)"` // 模型类型1-设定属性 2-调动服务
Instruct string `json:"instruct,omitempty" valid:"MaxSize(999)"` // 指令
Remark string `json:"remark,omitempty" valid:"MaxSize(255)"`
WarningStrategy AddTpWarningStrategyValidate `json:"warning_strategy,omitempty"`
}

type TpScenarioActionPaginationValidate struct {
Expand Down

0 comments on commit e7d82a1

Please sign in to comment.