From 0188dee359636723190f0f67a6b674b7b08f7bef Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期一, 21 三月 2022 09:55:01 +0800 Subject: [PATCH] 增加判断json数据格式 --- HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java | 99 ++++++++++++++++++++++++++++++------------------- 1 files changed, 61 insertions(+), 38 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 c124267..91b96ae 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 @@ -117,7 +117,7 @@ udpSocketBoot.bind(); } } catch (Exception e) { - return null; +// return null; } return udpSocketBoot; @@ -141,15 +141,15 @@ public void onMessage(Object msg) { NetworkAccessBroadcastResponse bean = getNetworkAccessBroadcastResponse(msg); if (bean != null) { - LogUtils.i(TAG, "缃戝叧鍏ョ綉骞挎挱IP: " + bean.getIPAddress()); + //绉婚櫎鐩戝惉 + EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST); + LogUtils.i("缃戝叧鍏ョ綉骞挎挱IP: " + bean.getIPAddress()); String ipStr = bean.getIPAddress(); if (!TextUtils.isEmpty(ipStr)) { sendAuthenticateRequest(ipStr, request, callBack); } } - //绉婚櫎鐩戝惉 - EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST); - LogUtils.i(TAG, "绉婚櫎鐩戝惉 authEvent"); + } }); } @@ -178,30 +178,39 @@ String requestStr = gs.toJson(request); LinkRequest message = new LinkRequest(topic, requestStr); - HdlSocketHelper.send(getUdpBoot(), 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)); @@ -219,6 +228,7 @@ } }); } + // /** // * 鍙戦�佸叆缃戝強璁よ瘉璇锋眰 @@ -328,7 +338,7 @@ GsonConvert.getGson().toJson(data)); String ip = IpUtils.getBroadcastAddress(); - HdlSocketHelper.send(getUdpBoot(), 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; @@ -350,9 +360,10 @@ * * @param topic 鍙戦�佹暟鎹� * @param bodyStr 鍥炲鐨勪富棰� + * @param broadcast 鏄惁瑕佸箍鎾� * @param callBack 鍥炶皟 */ - public void udpSendMsg(String topic, String bodyStr, HDLLinkResponseCallBack callBack) { + public void udpSendMsg(String topic, String bodyStr,boolean broadcast, HDLLinkResponseCallBack callBack) { if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) { if (callBack != null) { callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR)); @@ -369,7 +380,10 @@ LinkRequest message = new LinkRequest(topic, bodyStr); String ip = HDLLinkConfig.getInstance().getIpAddress(); - HdlSocketHelper.send(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { + if(broadcast) { + ip = IpUtils.getBroadcastAddress(); + } + HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { @Override public void onSucceed(Object msg) { if (callBack == null) return; @@ -390,19 +404,23 @@ * * @param topic 鍙戦�佹暟鎹� * @param bodyStr 鍥炲鐨勪富棰� + * @param broadcast 鏄惁骞挎挱 */ - public void udpSendMsg(String topic, String bodyStr) { + public void udpSendMsg(String topic, String bodyStr,boolean broadcast) { if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) { - LogUtils.e("udpSendMsg", "鍙傛暟涓嶈兘涓虹┖"); + LogUtils.e( "鍙傛暟涓嶈兘涓虹┖锛屼笉鑳藉彂閫乁DP鏁版嵁"); return; } if (!HDLLinkConfig.getInstance().checkIfCertified()) { - LogUtils.e("udpSendMsg", "鏈璇侊紝璇峰厛璁よ瘉"); + LogUtils.e("鏈璇侊紝璇峰厛璁よ瘉鍐嶈皟鐢║DP鍙戦�佹柟娉�"); return; } LinkRequest message = new LinkRequest(topic, bodyStr); - String ip = HDLLinkConfig.getInstance().getIpAddress();// IpUtils.getBroadcastAddress(); - getUdpBoot().sendMsg(ip, UDP_PORT, message.getSendBytes()); + String ip = HDLLinkConfig.getInstance().getIpAddress(); + if(broadcast) { + ip = IpUtils.getBroadcastAddress(); + } + HdlSocketHelper.sendUdpOne(getUdpBoot(), ip, UDP_PORT, message); } private GatewaySearchBean getGatewaySearchBean(Object msg) { @@ -474,12 +492,12 @@ }.getType()); GatewaySearchBean searchBean = response.getObjects(); if (searchBean != null && !TextUtils.isEmpty(searchBean.getGatewayId())) { - if (searchBean.getGatewayId().contains(searchGatewayId)) { + searchBean.setIp_address(linkResponse.getSource_ipAddress()); + if (searchBean.getGatewayId().contains(searchGatewayId)&&!TextUtils.isEmpty(searchGatewayId)) { removeSearchGatewayEvent();//绉婚櫎鎼滅储缃戝叧鐩戝惉 isSearchGatewaySuccess.set(true);//鎼滅储鎴愬姛鏍囪 searchGatewayCount.set(11);//娆℃暟鏍囪 HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//璁剧疆褰撳墠缃戝叧 - HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑 if (mSearchGatewayCallBack != null) { mSearchGatewayCallBack.onSuccess(searchBean); } @@ -509,11 +527,14 @@ }.getType()); GatewaySearchBean gateway = response.getObjects(); if (gateway != null && !TextUtils.isEmpty(gateway.getGatewayId())) { + //鍙兘缃戝叧甯﹁繃鏉ョ殑ip涓嶅 + gateway.setIp_address(linkResponse.getSource_ipAddress()); //涓荤綉鍏冲苟涓旀槸褰撳墠缁戝畾鐨勭綉鍏� if ("true".equals(gateway.getMaster().toLowerCase()) && gateway.getGatewayId().equals(HDLLinkConfig.getInstance().getGatewayId())) { HDLLinkConfig.getInstance().setCurrentGateway(gateway);//璁剧疆褰撳墠缃戝叧 - HDLLinkConfig.getInstance().setLocalEncrypt(gateway.isLocalEncrypt());//璁剧疆鏄惁鍔犲瘑 - HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address()); + if(!TextUtils.isEmpty( gateway.getIp_address())) { + HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address()); + } //鏇存柊褰撳墠缃戝叧鐨勪俊鎭� HDLLinkConfig.getInstance().reSaveConfig(); } @@ -566,9 +587,11 @@ } if (!isSearchGatewaySuccess.get()) { - //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃� - callBackSearchGatewayTimeout(); - LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�"); + try { + LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�"); + //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃� + callBackSearchGatewayTimeout(); + }catch (Exception e){} } } }).start(); -- Gitblit v1.8.0