浏览代码

增加日志节点

lijian 1 年之前
父节点
当前提交
7580165a15
共有 1 个文件被更改,包括 43 次插入0 次删除
  1. 43 0
      pkg/ruleEngine/nodes/log_node.go

+ 43 - 0
pkg/ruleEngine/nodes/log_node.go

@@ -0,0 +1,43 @@
+package nodes
+
+import (
+	"encoding/json"
+	"github.com/Sirupsen/logrus"
+	"sparrow/pkg/protocol"
+	"sparrow/pkg/ruleEngine"
+)
+
+type LogNodeConfig struct {
+	Stdout bool `json:"stdout"` // 标准控制台输出
+}
+
+// LogNode 日志打印节点
+type LogNode struct {
+	config *LogNodeConfig
+	mLog   *logrus.Logger
+}
+
+func (l *LogNode) Init(ctx ruleEngine.Context, config string) error {
+	if config == "" {
+		l.config = &LogNodeConfig{
+			Stdout: false,
+		}
+	} else {
+		c := new(LogNodeConfig)
+		err := json.Unmarshal([]byte(config), c)
+		if err != nil {
+			return err
+		}
+		l.config = c
+	}
+	l.mLog = logrus.New()
+	return nil
+}
+
+func (l *LogNode) OnMessage(ctx ruleEngine.Context, message *protocol.Message) error {
+	if l.config.Stdout {
+		l.mLog.Infof("日志节点:[设备ID:%s][消息类型:%s][消息内容:%s]", message.MetaData["device_id"], message.Type, message.Data)
+	}
+	ctx.TellNext(message, protocol.Success)
+	return nil
+}