From d3d67236f302677c87dc6c5ebde560d73c147578 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期一, 13 十二月 2021 16:36:01 +0800
Subject: [PATCH] 2021-12-13 1.更新aar
---
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java | 144 ++++++++++++++++++++++++++++++++++++-----------
1 files changed, 109 insertions(+), 35 deletions(-)
diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
index ed94f66..010263f 100644
--- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
+++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -71,8 +71,8 @@
private volatile static HDLAuthSocket instance;
private HDLAuthSocket() {
- initSearchGatewayEvent();
initListenerGatewayEvent();
+ initSearchGatewayEvent();
}
// public interface CallBack extends BaseCallBack {
@@ -89,8 +89,6 @@
synchronized (HDLLinkConfig.class) {
if (instance == null) {
instance = new HDLAuthSocket();
- //1.鍚姩Socket 寮�鍚洃鍚�
- instance.getUdpBoot();
}
}
}
@@ -109,6 +107,7 @@
/**
* 鑾峰彇褰撳墠udp瀵硅薄锛屽鏋滀笉瀛樺湪灏卞垱寤�
+ *
* @return 杩斿洖褰撳墠瀵硅薄
*/
private UdpSocketBoot getUdpBoot() {
@@ -118,7 +117,7 @@
udpSocketBoot.bind();
}
} catch (Exception e) {
- return null;
+// return null;
}
return udpSocketBoot;
@@ -132,6 +131,8 @@
*/
public void startAuthenticateRequest(AuthenticateRequest request, HDLLinkCallBack callBack) {
HDLLinkConfig.getInstance().clearConfig();
+ //1.鍚姩Socket 寮�鍚洃鍚�
+ getUdpBoot();
//2.鏋勫缓鐩戝惉Listener
// authEvent =
//3.鐩戝惉缃戝叧骞挎挱鐨勫叆缃戞寚浠�
@@ -177,30 +178,39 @@
String requestStr = gs.toJson(request);
LinkRequest message = new LinkRequest(topic, requestStr);
- HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(ip,UDP_PORT), message, new HdlSocketHelper.HdlSocketListener() {
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
@Override
public void onSucceed(Object msg) {
if (callBack == null) return;
try {
AuthenticateResponse bean = getAuthenticateResponseBean(msg);
if (bean != null) {
- String localSecret = "";
- String gatewayId = "";
- String ipAddress = "";
- if(bean.getAuth() != null) {
- localSecret = bean.getAuth().getLocalSecret();
- }
- if(bean.getObjects() != null){
- gatewayId = bean.getObjects().getGatewayID();
- ipAddress = bean.getObjects().getIPAddress();
- }
- //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔�
- if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
- //璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔�
- callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
- } else {
- HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
- callBack.onSuccess("璁よ瘉鎴愬姛");
+ if (bean.getCode().equals("200")) {
+ String localSecret = "";
+ String gatewayId = "";
+ String ipAddress = "";
+ if (bean.getAuth() != null) {
+ localSecret = bean.getAuth().getLocalSecret();
+ }
+ if (bean.getObjects() != null) {
+ gatewayId = bean.getObjects().getGatewayID();
+ ipAddress = bean.getObjects().getIPAddress();
+ }
+ //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔�
+ if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) {
+ //璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔�
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED));
+ } else {
+ HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress);
+ callBack.onSuccess("璁よ瘉鎴愬姛");
+ }
+ } else if (bean.getCode().equals("14013")){
+ //璁よ瘉澶辫触锛岃MAC瀵瑰簲鐨勮澶囧瘑閽ヤ笉瀛樺湪
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_MAC_KEY_ERROR_));
+ } else{
+ //璁よ瘉澶辫触锛岄敊璇爜锛�
+ LogUtils.e("璁よ瘉澶辫触锛岄敊璇爜锛�" + bean.getCode());
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR));
}
} else {
callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
@@ -293,7 +303,7 @@
* @param callBack 鍥炶皟
*/
public void searchGatewayMulticast(SearchGatewayCallBack callBack) {
- searchGateway(HDLLinkConfig.getInstance().getGatewayId(), InetSocketAddress.createUnresolved(UDP_GROUP_IP,UDP_PORT), callBack);
+ searchGateway(HDLLinkConfig.getInstance().getGatewayId(), UDP_GROUP_IP, UDP_PORT, callBack);
}
/**
@@ -302,8 +312,14 @@
* @param callBack 鍥炶皟
*/
public void searchGatewayBroadcast(SearchGatewayCallBack callBack) {
+ if (!HDLLinkConfig.getInstance().checkIfCertified()) {
+ if (callBack != null) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
+ }
+ return;
+ }
String ip = IpUtils.getBroadcastAddress();
- searchGateway(HDLLinkConfig.getInstance().getGatewayId(), InetSocketAddress.createUnresolved(ip,UDP_PORT), callBack);
+ searchGateway(HDLLinkConfig.getInstance().getGatewayId(), ip, UDP_PORT, callBack);
}
@@ -321,7 +337,7 @@
GsonConvert.getGson().toJson(data));
String ip = IpUtils.getBroadcastAddress();
- HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(ip,UDP_PORT), message, new HdlSocketHelper.HdlSocketListener() {
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
@Override
public void onSucceed(Object msg) {
if (callBack == null) return;
@@ -352,9 +368,17 @@
}
return;
}
+
+ if (!HDLLinkConfig.getInstance().checkIfCertified()) {
+ if (callBack != null) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
+ }
+ return;
+ }
+
LinkRequest message = new LinkRequest(topic, bodyStr);
- String ip = HDLLinkConfig.getInstance().getIpAddress(); IpUtils.getBroadcastAddress();
- HdlSocketHelper.send(getUdpBoot(), InetSocketAddress.createUnresolved(ip,UDP_PORT), message, new HdlSocketHelper.HdlSocketListener() {
+ String ip = HDLLinkConfig.getInstance().getIpAddress();
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
@Override
public void onSucceed(Object msg) {
if (callBack == null) return;
@@ -381,9 +405,13 @@
LogUtils.e("udpSendMsg", "鍙傛暟涓嶈兘涓虹┖");
return;
}
+ if (!HDLLinkConfig.getInstance().checkIfCertified()) {
+ LogUtils.e("udpSendMsg", "鏈璇侊紝璇峰厛璁よ瘉");
+ return;
+ }
LinkRequest message = new LinkRequest(topic, bodyStr);
String ip = HDLLinkConfig.getInstance().getIpAddress();// IpUtils.getBroadcastAddress();
- getUdpBoot().sendMsg( InetSocketAddress.createUnresolved(ip,UDP_PORT),message.getSendBytes());
+ HdlSocketHelper.sendUdpOne(getUdpBoot(), ip, UDP_PORT, message);
}
private GatewaySearchBean getGatewaySearchBean(Object msg) {
@@ -476,8 +504,8 @@
}
private void initListenerGatewayEvent() {
- LogUtils.i( "鍒濆鍖栨案涔呯洃鍚綉鍏冲箍鎾簨浠讹紝涓嶇敤绉婚櫎姝や簨浠�");
- EventListener gatewayEvent = new EventListener() {
+ LogUtils.i("鍒濆鍖栨案涔呯洃鍚綉鍏冲箍鎾簨浠讹紝涓嶇敤绉婚櫎姝や簨浠�");
+ EventListener gatewayEvent = new EventListener() {
@Override
public void onMessage(Object msg) {
try {
@@ -513,10 +541,11 @@
* 鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎锛屾悳绱㈠埌鍒欒繑鍥炴寚瀹氱殑缃戝叧瀵硅薄
*
* @param gatewayId 缃戝叧id
- * @param desInetSocketAddress 鐩爣鐨勫湴鍧�
+ * @param ipAddress 鐩爣鐨処P鍦板潃
+ * @param port 鐩爣鐨勭鍙�
* @param callBack 鍥炶皟
*/
- public void searchGateway(String gatewayId, InetSocketAddress desInetSocketAddress, SearchGatewayCallBack callBack) {
+ public void searchGateway(String gatewayId, String ipAddress, int port, SearchGatewayCallBack callBack) {
this.searchGatewayId = gatewayId;
this.mSearchGatewayCallBack = callBack;
//閲嶇疆鍙傛暟
@@ -538,7 +567,7 @@
//鎼滅储缃戝叧
searchGatewayCount.set(searchGatewayCount.get() + 1);
LogUtils.i("鎼滅储缃戝叧", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
- getUdpBoot().sendMsg(desInetSocketAddress,message.getSendBytes());
+ getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
@@ -548,12 +577,12 @@
if (!isSearchGatewaySuccess.get()) {
//鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
callBackSearchGatewayTimeout();
- LogUtils.e("鎼滅储缃戝叧--", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
+ LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
}
}
}).start();
-
}
+
/**
* 娉ㄥ唽鎼滅储缃戝叧鐩戝惉
@@ -586,4 +615,49 @@
});
}
+
+ /**
+ * 鎼滅储鍦ㄧ嚎鎵�鏈夌殑缃戝叧锛屽寘鎷綋鍓嶄綇瀹呯殑鍙婃病鏈夌粦瀹氳繃鍏跺畠浣忓畢鐨勭綉鍏�
+ *
+ * @param ipAddress 鐩爣鐨処P鍦板潃
+ * @param port 鐩爣鐨勭鍙�
+ * @param callBack 鍥炶皟
+ */
+ public void searchGateway(String ipAddress, int port, SearchGatewayCallBack callBack) {
+// this.searchGatewayId = gatewayId;
+// this.mSearchGatewayCallBack = callBack;
+// //閲嶇疆鍙傛暟
+// searchGatewayCount.set(0);
+// isSearchGatewaySuccess.set(false);
+ 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());
+ //娉ㄥ唽鎼滅储缃戝叧鐩戝惉
+ registerSearchGatewayEvent();
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
+ try {
+ //鎼滅储缃戝叧
+ searchGatewayCount.set(searchGatewayCount.get() + 1);
+ LogUtils.i("鎼滅储缃戝叧", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
+ getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
+ Thread.sleep(1000L);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ if (!isSearchGatewaySuccess.get()) {
+ //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
+ callBackSearchGatewayTimeout();
+ LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
+ }
+ }
+ }).start();
+ }
}
--
Gitblit v1.8.0