规则链

This commit is contained in:
XM-GO
2023-04-07 17:02:58 +08:00
parent adc96f4176
commit 282668c0f9
33 changed files with 403 additions and 104 deletions

View File

@@ -2,7 +2,7 @@ package api
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/flow/api/flow_work_info.go
// 生成路径: apps/flow/api/rulechain.go
// 生成人panda
// ==========================================================================
import (

View File

@@ -1,6 +1,6 @@
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/flow/router/flow_work_info.go
// 生成路径: apps/flow/router/rulechain.go
// 生成人panda
// ==========================================================================
package router

View File

@@ -1,6 +1,6 @@
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/flow/services/flow_work_info.go
// 生成路径: apps/flow/services/rulechain.go
// 生成人panda
// ==========================================================================

View File

@@ -1,7 +1,6 @@
package api
import (
"context"
"fmt"
"github.com/XM-GO/PandaKit/biz"
"github.com/XM-GO/PandaKit/restfulx"
@@ -10,8 +9,6 @@ import (
"github.com/gorilla/websocket"
"github.com/kakuilan/kgo"
"pandax/pkg/middleware"
"pandax/pkg/rule_engine"
"pandax/pkg/rule_engine/message"
"runtime"
)
@@ -83,15 +80,3 @@ func (s *System) ConnectWs(request *restful.Request, response *restful.Response)
la := rc.LoginAccount
ws.Put(uint64(la.UserId), wsConn)
}
func (s *System) TestRuleChain(request *restful.Request, response *restful.Response) {
parameter := request.QueryParameter("code")
instance, _ := rule_engine.NewRuleChainInstance([]byte(parameter))
newMessage := message.NewMessage()
instance.StartRuleChain(context.Background(), newMessage)
response.WriteEntity(map[string]any{
"code": 200,
"logs": []map[string]interface{}{},
})
}

View File

@@ -11,6 +11,5 @@ func InitSystemRouter(container *restful.Container) {
ws.Path("/system").Produces(restful.MIME_JSON)
ws.Route(ws.GET("/").To(s.ConnectWs))
ws.Route(ws.GET("/server").To(s.ServerInfo))
ws.Route(ws.GET("/test/rulechain").To(s.ServerInfo))
container.Add(ws)
}

View File

@@ -0,0 +1,76 @@
package api
import (
"context"
"github.com/XM-GO/PandaKit/model"
"github.com/XM-GO/PandaKit/restfulx"
"pandax/apps/visual/entity"
"pandax/apps/visual/services"
"pandax/pkg/rule_engine"
"pandax/pkg/rule_engine/message"
"pandax/pkg/rule_engine/nodes"
"strings"
)
type RuleChainApi struct {
VisualRuleChainApp services.VisualRuleChainModel
}
func (r *RuleChainApi) GetNodeLabels(rc *restfulx.ReqCtx) {
rc.ResData = nodes.GetCategory()
}
func (r *RuleChainApi) RuleChainTest(rc *restfulx.ReqCtx) {
code := restfulx.QueryParam(rc, "code")
instance, _ := rule_engine.NewRuleChainInstance([]byte(code))
newMessage := message.NewMessage()
newMessage.SetMetadata(message.NewMetadata())
instance.StartRuleChain(context.Background(), newMessage)
rc.ResData = []map[string]interface{}{}
}
// GetVisualRuleChainList WorkInfo列表数据
func (p *RuleChainApi) GetVisualRuleChainList(rc *restfulx.ReqCtx) {
data := entity.VisualRuleChain{}
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
data.RuleName = restfulx.QueryParam(rc, "ruleName")
data.Status = restfulx.QueryParam(rc, "status")
list, total := p.VisualRuleChainApp.FindListPage(pageNum, pageSize, data)
rc.ResData = model.ResultPage{
Total: total,
PageNum: int64(pageNum),
PageSize: int64(pageNum),
Data: list,
}
}
// GetVisualRuleChain 获取规则链
func (p *RuleChainApi) GetVisualRuleChain(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
rc.ResData = p.VisualRuleChainApp.FindOne(id)
}
// InsertVisualRuleChain 添加规则链
func (p *RuleChainApi) InsertVisualRuleChain(rc *restfulx.ReqCtx) {
var data entity.VisualRuleChain
restfulx.BindQuery(rc, &data)
data.Creator = rc.LoginAccount.UserName
p.VisualRuleChainApp.Insert(data)
}
// UpdateVisualRuleChain 修改规则链
func (p *RuleChainApi) UpdateVisualRuleChain(rc *restfulx.ReqCtx) {
var data entity.VisualRuleChain
restfulx.BindQuery(rc, &data)
p.VisualRuleChainApp.Update(data)
}
// DeleteVisualRuleChain 删除规则链
func (p *RuleChainApi) DeleteVisualRuleChain(rc *restfulx.ReqCtx) {
id := restfulx.PathParam(rc, "id")
ids := strings.Split(id, ",")
p.VisualRuleChainApp.Delete(ids)
}

View File

@@ -2,14 +2,14 @@ package entity
import "github.com/XM-GO/PandaKit/model"
type DataSetGroup struct {
type VisualDataSetGroup struct {
model.BaseModelD
Name string `gorm:"name;type:varchar(64);comment:数据源类型" json:"name"`
Pid string `json:"pid"`
Level int64 `json:"level"`
}
/*type DataSetTable struct {
/*type VisualDataSetTable struct {
model.BaseModelD
TableId string `gorm:"name;type:TEXT;comment:表id" json:"tableId"`
DataSourceId string `gorm:"name;type:TEXT;comment:数据圆ID" json:"data_source_Id"`

View File

@@ -2,7 +2,7 @@ package entity
import "github.com/XM-GO/PandaKit/model"
type DataSource struct {
type VisualDataSource struct {
model.BaseModel
SourceId string `gorm:"source_id;comment:数据源Id" json:"sourceId"` // 数据源Id
SourceType string `gorm:"source_type;type:varchar(50);comment:数据源类型" json:"sourceType"` // 数据源类型
@@ -14,7 +14,7 @@ type DataSource struct {
}
type Db struct {
type VisualDb struct {
DbIp string `gorm:"db_ip" json:"dbIp"`
DbPort string `gorm:"db_port" json:"dbPort"`
DbName string `gorm:"db_name" json:"dbName"`
@@ -23,7 +23,7 @@ type Db struct {
DbJointParam string `gorm:"db_joint_param" json:"dbJointParam"` //额外的链接参数
}
type Api struct {
type VisualApi struct {
Method string `gorm:"method" json:"method"`
url string `gorm:"url" json:"url"`
Headers map[string]interface{} `gorm:"headers" json:"headers"`
@@ -31,6 +31,6 @@ type Api struct {
Auth string `gorm:"db_password" json:"dbPassword"`
}
func (DataSource) TableName() string {
return "bi_data_source"
func (VisualDataSource) TableName() string {
return "visual_data_source"
}

View File

@@ -0,0 +1,18 @@
package entity
import (
"github.com/XM-GO/PandaKit/model"
)
type VisualRuleChain struct {
UserId string `json:"userId"`
RuleId string `json:"ruleId"`
RuleName string `json:"ruleName"`
RuleDataJson string `json:"ruleDataJson"`
RuleBase64 string `json:"ruleBase64"` //缩略图 base64
RuleRemark string `json:"ruleRemark"`
Status string `json:"status"`
DeviceId string `json:"deviceId"`
Creator string `json:"creator"` //创建者
model.BaseModel
}

View File

@@ -0,0 +1,79 @@
package api
import (
"github.com/XM-GO/PandaKit/model"
"github.com/XM-GO/PandaKit/restfulx"
restfulspec "github.com/emicklei/go-restful-openapi/v2"
"github.com/emicklei/go-restful/v3"
"pandax/apps/visual/api"
"pandax/apps/visual/entity"
)
func InitRuleChainRouter(container *restful.Container) {
s := &api.RuleChainApi{}
ws := new(restful.WebService)
ws.Path("/visual/rulechain").Produces(restful.MIME_JSON)
tags := []string{"rulechain"}
ws.Route(ws.GET("/nodeLabels").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("获取所有节点标签").Handle(s.GetNodeLabels)
}).
Doc("获取所有节点标签").
Metadata(restfulspec.KeyOpenAPITags, tags).
Metadata(restfulspec.KeyOpenAPITags, tags).
Returns(200, "OK", model.ResultPage{}))
ws.Route(ws.GET("/test").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithNeedCasbin(false).WithLog("测试规则引擎").Handle(s.RuleChainTest)
}).
Doc("测试规则引擎").
Param(ws.QueryParameter("code", "流程代码").DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags))
ws.Route(ws.GET("/list").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("获取规则引擎分页列表").Handle(s.GetVisualRuleChainList)
}).
Doc("获取规则引擎分页列表").
Param(ws.QueryParameter("pageNum", "页数").Required(true).DataType("int")).
Param(ws.QueryParameter("pageSize", "每页条数").Required(true).DataType("int")).
Param(ws.QueryParameter("ruleName", "规则名").Required(false).DataType("string")).
Param(ws.QueryParameter("status", "状态").Required(false).DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Writes(model.ResultPage{}).
Returns(200, "OK", model.ResultPage{}))
ws.Route(ws.GET("/{id}").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("获取规则引擎信息").Handle(s.GetVisualRuleChain)
}).
Doc("获取规则引擎信息").
Param(ws.PathParameter("id", "Id").DataType("string")).
Metadata(restfulspec.KeyOpenAPITags, tags).
Writes(entity.VisualRuleChain{}). // on the response
Returns(200, "OK", entity.VisualRuleChain{}).
Returns(404, "Not Found", nil))
ws.Route(ws.POST("").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("添加规则引擎信息").Handle(s.InsertVisualRuleChain)
}).
Doc("添加规则引擎信息").
Metadata(restfulspec.KeyOpenAPITags, tags).
Reads(entity.VisualRuleChain{}))
ws.Route(ws.PUT("").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("修改规则引擎信息").Handle(s.UpdateVisualRuleChain)
}).
Doc("修改规则引擎信息").
Metadata(restfulspec.KeyOpenAPITags, tags).
Reads(entity.VisualRuleChain{}))
ws.Route(ws.DELETE("/{id}").To(func(request *restful.Request, response *restful.Response) {
restfulx.NewReqCtx(request, response).WithLog("删除规则引擎信息").Handle(s.DeleteVisualRuleChain)
}).
Doc("删除规则引擎信息").
Metadata(restfulspec.KeyOpenAPITags, tags).
Param(ws.PathParameter("id", "多id 1,2,3").DataType("string")))
container.Add(ws)
}

View File

@@ -0,0 +1,101 @@
// ==========================================================================
// 生成日期2023-03-29 20:01:11 +0800 CST
// 生成路径: apps/visual/services/rulechain.go
// 生成人panda
// ==========================================================================
package services
import (
"github.com/XM-GO/PandaKit/biz"
"pandax/apps/visual/entity"
"pandax/pkg/global"
)
type (
VisualRuleChainModel interface {
Insert(data entity.VisualRuleChain) *entity.VisualRuleChain
FindOne(id string) *entity.VisualRuleChain
FindListPage(page, pageSize int, data entity.VisualRuleChain) (*[]entity.VisualRuleChain, int64)
FindList(data entity.VisualRuleChain) *[]entity.VisualRuleChain
Update(data entity.VisualRuleChain) *entity.VisualRuleChain
Delete(ids []string)
}
ruleChainModelImpl struct {
table string
}
)
var VisualRuleChainModelDao VisualRuleChainModel = &ruleChainModelImpl{
table: `visual_rule_chain`,
}
func (m *ruleChainModelImpl) Insert(data entity.VisualRuleChain) *entity.VisualRuleChain {
err := global.Db.Table(m.table).Create(&data).Error
biz.ErrIsNil(err, "添加规则链失败")
return &data
}
func (m *ruleChainModelImpl) FindOne(id string) *entity.VisualRuleChain {
resData := new(entity.VisualRuleChain)
db := global.Db.Table(m.table).Where("rule_id = ?", id)
err := db.First(resData).Error
biz.ErrIsNil(err, "查询规则链失败")
return resData
}
func (m *ruleChainModelImpl) FindListPage(page, pageSize int, data entity.VisualRuleChain) (*[]entity.VisualRuleChain, int64) {
list := make([]entity.VisualRuleChain, 0)
var total int64 = 0
offset := pageSize * (page - 1)
db := global.Db.Table(m.table)
// 此处填写 where参数判断
db.Where("delete_time IS NULL")
if data.UserId != "" {
db = db.Where("user_id = ?", data.UserId)
}
if data.RuleName != "" {
db = db.Where("rule_name = ?", data.RuleName)
}
if data.RuleRemark != "" {
db = db.Where("rule_remark like ?", "%"+data.RuleRemark+"%")
}
if data.Status != "" {
db = db.Where("status = ?", data.Status)
}
err := db.Count(&total).Error
err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error
biz.ErrIsNil(err, "查询规则链分页列表失败")
return &list, total
}
func (m *ruleChainModelImpl) FindList(data entity.VisualRuleChain) *[]entity.VisualRuleChain {
list := make([]entity.VisualRuleChain, 0)
db := global.Db.Table(m.table)
// 此处填写 where参数判断
db.Where("delete_time IS NULL")
if data.UserId != "" {
db = db.Where("user_id = ?", data.UserId)
}
if data.RuleName != "" {
db = db.Where("rule_name = ?", data.RuleName)
}
if data.RuleRemark != "" {
db = db.Where("rule_remark like ?", "%"+data.RuleRemark+"%")
}
if data.Status != "" {
db = db.Where("status = ?", data.Status)
}
biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询规则链列表失败")
return &list
}
func (m *ruleChainModelImpl) Update(data entity.VisualRuleChain) *entity.VisualRuleChain {
biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改规则链失败")
return &data
}
func (m *ruleChainModelImpl) Delete(ids []string) {
biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.VisualRuleChain{}, "rule_id in (?)", ids).Error, "删除规则链失败")
}

16
go.mod
View File

@@ -11,14 +11,16 @@ require (
github.com/emicklei/go-restful-openapi/v2 v2.9.0
github.com/emicklei/go-restful/v3 v3.9.0
github.com/go-openapi/spec v0.20.6
github.com/google/uuid v1.3.0
github.com/gorilla/websocket v1.4.2
github.com/kakuilan/kgo v0.1.8
github.com/mitchellh/mapstructure v1.5.0
github.com/mssola/user_agent v0.5.3
github.com/nats-io/nats.go v1.25.0
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.5.0
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4
golang.org/x/crypto v0.6.0
google.golang.org/grpc v1.48.0
gorm.io/gorm v1.22.3
)
@@ -44,7 +46,6 @@ require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.1 // indirect
@@ -66,6 +67,9 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/mojocn/base64Captcha v1.3.5 // indirect
github.com/mozillazg/go-httpheader v0.2.1 // indirect
github.com/nats-io/nats-server/v2 v2.9.15 // indirect
github.com/nats-io/nkeys v0.4.4 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
@@ -77,11 +81,11 @@ require (
github.com/xuri/efp v0.0.0-20210322160811-ab561f5b45e3 // indirect
github.com/xuri/excelize/v2 v2.4.1 // indirect
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/net v0.6.0 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.8 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect

28
go.sum
View File

@@ -217,6 +217,7 @@ github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHm
github.com/kakuilan/kgo v0.1.8 h1:b9UfGYNbUpWjPheOEgu/MsWUVDNWbcSit6BbNsBAPl0=
github.com/kakuilan/kgo v0.1.8/go.mod h1:S9driqss6OluzqiOfUx7xN8nw0H6bFu5v7c19P09RRc=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.16.0 h1:iULayQNOReoYUe+1qtKOqw9CwJv3aNQu8ivo7lw1HU4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
@@ -244,6 +245,7 @@ github.com/mattn/go-colorable v0.1.6/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -260,6 +262,15 @@ github.com/mozillazg/go-httpheader v0.2.1 h1:geV7TrjbL8KXSyvghnFm+NyTux/hxwueTSr
github.com/mozillazg/go-httpheader v0.2.1/go.mod h1:jJ8xECTlalr6ValeXYdOF8fFUISeBAdw6E61aqQma60=
github.com/mssola/user_agent v0.5.3 h1:lBRPML9mdFuIZgI2cmlQ+atbpJdLdeVl2IDodjBR578=
github.com/mssola/user_agent v0.5.3/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
github.com/nats-io/jwt/v2 v2.3.0 h1:z2mA1a7tIf5ShggOFlR1oBPgd6hGqcDYsISxZByUzdI=
github.com/nats-io/nats-server/v2 v2.9.15 h1:MuwEJheIwpvFgqvbs20W8Ish2azcygjf4Z0liVu2I4c=
github.com/nats-io/nats-server/v2 v2.9.15/go.mod h1:QlCTy115fqpx4KSOPFIxSV7DdI6OxtZsGOL1JLdeRlE=
github.com/nats-io/nats.go v1.25.0 h1:t5/wCPGciR7X3Mu8QOi4jiJaXaWM8qtkLu4lzGZvYHE=
github.com/nats-io/nats.go v1.25.0/go.mod h1:D2WALIhz7V8M0pH8Scx8JZXlg6Oqz5VG+nQkK8nJdvg=
github.com/nats-io/nkeys v0.4.4 h1:xvBJ8d69TznjcQl9t6//Q5xXuVhyYiSos6RPtvQNTwA=
github.com/nats-io/nkeys v0.4.4/go.mod h1:XUkxdLPTufzlihbamfzQ7mw/VGx6ObUs+0bN5sNvt64=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=
@@ -350,8 +361,8 @@ golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWP
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20190501045829-6d32002ffd75/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
@@ -377,8 +388,9 @@ golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwY
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b h1:PxfKdU9lEEDYjdIzOtC4qFWgkU2rGHdKlKowJSMN9h0=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0 h1:L4ZwwTvKW9gr0ZMS1yrHD9GZhIuVjOBBnaKH+SPQK0Q=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -415,8 +427,9 @@ golang.org/x/sys v0.0.0-20211020174200-9d6173849985/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
@@ -426,10 +439,11 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 h1:GZokNIeuVkl3aZHJchRrr13WCsols02MLUcz1U9is6M=
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=

View File

@@ -10,6 +10,7 @@ import (
logRouter "pandax/apps/log/router"
resRouter "pandax/apps/resource/router"
sysRouter "pandax/apps/system/router"
visualRouter "pandax/apps/visual/router"
"pandax/pkg/middleware"
)
@@ -50,6 +51,10 @@ func InitRouter() *transport.HttpServer {
flowRouter.InitFlowWorkInfoRouter(container)
flowRouter.InitFlowWorkTemplatesRouter(container)
}
// 可视化
{
visualRouter.InitRuleChainRouter(container)
}
// 任务
{
jobRouter.InitJobRouter(container)

View File

@@ -1,6 +1,11 @@
package message
import "github.com/sirupsen/logrus"
import (
"encoding/json"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"time"
)
// Message ...
type Message interface {
@@ -12,6 +17,7 @@ type Message interface {
SetMsg(map[string]interface{})
SetOriginator(string)
SetMetadata(Metadata)
MarshalBinary() ([]byte, error)
}
// Metadata ...
@@ -35,16 +41,18 @@ const (
// NewMessage ...
func NewMessage() Message {
return &defaultMessage{
id: uuid.New().String(),
ts: time.Now(),
msg: map[string]interface{}{},
}
}
type defaultMessage struct {
id string //uuid
ts int64 //时间戳
ts time.Time //时间戳
msgType string //消息类型, attributes参数telemetry遥测Connect连接事件
originator string //数据发布者 设备 规则链
customerId string //客户Id UUID
userId string //客户Id UUID
deviceId string //设备Id UUID
msg map[string]interface{} //数据 数据结构JSON 设备原始数据 msg
metadata Metadata //消息的元数据 包括,设备名称,设备类型,命名空间,时间戳等
@@ -68,6 +76,9 @@ func (t *defaultMessage) SetType(msgType string) { t.msgType = msgTyp
func (t *defaultMessage) SetMsg(msg map[string]interface{}) { t.msg = msg }
func (t *defaultMessage) SetOriginator(originator string) { t.originator = originator }
func (t *defaultMessage) SetMetadata(metadata Metadata) { t.metadata = metadata }
func (t *defaultMessage) MarshalBinary() ([]byte, error) {
return json.Marshal(t)
}
// NewMetadata ...
func NewMetadata() Metadata {

View File

@@ -1,14 +1,3 @@
// 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 (

View File

@@ -14,7 +14,7 @@ type messageGeneratorNode struct {
type messageGeneratorNodeFactory struct{}
func (f messageGeneratorNodeFactory) Name() string { return "MessageGeneratorNode" }
func (f messageGeneratorNodeFactory) Name() string { return "GeneratorNode" }
func (f messageGeneratorNodeFactory) Category() string { return NODE_CATEGORY_ACTION }
func (f messageGeneratorNodeFactory) Labels() []string { return []string{"Created", "Updated"} }
func (f messageGeneratorNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -16,7 +16,7 @@ type externalDingNode struct {
type externalDingNodeFactory struct{}
func (f externalDingNodeFactory) Name() string { return "ExternalDingNode" }
func (f externalDingNodeFactory) Name() string { return "DingNode" }
func (f externalDingNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalDingNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalDingNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -14,7 +14,7 @@ type externalKafkaNode struct {
type externalKafkaNodeFactory struct{}
func (f externalKafkaNodeFactory) Name() string { return "ExternalKafkaNode" }
func (f externalKafkaNodeFactory) Name() string { return "KafkaNode" }
func (f externalKafkaNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalKafkaNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalKafkaNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -1,28 +0,0 @@
package nodes
import (
"github.com/sirupsen/logrus"
"pandax/pkg/rule_engine/message"
)
type externalMqNode struct {
bareNode
}
type externalMqNodeFactory struct{}
func (f externalMqNodeFactory) Name() string { return "ExternalMqNode" }
func (f externalMqNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalMqNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalMqNodeFactory) Create(id string, meta Metadata) (Node, error) {
node := &externalMqNode{
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
}
return decodePath(meta, node)
}
func (n *externalMqNode) Handle(msg message.Message) error {
logrus.Infof("%s handle message '%s'", n.Name(), msg.GetType())
return nil
}

View File

@@ -24,7 +24,7 @@ type externalMqttNode struct {
type externalMqttNodeFactory struct{}
func (f externalMqttNodeFactory) Name() string { return "ExternalMqttNode" }
func (f externalMqttNodeFactory) Name() string { return "MqttNode" }
func (f externalMqttNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalMqttNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalMqttNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -0,0 +1,48 @@
package nodes
import (
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
"pandax/pkg/rule_engine/message"
)
type externalNatsNode struct {
bareNode
Url string `json:"url"`
Subject string `json:"subject"`
Body string
client *nats.Conn
}
type externalNatsNodeFactory struct{}
func (f externalNatsNodeFactory) Name() string { return "NatsNode" }
func (f externalNatsNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalNatsNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalNatsNodeFactory) Create(id string, meta Metadata) (Node, error) {
node := &externalNatsNode{
bareNode: newBareNode(f.Name(), id, meta, f.Labels()),
}
_, err := decodePath(meta, node)
if err != nil {
return node, err
}
connect, err := nats.Connect(node.Url)
if err != nil {
return node, err
}
node.client = connect
return node, nil
}
func (n *externalNatsNode) Handle(msg message.Message) error {
logrus.Infof("%s handle message '%s'", n.Name(), msg.GetType())
successLabelNode := n.GetLinkedNode("Success")
failureLabelNode := n.GetLinkedNode("Failure")
err := n.client.Publish(n.Subject, []byte(n.Body))
if err != nil {
n.client.Close()
return failureLabelNode.Handle(msg)
}
return successLabelNode.Handle(msg)
}

View File

@@ -7,20 +7,14 @@ import (
type externalRestapiNode struct {
bareNode
RestEndpointUrlPattern string `json:"restEndpointUrlPattern" yaml:"restEndpointUrlPattern"`
RequestMethod string `json:"requestMethod" yaml:"requestMethod"`
headers map[string]string `json:"headers" yaml:"headers"`
UseSimpleClientHttpFactory bool `json:"useSimpleClientHttpFactory" yaml:"useSimpleClientHttpFactory"`
ReadTimeoutMs int `json:"readTimeoutMs" yaml:"readTimeoutMs"`
MaxParallelRequestsCount int `json:"maxParallelRequestsCount" yaml:"maxParallelRequestsCount"`
UseRedisQueueForMsgPersistence bool `json:"useRedisQueueForMsgPersistence" yaml:"useRedisQueueForMsgPersistence"`
trimQueue bool `json:"trimQueue" yaml:"trimQueue"`
MaxQueueSize int `json:"maxQueueSize" yaml:"maxQueueSize"`
RestEndpointUrlPattern string `json:"restEndpointUrlPattern" yaml:"restEndpointUrlPattern"`
RequestMethod string `json:"requestMethod" yaml:"requestMethod"`
headers map[string]string `json:"headers" yaml:"headers"`
}
type externalRestapiNodeFactory struct{}
func (f externalRestapiNodeFactory) Name() string { return "ExternalRestapiNode" }
func (f externalRestapiNodeFactory) Name() string { return "RestapiNode" }
func (f externalRestapiNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalRestapiNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalRestapiNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -12,7 +12,7 @@ type externalRuleChainNode struct {
type externalRuleChainNodeFactory struct{}
func (f externalRuleChainNodeFactory) Name() string { return "ExternalRuleChainNode" }
func (f externalRuleChainNodeFactory) Name() string { return "RuleChainNode" }
func (f externalRuleChainNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalRuleChainNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalRuleChainNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -17,7 +17,7 @@ type externalSendEmailNode struct {
type externalSendEmailNodeFactory struct{}
func (f externalSendEmailNodeFactory) Name() string { return "ExternalSendEmailNode" }
func (f externalSendEmailNodeFactory) Name() string { return "SendEmailNode" }
func (f externalSendEmailNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalSendEmailNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalSendEmailNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -11,7 +11,7 @@ type externalSendSmsNode struct {
type externalSendSmsNodeFactory struct{}
func (f externalSendSmsNodeFactory) Name() string { return "ExternalSendSmslNode" }
func (f externalSendSmsNodeFactory) Name() string { return "SendSmsNode" }
func (f externalSendSmsNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalSendSmsNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalSendSmsNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -16,7 +16,7 @@ type externalWechatNode struct {
type externalWechatNodeFactory struct{}
func (f externalWechatNodeFactory) Name() string { return "ExternalWechatNode" }
func (f externalWechatNodeFactory) Name() string { return "WechatNode" }
func (f externalWechatNodeFactory) Category() string { return NODE_CATEGORY_EXTERNAL }
func (f externalWechatNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f externalWechatNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -30,6 +30,7 @@ var (
// allNodeCategories hold node's metadata by category
allNodeCategories map[string][]map[string]interface{} = make(map[string][]map[string]interface{})
allCategories []map[string]interface{} = make([]map[string]interface{}, 0)
)
// RegisterFactory add a new node factory and classify its category for
@@ -41,6 +42,7 @@ func RegisterFactory(f Factory) {
allNodeCategories[f.Category()] = []map[string]interface{}{}
}
allNodeCategories[f.Category()] = append(allNodeCategories[f.Category()], map[string]interface{}{"name": f.Name(), "labels": f.Labels()})
allCategories = append(allCategories, map[string]interface{}{"name": f.Name(), "labels": f.Labels()})
}
// NewNode is the only way to create a new node
@@ -53,3 +55,5 @@ func NewNode(nodeType string, id string, meta Metadata) (Node, error) {
// GetCategoryNodes return specified category's all nodes
func GetCategoryNodes() map[string][]map[string]interface{} { return allNodeCategories }
func GetCategory() []map[string]interface{} { return allCategories }

View File

@@ -19,7 +19,7 @@ type deviceTypeSwitchNode struct {
type deviceTypeSwitchNodeFactory struct{}
func (f deviceTypeSwitchNodeFactory) Name() string { return "DeviceTypeSwitch" }
func (f deviceTypeSwitchNodeFactory) Name() string { return "DeviceTypeSwitchNode" }
func (f deviceTypeSwitchNodeFactory) Category() string { return NODE_CATEGORY_FILTER }
func (f deviceTypeSwitchNodeFactory) Labels() []string { return []string{DEVICE, GATEWAY} }
func (f deviceTypeSwitchNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -24,7 +24,7 @@ func init() {
RegisterFactory(externalDingNodeFactory{})
RegisterFactory(externalWechatNodeFactory{})
RegisterFactory(externalKafkaNodeFactory{})
RegisterFactory(externalMqNodeFactory{})
RegisterFactory(externalNatsNodeFactory{})
RegisterFactory(externalMqttNodeFactory{})
RegisterFactory(externalRestapiNodeFactory{})
RegisterFactory(externalSendEmailNodeFactory{})

View File

@@ -12,7 +12,7 @@ type transformDeleteKeyNode struct {
}
type transformDeleteKeyNodeFactory struct{}
func (f transformDeleteKeyNodeFactory) Name() string { return "TransformDeleteKeyNode" }
func (f transformDeleteKeyNodeFactory) Name() string { return "DeleteKeyNode" }
func (f transformDeleteKeyNodeFactory) Category() string { return NODE_CATEGORY_TRANSFORM }
func (f transformDeleteKeyNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f transformDeleteKeyNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -16,7 +16,7 @@ type KeyName struct {
}
type transformRenameKeyNodeFactory struct{}
func (f transformRenameKeyNodeFactory) Name() string { return "TransformRenameKeyNode" }
func (f transformRenameKeyNodeFactory) Name() string { return "RenameKeyNode" }
func (f transformRenameKeyNodeFactory) Category() string { return NODE_CATEGORY_TRANSFORM }
func (f transformRenameKeyNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f transformRenameKeyNodeFactory) Create(id string, meta Metadata) (Node, error) {

View File

@@ -12,7 +12,7 @@ type transformScriptNode struct {
type transformScriptNodeFactory struct{}
func (f transformScriptNodeFactory) Name() string { return "TransformScriptNode" }
func (f transformScriptNodeFactory) Name() string { return "ScriptNode" }
func (f transformScriptNodeFactory) Category() string { return NODE_CATEGORY_TRANSFORM }
func (f transformScriptNodeFactory) Labels() []string { return []string{"Success", "Failure"} }
func (f transformScriptNodeFactory) Create(id string, meta Metadata) (Node, error) {