mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
优化
This commit is contained in:
@@ -19,17 +19,19 @@ type RuleChainInstance struct {
|
||||
}
|
||||
|
||||
func NewRuleChainInstance(ruleID string, data []byte) (*RuleChainInstance, error) {
|
||||
instance := &RuleChainInstance{}
|
||||
manifest, err := manifest.New(data)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Errorf("invalid manifest file")
|
||||
return nil, err
|
||||
}
|
||||
withManifest, err := newInstanceWithManifest(manifest)
|
||||
instance, err = newInstanceWithManifest(manifest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
withManifest.ruleID = ruleID
|
||||
return withManifest, nil
|
||||
instance.ruleID = ruleID
|
||||
|
||||
return instance, nil
|
||||
}
|
||||
|
||||
func newInstanceWithManifest(m *manifest.Manifest) (*RuleChainInstance, error) {
|
||||
|
||||
@@ -2,16 +2,22 @@ package nodes
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"pandax/apps/device/services"
|
||||
"pandax/iothub/client/mqttclient"
|
||||
"pandax/iothub/client/tcpclient"
|
||||
"pandax/iothub/client/udpclient"
|
||||
"pandax/kit/utils"
|
||||
devicerpc "pandax/pkg/device_rpc"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/global/model"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"time"
|
||||
|
||||
"github.com/kakuilan/kgo"
|
||||
)
|
||||
|
||||
type rpcRequestFromDeviceNode struct {
|
||||
bareNode
|
||||
RequestId int `json:"requestId"`
|
||||
RequestId string `json:"requestId"`
|
||||
}
|
||||
|
||||
type rpcRequestFromDeviceFactory struct{}
|
||||
@@ -34,42 +40,55 @@ func (n *rpcRequestFromDeviceNode) Handle(msg *message.Message) error {
|
||||
if msg.Msg.GetValue("method") == nil || msg.Msg.GetValue("params") == nil {
|
||||
return errors.New("指令请求格式错误")
|
||||
}
|
||||
var rpcp = model.RpcPayload{
|
||||
|
||||
var rpcp = devicerpc.RpcPayload{
|
||||
Method: msg.Msg.GetValue("method").(string),
|
||||
Params: msg.Msg.GetValue("params"),
|
||||
}
|
||||
result, err := rpcp.GetRequestResult()
|
||||
if err != nil {
|
||||
if failureLableNode != nil {
|
||||
n.Debug(msg, message.DEBUGOUT, err.Error())
|
||||
return failureLableNode.Handle(msg)
|
||||
} else {
|
||||
return err
|
||||
var err error
|
||||
// 指令下发响应
|
||||
if rpcp.Method == "cmdResp" {
|
||||
if requestId, ok := msg.Metadata.GetValue("requestId").(string); ok {
|
||||
services.DeviceCmdLogModelDao.UpdateResp(requestId, kgo.KConv.ToStr(rpcp.Params), time.Now().Format("2006-01-02 15:04:05"))
|
||||
}
|
||||
}
|
||||
// 判断设备协议,根据不通协议,发送不通内容
|
||||
deviceProtocol := global.MQTTProtocol
|
||||
if msg.Metadata.GetValue("deviceProtocol") != nil && msg.Metadata.GetValue("deviceProtocol").(string) != "" {
|
||||
deviceProtocol = msg.Metadata.GetValue("deviceProtocol").(string)
|
||||
}
|
||||
deviceId := msg.Metadata.GetValue("deviceId").(string)
|
||||
if deviceProtocol == global.MQTTProtocol {
|
||||
rpc := &mqttclient.RpcRequest{}
|
||||
RequestId := n.RequestId
|
||||
if RequestId == 0 {
|
||||
if msg.Metadata.GetValue("requestId") == nil {
|
||||
rpc.GetRequestId()
|
||||
} else {
|
||||
result, err := rpcp.GetRequestResult()
|
||||
if err != nil {
|
||||
if failureLableNode != nil {
|
||||
n.Debug(msg, message.DEBUGOUT, err.Error())
|
||||
return failureLableNode.Handle(msg)
|
||||
} else {
|
||||
RequestId = int(msg.Metadata.GetValue("requestId").(float64))
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
rpc.RequestId = RequestId
|
||||
}
|
||||
err = rpc.Pub(deviceId, result)
|
||||
}
|
||||
if deviceProtocol == global.TCPProtocol {
|
||||
err = tcpclient.Send(deviceId, result)
|
||||
// 判断设备协议,根据不通协议,发送不通内容
|
||||
deviceProtocol := global.MQTTProtocol
|
||||
if msg.Metadata.GetValue("deviceProtocol") != nil && msg.Metadata.GetValue("deviceProtocol").(string) != "" {
|
||||
deviceProtocol = msg.Metadata.GetValue("deviceProtocol").(string)
|
||||
}
|
||||
deviceId := msg.Metadata.GetValue("deviceId").(string)
|
||||
if deviceProtocol == global.MQTTProtocol || deviceProtocol == global.CoAPProtocol || deviceProtocol == global.LwM2MProtocol {
|
||||
rpc := &mqttclient.RpcRequest{}
|
||||
RequestId := n.RequestId
|
||||
if RequestId == "" {
|
||||
if msg.Metadata.GetValue("requestId") == nil {
|
||||
rpc.RequestId = utils.GenerateID()
|
||||
} else {
|
||||
rpc.RequestId = msg.Metadata.GetValue("requestId").(string)
|
||||
}
|
||||
} else {
|
||||
rpc.RequestId = RequestId
|
||||
}
|
||||
err = rpc.Pub(deviceId, result)
|
||||
}
|
||||
if deviceProtocol == global.TCPProtocol {
|
||||
err = tcpclient.Send(deviceId, result)
|
||||
}
|
||||
if deviceProtocol == global.UDPProtocol {
|
||||
err = udpclient.Send(deviceId, result)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
n.Debug(msg, message.DEBUGOUT, err.Error())
|
||||
if failureLableNode != nil {
|
||||
|
||||
@@ -3,11 +3,18 @@ package nodes
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"pandax/apps/device/entity"
|
||||
"pandax/apps/device/services"
|
||||
"pandax/iothub/client/mqttclient"
|
||||
"pandax/iothub/client/tcpclient"
|
||||
"pandax/iothub/client/udpclient"
|
||||
"pandax/kit/utils"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/global/model"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"time"
|
||||
|
||||
"github.com/kakuilan/kgo"
|
||||
)
|
||||
|
||||
type rpcRequestToDeviceNode struct {
|
||||
@@ -34,11 +41,24 @@ func (n *rpcRequestToDeviceNode) Handle(msg *message.Message) error {
|
||||
if msg.Msg.GetValue("method") == nil || msg.Msg.GetValue("params") == nil {
|
||||
return errors.New("指令下发格式错误")
|
||||
}
|
||||
deviceId := msg.Metadata.GetValue("deviceId").(string)
|
||||
// 创建请求格式
|
||||
var datas = model.RpcPayload{
|
||||
Method: msg.Msg.GetValue("method").(string),
|
||||
Params: msg.Msg.GetValue("params"),
|
||||
}
|
||||
payload, _ := json.Marshal(datas)
|
||||
|
||||
// 构建指令记录
|
||||
var data entity.DeviceCmdLog
|
||||
data.Id = utils.GenerateID()
|
||||
data.DeviceId = deviceId
|
||||
data.CmdName = datas.Method
|
||||
data.CmdContent = kgo.KConv.ToStr(datas.Params)
|
||||
data.Mode = msg.Metadata.GetValue("mode").(string)
|
||||
data.State = "2"
|
||||
data.RequestTime = time.Now().Format("2006-01-02 15:04:05")
|
||||
|
||||
mode := mqttclient.SingleMode
|
||||
if n.Timeout > 0 {
|
||||
mode = mqttclient.DoubleMode
|
||||
@@ -49,16 +69,20 @@ func (n *rpcRequestToDeviceNode) Handle(msg *message.Message) error {
|
||||
deviceProtocol = msg.Metadata.GetValue("deviceProtocol").(string)
|
||||
}
|
||||
var err error
|
||||
deviceId := msg.Metadata.GetValue("deviceId").(string)
|
||||
if deviceProtocol == global.MQTTProtocol {
|
||||
var rpc = &mqttclient.RpcRequest{Mode: mode, Timeout: n.Timeout}
|
||||
rpc.GetRequestId()
|
||||
if deviceProtocol == global.MQTTProtocol || deviceProtocol == global.CoAPProtocol || deviceProtocol == global.LwM2MProtocol {
|
||||
var rpc = &mqttclient.RpcRequest{Mode: mode, Timeout: n.Timeout, RequestId: data.Id}
|
||||
err = rpc.RequestCmd(deviceId, string(payload))
|
||||
}
|
||||
if deviceProtocol == global.TCPProtocol {
|
||||
err = tcpclient.Send(deviceId, string(payload))
|
||||
}
|
||||
if deviceProtocol == global.UDPProtocol {
|
||||
err = udpclient.Send(deviceId, string(payload))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
data.State = "1"
|
||||
services.DeviceCmdLogModelDao.Insert(data)
|
||||
n.Debug(msg, message.DEBUGOUT, err.Error())
|
||||
if failureLableNode != nil {
|
||||
return failureLableNode.Handle(msg)
|
||||
@@ -66,7 +90,10 @@ func (n *rpcRequestToDeviceNode) Handle(msg *message.Message) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if successLableNode != nil {
|
||||
data.State = "0"
|
||||
services.DeviceCmdLogModelDao.Insert(data)
|
||||
n.Debug(msg, message.DEBUGOUT, "")
|
||||
return successLableNode.Handle(msg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user