hxb
2023-09-14 a8c3ca4f1d9433e914325222ae5f26da2f9e2489
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.hdl.sdk.common.utils;
 
import android.text.TextUtils;
 
import com.google.gson.reflect.TypeToken;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.bean.request.GatewayRemoteEditRequest;
import com.hdl.sdk.connect.bean.response.DeviceInfoResponse;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.connect.socket.HDLSocket;
 
import java.time.chrono.IsoChronology;
 
/**
 * Created by hxb on 2023/9/14.
 */
public  class AllTopicManagerUtils {
 
    /**
     * 全局主题处理方法,可以处理所有接收到的数据
     *
     * @param linkResponse
     */
    public static void manager(LinkResponse linkResponse) {
        try {
            String topic = linkResponse.getTopic();
            if (TextUtils.isEmpty(topic)) {
                return;
            }
            String[] topics = topic.split("/");
            //非当前住宅网关的数据返回
            if (topics.length < 3) {
                return;
            }
 
            String mac_Oid_GatewayId = topics[2];
 
            if(!isLocalDevice(mac_Oid_GatewayId)){
                LogUtils.i("不是当前设备的网关Id,Id是"+mac_Oid_GatewayId);
                //非当前设备的数据
                return;
            }
 
            if(isSameTopic(TopicConstant.GATEWAY_EDIT_REMOTE,topic)){
                gatewayRemoteEditRequest(mac_Oid_GatewayId, linkResponse.getData());
            }
 
            //TODO 通过增加if else 增加其它主题类似这样使用
 
        } catch (Exception e) {
            LogUtils.e("全局处理模块异常:" + e.getMessage());
        }
    }
 
    /**
     * 是否当前的设备
     * @param mac_Oid_GatewayId
     * @return
     */
    private static boolean isLocalDevice(String mac_Oid_GatewayId) {
        if (TextUtils.isEmpty(mac_Oid_GatewayId)) {
            return false;
        }
        String mac = HDLLinkConfig.getInstance().getDeviceInfoBean().getDeviceMAC();
        String oid = "123";//HDLLinkConfig.getInstance().getDeviceInfoBean().getOID();
        String gatewayId = HDLLinkConfig.getInstance().getGatewayId();
 
        return mac_Oid_GatewayId.equals(mac) || mac_Oid_GatewayId.equals(oid) || mac_Oid_GatewayId.equals(gatewayId);
    }
 
    /**
     * 是否相同主题
     * @param targetTopic 目标的主题
     * @param sourceTopic 接收的主题
     * @return
     */
    private static boolean isSameTopic(String targetTopic,String sourceTopic) {
        if (TextUtils.isEmpty(targetTopic) || TextUtils.isEmpty(sourceTopic)) {
            return false;
        }
        String[] targetTopics = targetTopic.split("/");
        String[] sourceTopics = sourceTopic.split("/");
 
        if (targetTopics.length != sourceTopics.length) {
            return false;
        }
        for (int i = 0; i < targetTopics.length; i++) {
            if(i==2){
                //这个位置不匹配,是网关id
                continue;
            }
            if (!targetTopics[i] .equals( sourceTopics[i])) {
                return false;
            }
        }
        return true;
    }
 
    /**
     * 网关信息配置
     * @param body
     */
    private static void gatewayRemoteEditRequest(String mac_Oid_GatewayId,String body) {
        if (TextUtils.isEmpty(body)) {
            return;
        }
        GatewayRemoteEditRequest gatewayRemoteEditRequest = GsonConvert.getGson().fromJson(body, new TypeToken<GatewayRemoteEditRequest>() {
        }.getType());
 
        if (gatewayRemoteEditRequest == null || gatewayRemoteEditRequest.getObjects() == null) {
            return;
        }
 
        String homeId = gatewayRemoteEditRequest.getObjects().getHomeId();
        HDLLinkConfig.getInstance().setHomeId(homeId);
 
        HDLSocket.getInstance().gatewayRemoteEditReply(mac_Oid_GatewayId, gatewayRemoteEditRequest.getId(), null);
    }
}