Kaynağa Gözat

更新规则链配置

liuxiulin 2 yıl önce
ebeveyn
işleme
281f04c430
2 değiştirilmiş dosya ile 247 ekleme ve 38 silme
  1. 203 0
      run.sh
  2. 44 38
      services/knowoapi/services/rule_chain.go

+ 203 - 0
run.sh

@@ -1,3 +1,206 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 export GOPATH=/Users/lijian/go
 LEVEL="error"
 sudo killall -9 httpaccess registry apiprovider devicemanager controller mqttaccess knowoapi fileaccess coapaccess

+ 44 - 38
services/knowoapi/services/rule_chain.go

@@ -3,6 +3,7 @@ package services
 import (
 	"encoding/json"
 	"fmt"
+	"github.com/gogf/gf/encoding/gjson"
 	"github.com/gogf/gf/util/guid"
 	"github.com/jinzhu/gorm"
 	"sparrow/pkg/models"
@@ -16,6 +17,7 @@ var nodeType = map[string]string{
 	"MsgTypeSwitchNode": "MsgTypeSwitchNode",
 	"JavascriptFilter":  "FilterJavascriptNode",
 	"RestApiAction":     "RestApiRequestNode",
+	"InfluxDBNode":      "InfluxDBNode",
 }
 
 // RuleChainService 业务接口
@@ -65,7 +67,7 @@ func (a ruleChainService) Create(ruleChain *models.RuleChain) error {
 	return nil
 }
 
-func (a ruleChainService) create(params *models.RuleChainParams) error {
+func (a ruleChainService) create(params *models.RuleChainParams) (firstNodeId string, err error) {
 
 	nodeMap := make(map[string]models.RuleNode)
 	var inputNodeId string
@@ -85,45 +87,49 @@ func (a ruleChainService) create(params *models.RuleChainParams) error {
 				Intro:       v.Data.Desc,
 			}
 
-			switch ruleNode.Type {
-			case "MsgTypeSwitchNode":
-				ruleNode.Name = "消息类型路由器"
-			case "MsgTypeFilter":
-				conMap := map[string]interface{}{
-					"msg_type": v.Data.MesType,
-				}
-				marshal, _ := json.Marshal(conMap)
-				ruleNode.Configuration = fmt.Sprintf("%s", marshal)
-
-			case "FilterJavascriptNode":
-				conMap := map[string]string{
-					"func_body": v.Data.FuncBody,
-				}
-				marshal, _ := json.Marshal(conMap)
-				ruleNode.Configuration = fmt.Sprintf("%s", marshal)
-			case "RestApiRequestNode":
-				configuration := models.NodeConfiguration{
-					Url:       v.Data.Url,
-					Method:    v.Data.Method,
-					Retry:     v.Data.Retry,
-					TimeOut:   v.Data.TimeOut,
-					RetryWait: v.Data.RetryWait,
-					Headers:   make(map[string]interface{}),
-				}
-				if len(v.Data.Headers) > 0 {
-					for _, header := range v.Data.Headers {
-						configuration.Headers[header.Key] = header.Value
-					}
-				}
-				marshal, _ := json.Marshal(configuration)
-				ruleNode.Configuration = fmt.Sprintf("%s", marshal)
-			}
+			//switch ruleNode.Type {
+			//case "MsgTypeSwitchNode":
+			//	ruleNode.Name = "消息类型路由器"
+			//case "MsgTypeFilter":
+			//	conMap := map[string]interface{}{
+			//		"msg_type": v.Data.MesType,
+			//	}
+			//	marshal, _ := json.Marshal(conMap)
+			//	ruleNode.Configuration = fmt.Sprintf("%s", marshal)
+			//
+			//case "FilterJavascriptNode":
+			//	conMap := map[string]string{
+			//		"func_body": v.Data.FuncBody,
+			//	}
+			//	marshal, _ := json.Marshal(conMap)
+			//	ruleNode.Configuration = fmt.Sprintf("%s", marshal)
+			//case "RestApiRequestNode":
+			//	configuration := models.NodeConfiguration{
+			//		Url:       v.Data.Url,
+			//		Method:    v.Data.Method,
+			//		Retry:     v.Data.Retry,
+			//		TimeOut:   v.Data.TimeOut,
+			//		RetryWait: v.Data.RetryWait,
+			//		Headers:   make(map[string]interface{}),
+			//	}
+			//	if len(v.Data.Headers) > 0 {
+			//		for _, header := range v.Data.Headers {
+			//			configuration.Headers[header.Key] = header.Value
+			//		}
+			//	}
+			//}
+
+			str, _ := gjson.DecodeToJson(v.Data)
+			ruleNode.Configuration = fmt.Sprintf("%s", str.MustToJsonString())
 
 			nodeMap[v.Id] = ruleNode
 
 			err := a.model.RuleNode.Create(&ruleNode)
 			if err != nil {
-				return err
+				return firstNodeId, err
+			}
+			if firstNodeId == "" {
+				firstNodeId = ruleNode.RecordId
 			}
 		}
 	}
@@ -140,12 +146,12 @@ func (a ruleChainService) create(params *models.RuleChainParams) error {
 				RelationType: v.Data.Label,
 			})
 			if err != nil {
-				return err
+				return firstNodeId, err
 			}
 		}
 	}
 
-	return nil
+	return firstNodeId, err
 }
 
 func (a ruleChainService) Delete(ruleChain *models.RuleChain) error {
@@ -210,7 +216,7 @@ func (a ruleChainService) Update(params *models.RuleChainParams) error {
 		if err != nil {
 			return err
 		}
-		err = a.create(params)
+		ruleChain.FirstRuleNodeID, err = a.create(params)
 		if err != nil {
 			return err
 		}