panlili2024
2025-03-19 7c8ce9b9a7d3fc1aaa4a621e86415b25ad10a34f
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package com.hdl.sdk.common.utils;
 
import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
 
import com.google.gson.reflect.TypeToken;
import com.hdl.sdk.common.config.TopicConstant;
import com.hdl.sdk.common.exception.HDLLinkException;
import com.hdl.sdk.common.utils.gson.GsonConvert;
import com.hdl.sdk.connect.HDLLink;
import com.hdl.sdk.connect.bean.LinkResponse;
import com.hdl.sdk.connect.bean.request.GatewayRemoteEditRequest;
import com.hdl.sdk.connect.bean.response.DeviceDeleteResponse;
import com.hdl.sdk.connect.bean.response.DeviceInfoResponse;
import com.hdl.sdk.connect.callback.HDLLinkCallBack;
import com.hdl.sdk.connect.config.HDLLinkConfig;
import com.hdl.sdk.connect.socket.HDLAuthSocket;
import com.hdl.sdk.connect.socket.HDLSocket;
import com.hdl.sdk.connect.utils.AesUtil;
 
/**
 * Created by hxb on 2023/9/14.
 */
public class AllTopicManagerUtils {
 
    /**
     * 全局主题处理方法,可以处理所有接收到的数据
     *
     * @param linkResponse
     */
    public static void manager(LinkResponse linkResponse) {
        try {
            if (linkResponse == null) {
                return;
            }
            String topic = linkResponse.getTopic();
            if (TextUtils.isEmpty(topic)) {
                return;
            }
            String[] topics = topic.split("/");
            //非当前住宅网关的数据返回
            if (topics.length < 3) {
                return;
            }
 
            String mac_Oid_GatewayId = topics[2];
            Log.d("panlili", "AllTopicManagerUtils.java:manager----->topic= "  + topic);
 
            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());
            } else if (isSameTopic(TopicConstant.GATEWAY_SEARCH, topic)) {
                HDLAuthSocket.getInstance().UploadGatewayInfo(null);
            } else if (isSameTopic(TopicConstant.LINK_BROADCAST, topic)) {
                managerLinkBroadcast(linkResponse.getData());
            } else if (isSameTopic(TopicConstant.DELETE_NOTIFY, topic)) {
                managerDeleteNofity(linkResponse.getData());
            }
 
        } 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 = HDLLinkConfig.getInstance().getDeviceInfoBean().getOID();
        String gatewayId = HDLLinkConfig.getInstance().getGatewayId();
        String all = "all";
 
        return mac_Oid_GatewayId.equals(mac) || mac_Oid_GatewayId.equals(oid) || mac_Oid_GatewayId.equals(gatewayId) || mac_Oid_GatewayId.toLowerCase().equals(all);
    }
 
    /**
     * 是否相同主题
     *
     * @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);
    }
 
    /**
     * 更新密钥
     *
     * @param body
     */
    private static void managerLinkBroadcast(String body) {
        try {
            DeviceInfoResponse deviceInfoResponse = GsonConvert.getGson().fromJson(body, new TypeToken<DeviceInfoResponse>() {
            }.getType());
 
            if (deviceInfoResponse == null || deviceInfoResponse.getObjects() == null) {
                return;
            }
            if (!TextUtils.isEmpty(deviceInfoResponse.getObjects().getLocalSecret())) {
                byte[] baseBytes = Base64.decode(deviceInfoResponse.getObjects().getLocalSecret(), Base64.NO_WRAP);
                String mackey = "";
                if (!TextUtils.isEmpty(SPUtils.getString("auth_mackey_key", ""))) {
                    mackey = SPUtils.getString("auth_mackey_key", "");
                    byte[] bodyBytes = AesUtil.aesDecrypt(baseBytes, mackey.substring(mackey.length() - 16));
                    String localSecret = new String(bodyBytes, "utf-8");
                    Log.d("panlili", "更新密钥----->localSecret= " + localSecret);
                    HDLLinkConfig.getInstance().setLocalSecret(localSecret);
                }
            }
        } catch (Exception e) {
            LogUtils.i("LinkMessageDecoder.java:getLocalSecret----->e= " + e.getMessage());
        }
    }
 
    /**
     * 退网
     *
     * @param body
     */
    private static void managerDeleteNofity(String body) {
        try {
            DeviceDeleteResponse deviceDeleteResponse = GsonConvert.getGson().fromJson(body, new TypeToken<DeviceDeleteResponse>() {
            }.getType());
 
            if (deviceDeleteResponse == null || deviceDeleteResponse.getObjects() == null) {
                return;
            }
            HDLSocket.getInstance().deleteNetwork(deviceDeleteResponse.getObjects().get(0).getOID(), new HDLLinkCallBack() {
                @Override
                public void onSuccess(String msg) {
 
                }
 
                @Override
                public void onError(HDLLinkException e) {
 
                }
            });
        } catch (Exception e) {
            LogUtils.i("LinkMessageDecoder.java:deleteNetwork----->e= " + e.getMessage());
        }
    }
}