package service
|
|
import (
|
"encoding/json"
|
"vamicro/saas-service/service/nodeService"
|
|
"basic.com/valib/bhomeclient.git"
|
"basic.com/valib/go-aiot.git/aiotProto/aiot"
|
"basic.com/valib/go-aiot.git/client"
|
"basic.com/valib/logger.git"
|
)
|
|
// 同步设备信息
|
func syncDeviceInfo(cli *client.Client) {
|
// 获取设备详情信息
|
deviceList := nodeService.Node.NodeLists
|
logger.Debugf("syncDeviceInfo len(deviceList)=%v", len(deviceList))
|
if deviceList == nil {
|
return
|
}
|
for _, node := range deviceList {
|
// 获取设备信息
|
topic := "/data/api-v/sysset/getDeviceInfo"
|
param := make(map[string]interface{})
|
data, err := DoBusReq(topic, node.Id, aiot.RequestMethod_Post, aiot.RequestContentType_ApplicationJson, param)
|
if err != nil {
|
logger.Errorf("fail to do bus request topic=%v, err=%v", topic, err)
|
// 查询错误
|
continue
|
}
|
dataResp := bhomeclient.Reply{}
|
err = json.Unmarshal(data, &dataResp)
|
if err != nil {
|
logger.Errorf("fail to Unmarshal topic=%v, err=%v", topic, err)
|
return
|
}
|
if dataResp.Data == nil {
|
logger.Errorf("fail to do bus request topic=%v, data=%v", topic, dataResp)
|
return
|
}
|
if !dataResp.Success {
|
logger.Errorf("getDeviceInfo dataResp.Success is false, data=%v", string(data))
|
return
|
}
|
dataRes := dataResp.Data.(map[string]interface{})
|
if dataResp.Success != true {
|
logger.Errorf("topic error topic=%v, dataResp=%v", topic, dataResp)
|
return
|
}
|
// 整理数据
|
deviceDataReport := make(map[string]interface{})
|
deviceDataReport["detail"] = dataRes["detail"]
|
|
// 解码sdk信息
|
type sdkStruct struct {
|
Id string `json:"id"`
|
UpdateTime string `json:"update_time"`
|
Version string `json:"version"`
|
}
|
sdkJson, _ := json.Marshal(dataRes["sdks"])
|
sdkList := make([]sdkStruct, 0)
|
err = json.Unmarshal(sdkJson, &sdkList)
|
deviceDataReport["sdks"] = sdkList
|
|
// 整理APP信息
|
type appStruct struct {
|
Id string `json:"id"`
|
Version string `json:"version"`
|
}
|
appJson, _ := json.Marshal(dataRes["apps"])
|
appList := make([]appStruct, 0)
|
err = json.Unmarshal(appJson, &appList)
|
deviceDataReport["apps"] = appList
|
|
deviceData, _ := json.Marshal(deviceDataReport)
|
report := aiot.DataReport{
|
DataKey: DeviceSdkAppDataReport,
|
Data: deviceData,
|
}
|
logger.Debugf("syncDeviceInfo data=%v, dataKey=%v", string(deviceData), report.DataKey)
|
reportData, _ := json.Marshal(report)
|
syncData := &aiot.Protocol{
|
Receiver: aiot.RECEIVER_TO_SAAS,
|
SenderId: node.Id,
|
MsgType: aiot.MSG_TYPE_DATA_REPORT,
|
ReqType: aiot.REQ_TYPE_REQUEST,
|
MsgProto: client.GetMsgProto(""),
|
Data: reportData,
|
}
|
|
//logger.Warn("sync device", syncData)
|
// 上报集群数据
|
_ = cli.WriteBody(syncData)
|
}
|
}
|