From 05552c8b73a16b69aded2d4d877fa968c29e7cce Mon Sep 17 00:00:00 2001 From: hxb <hxb@hdlchina.com.cn> Date: 星期二, 07 十二月 2021 14:32:34 +0800 Subject: [PATCH] 第一次优化调整 --- HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/exception/HDLLinkCode.java | 1 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HdlSocketHelper.java | 1 HDLSDK/app/src/main/java/com/hdl/hdlsdk/App.java | 2 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/config/HDLLinkConfig.java | 22 -- HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java | 11 - HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java | 109 ++++++++++++- HDLSDK/build.gradle | 2 .DS_Store | 0 HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java | 239 +++++++++++++++++++++++------ HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java | 1 HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java | 61 ------ README.md | 5 12 files changed, 314 insertions(+), 140 deletions(-) diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..360042d --- /dev/null +++ b/.DS_Store Binary files differ diff --git a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/App.java b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/App.java index 2935d87..f7e8f3c 100644 --- a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/App.java +++ b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/App.java @@ -21,7 +21,7 @@ //鍒濆鍖朣DK HDLSdk.getInstance().init(this); //鎺у埗SDK鏃ュ織鎵撳嵃 - HDLSdk.getInstance().setLogEnabled(true); + HDLSdk.getInstance().setLogEnabled(false); } diff --git a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java index ea90450..fcac387 100644 --- a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java +++ b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java @@ -10,7 +10,6 @@ import android.Manifest; import android.os.Bundle; -import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.TextView; @@ -18,17 +17,14 @@ import com.chad.library.adapter.base.BaseQuickAdapter; import com.chad.library.adapter.base.listener.OnItemClickListener; -import com.google.gson.reflect.TypeToken; import com.hdl.sdk.common.event.EventListener; import com.hdl.sdk.common.exception.HDLLinkException; import com.hdl.sdk.common.utils.IdUtils; import com.hdl.sdk.common.utils.IpUtils; -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.AuthenticateRequest; import com.hdl.sdk.connect.bean.request.PropertyReadRequest; -import com.hdl.sdk.connect.bean.response.BaseLocalResponse; import com.hdl.sdk.connect.bean.response.GatewaySearchBean; import com.hdl.sdk.connect.callback.HDLLinkCallBack; import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack; @@ -47,18 +43,15 @@ import java.util.List; import java.util.Map; -import static com.hdl.sdk.common.config.TopicConstant.GATEWAY_SEARCH_REPLY; - public class MainActivity extends AppCompatActivity { - private static final String TAG = "MainActivity"; private DemoAdapter demoAdapter; private RecyclerView rv; private TextView tv; private TextView responseTv; boolean isOn; private EventListener allTopicsListener; - private String testLightSid = "0001010D48C71B02020100010101"; + private String testLightSid = "000101B847C71B02020100010101"; @Override protected void onDestroy() { @@ -76,7 +69,6 @@ rv.setLayoutManager(new LinearLayoutManager(this)); checkIfCertified(); - registerAllTopicsListener(); ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { @@ -103,7 +95,9 @@ // final SocketOptions options = new SocketOptions(); // - MessagePipeLine pipeLine = new MessagePipeLine(); +// MessagePipeLine pipeLine = new MessagePipeLine(); +// // pipeLine.add(new LinkMessageDecoder()); +// pipeLine.add(new LinkMessageEncoder()); // options.setHandleMessage(pipeLine); // options.setEnabledHeartbeat(false); @@ -169,52 +163,11 @@ @Override public void onMessage(Object msg) { LinkResponse response = (LinkResponse)msg; - if(response != null){ - Log.i(TAG, "AllTopicsListener" - + "\nTopic: " + response.getTopic() - +"\n Data: " + response.getData()); - handleLinkResponse(response); - } - + //response.getData() 浣跨敤杩欎釜鏂规硶鑾峰彇鏁版嵁杞琷son灏卞彲浠ヤ簡 +// Log.i("TAG", "AllTopicsListener onMessage: "+msg.toString()); } }; HDLLink.getInstance().registerAllTopicsListener(allTopicsListener); - } - - /** - * 澶勭悊鏀跺埌鐨勪富棰� - * @param response - */ - private void handleLinkResponse(LinkResponse response){ - //缃戝叧鎼滅储鍥炲 - if(response.getTopic().contains("/user/all/custom/gateway/search_reply")){ - String data = response.getData(); - if (!TextUtils.isEmpty(data)) { - Log.i("handleLinkResponse", "data:"+data); - final BaseLocalResponse<GatewaySearchBean> bean = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() { - }.getType()); - GatewaySearchBean searchBean = bean.getObjects(); - Log.i("handleLinkResponse", "GatewaySearchBean: "+searchBean.getGatewayId()); - } - }else{ - //鍏跺畠涓婚 - - } - } - - private GatewaySearchBean getGatewaySearchBean(Object msg) { - GatewaySearchBean searchBean = null; - if (msg instanceof LinkResponse) { - LinkResponse linkResponse = (LinkResponse) msg; - String data = linkResponse.getData(); - if (!TextUtils.isEmpty(data)) { - final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() { - }.getType()); - searchBean = response.getObjects(); - } - - } - return searchBean; } /** @@ -256,7 +209,7 @@ infoBean.setDeviceName("绾㈠瀹�");//璁惧鍚嶅瓧 infoBean.setDeviceModel("HDL");// infoBean.setAccessMode("WIFI"); - infoBean.setIPGateway("192.168.12.1"); + infoBean.setIPGateway("192.168.10.1"); infoBean.setIPAddress(IpUtils.getIP(this)); infoBean.setOID("010105000000FE08"); diff --git a/HDLSDK/build.gradle b/HDLSDK/build.gradle index 1755efa..693faec 100644 --- a/HDLSDK/build.gradle +++ b/HDLSDK/build.gradle @@ -6,7 +6,7 @@ mavenCentral() } dependencies { - classpath "com.android.tools.build:gradle:4.2.2" + classpath "com.android.tools.build:gradle:4.1.2" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/exception/HDLLinkCode.java b/HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/exception/HDLLinkCode.java index 7847526..908f391 100644 --- a/HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/exception/HDLLinkCode.java +++ b/HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/exception/HDLLinkCode.java @@ -13,7 +13,6 @@ HDL_SEND_ERROR(-2003,"鍙戦�佸け璐�"), HDL_TIMEOUT_ERROR(-2004,"瓒呮椂"), HDL_UNAUTHORIZED_ERROR(-2005,"鏈璇侊紝璇峰厛璁よ瘉"), - HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED(-2006,"璁よ瘉澶辫触锛岀綉鍏虫湭娉ㄥ唽鍒颁簯绔�"), HDL_GET_DEVICE_LIST_ERROR(-2100,"鑾峰彇璁惧鍒楄〃澶辫触"), HDL_GET_FUNCTION_LIST_ERROR(-2101,"鑾峰彇鍔熻兘鍒楄〃澶辫触"), HDL_GET_FUNCTION_PROPERTIES_ERROR(-2102,"鑾峰彇鍔熻兘灞炴�уけ璐�"), diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/config/HDLLinkConfig.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/config/HDLLinkConfig.java index 5fb4298..a212728 100644 --- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/config/HDLLinkConfig.java +++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/config/HDLLinkConfig.java @@ -28,7 +28,6 @@ * instance */ private volatile static HDLLinkConfig instance; - /** * getInstance * @@ -63,16 +62,15 @@ /** * 鍔犺浇缂撳瓨 */ - void loadConfig() { + void loadConfig(){ localSecret = SPUtils.getString(AUTHENTICATE_LS_KEY, ""); gatewayId = SPUtils.getString(AUTHENTICATE_GATEWAYID_KEY, ""); ipAddress = SPUtils.getString(AUTHENTICATE_IPADDRESS_KEY, ""); - isLocalEncrypt = SPUtils.getBoolean(AUTHENTICATE_IS_LS_KEY, false); + isLocalEncrypt = SPUtils.getBoolean(AUTHENTICATE_IS_LS_KEY,false); } /** * 淇濆瓨閰嶇疆 - * * @param localSecret * @param gatewayId * @param ipAddress @@ -88,10 +86,9 @@ /** * 妫�娴嬫槸鍚﹀凡缁忚璇佽繃 - * * @return */ - public boolean checkIfCertified() { + public boolean checkIfCertified(){ //localSecret涓嶄负绌哄苟涓旈暱搴︾瓑浜�16 缃戝叧id涓嶈兘涓虹┖ return !TextUtils.isEmpty(localSecret) && localSecret.length() == 16 @@ -130,22 +127,14 @@ public void setCurrentGateway(GatewaySearchBean currentGateway) { this.currentGateway = currentGateway; - if (this.currentGateway != null && !TextUtils.isEmpty(this.currentGateway.getIp_address())) { - if (!this.ipAddress.equals(this.currentGateway.getIp_address())) { - //濡傛灉IP鍦板潃鏈夊彉鍖栧垯鏇存柊 - this.ipAddress = this.currentGateway.getIp_address(); - SPUtils.put(AUTHENTICATE_IPADDRESS_KEY, ipAddress); - } - } } /** * 鍒ゆ柇褰撳墠涓婚鏁版嵁鏄惁闇�瑕佸姞瀵� - * * @param topicStr 褰撳墠涓婚 * @return */ - public boolean ifNeedEncrypt(String topicStr) { + public boolean ifNeedEncrypt(String topicStr){ //杩囨护鐩稿叧闇�瑕佸姞瀵嗙殑涓婚 return (!topicStr.contains(TopicConstant.GATEWAY_AUTH_BROADCAST) //缃戝叧骞挎挱鍏ョ綉鎸囦护 && !topicStr.contains(TopicConstant.DEIVCE_AUTH_REQUEST) //鍏ョ綉璁よ瘉 @@ -156,11 +145,10 @@ /** * 鎷兼帴缃戝叧ID鑾峰彇瀹屾暣鐨勪富棰� - * * @param topicStr * @return */ - public String getFullTopic(String topicStr) { + public String getFullTopic(String topicStr){ return String.format(topicStr, gatewayId); } diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java index 4cc8a8f..dc3afa8 100644 --- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java +++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/protocol/LinkMessageDecoder.java @@ -1,6 +1,8 @@ package com.hdl.sdk.connect.protocol; +import android.util.Log; + import com.hdl.sdk.common.utils.LogUtils; import com.hdl.sdk.connect.config.HDLLinkConfig; import com.hdl.sdk.common.event.EventDispatcher; @@ -22,77 +24,214 @@ private final List<Byte> bytes; private final byte[] head = "Topic:".getBytes(); - private final byte[] body = "\r\n\r\n".getBytes(); +// private final byte[] body = "\r\n\r\n".getBytes(); public LinkMessageDecoder() { this.bytes = new ArrayList<>(); } + /// <summary> + /// 鑾峰彇鍐呭闀垮害 + /// </summary> + /// <param name="topMsgs"></param> + /// <returns></returns> + int getLenght(String[] topMsgs) { + for (int i = 0; i < topMsgs.length; i++) { + String topMsg = topMsgs[i].trim(); + if (topMsg.startsWith("Length:")) { + return Integer.parseInt(topMsg.replace("Length:", "")); + } + } + //鎵句笉鍒伴暱搴� + return -1; + } + + /// <summary> + /// 鑾峰彇涓婚 + /// </summary> + /// <param name="topMsgs"></param> + /// <returns></returns> + private String getTopic(String[] topMsgs) { + for (int i = 0; i < topMsgs.length; i++) { + String topMsg = topMsgs[i].trim(); + if (topMsg.startsWith("Topic:")) { + return topMsg.replace("Topic:", ""); + } + } + //鎵句笉鍒颁富棰� + return null; + } + + /** + * 鑾峰彇鏁版嵁鐨勫紑濮嬩綅缃� + * + * @param arrayList 鎺ユ敹鍒扮殑鎵�鏈夋暟鎹� + * @return 鏁版嵁浣嶇殑寮�濮嬬储寮� + */ + int getDataIndex(List<Byte> arrayList) { + byte r = (byte) '\r'; + byte n = (byte) '\n'; + for (int i = 0; i < arrayList.size(); i++) { + //鎵惧嚭鏁版嵁鍐呭鍓嶉潰鐨勪袱涓崲琛� + if (3 <= i && arrayList.get(i - 3) == r && arrayList.get(i - 2) == n && arrayList.get(i - 1) == r && arrayList.get(i) == n) { + //鍓╀綑鐨勬暟鎹� + return i + 1; + } + } + return -1; + } + + void initReceiveData(List<Byte> list) { + + int index = 0; + boolean isMatch = false; + for (; index < list.size() - head.length; index++) { + isMatch = true; + for (int j = 0, k = 0; j < head.length; j++, k++) { + if (head[j] != list.get(index + k)) { + isMatch = false; + break; + } + } + if (isMatch) { + break; + } + } + + if (0 < index && isMatch) { + List<Byte> tempList = new ArrayList<Byte>(); + for (int i = index; i < list.size(); i++) { + tempList.add(list.get(index)); + } + + list.clear(); + for(int i=0;i<tempList.size();i++){ + list.add(tempList.get(i)); + } + } + } + @Override protected LinkResponse decoder(Object msg) throws Exception { - LinkResponse response = new LinkResponse(); if (msg instanceof byte[]) { - //瑙f瀽娴� - byte[] data = (byte[]) msg; - bytes.addAll(ByteUtils.toByteList(data)); + bytes.addAll(ByteUtils.toByteList((byte[]) msg)); + //濡傛灉澶氭潯鍛戒护鎵撳寘鍦ㄤ竴鏉℃暟鎹腑锛岄兘闇�瑕佸鐞嗗畬 + while (true) { + initReceiveData(bytes); + byte[] recevieBytes = ByteUtils.toByteArray(bytes); + String data = new String(recevieBytes); - byte[] byteArray = ByteUtils.toByteArray(bytes); - int headIndex = ByteUtils.getByteIndexOf(byteArray, head); - if (headIndex > 0) { - //绉诲姩鍒癶ead 寮�濮嬩綅缃� - bytes.subList(0, headIndex).clear(); - byteArray = ByteUtils.toByteArray(bytes); - } + String[] topMsgs = data.split("\r\n"); - int bodyIndex = ByteUtils.getByteIndexOf(byteArray, body); - if (bodyIndex < 0) { - //澶撮儴鏈幏鍙栧畬鎴� - return null; - } - int bodyStartIndex = bodyIndex + body.length; + String topic = getTopic(topMsgs); + if (topic == null) { + break; + } - //瑙f瀽澶撮儴 - ProtocolParse parse = new ProtocolParse(byteArray); - response.setTopic(parse.getTopic()); + int lenght = getLenght(topMsgs); - int bodyLength = parse.getLength(); - if (bodyLength > 0) { - if (byteArray.length >= bodyLength + bodyStartIndex) { - byte[] body = ByteUtils.getRangeBytes(bytes, bodyStartIndex, bodyStartIndex + bodyLength); + if (lenght <= 0) { + //澶撮儴鏁版嵁杩樻病鏈夋帴鏀跺畬鎴� + break; + } + //鏄惁宸茬粡鑾峰彇瀹屾暣鎵�鏈夌殑鏁版嵁 + byte[] body = new byte[lenght]; + int index = getDataIndex(bytes); + if (bytes.size() < index + lenght) { + //褰撳墠鏁版嵁杩樻病鏈夋帴鏀跺畬鎴� + break; + } + //澶嶅埗鍑篵ody鏁版嵁 + System.arraycopy(recevieBytes, index, body, 0, body.length); - if (HDLLinkConfig.getInstance().ifNeedEncrypt(response.getTopic())) { - //闇�瑕佽В瀵� - byte[] bodyBytes = AesUtil.aesDecrypt(body, HDLLinkConfig.getInstance().getLocalSecret()); + //淇濈暀鍓╀綑鐨勬暟鎹紝浠ユ鐢� + bytes.clear(); + for (int i = index + lenght; i < recevieBytes.length; i++) { + bytes.add(recevieBytes[i]); + } + LinkResponse response = new LinkResponse(); + response.setTopic(topic); + if (HDLLinkConfig.getInstance().ifNeedEncrypt(response.getTopic())) { + //闇�瑕佽В瀵� + byte[] bodyBytes = AesUtil.aesDecrypt(body, HDLLinkConfig.getInstance().getLocalSecret()); // byte[] bodyBytes = AESUtils.decryptAES(body,AuthenticateConfig.getInstance().getLocalSecret()); - if (bodyBytes != null) { - response.setData(new String(bodyBytes, "utf-8")); + if (bodyBytes != null) { + response.setData(new String(bodyBytes, "utf-8")); // LogUtils.i("TAG", "瑙e瘑 涓婚锛�"+response.getTopic()+ " body: "+response.getData()); - } else { - //瑙e瘑澶辫触锛岃繑鍥炲師鏁版嵁 - response.setData(new String(body, "utf-8")); - LogUtils.e( "瑙e瘑澶辫触"); - } - } else { + //瑙e瘑澶辫触锛岃繑鍥炲師鏁版嵁 response.setData(new String(body, "utf-8")); } - if (byteArray.length >= bodyLength + bodyStartIndex) { - //淇濆瓨浣欑暀 - byte[] remaining = ByteUtils.getRangeBytes(bytes, bodyStartIndex + bodyLength, byteArray.length); - bytes.clear(); - for (byte b : remaining) { - bytes.add(b); - } - } - //瑙f瀽瀹屾垚,topic鍙戦�佷竴娆� - EventDispatcher.getInstance().post(response.getTopic(), response); - return response; + } else { + response.setData(new String(body, "utf-8")); } - } else if (bodyLength == 0) { - //body涓虹┖ - return response; + Log.i("TAG", "LinkMessageDecoder->decoder:" + response.getTopic() + "\r\n" + response.getData()); + //瑙f瀽瀹屾垚,topic鍙戦�佷竴娆� + EventDispatcher.getInstance().post(response.getTopic(), response); } + return null; + +// //瑙f瀽娴� +// byte[] data = (byte[]) msg; +// bytes.addAll(ByteUtils.toByteList(data)); +// +// byte[] byteArray = ByteUtils.toByteArray(bytes); +// int headIndex = ByteUtils.getByteIndexOf(byteArray, head); +// if (headIndex > 0) { +// //绉诲姩鍒癶ead 寮�濮嬩綅缃� +// bytes.subList(0, headIndex).clear(); +// byteArray = ByteUtils.toByteArray(bytes); +// } +// +// int bodyIndex = ByteUtils.getByteIndexOf(byteArray, body); +// if (bodyIndex < 0) { +// //澶撮儴鏈幏鍙栧畬鎴� +// return null; +// } +// int bodyStartIndex = bodyIndex + body.length; +// +// //瑙f瀽澶撮儴 +// ProtocolParse parse = new ProtocolParse(byteArray); +// response.setTopic(parse.getTopic()); +// +// int bodyLength = parse.getLength(); +// if (bodyLength > 0) { +// if (byteArray.length >= bodyLength + bodyStartIndex) { +// byte[] body = ByteUtils.getRangeBytes(bytes, bodyStartIndex, bodyStartIndex + bodyLength); +// +// if (HDLLinkConfig.getInstance().ifNeedEncrypt(response.getTopic())) { +// //闇�瑕佽В瀵� +// byte[] bodyBytes = AesUtil.aesDecrypt(body, HDLLinkConfig.getInstance().getLocalSecret()); +//// byte[] bodyBytes = AESUtils.decryptAES(body,AuthenticateConfig.getInstance().getLocalSecret()); +// if (bodyBytes != null) { +// response.setData(new String(bodyBytes, "utf-8")); +//// LogUtils.i("TAG", "瑙e瘑 涓婚锛�"+response.getTopic()+ " body: "+response.getData()); +// } else { +// //瑙e瘑澶辫触锛岃繑鍥炲師鏁版嵁 +// response.setData(new String(body, "utf-8")); +// } +// +// } else { +// response.setData(new String(body, "utf-8")); +// } +// +// if (byteArray.length >= bodyLength + bodyStartIndex) { +// //淇濆瓨浣欑暀 +// byte[] remaining = ByteUtils.getRangeBytes(bytes, bodyStartIndex + bodyLength, byteArray.length); +// bytes.clear(); +// for (byte b : remaining) { +// bytes.add(b); +// } +// } +// //瑙f瀽瀹屾垚,topic鍙戦�佷竴娆� +// EventDispatcher.getInstance().post(response.getTopic(), response); +// return response; +// } +// } else if (bodyLength == 0) { +// //body涓虹┖ +// return response; +// } } return null; 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 0fb853c..b6e525a 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 @@ -164,17 +164,12 @@ String localSecret = bean.getAuth().getLocalSecret(); String gatewayId = bean.getObjects().getGatewayID(); String 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("璁よ瘉鎴愬姛"); - } + HDLLinkConfig.getInstance().saveConfig(localSecret,gatewayId,ipAddress); + callBack.onSuccess("璁よ瘉鎴愬姛"); }else{ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR)); } + } @Override diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java index 5d7ddc0..ae90c48 100644 --- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java +++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLSocket.java @@ -702,6 +702,7 @@ } threadPool.shutdownNow(); } + EventDispatcher.getInstance().remove(eventTag, this); } }; diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HdlSocketHelper.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HdlSocketHelper.java index 53ef271..1921b51 100644 --- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HdlSocketHelper.java +++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HdlSocketHelper.java @@ -129,5 +129,6 @@ if (listener != null) { listener.onFailure(); } + EventDispatcher.getInstance().remove(eventListener); } } diff --git a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java index 3cd750c..0f9fcae 100644 --- a/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java +++ b/HDLSDK/hdl-socket/src/main/java/com/hdl/sdk/socket/client/TcpClient.java @@ -2,6 +2,7 @@ +import com.hdl.sdk.common.utils.ByteUtils; import com.hdl.sdk.common.utils.ThreadToolUtils; import com.hdl.sdk.socket.SocketBoot; import com.hdl.sdk.socket.SocketOptions; @@ -14,7 +15,7 @@ import java.io.OutputStream; import java.net.InetSocketAddress; import java.net.Socket; -import java.util.Arrays; +import java.util.ArrayList; import java.util.List; @@ -22,6 +23,11 @@ * Created by Tong on 2021/9/15. */ public final class TcpClient implements IClient { + /* + *鎺ユ敹鏁版嵁鐨勭紦鍐插尯 + */ + private final List<Byte> byteList; + private final byte[] head = "Topic:".getBytes(); private SocketOptions socketOptions; @@ -33,6 +39,7 @@ private byte[] readBuffer; private TcpClient(String ip, int port, SocketOptions socketOptions) { + this.byteList = new ArrayList<>(); this.socketOptions = socketOptions; this.ip = ip; this.port = port; @@ -84,18 +91,104 @@ return socketOptions; } + /// <summary> + /// 鑾峰彇鍐呭闀垮害 + /// </summary> + /// <param name="topMsgs"></param> + /// <returns></returns> + int getLenght(String[] topMsgs) + { + for (int i = 0; i < topMsgs.length; i++) + { + String topMsg = topMsgs[i].trim(); + if (topMsg.startsWith("Length:")) + { + return Integer.parseInt(topMsg.replace("Length:", "")); + } + } + //鎵句笉鍒伴暱搴� + return -1; + } + + /// <summary> + /// 鑾峰彇涓婚 + /// </summary> + /// <param name="topMsgs"></param> + /// <returns></returns> + private String getTopic(String[] topMsgs) + { + for (int i = 0; i < topMsgs.length; i++) + { + String topMsg = topMsgs[i].trim(); + if (topMsg.startsWith("Topic:")) + { + return topMsg.replace("Topic:", ""); + } + } + //鎵句笉鍒颁富棰� + return null; + } + + /** + * 鑾峰彇鏁版嵁鐨勫紑濮嬩綅缃� + * @param arrayList 鎺ユ敹鍒扮殑鎵�鏈夋暟鎹� + * @return 鏁版嵁浣嶇殑寮�濮嬬储寮� + */ + int getDataIndex(List<Byte> arrayList) + { + byte r = (byte)'\r'; + byte n = (byte)'\n'; + for (int i = 0; i < arrayList.size(); i++) + { + //鎵惧嚭鏁版嵁鍐呭鍓嶉潰鐨勪袱涓崲琛� + if (3 <= i && arrayList.get(i - 3) == r && arrayList.get(i - 2) == n && arrayList.get(i - 1) == r && arrayList.get(i) == n) + { + //鍓╀綑鐨勬暟鎹� + return i + 1; + } + } + return -1; + } + + void initReceiveData(List<Byte> list) { + + int index = 0; + boolean isMatch=false; + for (; index < list.size() - head.length; index++) { + isMatch=true; + for (int j = 0, k = 0; j < head.length; j++, k++) { + if (head[j] != list.get(index + k)) { + isMatch=false; + break; + } + } + if(isMatch) { + break; + } + } + + if (0 < index&&isMatch) { + List<Byte> tempList = new ArrayList<Byte>(); + for(int i=index;i<list.size();i++) + { + tempList.add(list.get(index)); + } + list=tempList; + } + + } @Override public void onHandleResponse() throws Exception { final InputStream stream = getInputStream(); - if (stream != null && getOptions() != null) { - readBuffer = new byte[1024]; - int len; - while ((len = getInputStream().read(readBuffer)) != -1) { + int len=0; + while ( (len=getInputStream().read(readBuffer)) != -1) { IHandleMessage handleMessage = getOptions().getHandleMessage(); - if (handleMessage != null && len > 0) { - handleMessage.read(Arrays.copyOfRange(readBuffer, 0, len)); -// handleMessage.read(readBuffer); + if (handleMessage != null) { + byte []bytes = new byte[len]; + System.arraycopy(readBuffer,0,bytes,0,len); + //瀹屾暣鐨勬暟鎹墠鍥炶皟 + handleMessage.read(bytes); } } } diff --git a/README.md b/README.md index bc96413..7d0b13d 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,8 @@ HDLSDK_A_Android +娴嬭瘯璁よ瘉淇℃伅 +"supplier": "WISE", +"mac": "AA000000000000AB", +"spk": "music.standard", +"deviceSecret": "ca5edb13106847a8" -- Gitblit v1.8.0