From a4246a571c78ac6c46e7bf7dbfc123b7148caed8 Mon Sep 17 00:00:00 2001
From: JLChen <551775569@qq.com>
Date: 星期二, 16 十一月 2021 13:43:48 +0800
Subject: [PATCH] 2021-11-16 1.更新

---
 HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java |  320 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 234 insertions(+), 86 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 4223f60..2a652c1 100644
--- a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
+++ b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -17,15 +17,20 @@
 
 import com.chad.library.adapter.base.BaseQuickAdapter;
 import com.chad.library.adapter.base.listener.OnItemClickListener;
+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.connect.HDLLink;
-import com.hdl.sdk.connect.bean.AuthenticateRequest;
-import com.hdl.sdk.connect.bean.GatewaySearchBean;
-import com.hdl.sdk.connect.config.HDLLinkConfig;
+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.GatewaySearchBean;
+import com.hdl.sdk.connect.callback.HDLLinkCallBack;
+import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack;
 import com.hdl.sdk.connect.socket.HDLAuthSocket;
 import com.hdl.sdk.connect.socket.HDLSocket;
-import com.hdl.sdk.connect.bean.DeviceControlRequest;
+import com.hdl.sdk.connect.bean.request.DeviceControlRequest;
 import com.hdl.sdk.connect.protocol.LinkMessageDecoder;
 import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
 import com.hdl.sdk.socket.SocketOptions;
@@ -45,7 +50,14 @@
     private TextView tv;
     private TextView responseTv;
     boolean isOn;
+    private EventListener allTopicsListener;
+    private String testLightSid = "000101B847C71B02020100010101";
 
+    @Override
+    protected void onDestroy() {
+        super.onDestroy();
+        removeAllTopicsListener();
+    }
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -57,6 +69,7 @@
         rv.setLayoutManager(new LinearLayoutManager(this));
 
         checkIfCertified();
+        registerAllTopicsListener();
 
         ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
             @Override
@@ -68,14 +81,14 @@
         launcher.launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE});
 
         final List<DemoBean> beans = new ArrayList<>();
-        beans.add(new DemoBean("鎼滅储缃戝叧"));
+        beans.add(new DemoBean("鍏ョ綉璁よ瘉"));
+        beans.add(new DemoBean("鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎"));
         beans.add(new DemoBean("鑾峰彇鍔熻兘鍒楄〃"));
         beans.add(new DemoBean("鍔熻兘灞炴�ц鍙�"));
         beans.add(new DemoBean("璁惧鎺у埗"));
-        beans.add(new DemoBean("鐘舵�佷笂鎶�"));
         beans.add(new DemoBean("璇诲彇鐘舵��"));
-        beans.add(new DemoBean("鍏ョ綉璁よ瘉"));
-        beans.add(new DemoBean("鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎"));
+        beans.add(new DemoBean("鑾峰彇鍦烘櫙鍒楄〃"));
+        beans.add(new DemoBean("鍦烘櫙鎺у埗"));
         demoAdapter = new DemoAdapter(beans);
         rv.setAdapter(demoAdapter);
 
