37 Star 391 Fork 118

联犀/物联网iot模块

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gatewayGetFoundSendLogic.go 3.32 KB
一键复制 编辑 原始数据 按行查看 历史
杨磊 提交于 2024-10-10 22:13 . feat: 更新mod
package deviceinteractlogic
import (
"context"
"encoding/json"
"gitee.com/unitedrhino/share/def"
"gitee.com/unitedrhino/share/devices"
"gitee.com/unitedrhino/share/domain/deviceMsg"
"gitee.com/unitedrhino/share/domain/deviceMsg/msgGateway"
"gitee.com/unitedrhino/share/domain/deviceMsg/msgThing"
"gitee.com/unitedrhino/share/errors"
"gitee.com/unitedrhino/share/utils"
"gitee.com/unitedrhino/things/service/dmsvr/internal/logic"
"time"
"gitee.com/unitedrhino/things/service/dmsvr/internal/svc"
"gitee.com/unitedrhino/things/service/dmsvr/pb/dm"
"github.com/zeromicro/go-zero/core/logx"
)
type GatewayGetFoundSendLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGatewayGetFoundSendLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GatewayGetFoundSendLogic {
return &GatewayGetFoundSendLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// 实时获取网关拓扑关系
func (l *GatewayGetFoundSendLogic) GatewayGetFoundSend(in *dm.GatewayGetFoundReq) (*dm.Empty, error) {
_, err := logic.SchemaAccess(l.ctx, l.svcCtx, def.AuthRead, devices.Core{
ProductID: in.ProductID,
DeviceName: in.DeviceName,
}, nil)
if err != nil {
return nil, err
}
//return &dm.Empty{}, nil
var protocolCode string
if protocolCode, err = CheckIsOnline(l.ctx, l.svcCtx, devices.Core{
ProductID: in.ProductID,
DeviceName: in.DeviceName,
}); err != nil {
return nil, err
}
MsgToken := devices.GenMsgToken(l.ctx, l.svcCtx.NodeID)
req := msgGateway.Msg{
CommonMsg: deviceMsg.CommonMsg{
Method: deviceMsg.GetFound,
MsgToken: MsgToken,
//Timestamp: time.Now().UnixMilli(),
},
}
payload, _ := json.Marshal(req)
reqMsg := deviceMsg.PublishMsg{
Handle: devices.Gateway,
Type: msgGateway.TypeTopo,
Payload: payload,
Timestamp: time.Now().UnixMilli(),
ProductID: in.ProductID,
DeviceName: in.DeviceName,
ProtocolCode: protocolCode,
}
var resp []byte
resp, err = l.svcCtx.PubDev.ReqToDeviceSync(l.ctx, &reqMsg, time.Second*10, func(payload []byte) bool {
var dresp msgThing.Resp
err = utils.Unmarshal(payload, &dresp)
if err != nil { //如果是没法解析的说明不是需要的包,直接跳过即可
return false
}
if dresp.MsgToken != req.MsgToken { //不是该请求的回复.跳过
return false
}
return true
})
if err != nil {
return nil, err
}
var dresp msgGateway.Msg
err = utils.Unmarshal(resp, &dresp)
if err != nil {
return nil, err
}
if dresp.Code != errors.OK.GetCode() {
return nil, errors.DeviceResp.AddMsgf("设备返回错误: msg:%v code:%v", dresp.Msg, dresp.Code)
}
return &dm.Empty{}, nil
//if dresp.Payload != nil || len(dresp.Payload.Devices) == 0 {
// return &dm.GatewayGetFoundResp{}, nil
//}
//var devs []*devices.Core
//for _, v := range dresp.Payload.Devices {
// devs = append(devs, &devices.Core{
// ProductID: v.ProductID,
// DeviceName: v.DeviceName,
// })
//}
//diDB := relationDB.NewDeviceInfoRepo(l.ctx)
////只获取已经入网的设备,未入网的设备需要网关自己注册或提前入网
//dis, err := diDB.FindByFilter(l.ctx, relationDB.DeviceFilter{Cores: devs}, nil)
//if err != nil {
// return nil, err
//}
////gdDB := relationDB.NewGatewayDeviceRepo(l.ctx)
////gdDB.FindByFilter()
//fmt.Println(dis)
//return &dm.GatewayGetFoundResp{}, nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/unitedrhino/things.git
git@gitee.com:unitedrhino/things.git
unitedrhino
things
物联网iot模块
v1.1.1

搜索帮助