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

Commit

Permalink
Merge pull request ThingsPanel#220 from November-12/main
Browse files Browse the repository at this point in the history
手动激活策略
  • Loading branch information
November-12 authored Sep 27, 2023
2 parents 9490ef8 + 0b95d14 commit 9c9afda
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
29 changes: 29 additions & 0 deletions controllers/tp_scenario_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,32 @@ func (TpScenarioStrategyController *TpScenarioStrategyController) Delete() {
utils.SuccessWithMessage(400, "无法删除;可能的原因:1.被自动化关联的场景无法删除,需要先取消关联;"+req_err.Error(), (*context2.Context)(TpScenarioStrategyController.Ctx))
}
}

// 激活
func (TpScenarioStrategyController *TpScenarioStrategyController) Activate() {
TpScenarioStrategyIdValidate := valid.TpScenarioStrategyIdValidate{}
err := json.Unmarshal(TpScenarioStrategyController.Ctx.Input.RequestBody, &TpScenarioStrategyIdValidate)
if err != nil {
fmt.Println("参数解析失败", err.Error())
}
v := validation.Validation{}
status, _ := v.Valid(TpScenarioStrategyIdValidate)
if !status {
for _, err := range v.Errors {
// 获取字段别称
alias := gvalid.GetAlias(TpScenarioStrategyIdValidate, err.Field)
message := strings.Replace(err.Message, err.Field, alias, 1)
utils.SuccessWithMessage(1000, message, (*context2.Context)(TpScenarioStrategyController.Ctx))
break
}
return
}

var s services.TpScenarioActionService

if s.ExecuteScenarioAction(TpScenarioStrategyIdValidate.Id, models.ManualActivation) != nil {
utils.SuccessWithMessage(400, err.Error(), (*context2.Context)(TpScenarioStrategyController.Ctx))
return
}
utils.SuccessWithMessage(200, "success", (*context2.Context)(TpScenarioStrategyController.Ctx))
}
5 changes: 5 additions & 0 deletions models/tp_scenario_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ type TpScenarioLog struct {
func (t *TpScenarioLog) TableName() string {
return "tp_scenario_log"
}

const (
AutomaticallyActivated = 1
ManualActivation = 2
)
12 changes: 7 additions & 5 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func init() {

api := web.NewNamespace("/api",
// 登录
web.NSRouter("/auth/login", &controllers.AuthController{}, "*:Login"), //ty
web.NSRouter("/auth/logout", &controllers.AuthController{}, "*:Logout"), //ty
web.NSRouter("/auth/refresh", &controllers.AuthController{}, "*:Refresh"), //ty
web.NSRouter("/auth/me", &controllers.AuthController{}, "*:Me"), //ty
web.NSRouter("/auth/change_password", &controllers.AuthController{}, "*:ChangePassword"), //ty
web.NSRouter("/auth/login", &controllers.AuthController{}, "*:Login"), //ty
web.NSRouter("/auth/logout", &controllers.AuthController{}, "*:Logout"), //ty
web.NSRouter("/auth/refresh", &controllers.AuthController{}, "*:Refresh"), //ty
web.NSRouter("/auth/me", &controllers.AuthController{}, "*:Me"), //ty
web.NSRouter("/auth/change_password", &controllers.AuthController{}, "*:ChangePassword"), //ty

// 获取验证码
web.NSRouter("/auth/captcha", &controllers.TpNotification{}, "*:GetCaptcha"),
Expand Down Expand Up @@ -427,6 +427,8 @@ func init() {
web.NSRouter("/scenario/strategy/detail", &controllers.TpScenarioStrategyController{}, "*:Detail"),
web.NSRouter("/scenario/strategy/edit", &controllers.TpScenarioStrategyController{}, "*:Edit"),
web.NSRouter("/scenario/strategy/delete", &controllers.TpScenarioStrategyController{}, "*:Delete"),
// 手动激活一次
web.NSRouter("/scenario/strategy/activate", &controllers.TpScenarioStrategyController{}, "*:Activate"),

web.NSRouter("/v1/automation/add", &controllers.TpAutomationController{}, "*:Add"),
web.NSRouter("/v1/automation/list", &controllers.TpAutomationController{}, "*:List"),
Expand Down
2 changes: 1 addition & 1 deletion services/conditions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func (*ConditionsService) ExecuteAutomationAction(automationId string, automatio
automationLogDetail.TargetId = automationAction.ScenarioStrategyId
//触发场景
var scenarioActionService TpScenarioActionService
err := scenarioActionService.ExecuteScenarioAction(automationAction.ScenarioStrategyId)
err := scenarioActionService.ExecuteScenarioAction(automationAction.ScenarioStrategyId, models.AutomaticallyActivated)
if err != nil {
logMessage = "触发场景失败:" + err.Error()
automationLogDetail.ProcessDescription = "触发场景失败:" + err.Error()
Expand Down
10 changes: 8 additions & 2 deletions services/tp_scenario_action_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (*TpScenarioActionService) DeleteTpScenarioAction(tp_automation_action mode
}

// 触发场景动作
func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string) error {
func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string, source int) error {
var scenarioActions []models.TpScenarioAction
result := psql.Mydb.Model(&models.TpScenarioAction{}).Where("scenario_strategy_id = ?", scenarioStrategyId).Find(&scenarioActions)
if result.Error != nil {
Expand All @@ -106,7 +106,13 @@ func (*TpScenarioActionService) ExecuteScenarioAction(scenarioStrategyId string)
logs.Error(result.Error)
return result.Error
}
scenarioLog.ProcessDescription = "执行成功"

if source == models.AutomaticallyActivated {
scenarioLog.ProcessDescription = "执行成功"
} else {
scenarioLog.ProcessDescription = "执行激活"
}

scenarioLog.ProcessResult = "1"
for _, scenarioAction := range scenarioActions {
var scenarioLogDetail models.TpScenarioLogDetail
Expand Down
2 changes: 1 addition & 1 deletion validate/tp_scenario_strategy_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ type RspTpScenarioStrategyPaginationValidate struct {
}

type TpScenarioStrategyIdValidate struct {
Id string `json:"id" gorm:"primaryKey" valid:"Required;MaxSize(36)"`
Id string `json:"id" alias:"id" gorm:"primaryKey" valid:"Required;MaxSize(36)"`
}

0 comments on commit 9c9afda

Please sign in to comment.