qixiaoning
2025-07-08 fe724b50b3f1b3dfe2219eb9af4bcca96c89a158
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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)
    }
}