hxb
2022-09-08 2a01ef5e49422cca49bc7476fc1b8be8c8556561
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
package com.hdl.sdk.link.gateway;
 
 
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.hdl.sdk.link.common.config.TopicConstant;
import com.hdl.sdk.link.common.event.EventDispatcher;
import com.hdl.sdk.link.common.event.EventListener;
import com.hdl.sdk.link.common.exception.HDLLinkCode;
import com.hdl.sdk.link.common.exception.HDLLinkException;
import com.hdl.sdk.link.common.utils.IdUtils;
import com.hdl.sdk.link.common.utils.IpUtils;
import com.hdl.sdk.link.common.utils.TextUtils;
import com.hdl.sdk.link.common.utils.ThreadToolUtils;
import com.hdl.sdk.link.common.utils.gson.GsonConvert;
 
import com.hdl.sdk.link.core.bean.LinkRequest;
import com.hdl.sdk.link.core.bean.LinkResponse;
import com.hdl.sdk.link.core.bean.gateway.GatewayBean;
import com.hdl.sdk.link.core.bean.response.BaseLocalResponse;
import com.hdl.sdk.link.core.callback.GatewayCallBack;
import com.hdl.sdk.link.core.callback.HDLLinkCallBack;
import com.hdl.sdk.link.core.connect.HDLConnectHelper;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * Created by hxb on 2021/12/23.
 */
public class HDLLinkLocalGateway {
    //instance
    private volatile static HDLLinkLocalGateway instance;
 
    //getInstance
    public static synchronized HDLLinkLocalGateway getInstance() {
        if (instance == null) {
            synchronized (HDLLinkLocalGateway.class) {
                if (instance == null) {
                    instance = new HDLLinkLocalGateway();
                }
            }
        }
        return instance;
    }
 
    /**
     * 网关列表,记录所有搜索到的网关,可能包含断线的网关
     */
    private final List<GatewayBean> gatewayBeanList = new ArrayList();
 
    /**
     * 获取缓存的网关列表,可能包含断线的网关
     *
     * @return 网关列表
     */
    public List<GatewayBean> getGatewayList() {
        return gatewayBeanList;
    }
 
    /**
     * 通过oid或者网关ID或者AMC获取内存中的网关
     *
     * @param oidOrGatewayId
     * @return
     */
    public GatewayBean getGatewayByOidOrGatewayId(String oidOrGatewayId) {
        if (TextUtils.isEmpty(oidOrGatewayId)) {
            return null;
        }
        for (GatewayBean gatewayBean : gatewayBeanList) {
            if (oidOrGatewayId.equals(gatewayBean.getOid())
                    || oidOrGatewayId.equals(gatewayBean.getGatewayId())
                    || oidOrGatewayId.equals(gatewayBean.getDevice_mac())
                    || oidOrGatewayId.equals(gatewayBean.getIp_address())
            )
                return gatewayBean;
        }
        return null;
    }
 
    /**
     * 通过IP获取网关信息
     *
     * @param ipAddress
     * @return
     */
    public GatewayBean getGatewayByIpAddress(String ipAddress) {
        if (TextUtils.isEmpty(ipAddress)) {
            return null;
        }
        for (GatewayBean gatewayBean : gatewayBeanList) {
            if (ipAddress.equals(gatewayBean.getIp_address()))
                return gatewayBean;
        }
        return null;
    }
 
    /**
     * 搜索网关,只发一次,没有回调
     */
    public void serchGatewayOneTime() {
        String time = String.valueOf(System.currentTimeMillis());
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("id", IdUtils.getUUId());
        jsonObject.addProperty("time_stamp", time);
        LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
                jsonObject.toString(), false);
 
