// Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . //go:build plus package services import ( "context" "encoding/json" "errors" "github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" ) // NodeActionService 节点动作服务 type NodeActionService struct { BaseService } // CreateNodeAction 添加动作 func (this *NodeActionService) CreateNodeAction(ctx context.Context, req *pb.CreateNodeActionRequest) (*pb.CreateNodeActionResponse, error) { _, err := this.ValidateAdmin(ctx) if err != nil { return nil, err } var tx = this.NullTx() // 条件 var condsConfig = nodeconfigs.NewNodeActionCondsConfig() err = json.Unmarshal(req.CondsJSON, condsConfig) if err != nil { return nil, errors.New("decode 'condsJSON' failed: " + err.Error()) } // 动作 var actionConfig = nodeconfigs.NewNodeActionConfig() err = json.Unmarshal(req.ActionJSON, actionConfig) if err != nil { return nil, errors.New("decode 'actionsJSON' failed: " + err.Error()) } // 持续时间 var duration = &shared.TimeDuration{} err = json.Unmarshal(req.DurationJSON, duration) if err != nil { return nil, errors.New("decode 'durationJSON' failed: " + err.Error()) } actionId, err := models.SharedNodeActionDAO.CreateAction(tx, req.Role, req.NodeId, condsConfig, actionConfig, duration) if err != nil { return nil, err } return &pb.CreateNodeActionResponse{NodeActionId: actionId}, nil } // DeleteNodeAction 删除动作 func (this *NodeActionService) DeleteNodeAction(ctx context.Context, req *pb.DeleteNodeActionRequest) (*pb.RPCSuccess, error) { _, err := this.ValidateAdmin(ctx) if err != nil { return nil, err } var tx = this.NullTx() err = models.SharedNodeActionDAO.DisableNodeAction(tx, req.NodeActionId) if err != nil { return nil, err } return this.Success() } // UpdateNodeAction 修改动作 func (this *NodeActionService) UpdateNodeAction(ctx context.Context, req *pb.UpdateNodeActionRequest) (*pb.RPCSuccess, error) { _, err := this.ValidateAdmin(ctx) if err != nil { return nil, err } var tx = this.NullTx() // 条件 var condsConfig = nodeconfigs.NewNodeActionCondsConfig() err = json.Unmarshal(req.CondsJSON, condsConfig) if err != nil { return nil, errors.New("decode 'condsJSON' failed: " + err.Error()) } // 动作 var actionConfig = nodeconfigs.NewNodeActionConfig() err = json.Unmarshal(req.ActionJSON, actionConfig) if err != nil { return nil, errors.New("decode 'actionsJSON' failed: " + err.Error()) } // 持续时间 var duration = &shared.TimeDuration{} err = json.Unmarshal(req.DurationJSON, duration) if err != nil { return nil, errors.New("decode 'durationJSON' failed: " + err.Error()) } err = models.SharedNodeActionDAO.UpdateAction(tx, req.NodeActionId, condsConfig, actionConfig, duration, req.IsOn) if err != nil { return nil, err } return this.Success() } // FindAllNodeActions 列出某个节点的所有动作 func (this *NodeActionService) FindAllNodeActions(ctx context.Context, req *pb.FindAllNodeActionsRequest) (*pb.FindAllNodeActionsResponse, error) { _, err := this.ValidateAdmin(ctx) if err != nil { return nil, err } var tx = this.NullTx() actions, err := models.SharedNodeActionDAO.FindAllNodeActions(tx, req.Role, req.NodeId) if err != nil { return nil, err } var pbActions = []*pb.NodeAction{} for _, action := range actions { pbActions = append(pbActions, &pb.NodeAction{ Id: int64(action.Id), NodeId: int64(action.NodeId), Role: action.Role, IsOn: action.IsOn, CondsJSON: action.Conds, ActionJSON: action.Action, DurationJSON: action.Duration, }) } return &pb.FindAllNodeActionsResponse{ NodeActions: pbActions, }, nil } // FindNodeAction 查找单个节点动作 func (this *NodeActionService) FindNodeAction(ctx context.Context, req *pb.FindNodeActionRequest) (*pb.FindNodeActionResponse, error) { _, err := this.ValidateAdmin(ctx) if err != nil { return nil, err } var tx = this.NullTx() action, err := models.SharedNodeActionDAO.FindEnabledNodeAction(tx, req.NodeActionId) if err != nil { return nil, err } if action == nil { return &pb.FindNodeActionResponse{ NodeAction: nil, }, nil } return &pb.FindNodeActionResponse{ NodeAction: &pb.NodeAction{ Id: int64(action.Id), NodeId: int64(action.NodeId), Role: action.Role, IsOn: action.IsOn, CondsJSON: action.Conds, ActionJSON: action.Action, DurationJSON: action.Duration, }, }, nil } // UpdateNodeActionOrders 设置节点动作排序 func (this *NodeActionService) UpdateNodeActionOrders(ctx context.Context, req *pb.UpdateNodeActionOrdersRequest) (*pb.RPCSuccess, error) { _, err := this.ValidateAdmin(ctx) if err != nil { return nil, err } var tx = this.NullTx() err = models.SharedNodeActionDAO.UpdateNodeActionOrders(tx, req.NodeActionIds) if err != nil { return nil, err } return this.Success() }