@@ -93,91 +106,36 @@
             public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                 switch (position) {
                     case 0:
-                        tv.setText("鎼滅储缃戝叧涓�");
-                        responseTv.setText("");
-
-                        HDLSocket.getInstance().searchGateway(new HDLSocket.CallBack() {
-                            @Override
-                            public void onError(String error) {
-                                tv.setText("缃戝叧鑾峰彇澶辫触");
-                            }
-
-                            @Override
-                            public void onResponse(String data) {
-                                tv.setText("鑾峰彇缃戝叧鎴愬姛");
-                                responseTv.setText(data);
-                            }
-                        });
-
+                        //鍏ョ綉璁よ瘉
+                        sendAuthenticateRequest();
                         break;
                     case 1:
-
-                        tv.setText("鑾峰彇鍔熻兘鍒楄〃涓�");
-                        responseTv.setText("");
-                        HDLSocket.getInstance().getFunctionList(new HDLSocket.CallBack() {
-                            @Override
-                            public void onError(String error) {
-                                tv.setText(error);
-                            }
-
-                            @Override
-                            public void onResponse(String data) {
-                                tv.setText("鑾峰彇鍔熻兘鍒楄〃鎴愬姛");
-                                responseTv.setText(data);
-                            }
-                        });
+                        //鎼滅储缃戝叧
+                        searchGatewayBroadcast();
                         break;
                     case 2:
-                        //鍔熻兘灞炴�ц鍙�
-                      //  HDLSocket.getInstance().getFunctionAttribute();
-
+                       //鑾峰彇鍔熻兘鍒楄〃
+                        getFunctionList();
                         break;
                     case 3:
-                        //璁惧鎺у埗
-                        isOn = !isOn;
-                        List<DeviceControlRequest> requestList = new ArrayList<>();
-                        DeviceControlRequest request = new DeviceControlRequest();
-                        request.setSid("000101B847C71B02020100010101");
-                        List<DeviceControlRequest.StatusBean>  statusBeanList= new ArrayList<>();
-                        DeviceControlRequest.StatusBean bean =  new DeviceControlRequest.StatusBean();
-                        bean.setKey("on_off");
-                        bean.setValue(isOn ? "on" : "off");
-                        statusBeanList.add(bean);
-                        request.setStatus(statusBeanList);
-                        requestList.add(request);
-                        HDLAuthSocket.getInstance().propertyDown(requestList,null);
-
-
+                        //鍔熻兘灞炴�ц鍙�
+                       getFunctionAttribute();
                         break;
                     case 4:
-                        //鐘舵�佷笂鎶�
-                        //HDLSocket.getInstance().propertyUp();
+                        //璁惧鎺у埗
+                        controlDecide();
                         break;
                     case 5:
                         //璇诲彇鐘舵��
-                        // HDLSocket.getInstance().propertyRead();
+                        propertyRead();
                         break;
                     case 6:
-//                        //鍏ョ綉璁よ瘉
-                        sendAuthenticateRequest();
+                        //鑾峰彇鍦烘櫙鍒楄〃
+                        getSceneList();
                         break;
                     case 7:
-//                        //濡傛灉宸茬粡璁よ瘉鍚庯紝鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎
-                        HDLLink.getInstance().searchGateway(new HDLAuthSocket.SearchGatewayCallBack() {
-                            @Override
-                            public void onEnd(String error) {
-                                tv.setText("缃戝叧涓嶅湪绾�");
-                            }
-
-                            @Override
-                            public void onSuccess(GatewaySearchBean gatewaySearchBean) {
-                                tv.setText("缃戝叧鍦ㄧ嚎");
-                                responseTv.setText("缃戝叧id锛�"+gatewaySearchBean.getGatewayId());
-//                                Log.i("TAG", "onSuccess: 鎼滅储鎴愬姛锛�"+gatewaySearchBean.getGatewayId());
-                            }
-                        });
-
-
+                        //鑾峰彇鍦烘櫙鍒楄〃
+                        controlScene();
                         break;
                 }
             }
@@ -187,16 +145,42 @@
         Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
     }
 
