mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
规则链
This commit is contained in:
@@ -11,8 +11,8 @@ const (
|
||||
GATEWAY = "GATEWAY"
|
||||
)
|
||||
|
||||
// 检查关联关系
|
||||
// 该消息来自与哪个实体或到那个实体
|
||||
//检查关联关系
|
||||
//该消息来自与哪个实体或到那个实体
|
||||
type deviceTypeSwitchNode struct {
|
||||
bareNode
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ type messageTypeFilterNode struct {
|
||||
|
||||
type messageTypeFilterNodeFactory struct{}
|
||||
|
||||
func (f messageTypeFilterNodeFactory) Name() string { return "MessageTypeFilterNode" }
|
||||
func (f messageTypeFilterNodeFactory) Name() string { return "MessageTypeNode" }
|
||||
func (f messageTypeFilterNodeFactory) Category() string { return NODE_CATEGORY_FILTER }
|
||||
func (f messageTypeFilterNodeFactory) Labels() []string { return []string{"True", "False"} }
|
||||
|
||||
@@ -32,9 +32,12 @@ func (n *messageTypeFilterNode) Handle(msg message.Message) error {
|
||||
messageType := msg.GetType()
|
||||
|
||||
for _, filterType := range n.MessageTypes {
|
||||
if filterType == messageType {
|
||||
if filterType == messageType && trueLabelNode != nil {
|
||||
return trueLabelNode.Handle(msg)
|
||||
}
|
||||
}
|
||||
return falseLabelNode.Handle(msg)
|
||||
if falseLabelNode != nil {
|
||||
return falseLabelNode.Handle(msg)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"log"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
)
|
||||
|
||||
type messageTypeSwitchNode struct {
|
||||
@@ -16,10 +15,12 @@ func (f messageTypeSwitchNodeFactory) Name() string { return "MessageTypeSwi
|
||||
func (f messageTypeSwitchNodeFactory) Category() string { return NODE_CATEGORY_FILTER }
|
||||
func (f messageTypeSwitchNodeFactory) Labels() []string {
|
||||
return []string{
|
||||
message.MessageTypePostAttributesRequest,
|
||||
message.MessageTypePostTelemetryRequest,
|
||||
message.MessageTypeConnectEvent,
|
||||
message.MessageTypeDisconnectEvent,
|
||||
message.EventAttributesType,
|
||||
message.EventAlarmType,
|
||||
message.EventTelemetryType,
|
||||
message.EventUpEventType,
|
||||
message.EventConnectType,
|
||||
message.EventDisConnectType,
|
||||
"Other",
|
||||
}
|
||||
}
|
||||
@@ -35,7 +36,7 @@ func (n *messageTypeSwitchNode) Handle(msg message.Message) error {
|
||||
|
||||
nodes := n.GetLinkedNodes()
|
||||
messageType := msg.GetType()
|
||||
|
||||
log.Println("开始执行messageTypeSwitchNode")
|
||||
for label, node := range nodes {
|
||||
if messageType == label {
|
||||
return node.Handle(msg)
|
||||
@@ -46,5 +47,5 @@ func (n *messageTypeSwitchNode) Handle(msg message.Message) error {
|
||||
return node.Handle(msg)
|
||||
}
|
||||
// not found
|
||||
return fmt.Errorf("%s no label to handle message", n.Name())
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package nodes
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"log"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
)
|
||||
|
||||
@@ -9,7 +10,7 @@ const ScriptFilterNodeName = "ScriptFilterNode"
|
||||
|
||||
type scriptFilterNode struct {
|
||||
bareNode
|
||||
Scripts string `json:"scripts" yaml:"scripts"`
|
||||
Script string `json:"script" yaml:"script"`
|
||||
}
|
||||
|
||||
type scriptFilterNodeFactory struct{}
|
||||
@@ -30,9 +31,14 @@ func (n *scriptFilterNode) Handle(msg message.Message) error {
|
||||
trueLabelNode := n.GetLinkedNode("True")
|
||||
falseLabelNode := n.GetLinkedNode("False")
|
||||
scriptEngine := NewScriptEngine()
|
||||
isTrue, error := scriptEngine.ScriptOnFilter(msg, n.Scripts)
|
||||
if isTrue == true && error == nil {
|
||||
isTrue, error := scriptEngine.ScriptOnFilter(msg, n.Script)
|
||||
log.Println(isTrue)
|
||||
if isTrue == true && error == nil && trueLabelNode != nil {
|
||||
return trueLabelNode.Handle(msg)
|
||||
} else {
|
||||
if falseLabelNode != nil {
|
||||
return falseLabelNode.Handle(msg)
|
||||
}
|
||||
}
|
||||
return falseLabelNode.Handle(msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,24 +1,14 @@
|
||||
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
// not use p file except in compliance with the License. You may obtain
|
||||
// a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
// License for the specific language governing permissions and limitations
|
||||
// under the License.
|
||||
package nodes
|
||||
|
||||
import (
|
||||
"github.com/sirupsen/logrus"
|
||||
"log"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
)
|
||||
|
||||
type switchFilterNode struct {
|
||||
bareNode
|
||||
Scripts string `json:"scripts" yaml:"scripts"`
|
||||
Script string `json:"script" yaml:"script"`
|
||||
}
|
||||
|
||||
type switchFilterNodeFactory struct{}
|
||||
@@ -27,8 +17,13 @@ func (f switchFilterNodeFactory) Name() string { return "SwitchNode" }
|
||||
func (f switchFilterNodeFactory) Category() string { return NODE_CATEGORY_FILTER }
|
||||
func (f switchFilterNodeFactory) Labels() []string {
|
||||
return []string{
|
||||
"Failure", "True", "False", message.MessageTypePostTelemetryRequest,
|
||||
message.MessageTypeConnectEvent,
|
||||
"Failure", "True", "False",
|
||||
message.EventAttributesType,
|
||||
message.EventAlarmType,
|
||||
message.EventTelemetryType,
|
||||
message.EventUpEventType,
|
||||
message.EventConnectType,
|
||||
message.EventDisConnectType,
|
||||
}
|
||||
}
|
||||
func (f switchFilterNodeFactory) Create(id string, meta Metadata) (Node, error) {
|
||||
@@ -42,7 +37,8 @@ func (n *switchFilterNode) Handle(msg message.Message) error {
|
||||
logrus.Infof("%s handle message '%s'", n.Name(), msg.GetType())
|
||||
|
||||
scriptEngine := NewScriptEngine()
|
||||
SwitchResults, err := scriptEngine.ScriptOnSwitch(msg, n.Scripts)
|
||||
SwitchResults, err := scriptEngine.ScriptOnSwitch(msg, n.Script)
|
||||
log.Println("开始执行switchFilterNode", SwitchResults)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@@ -50,7 +46,7 @@ func (n *switchFilterNode) Handle(msg message.Message) error {
|
||||
for label, node := range nodes {
|
||||
for _, switchresult := range SwitchResults {
|
||||
if label == switchresult {
|
||||
return node.Handle(msg)
|
||||
node.Handle(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,9 @@ package nodes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"pandax/pkg/rule_engine/manifest"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -63,3 +65,46 @@ func decodePath(meta Metadata, n Node) (Node, error) {
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func GetNodes(m *manifest.Manifest) (map[string]Node, error) {
|
||||
nodes := make(map[string]Node)
|
||||
// Create All nodes
|
||||
for _, n := range m.Nodes {
|
||||
metadata := NewMetadataWithValues(n.Properties)
|
||||
node, err := NewNode(n.Type, n.Id, metadata)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if _, found := nodes[n.Id]; found {
|
||||
logrus.Errorf("node '%s' already exist in rulechain", n.Id)
|
||||
continue
|
||||
}
|
||||
nodes[n.Id] = node
|
||||
}
|
||||
for _, edge := range m.Edges {
|
||||
originalNode, found := nodes[edge.SourceNodeId]
|
||||
if !found {
|
||||
logrus.Errorf("original node '%s' no exist in", originalNode.Name())
|
||||
continue
|
||||
}
|
||||
targetNode, found := nodes[edge.TargetNodeId]
|
||||
if !found {
|
||||
logrus.Errorf("target node '%s' no exist in rulechain", targetNode.Name())
|
||||
continue
|
||||
}
|
||||
//可以有多个类型
|
||||
//可以有多个类型
|
||||
types := make([]string, 0)
|
||||
if _, ok := edge.Properties["type"]; !ok {
|
||||
types = append(types, "True")
|
||||
} else {
|
||||
types = strings.Split(edge.Properties["type"].(string), "/")
|
||||
}
|
||||
for _, ty := range types {
|
||||
originalNode.AddLinkedNode(ty, targetNode)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nodes, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user