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
hezhuozhuo committed Sep 28, 2023
2 parents 799608e + b4bc557 commit 71b937b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
2 changes: 2 additions & 0 deletions services/tp_scenario_action_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string,
} else if scenarioAction.ActionType == "2" {
// 触发其他场景
scenarioLogDetail.ActionType = "2"
scenarioLogDetail.ProcessResult = "1"
instructMap := make(map[string]string)
err := json.Unmarshal([]byte(scenarioAction.Instruct), &instructMap)
if err != nil {
Expand Down Expand Up @@ -204,6 +205,7 @@ func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string,
}
} else if scenarioAction.ActionType == "3" {
scenarioLogDetail.ActionType = "3"
scenarioLogDetail.ProcessResult = "1"
instructMap := make(map[string]string)
err := json.Unmarshal([]byte(scenarioAction.Instruct), &instructMap)
if err != nil {
Expand Down
49 changes: 49 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 @@ -42,6 +43,32 @@ func (*TpScenarioStrategyService) GetTpScenarioStrategyDetail(tp_scenario_strate
if result.Error != nil {
return tp_scenario_strategy, result.Error
}

for i, action := range tp_scenario_action {
if value, ok := action["action_type"].(string); ok {
if value == "3" {
if instruct, ok := action["instruct"].(string); ok {
// 解析json
m := make(map[string]string)
json.Unmarshal([]byte(instruct), &m)
if warning_strategy_id, ok := m["warning_strategy_id"]; ok {

var tp_warning_strategy = make(map[string]interface{})
result := psql.Mydb.Model(models.TpWarningStrategy{}).Where(&models.TpWarningStrategy{Id: warning_strategy_id}).First(&tp_warning_strategy)
if result.Error != nil {
if !errors.Is(result.Error, gorm.ErrRecordNotFound) {
return tp_scenario_strategy, result.Error
}
}
tp_scenario_action[i]["warning_strategy"] = tp_warning_strategy

}

}
}
}
}

tp_scenario_strategy["scenario_actions"] = tp_scenario_action
return tp_scenario_strategy, nil
}
Expand Down Expand Up @@ -80,13 +107,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 71b937b

Please sign in to comment.