mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
[优化] 设置状态监控
This commit is contained in:
102
pkg/rule_engine/engine.go
Normal file
102
pkg/rule_engine/engine.go
Normal file
@@ -0,0 +1,102 @@
|
||||
package rule_engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"pandax/pkg/rule_engine/nodes"
|
||||
"sync"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var RuleEngine = (*RuleChainEngine)(nil)
|
||||
|
||||
type RuleChainEngine struct {
|
||||
pool sync.Map
|
||||
ruleChainDebugData *message.RuleChainDebugData
|
||||
}
|
||||
|
||||
func init() {
|
||||
RuleEngine = &RuleChainEngine{
|
||||
pool: sync.Map{},
|
||||
ruleChainDebugData: message.NewRuleChainDebugData(100),
|
||||
}
|
||||
}
|
||||
|
||||
func (en *RuleChainEngine) GetRuleInstance(ruleID string) *RuleChainInstance {
|
||||
instance, ok := en.pool.Load(ruleID)
|
||||
if ok {
|
||||
return instance.(*RuleChainInstance)
|
||||
}
|
||||
// TODO 没有就去数据库查询并返回
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// iD为产品Id, 一个产品对应一个规则实体
|
||||
func (en *RuleChainEngine) SaveRuleInstance(id string, instance *RuleChainInstance) (*RuleChainInstance, error) {
|
||||
en.pool.Store(id, instance)
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
func (en *RuleChainEngine) DeletRuleInstance(id string) {
|
||||
en.pool.Delete(id)
|
||||
}
|
||||
|
||||
func (en *RuleChainEngine) StartRuleInstance(instance *RuleChainInstance, msg *message.Message) error {
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case debugMsg := <-msg.DeBugChan:
|
||||
en.ruleChainDebugData.Add(instance.ruleId, debugMsg.NodeId, debugMsg)
|
||||
case <-msg.EndDeBugChan:
|
||||
logrus.Debugf("规则链%s,执行结束", msg.Id)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
node, found := instance.nodes[instance.firstRuleNodeID]
|
||||
if !found {
|
||||
return errors.New("first rule node not found")
|
||||
}
|
||||
|
||||
err := node.Handle(msg)
|
||||
msg.EndDeBugChan <- struct{}{}
|
||||
return err
|
||||
}
|
||||
|
||||
func (en *RuleChainEngine) GetDebugData(ruleId, nodeId string) []message.DebugData {
|
||||
if data, ok := en.ruleChainDebugData.Data[ruleId]; ok {
|
||||
return data.Get(nodeId).Items
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (en *RuleChainEngine) ClearDebugData(ruleId, nodeId string) {
|
||||
if data, ok := en.ruleChainDebugData.Data[ruleId]; ok {
|
||||
data.Clear(nodeId)
|
||||
}
|
||||
}
|
||||
|
||||
func (en *RuleChainEngine) GetDebugDataPage(page, pageSize int, ruleId, nodeId string) (int64, []message.DebugData, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
offset := pageSize * (page - 1)
|
||||
if data, ok := en.ruleChainDebugData.Data[ruleId]; ok {
|
||||
nodeData := data.Get(nodeId)
|
||||
if nodeData != nil {
|
||||
total := len(nodeData.Items)
|
||||
end := offset + pageSize
|
||||
if end >= total {
|
||||
end = total
|
||||
}
|
||||
return int64(total), nodeData.Items[offset:end], nil
|
||||
}
|
||||
}
|
||||
return 0, nil, errors.New("规则不存在")
|
||||
}
|
||||
|
||||
func GetCategory() []map[string]interface{} {
|
||||
return nodes.GetCategory()
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
package rule_engine
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"pandax/pkg/rule_engine/nodes"
|
||||
)
|
||||
|
||||
func GetCategory() []map[string]interface{} {
|
||||
return nodes.GetCategory()
|
||||
}
|
||||
|
||||
func GetDebugData(ruleId, nodeId string) []message.DebugData {
|
||||
if data, ok := ruleChainDebugData.Data[ruleId]; ok {
|
||||
return data.Get(nodeId).Items
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ClearDebugData(ruleId, nodeId string) {
|
||||
if data, ok := ruleChainDebugData.Data[ruleId]; ok {
|
||||
data.Clear(nodeId)
|
||||
}
|
||||
}
|
||||
|
||||
func GetDebugDataPage(page, pageSize int, ruleId, nodeId string) (int64, []message.DebugData, error) {
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
offset := pageSize * (page - 1)
|
||||
if data, ok := ruleChainDebugData.Data[ruleId]; ok {
|
||||
nodeData := data.Get(nodeId)
|
||||
if nodeData != nil {
|
||||
total := len(nodeData.Items)
|
||||
end := offset + pageSize
|
||||
if end >= total {
|
||||
end = total
|
||||
}
|
||||
return int64(total), nodeData.Items[offset:end], nil
|
||||
}
|
||||
}
|
||||
return 0, nil, errors.New("规则不存在")
|
||||
}
|
||||
@@ -1,37 +1,30 @@
|
||||
package rule_engine
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"pandax/pkg/rule_engine/manifest"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"pandax/pkg/rule_engine/nodes"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var ruleChainDebugData = message.NewRuleChainDebugData(100)
|
||||
|
||||
type RuleChainInstance struct {
|
||||
ruleID string
|
||||
ruleId string
|
||||
firstRuleNodeID string
|
||||
nodes map[string]nodes.Node
|
||||
}
|
||||
|
||||
func NewRuleChainInstance(ruleID string, data []byte) (*RuleChainInstance, error) {
|
||||
instance := &RuleChainInstance{}
|
||||
func NewRuleChainInstance(ruleId string, data []byte) (*RuleChainInstance, error) {
|
||||
manifest, err := manifest.New(data)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("invalid manifest file")
|
||||
return nil, err
|
||||
}
|
||||
instance, err = newInstanceWithManifest(manifest)
|
||||
withManifest, err := newInstanceWithManifest(manifest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
instance.ruleID = ruleID
|
||||
|
||||
return instance, nil
|
||||
withManifest.ruleId = ruleId
|
||||
return withManifest, nil
|
||||
}
|
||||
|
||||
func newInstanceWithManifest(m *manifest.Manifest) (*RuleChainInstance, error) {
|
||||
@@ -45,29 +38,3 @@ func newInstanceWithManifest(m *manifest.Manifest) (*RuleChainInstance, error) {
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (c *RuleChainInstance) StartRuleChain(ctx context.Context, msg *message.Message) error {
|
||||
debugChan := make(chan *message.DebugData, 100)
|
||||
endDebugChan := make(chan struct{})
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case debugMsg := <-debugChan:
|
||||
ruleChainDebugData.Add(c.ruleID, debugMsg.NodeId, *debugMsg)
|
||||
case <-endDebugChan:
|
||||
logrus.Debugf("规则链%s,执行结束", msg.Id)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
node, found := c.nodes[c.firstRuleNodeID]
|
||||
if !found {
|
||||
return errors.New("first rule node not found")
|
||||
}
|
||||
|
||||
err := node.Handle(msg)
|
||||
endDebugChan <- struct{}{}
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
"pandax/iothub/client/mqttclient"
|
||||
"pandax/iothub/client/tcpclient"
|
||||
"pandax/iothub/client/udpclient"
|
||||
devicerpc "pandax/pkg/device_rpc"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/global/model"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"time"
|
||||
|
||||
@@ -48,7 +48,7 @@ func (n *rpcRequestToDeviceNode) Handle(msg *message.Message) error {
|
||||
return errors.New("元数据中为获取到设备ID")
|
||||
}
|
||||
// 创建请求格式
|
||||
var datas = model.RpcPayload{
|
||||
var datas = devicerpc.RpcPayload{
|
||||
Params: msg.Msg.GetValue("params"),
|
||||
}
|
||||
if method, ok := msg.Msg.GetValue("method").(string); ok {
|
||||
|
||||
Reference in New Issue
Block a user