From 84bb9a10e795b8839d9770eeb37aab71bf22e107 Mon Sep 17 00:00:00 2001 From: panlili2024 <14743743+panlili2024@user.noreply.gitee.com> Date: 星期一, 29 九月 2025 10:13:18 +0800 Subject: [PATCH] 代码优化 --- HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java | 224 +++++++++++++++++++++++++++++++++++++++----------------- 1 files changed, 156 insertions(+), 68 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 0fa735a..b1021cd 100644 --- a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java +++ b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java @@ -35,8 +35,10 @@ import com.hdl.sdk.common.exception.HDLLinkException; import com.hdl.sdk.common.utils.IdUtils; import com.hdl.sdk.common.utils.LogUtils; +import com.hdl.sdk.common.utils.SPUtils; import com.hdl.sdk.common.utils.gson.GsonConvert; import com.hdl.sdk.connect.HDLLink; +import com.hdl.sdk.connect.bean.LinkFunctionInfo; import com.hdl.sdk.connect.bean.LinkResponse; import com.hdl.sdk.connect.bean.request.AttributesRequest; import com.hdl.sdk.connect.bean.request.AuthenticateRequest; @@ -44,15 +46,23 @@ import com.hdl.sdk.connect.bean.request.ListSidRequest; import com.hdl.sdk.connect.bean.request.ListUploadRequest; import com.hdl.sdk.connect.bean.response.BaseLocalResponse; +import com.hdl.sdk.connect.bean.response.BindInfoBean; import com.hdl.sdk.connect.bean.response.GatewaySearchBean; import com.hdl.sdk.connect.bean.response.UpdateInfo; import com.hdl.sdk.connect.callback.HDLLinkCallBack; import com.hdl.sdk.connect.callback.HDLLinkResponseCallBack; +import com.hdl.sdk.connect.callback.HDLLinkTCallBack; import com.hdl.sdk.connect.cloud.CallBackListener; import com.hdl.sdk.connect.cloud.CheckAppVersionListener; import com.hdl.sdk.connect.cloud.HDLException; +import com.hdl.sdk.connect.cloud.bean.GatewayInfo; +import com.hdl.sdk.connect.cloud.listener.GatewayListener; +import com.hdl.sdk.connect.cloud.listener.SibichiListener; +import com.hdl.sdk.connect.cloud.bean.AiLoginInfo; import com.hdl.sdk.connect.config.HDLLinkConfig; import com.hdl.sdk.connect.socket.HDLAuthSocket; +import com.hdl.sdk.connect.socket.HDLSocket; +import com.hdl.sdk.sourceos.utils.SPKey; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; @@ -74,37 +84,28 @@ private EventListener allTopicsListener; private String testLightSid = "020101A19B485D02020100010101"; private String secret = ""; - private List<FunctionBean> devicesList = new ArrayList<>(); - private List<FunctionBean> roomDevicesList = new ArrayList<>(); + private List<LinkFunctionInfo> devicesList = new ArrayList<>(); + private List<LinkFunctionInfo> roomDevicesList = new ArrayList<>(); private List<SceneBean> sceneList = new ArrayList<>(); private List<SceneDetailBean> sceneDetailList = new ArrayList<>(); private List<SceneDetailBean> roomSceneList = new ArrayList<>(); + private GatewayInfo gatewayInfo; void applyDeviceSecret() { tv.setText("寮�濮嬬敵璇疯澶囧瘑閽�..."); responseTv.setText(""); -// //姝e紡鏈嶅姟鍣� -// String appKey = "i8hR07jzrIS";//appkey -// String appSecret = "BmnJ8RWTtaVEBk24zPPF4UMwfYu0lAWU";//appsecret - - //娴嬭瘯鏈嶅姟鍣� - String appKey = "FcRyUJlLJFF"; - String appSecret = "wz8wn75ABidx8vXcFGUotqhwFkTaYvvJ"; - -// String appKey = "L2OZliZRxHc"; -// String appSecret = "aCIWSvJDOukXfx3kivsKW11x9xdR3IbV"; String supplier = "JINMAOYUN";//鍘傚晢 -// String mac = "AA00000000000100";//璁惧鍞竴MAC鍦板潃 - String mac = editText.getText().toString(); + String mac = "f2c5d8bad48f";//璁惧鍞竴MAC鍦板潃 +// String mac = editText.getText().toString(); String spk = "screen.touch";//浜у搧spk if (TextUtils.isEmpty(mac)) { Toast.makeText(this, "mac涓嶈兘涓虹┖锛�", Toast.LENGTH_SHORT).show(); return; } - - HDLLink.getInstance().applyDeviceSecret(this, appKey, appSecret, supplier, mac, spk, new CallBackListener() { + //璋冪敤浜戠鎺ュ彛闇�鍒濆鍖朒DLLink.getInstance().initCloud(); + HDLLink.getInstance().applyDeviceSecret(supplier, mac, spk, new CallBackListener() { @Override public void onError(HDLException e) { tv.setText("鐢宠澶辫触"); @@ -126,13 +127,10 @@ tv.setText("寮�濮嬫娴嬫洿鏂�..."); responseTv.setText(""); -// //姝e紡鏈嶅姟鍣� - String appKey = "i8hR07jzrIS";//appkey - String appSecret = "BmnJ8RWTtaVEBk24zPPF4UMwfYu0lAWU";//appsecret - String appCode = "1697150870315999233";//appCode - HDLLink.getInstance().checkAppVersion(this, appKey, appSecret, getAppVersionName(this), appCode, new CheckAppVersionListener() { + //璋冪敤浜戠鎺ュ彛闇�鍒濆鍖朒DLLink.getInstance().initCloud(); + HDLLink.getInstance().checkAppVersion(getAppVersionName(this), appCode, new CheckAppVersionListener() { @Override public void onSuccess(UpdateInfo info) { tv.setText("鏈夋柊鏇存柊"); @@ -142,6 +140,59 @@ @Override public void onError(HDLException e) { tv.setText("妫�娴嬫洿鏂板け璐�"); + responseTv.setText(e.getMsg()); + } + }); + + } + + void getSibichiToken() { + tv.setText("鑾峰彇鎬濆繀椹皌oken..."); + responseTv.setText(""); + + BindInfoBean bindInfoBean = (BindInfoBean) SPUtils.getSerializableEntity(SPKey.BIND_HOME_INFO); + String clientId = "4ED634B5A7AD97A770A52AC00FF43805";//鎬濆繀椹癱lientId + + //璋冪敤浜戠鎺ュ彛闇�鍒濆鍖朒DLLink.getInstance().initCloud(); + HDLLink.getInstance().getSibichiToken(bindInfoBean.getHomeId(), clientId, new SibichiListener() { + @Override + public void onSuccess(AiLoginInfo info) { + tv.setText("鎬濆繀椹皌oken"); + responseTv.setText(info.toString()); + } + + @Override + public void onError(HDLException e) { + tv.setText("鑾峰彇鎬濆繀椹皌oken澶辫触"); + responseTv.setText(e.getMsg()); + } + }); + + } + + void syncMainGateway() { + tv.setText("鑾峰彇涓荤綉鍏充俊鎭�..."); + responseTv.setText(""); + + BindInfoBean bindInfoBean = (BindInfoBean) SPUtils.getSerializableEntity(SPKey.BIND_HOME_INFO); + if (bindInfoBean == null) { + Toast.makeText(this, "璇峰厛鎵爜缁戝畾浣忓畢锛�", Toast.LENGTH_SHORT).show(); + return; + } + //璋冪敤浜戠鎺ュ彛闇�鍒濆鍖朒DLLink.getInstance().initCloud(); + HDLLink.getInstance().syncMainGateway(bindInfoBean.getHomeId(), new GatewayListener() { + @Override + public void onSuccess(GatewayInfo info) { + tv.setText("鑾峰彇涓荤綉鍏充俊鎭�"); + responseTv.setText(info.toString()); + if (info != null) { + HDLLinkConfig.getInstance().setGatewayId(info.getGatewayId()); + } + } + + @Override + public void onError(HDLException e) { + tv.setText("鑾峰彇涓荤綉鍏充俊鎭け璐�"); responseTv.setText(e.getMsg()); } }); @@ -180,7 +231,7 @@ selectnetwork(); checkIfCertified(); - initDeviceInfo();//鍒濆鍖� + initDeviceInfo();//涓嶈蛋浠庢満鍏ョ綉鐨勬柟寮忎笉闇�瑕佸垵濮嬪寲锛岃蛋浠庢満鍏ョ綉鍒欓渶瑕佸垵濮嬪寲 registerAllTopicsListener(); HDLLink.getInstance().setDeleteNetworkListener(new DeleteNetworkListener() { @Override @@ -226,6 +277,9 @@ beans.add(new DemoBean("鑾峰彇鎴块棿鍦烘櫙鍒楄〃")); beans.add(new DemoBean("鑾峰彇鑷姩鍖栧垪琛�")); beans.add(new DemoBean("饩冨姩鍖栧惎饨ょ饨�")); + beans.add(new DemoBean("鐢熸垚浜岀淮鐮佺粦瀹氫綇瀹�")); + beans.add(new DemoBean("鑾峰彇鎬濆繀椹皌oken")); + beans.add(new DemoBean("鑾峰彇缃戝叧淇℃伅")); demoAdapter = new DemoAdapter(beans); rv.setAdapter(demoAdapter); @@ -314,6 +368,18 @@ //饩冨姩鍖栧惎饨ょ饨� editEnableLogic(); break; + case 20: + //鐢熸垚浜岀淮鐮佺粦瀹氫綇瀹� + startSourceBindActivity(); + break; + case 21: + //鑾峰彇鎬濆繀椹皌oken + getSibichiToken(); + break; + case 22: + //鑾峰彇缃戝叧淇℃伅 + syncMainGateway(); + break; } } }); @@ -347,9 +413,14 @@ public void onMessage(Object msg) { LinkResponse response = (LinkResponse) msg; Log.i("onMessage", "response.getTopic():" + response.getTopic()); + String topic = String.format(TopicConstant.GATEWAY_LOGIN, + HDLLinkConfig.getInstance().getGatewayId()) + "_reply"; + ; + if ("/user/all/custom/gateway/broadcast_reply".equals(response.getTopic())) { localSecret.setText("瀵嗛挜锛�" + HDLLinkConfig.getInstance().getLocalSecret()); - + } else if (topic.equals(response.getTopic())) { + //tcp杩炴帴鎴愬姛锛屽鐞嗕笟鍔¢�昏緫 } } }; @@ -406,8 +477,8 @@ infoBean.setDeviceName("榄旈暅");//璁惧鍚嶅瓧 infoBean.setDeviceModel("HDLSDK");//璁惧鍨嬪彿 infoBean.setAccessMode("WIFI"); - infoBean.setIPGateway("192.168.31.1"); - infoBean.setIPAddress("192.168.31.103"); + infoBean.setIPGateway("192.168.1.1"); + infoBean.setIPAddress("192.168.1.103"); infoBean.setGateway_type("screen.touch"); infoBean.setHw_version("HW2.0"); infoBean.setFw_version("Fw1.0"); @@ -418,6 +489,20 @@ } + void initLink() { + //step1:鍏堢敓鎴愪簩缁寸爜锛岀敤onpro鎵爜缁戝畾浣忓畢鑾峰彇浣忓畢淇℃伅 + BindInfoBean bindInfoBean = (BindInfoBean) SPUtils.getSerializableEntity(SPKey.BIND_HOME_INFO); + if (bindInfoBean != null) { + HDLLinkConfig.getInstance().setHomeId(bindInfoBean.getHomeId()); + HDLLinkConfig.getInstance().setLocalSecret(bindInfoBean.getLocalSecret()); + + } + //step2锛氬啀鑾峰彇缃戝叧淇℃伅 + if (gatewayInfo != null) { + HDLLinkConfig.getInstance().setGatewayId(gatewayInfo.getGatewayId());//褰撳墠涓荤綉鍏砳d + } + } + /** * 鍏ョ綉璁よ瘉 */ @@ -425,15 +510,9 @@ tv.setText("寮�濮嬪叆缃戣璇�..."); //璁よ瘉鎻愪氦鍙傛暟鍑嗗 -// 娴嬭瘯鏈嶅姟 -// String spkStr = "ir.module";//浜у搧spk -// String macStr = "AA000000000000AF";//璁惧鍞竴MAC鍦板潃 -// String secret = "44b360eb74b7ba64";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret - -// 姝e紡鏈嶅姟鍣� - String spkStr = "screen.mirror";//浜у搧spk + //姝e紡鏈嶅姟鍣� + String spkStr = "screen.touch";//浜у搧spk String macStr = "f2c5d8bad48f";//璁惧鍞竴MAC鍦板潃 -// String secret = "e186beeb7974998e";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret String mac_key = stringToMD5(stringToMD5(macStr + secret)); String versionString = "HDL_V1.0.1";// @@ -496,7 +575,9 @@ void searchGatewayBroadcast() { tv.setText("鎼滅储缃戝叧涓�..."); responseTv.setText(""); - HDLLinkConfig.getInstance().getGatewayId(); + + String gatewayId = HDLLinkConfig.getInstance().getGatewayId(); + Log.d("panlili", "MainActivity.java:searchGatewayBroadcast----->gatewayId= " + gatewayId); HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() { @Override public void onError(HDLLinkException e) { @@ -519,37 +600,23 @@ void getFunctionList() { tv.setText("鑾峰彇璁惧鍒楄〃涓�..."); responseTv.setText(""); - HDLLink.getInstance().getFunctionList(new HDLLinkCallBack() { + HDLLink.getInstance().getFunctionInfoList(new HDLLinkTCallBack<List<LinkFunctionInfo>>() { + @Override + public void onSuccess(List<LinkFunctionInfo> data) { + if (data != null && data.size() != 0) { + devicesList.clear(); + devicesList.addAll(data); + + tv.setText("鑾峰彇璁惧鍒楄〃鎴愬姛"); + responseTv.setText(data.toString()); + } + } + @Override public void onError(HDLLinkException error) { tv.setText(error.getMsg()); } - - @Override - public void onSuccess(String data) { - tv.setText("鑾峰彇璁惧鍒楄〃鎴愬姛"); - responseTv.setText(data); - - handelFunctionList(data); - } }); - } - - void handelFunctionList(String data) { - try { - final LinkResponse linkResponse = GsonConvert.getGson().fromJson(data, new TypeToken<LinkResponse>() { - }.getType()); - - final BaseLocalResponse<List<FunctionBean>> bean = GsonConvert.getGson().fromJson(linkResponse.getData(), new TypeToken<BaseLocalResponse<List<FunctionBean>>>() { - }.getType()); - devicesList.clear(); - devicesList.addAll(bean.getObjects()); - - Log.d(TAG, "handelFunList-----> " + devicesList.toString()); - } catch (Exception e) { - Log.e(TAG, "handelFunList: " + e.getMessage()); - } - } /** @@ -560,11 +627,18 @@ tv.setText("璁惧鍔熻兘灞炴�ц鍙�"); responseTv.setText(""); List<String> sids = new ArrayList<>(); - sids.add(testLightSid); - HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() { + if (devicesList.size() != 0) { + sids.add(devicesList.get(0).getSid()); + } else { + sids.add(testLightSid); + } + + HDLLink.getInstance().getFunctionAttributeInfo(sids, new HDLLinkTCallBack<List<LinkFunctionInfo>>() { @Override - public void onSuccess(String msg) { - responseTv.setText(msg); + public void onSuccess(List<LinkFunctionInfo> data) { + if (data != null && data.size() != 0) { + responseTv.setText(data.toString()); + } } @Override @@ -582,7 +656,11 @@ tv.setText("璇诲彇鐘舵�佷腑..."); responseTv.setText(""); List<String> list = new ArrayList<>(); - list.add(testLightSid);//瑕佽鍙栬澶囩殑sid + if (devicesList.size() != 0) { + list.add(devicesList.get(0).getSid()); + } else { + list.add(testLightSid);//瑕佽鍙栬澶囩殑sid + } HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() { @Override public void onSuccess(String data) { @@ -608,7 +686,11 @@ isOn = !isOn; List<DeviceControlRequest> requestList = new ArrayList<>(); DeviceControlRequest request = new DeviceControlRequest(); - request.setSid(testLightSid); + if (devicesList.size() != 0) { + request.setSid(devicesList.get(0).getSid()); + } else { + request.setSid(testLightSid);//瑕佽鍙栬澶囩殑sid + } List<DeviceControlRequest.StatusBean> statusBeanList = new ArrayList<>(); DeviceControlRequest.StatusBean bean = new DeviceControlRequest.StatusBean(); bean.setKey("on_off"); @@ -617,7 +699,7 @@ request.setStatus(statusBeanList); requestList.add(request); - HDLLink.getInstance().propertyDown(requestList, new HDLLinkCallBack() { + HDLLink.getInstance().propertyDownDevice(requestList, new HDLLinkTCallBack<String>() { @Override public void onSuccess(String data) { responseTv.setText(data); @@ -873,7 +955,7 @@ void getRoomSceneList() { tv.setText("鑾峰彇鎴块棿鍦烘櫙鍒楄〃"); responseTv.setText(""); - String uid = "64a4fc6f-4eae-4d1d-82e4-40140c802c48"; + String uid = "64a4fc6f-4eae-4d1d-82e4-40140c802c48";//鎴块棿uid roomSceneList.clear(); if (sceneDetailList != null && sceneDetailList.size() != 0) { @@ -905,6 +987,11 @@ startActivity(intent); } + void startSourceBindActivity() { + Intent intent = new Intent(this, SourceBindActivity.class); + startActivity(intent); + } + /** * TCP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� */ @@ -914,6 +1001,7 @@ String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); String bodyStr = getPropertyDownBodyStr(); HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr); + } /** -- Gitblit v1.8.0