        String ipAddress = IpUtils.getBroadcastAddress();
        new HDLConnectHelper(1, ipAddress, message, false).send();
    }
 
    /**
     * 创新所有在线的网关,包括当前住宅的及没有绑定过的网关
     *
     * @param callBack 回调
     */
    public void refreshGatewayByHome(String homeId, GatewayCallBack callBack) {
        refreshGatewayByHome(homeId,callBack,false);
    }
 
    /**
     * 创新所有在线的网关,包括当前住宅的及没有绑定过的网关
     *
     * @param callBack 回调
     */
    public void refreshGatewayByHome(String homeId, GatewayCallBack callBack,boolean needOldGateway) {
 
        String topicReply = TopicConstant.GATEWAY_SEARCH_REPLY;
        final List<GatewayBean> tempGatewayBeanList = new ArrayList<>();
        EventListener eventListener = getSearchGatewayEvent(homeId, tempGatewayBeanList);
        EventDispatcher.getInstance().register(topicReply, eventListener);
 
        ThreadToolUtils.getInstance().newFixedThreadPool(1).execute(new Runnable() {
            @Override
            public void run() {
                int count = 5;
                while (0 < count--) {
                    try {
                        //搜索网关
                        serchGatewayOneTime();
                        Thread.sleep(300L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //超出次数后移除监听事件
                EventDispatcher.getInstance().remove(topicReply, eventListener);
                if (callBack != null) {
                    if (tempGatewayBeanList.size() == 0 && needOldGateway) {
                        for (int i = 0; i < gatewayBeanList.size(); i++) {
                            if (homeId.equals(gatewayBeanList.get(i).getHomeId())) {
                                tempGatewayBeanList.add(gatewayBeanList.get(i));
                            }
                        }
                    }
                    if (tempGatewayBeanList.size() == 0) {
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_GATEWAY_FAILURE_ERROR));
                    } else {
                        callBack.onSuccess(tempGatewayBeanList);
                    }
                }
            }
        });
    }
 
    /**
     * 创新所有在线的网关,包括当前住宅的及没有绑定过的网关
     *
     * @param callBack 回调
     */
    public void refreshGateway(GatewayCallBack callBack) {
 
        String topicReply = TopicConstant.GATEWAY_SEARCH_REPLY;
        final List<GatewayBean> tempGatewayBeanList = new ArrayList<>();
        EventListener eventListener = getSearchGatewayEvent(tempGatewayBeanList);
        EventDispatcher.getInstance().register(topicReply, eventListener);
 
        ThreadToolUtils.getInstance().newFixedThreadPool(1).execute(new Runnable() {
            @Override
            public void run() {
                int count = 5;
                while (0 < count--) {
                    try {
                        //搜索网关
                        serchGatewayOneTime();
                        Thread.sleep(300L);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
 
 
                //超出次数后移除监听事件
                EventDispatcher.getInstance().remove(topicReply, eventListener);
                if (callBack != null) {
                    if (tempGatewayBeanList.size() == 0) {
                        callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_GATEWAY_FAILURE_ERROR));
                    } else {
 
                        callBack.onSuccess(tempGatewayBeanList);
                    }
                }
            }
        });
    }
 
    /**
     * 获取搜索网关事件
     *
     * @param homeId
     * @return
     */
    private EventListener getSearchGatewayEvent(String homeId, final List<GatewayBean> tempGatewayBeanList) {
        //注册搜索网关监听
        return new EventListener() {
            @Override
            public void onMessage(Object msg) {
                if (!(msg instanceof LinkResponse)) {
                    return;
                }
                GatewayBean gateway = getGatewayBeanByResponse((LinkResponse) msg);
                if (gateway == null) {
                    return;
                }
                gateway.setOnline(true);
                gateway.setIsLocalGateWay(true);//本地搜索到的网关标识为本地网关
                //只加载住宅一样的或者网关还没有配置过的
                if (homeId.equals(gateway.getHomeId()) || TextUtils.isEmpty(gateway.getHomeId())) {
                    //更新缓存网关,会记录所有收到的网关,以为了保存网关的IP信息
                    updateGatewayList(gatewayBeanList, gateway);
                    //更新当前读取网关的列表,这个列表每次都是清空再读取
                    updateGatewayList(tempGatewayBeanList, gateway);
                }
            }
        };
    }
 
    /**
     * 获取搜索网关事件 这边不过滤homeId
     *
     * @return
     */
    private EventListener getSearchGatewayEvent(final List<GatewayBean> tempGatewayBeanList) {
        //注册搜索网关监听
        return new EventListener() {
            @Override
            public void onMessage(Object msg) {
                if (!(msg instanceof LinkResponse)) {
                    return;
                }
                GatewayBean gateway = getGatewayBeanByResponse((LinkResponse) msg);
                if (gateway == null) {
                    return;
                }
                gateway.setIsLocalGateWay(true);//本地搜索到的网关标识为本地网关
                //升级网关驱动  需要显示所有网关
                //⚠️这边不需要添加到gatewayBeanList   因为这个是网关离线升级的时候的功能
//                updateGatewayList(gatewayBeanList, gateway);
                //更新当前读取网关的列表,这个列表每次都是清空再读取
                updateGatewayList(tempGatewayBeanList, gateway);
            }
        };
    }
 
    /**
     * 更新收到的网关到列表
     *
     * @param gatewayBeanList
     * @param gateway         当前收到的网关
     */
    void updateGatewayList(final List<GatewayBean> gatewayBeanList, GatewayBean gateway) {
        boolean isFound = false;//是否在内存中找到网关对象
        //找出mac一样的网关更新最新数据
        for (int i = 0; i < gatewayBeanList.size(); i++) {
            if (gatewayBeanList.get(i).getOid().equals(""))
                continue;
            if (gatewayBeanList.get(i).getOid().equals(gateway.getOid())) {
                isFound = true;
                gatewayBeanList.set(i, gateway);
//                break;
            }
        }
 
        //清除oid一样,mac不一样的网关缓存。一般是在网关替换的情况空间出现
        for (int i = 0; i < gatewayBeanList.size(); i++) {
            if (gatewayBeanList.get(i).getOid().equals("")|| gatewayBeanList.get(i).getDevice_mac().equals(""))
                continue;
            if (gatewayBeanList.get(i).getOid().equals(gateway.getOid()) && !gatewayBeanList.get(i).getDevice_mac().equals(gateway.getDevice_mac())
                    || gatewayBeanList.get(i).getDevice_mac().equals(gateway.getDevice_mac()) && !gatewayBeanList.get(i).getOid().equals(gateway.getOid())) {
                gatewayBeanList.remove(i--);
            }
        }
 
        //没有找到就添加
        if (!isFound) {
            gatewayBeanList.add(gateway);
        }
    }
 
    /**
     * 获取网关对象
     *
     * @param linkResponse
     * @return
     */
    private GatewayBean getGatewayBeanByResponse(LinkResponse linkResponse) {
        String data = linkResponse.getData();
        if (!TextUtils.isEmpty(data)) {
            final BaseLocalResponse<GatewayBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewayBean>>() {
            }.getType());
            return response.getObjects();
        }
        return null;
    }
 
    /*
     * 绑定网关
     * */
    public void bindGateway(String name, String mac, String homeId, String regionUrl, String ip, boolean isEncrypt, HDLLinkCallBack callBack) {
        String topic = String.format(TopicConstant.GATEWAY_EDIT_REMOTE, mac);
 
        JsonObject jObject = new JsonObject();
        jObject.addProperty("homeId", homeId);
        jObject.addProperty("server_addr", regionUrl);
//         jObject.addProperty("device_name", name);
        jObject.addProperty("remote", "true");
 
        JsonObject sendJsonObj = new JsonObject();
        sendJsonObj.add("objects", jObject);
        String time = String.valueOf(System.currentTimeMillis());
        sendJsonObj.addProperty("time_stamp", time);
        sendJsonObj.addProperty("id", IdUtils.getUUId());
        String sendStr = sendJsonObj.toString();
 
        LinkRequest message = new LinkRequest(topic, sendStr, isEncrypt);
        new HDLConnectHelper(ip, message, topic + "_reply", new HDLConnectHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                callBack.onSuccess("1");
            }
 
            @Override
            public void onFailure() {
                if (callBack == null) return;
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
            }
        }, true).send();
    }
 
    /*
     * 初始化网关
     * */
    public void initializeGateway(String mac, String oid, String ip, boolean isEncrypt, HDLLinkCallBack callBack) {
        String topic = String.format(TopicConstant.GATEWAY_INITIALIZE_REMOTE, oid);
 
        JsonObject sendJsonObj = new JsonObject();
        String time = String.valueOf(System.currentTimeMillis());
        sendJsonObj.addProperty("time_stamp", time);
        sendJsonObj.addProperty("id", IdUtils.getUUId());
        JsonObject jObject = new JsonObject();
        jObject.addProperty("device_mac", mac);
        sendJsonObj.add("objects", jObject);
        String sendStr = sendJsonObj.toString();
 
        LinkRequest message = new LinkRequest(topic, sendStr, isEncrypt);
        new HDLConnectHelper(ip, message, topic + "_reply", new HDLConnectHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                callBack.onSuccess("1");
            }
 
            @Override
            public void onFailure() {
                if (callBack == null) return;
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
            }
        }, true).send();
    }
 
    /*
     * 设置主从网关标记
     * */
    public void setMasterGateway(String ip, String mac, String master, boolean isEncrypt, HDLLinkCallBack callBack) {
        String topic = String.format(TopicConstant.GATEWAY_EDIT_LOCAL, mac);
 
        JsonObject jObject = new JsonObject();
        jObject.addProperty("master", master);
 
        JsonObject sendJsonObj = new JsonObject();
        sendJsonObj.add("objects", jObject);
        String time = String.valueOf(System.currentTimeMillis());
        sendJsonObj.addProperty("time_stamp", time);
        sendJsonObj.addProperty("id", IdUtils.getUUId());
        String sendStr = sendJsonObj.toString();
 
        LinkRequest message = new LinkRequest(topic, sendStr, isEncrypt);
        new HDLConnectHelper(ip, message, topic + "_reply", new HDLConnectHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                callBack.onSuccess("1");
            }
 
            @Override
            public void onFailure() {
                if (callBack == null) return;
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
            }
        }, true).send();
    }
 
    /**
     * 设置网关入网从机模式
     */
    public void AuthGateway(String ip, String mac) {
        String topic = String.format(TopicConstant.GATEWAY_AUTH, mac);
 
        JsonObject sendJsonObj = new JsonObject();
        String time = String.valueOf(System.currentTimeMillis());
        sendJsonObj.addProperty("time_stamp", time);
        sendJsonObj.addProperty("id", IdUtils.getUUId());
        JsonObject jObject = new JsonObject();
        jObject.addProperty("spk", "LINKDEVICE");
        jObject.addProperty("time", "120");
        sendJsonObj.add("objects", jObject);
 
 
        String sendStr = sendJsonObj.toString();
        LinkRequest message = new LinkRequest(topic, sendStr, false);
        new HDLConnectHelper(ip, message, topic + "_reply", new HDLConnectHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object msg) {
            }
 
            @Override
            public void onFailure() {
            }
        }, true).send();
    }
 
    /*
     * 获取网关信息
     * */
    public void getGatewayInfo(String ip, String mac, boolean isEncrypt, HDLLinkCallBack callBack) {
        String topic = String.format(TopicConstant.GATEWAY_GET, mac);
 
        JsonObject sendJsonObj = new JsonObject();
        String time = String.valueOf(System.currentTimeMillis());
        sendJsonObj.addProperty("time_stamp", time);
        sendJsonObj.addProperty("id", IdUtils.getUUId());
        String sendStr = sendJsonObj.toString();
 
        LinkRequest message = new LinkRequest(topic, sendStr, isEncrypt);
        new HDLConnectHelper(ip, message, topic + "_reply", new HDLConnectHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                callBack.onSuccess("1");
            }
 
            @Override
            public void onFailure() {
                if (callBack == null) return;
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
            }
        }, true).send();
    }
 
 
 
    /**
     * 发送数据到Link网关
     * @param ip        网关IP
     * @param mac       网关mac
     * @param isEncrypt 是否加密
     * @param topic     请求主题
     * @param jObject   负载数据<没有填null></>
     * @param sendPath  发送路径<类名+方法名>class->methodName</>
     */
    public void sendDataToLinkGateway(String ip, String mac, boolean isEncrypt,
                                      String topic, Object jObject,String sendPath, HDLLinkCallBack callBack) {
        String topicSend = topic.replace("%s", mac);
        //组装需要发送的数据
        String sendStr = createSendData(jObject);
        System.out.print("sendDataToLinkGateway->"+sendPath+"->本地发送\r\n"+topicSend+"\r\n"+sendStr);
        LinkRequest message = new LinkRequest(topicSend, sendStr, isEncrypt);
        new HDLConnectHelper(ip, message, topicSend + "_reply", new HDLConnectHelper.HdlSocketListener() {
            @Override
            public void onSucceed(Object msg) {
                if (callBack == null) return;
                callBack.onSuccess(msg.toString());
                System.out.print("sendDataToLinkGateway->"+sendPath+"->本地接收数据\r\n"+msg.toString());
            }
 
            @Override
            public void onFailure() {
                if (callBack == null) return;
                callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
                System.out.print("sendDataToLinkGateway->"+sendPath+"->本地接收数据->失败(-200)");
            }
        }, true).send();
    }
 
    /**
     * 组装需要发送的数据
     *
     * @param jObject
     * @return 负载数据<没有填null>
     */
    public String createSendData(Object jObject) {
        //topic = String.format(TopicConstant.GATEWAY_EDIT_LOCAL, mac);
        JsonObject sendJsonObj = new JsonObject();
        if (jObject != null) {
            if (jObject instanceof JsonObject) {
                sendJsonObj.add("objects", (JsonObject) jObject);
            } else if (jObject instanceof JsonArray) {
                sendJsonObj.add("objects", (JsonArray) jObject);
            }
        }
        String time = String.valueOf(System.currentTimeMillis());
        sendJsonObj.addProperty("time_stamp", time);
        sendJsonObj.addProperty("id", IdUtils.getUUId());
        String sendStr = sendJsonObj.toString();
        return sendStr;
    }
}