From dfb62af125bcb4eed4eadbcfc5a49a1232501c0c Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期五, 10 十二月 2021 16:23:41 +0800
Subject: [PATCH] 2021-12-10 1.增加tcp和UDP 单次发送,不监听回复

---
 HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java |  118 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 104 insertions(+), 14 deletions(-)

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 33838bc..d03f334 100644
--- a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
+++ b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -9,7 +9,9 @@
 import androidx.recyclerview.widget.RecyclerView;
 
 import android.Manifest;
+import android.content.Intent;
 import android.os.Bundle;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.View;
 import android.widget.TextView;
@@ -17,14 +19,21 @@
 
 import com.chad.library.adapter.base.BaseQuickAdapter;
 import com.chad.library.adapter.base.listener.OnItemClickListener;
+import com.google.gson.JsonObject;
+import com.google.gson.reflect.TypeToken;
+import com.hdl.hdlsdk.device.DevicesListActivity;
+import com.hdl.sdk.common.config.TopicConstant;
 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.LinkRequest;
 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;
@@ -43,15 +52,18 @@
 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 = "000101B847C71B02020100010101";
+    private String testLightSid = "0001010D48C71B02020100010101";
 
     @Override
     protected void onDestroy() {
@@ -90,17 +102,18 @@
         beans.add(new DemoBean("璇诲彇鐘舵��"));
         beans.add(new DemoBean("鑾峰彇鍦烘櫙鍒楄〃"));
         beans.add(new DemoBean("鍦烘櫙鎺у埗"));
+        beans.add(new DemoBean("璁惧鍔熻兘鍒楄〃"));
+        beans.add(new DemoBean("UDP鍙戦��"));
+        beans.add(new DemoBean("TCP鍙戦��"));
         demoAdapter = new DemoAdapter(beans);
         rv.setAdapter(demoAdapter);
 
 
-        final SocketOptions options = new SocketOptions();
-
-        MessagePipeLine pipeLine = new MessagePipeLine();
-        pipeLine.add(new LinkMessageDecoder());
-        pipeLine.add(new LinkMessageEncoder());
-        options.setHandleMessage(pipeLine);
-        options.setEnabledHeartbeat(false);
+//        final SocketOptions options = new SocketOptions();
+//
+//        MessagePipeLine pipeLine = new MessagePipeLine();
+//        options.setHandleMessage(pipeLine);
+//        options.setEnabledHeartbeat(false);
 
         demoAdapter.setOnItemClickListener(new OnItemClickListener() {
             @Override
@@ -138,10 +151,25 @@
                         //鑾峰彇鍦烘櫙鍒楄〃
                         controlScene();
                         break;
+                    case 8:
+                        //鍔熻兘鍒楄〃
+                        startDevicesListActivity();
+                        break;
+                    case 9:
+                        //UDP鍙戦��
+                        udpSend();
+                        break;
+                    case 10:
+                        //TCP鍙戦��
+                        tcpSend();
+                        break;
                 }
             }
         });
     }
+
+
+
     public void showToast(String text) {
         Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
     }
@@ -164,10 +192,45 @@
             @Override
             public void onMessage(Object msg) {
                 LinkResponse response = (LinkResponse)msg;
-                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;
     }
 
     /**
@@ -184,9 +247,9 @@
         tv.setText("寮�濮嬪叆缃戣璇�...");
         //璁よ瘉鎻愪氦鍙傛暟鍑嗗
 
-        String spkStr = "ir.module";//浜у搧spk
-        String macStr = "AA000000000000BB";//璁惧鍞竴MAC鍦板潃
-        String secret = "87ae414b7a853f65";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret
+        String spkStr = "screen.mirror";//浜у搧spk
+        String macStr = "AA000000000000AC";//璁惧鍞竴MAC鍦板潃
+        String secret = "ee62124c151b737c";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret
         String mac_key = stringToMD5(stringToMD5(macStr + secret));
         String versionString = "HDL_V1.0.1";//
         String time = String.valueOf(System.currentTimeMillis());
@@ -194,7 +257,7 @@
         //1.璁剧疆璁よ瘉淇℃伅
         AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean();
         requestBean.setMAC(macStr);
-        requestBean.setSupplier("HDL");
+        requestBean.setSupplier("raysgem");
         requestBean.setFirmwareVersion(versionString);
         requestBean.setHardwareModel("1956F");
         AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean();
@@ -209,7 +272,7 @@
         infoBean.setDeviceName("绾㈠瀹�");//璁惧鍚嶅瓧
         infoBean.setDeviceModel("HDL");//
         infoBean.setAccessMode("WIFI");
-        infoBean.setIPGateway("192.168.10.1");
+        infoBean.setIPGateway("192.168.88.1");
         infoBean.setIPAddress(IpUtils.getIP(this));
 
         infoBean.setOID("010105000000FE08");
@@ -266,6 +329,7 @@
             @Override
             public void onError(HDLLinkException e) {
                 tv.setText("缃戝叧涓嶅湪绾�");
+                responseTv.setText(e.getMsg());
             }
             @Override
             public void onSuccess(GatewaySearchBean gatewaySearchBean) {
@@ -417,4 +481,30 @@
     }
 
 
+    void startDevicesListActivity(){
+        Intent intent = new Intent(this, DevicesListActivity.class);
+        startActivity(intent);
+    }
+
+    /**
+     * TCP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�
+     */
+    private void tcpSend() {
+        String time = String.valueOf(System.currentTimeMillis());
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("id", IdUtils.getUUId());
+        jsonObject.addProperty("time_stamp", time);
+        HDLLink.getInstance().tcpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString());
+    }
+
+    /**
+     * 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�
+     */
+    private void udpSend() {
+        String time = String.valueOf(System.currentTimeMillis());
+        JsonObject jsonObject = new JsonObject();
+        jsonObject.addProperty("id", IdUtils.getUUId());
+        jsonObject.addProperty("time_stamp", time);
+        HDLLink.getInstance().udpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString());
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0