-
+    /**
+     * 妫�娴嬫槸鍚﹁璇佽繃
+     */
     void checkIfCertified(){
         boolean isCertified = HDLLink.getInstance().checkIfCertified();
-        showToast(isCertified?"宸茬粡璁よ瘉杩�":"鏈璇�");
+        String mes = isCertified?"宸茬粡璁よ瘉杩�":"鏈璇�";
+        showToast(mes);
+        tv.setText(mes);
+    }
+
+    /**
+     * 娉ㄥ唽鎵�鏈変富棰樻暟鎹殑鐩戝惉
+     */
+    void registerAllTopicsListener(){
+        allTopicsListener = new EventListener() {
+            @Override
+            public void onMessage(Object msg) {
+                LinkResponse response = (LinkResponse)msg;
+                Log.i("TAG", "AllTopicsListener onMessage: "+msg.toString());
+            }
+        };
+        HDLLink.getInstance().registerAllTopicsListener(allTopicsListener);
+    }
+
+    /**
+     * 绉婚櫎鎵�鏈変富棰樻暟鎹殑鐩戝惉
+     */
+    void removeAllTopicsListener(){
+        HDLLink.getInstance().removeAllTopicsListener(allTopicsListener);
     }
 
     /**
      * 鍏ョ綉璁よ瘉
      */
     void sendAuthenticateRequest(){
+        tv.setText("寮�濮嬪叆缃戣璇�...");
 
         String macStr = "AA000000000000BB";
         String secret = "87ae414b7a853f65";
@@ -236,20 +220,21 @@
         String ip = IpUtils.getBroadcastAddress();
 //        ip = "192.168.10.102";
 
-        HDLLink.getInstance().sendAuthenticateRequest(ip, request, new HDLAuthSocket.CallBack() {
+        HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() {
             @Override
-            public void onError(String error) {
-                Log.i("TAG", "onError: 璁よ瘉澶辫触");
+            public void onError(HDLLinkException e) {
+                tv.setText("璁よ瘉澶辫触");
+                responseTv.setText(e.getMsg());
+//                Log.i("TAG", "onError: 璁よ瘉澶辫触");
             }
 
             @Override
-            public void onSuccess(String data) {
+            public void onSuccess(String msg) {
                 tv.setText("璁よ瘉鎴愬姛");
-                responseTv.setText(data.toString());
+                responseTv.setText(msg.toString());
             }
         });
     }
-
 
     String stringToMD5(String text) {
         byte[] hash;
@@ -272,4 +257,167 @@
 
         return hex.toString();
     }
+
+    /**
+     * 濡傛灉宸茬粡璁よ瘉鍚庯紝鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎
+     * 骞挎挱鎼滅储璁よ瘉杩囩殑缃戝叧鏄惁鍦ㄧ嚎
+     */
+    void searchGatewayBroadcast(){
+        tv.setText("鎼滅储缃戝叧涓�...");
+        HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() {
+            @Override
+            public void onError(HDLLinkException e) {
+                tv.setText("缃戝叧涓嶅湪绾�");
+            }
+            @Override
+            public void onSuccess(GatewaySearchBean gatewaySearchBean) {
+                tv.setText("缃戝叧鍦ㄧ嚎");
+                responseTv.setText("鎼滅储鎴愬姛 缃戝叧id锛�"+gatewaySearchBean.getGatewayId());
+//                                LogUtils.i("TAG", "onSuccess: 鎼滅储鎴愬姛锛�"+gatewaySearchBean.getGatewayId());
+            }
+        });
+    }
+
+    /**
+     * 鑾峰彇鍔熻兘鍒楄〃
+     */
+    void getFunctionList(){
+        tv.setText("鑾峰彇鍔熻兘鍒楄〃涓�...");
+        responseTv.setText("");
+        HDLSocket.getInstance().getFunctionList(new HDLLinkCallBack() {
+
+            @Override
+            public void onError(HDLLinkException error) {
+                tv.setText(error.getMsg());
+            }
+
+            @Override
+            public void onSuccess(String data) {
+                tv.setText("鑾峰彇鍔熻兘鍒楄〃鎴愬姛");
+                responseTv.setText(data);
+            }
+        });
+    }
+
+    /**
+     * 鍔熻兘灞炴�ц鍙�
+     * 鏀寔鎵归噺璇诲彇
+     */
+    void getFunctionAttribute(){
+        tv.setText("鍔熻兘灞炴�ц鍙�");
+        List<String> sids = new ArrayList<>();
+        sids.add(testLightSid);
+        HDLSocket.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() {
+            @Override
+            public void onSuccess(String msg) {
+                responseTv.setText(msg);
+            }
+
+            @Override
+            public void onError(HDLLinkException e) {
+                responseTv.setText(e.getMsg());
+            }
+        });
+    }
+
+    /**
+     * 璇诲彇璁惧鐘舵��
+     * 鏀寔鎵归噺璇诲彇
+     */
+    void propertyRead(){
+        tv.setText("璇诲彇鐘舵�佷腑...");
+        List<String> list = new ArrayList<>();
+        list.add(testLightSid);//瑕佽鍙栬澶囩殑sid
+        HDLSocket.getInstance().propertyRead(list, new HDLLinkCallBack() {
+            @Override
+            public void onSuccess(String data) {
+                tv.setText("璇诲彇鎴愬姛");
+                responseTv.setText(data);
+            }
+            @Override
+            public void onError(HDLLinkException e) {
+                tv.setText("璇诲彇澶辫触");
+                responseTv.setText(e.getMsg());
+            }
+        });
+    }
+
+    /**
+     * 鎺у埗澶辫触
+     * 鍥炲鍝嶅簲code涓�200 浠h〃鎵ц鎴愬姛
+     */
+    void controlDecide(){
+        tv.setText("鎺у埗璁惧");
+        isOn = !isOn;
+        List<DeviceControlRequest> requestList = new ArrayList<>();
+        DeviceControlRequest request = new DeviceControlRequest();
+        request.setSid(testLightSid);
+        List<DeviceControlRequest.StatusBean>  statusBeanList= new ArrayList<>();
+        DeviceControlRequest.StatusBean bean =  new DeviceControlRequest.StatusBean();
+        bean.setKey("on_off");
+        bean.setValue(isOn ? "on" : "off");
+        statusBeanList.add(bean);
+        request.setStatus(statusBeanList);
+        requestList.add(request);
+        HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() {
+            @Override
+            public void onSuccess(String data) {
+                responseTv.setText(data);
+            }
+
+            @Override
+            public void onError(HDLLinkException e) {
+                responseTv.setText(e.getMsg());
+            }
+        });
+    }
+
+    /**
+     * 鑾峰彇鍦烘櫙鍒楄〃
+     */
+    void getSceneList(){
+        tv.setText("璇诲彇鍦烘櫙鍒楄〃");
+        HDLLink.getInstance().getSceneList(new HDLLinkCallBack() {
+            @Override
+            public void onSuccess(String msg) {
+                responseTv.setText(msg);
+            }
+
+            @Override
+            public void onError(HDLLinkException e) {
+                responseTv.setText(e.getMsg());
+            }
+        });
+    }
+
+    //鍦烘櫙鍒楄〃
+    // {"id":"8a5eaa143ce943b987b577df5a66759b","time_stamp":"1637040217235","objects":[{"sid":"04010560D2C7170A0A0100000000","name":"鍥炲妯″紡","status":"off","group":"255","delay":"0","modify_time":"1634871490"},{"sid":"04010560D2C76E0A0A0100010000","name":"绂诲妯″紡","status":"off","group":"255","delay":"0","modify_time":"1634785823"}]}
+    /**
+     * 鎺у埗鍦烘櫙
+     * 鎵ц鎴愬姛鐨勮瘽 鍝嶅簲code涓�200
+     */
+    void controlScene(){
+        tv.setText("鍦烘櫙鎺у埗");
+        //鎺у埗鍦烘櫙sid鍒楄〃锛屾敮鎸佹壒閲忔帶鍒�
+        List<String> sids = new ArrayList<>();
+        isOn = !isOn;
+        if(isOn){
+            sids.add("04010560D2C7170A0A0100000000");
+        }else{
+            sids.add("04010560D2C76E0A0A0100010000");
+        }
+        HDLLink.getInstance().controlScene(sids,new HDLLinkCallBack() {
+            @Override
+            public void onSuccess(String msg) {
+                responseTv.setText(msg);
+            }
+
+            @Override
+            public void onError(HDLLinkException e) {
+                responseTv.setText(e.getMsg());
+            }
+        });
+    }
+
+
 }
\ No newline at end of file

--
Gitblit v1.8.0