mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 10:58:35 +08:00
[优化] 属性下发采用规则链rpc请求
This commit is contained in:
51
apps/device/util/device_rpc.go
Normal file
51
apps/device/util/device_rpc.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"pandax/apps/device/services"
|
||||
ruleEntity "pandax/apps/rule/entity"
|
||||
ruleService "pandax/apps/rule/services"
|
||||
"pandax/pkg/global"
|
||||
"pandax/pkg/rule_engine"
|
||||
"pandax/pkg/rule_engine/message"
|
||||
"pandax/pkg/tool"
|
||||
)
|
||||
|
||||
func BuildRunDeviceRpc(deviceId, mode string, metadata map[string]interface{}) error {
|
||||
one := services.DeviceModelDao.FindOne(deviceId)
|
||||
if one.LinkStatus != global.ONLINE {
|
||||
return errors.New("设备不在线无法设置属性")
|
||||
}
|
||||
findOne := ruleService.RuleChainModelDao.FindOne(one.Product.RuleChainId)
|
||||
ruleData := ruleEntity.RuleDataJson{}
|
||||
err := tool.StringToStruct(findOne.RuleDataJson, &ruleData)
|
||||
if err != nil {
|
||||
global.Log.Error("规则链数据转化失败", err)
|
||||
return errors.New("规则链数据转化失败")
|
||||
}
|
||||
dataCode := ruleData.LfData.DataCode
|
||||
code, _ := json.Marshal(dataCode)
|
||||
//新建规则链实体
|
||||
instance, errs := rule_engine.NewRuleChainInstance(code)
|
||||
if len(errs) > 0 {
|
||||
return errs[0]
|
||||
}
|
||||
metadataVals := map[string]interface{}{
|
||||
"deviceId": one.Id,
|
||||
"mode": mode,
|
||||
"deviceName": one.Name,
|
||||
"deviceType": one.DeviceType,
|
||||
"deviceProtocol": one.Product.ProtocolName,
|
||||
"productId": one.Pid,
|
||||
"orgId": one.OrgId,
|
||||
"owner": one.Owner,
|
||||
}
|
||||
msg := message.NewMessage(one.Owner, message.RpcRequestToDevice, metadata, metadataVals)
|
||||
err = instance.StartRuleChain(context.Background(), msg)
|
||||
if err != nil {
|
||||
global.Log.Error("规则链执行失败", errs)
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user