From 86116427931cc724ae1e3105738a34e0619ca287 Mon Sep 17 00:00:00 2001 From: 562935844@qq.com Date: 星期二, 04 四月 2023 16:12:47 +0800 Subject: [PATCH] 增加网关未上云密钥为空的问题处理 --- HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java | 240 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 190 insertions(+), 50 deletions(-) diff --git a/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java b/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java index d332fdc..5054c03 100644 --- a/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java +++ b/HDLSDK_DEMO/app/src/main/java/com/hdl/hdlsdk/MainActivity.java @@ -1,5 +1,15 @@ package com.hdl.hdlsdk; +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.EditText; +import android.widget.TextView; +import android.widget.Toast; + import androidx.activity.result.ActivityResultCallback; import androidx.activity.result.ActivityResultLauncher; import androidx.activity.result.contract.ActivityResultContracts; @@ -8,43 +18,28 @@ import androidx.recyclerview.widget.LinearLayoutManager; 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; -import android.widget.Toast; - 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.request.DeviceControlRequest; +import com.hdl.sdk.connect.bean.request.ListUploadRequest; 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; +import com.hdl.sdk.connect.cloud.CallBackListener; +import com.hdl.sdk.connect.cloud.HDLException; 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.connect.bean.request.DeviceControlRequest; -import com.hdl.sdk.connect.protocol.LinkMessageDecoder; -import com.hdl.sdk.connect.protocol.LinkMessageEncoder; -import com.hdl.sdk.socket.SocketOptions; -import com.hdl.sdk.socket.codec.MessagePipeLine; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; @@ -53,8 +48,6 @@ 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"; @@ -62,9 +55,11 @@ private RecyclerView rv; private TextView tv; private TextView responseTv; + private EditText editText; boolean isOn; private EventListener allTopicsListener; private String testLightSid = "0001010D48C71B02020100010101"; + private String secret = ""; @Override protected void onDestroy() { @@ -72,22 +67,64 @@ removeAllTopicsListener(); } - void initDeviceInfo() - { + void initDeviceInfo() { AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); - infoBean.setDeviceMAC("AA00000000000100"); - infoBean.setIPMAC("AA00000000000100"); + infoBean.setDeviceMAC("f2c5d8bad48f"); + infoBean.setIPMAC("f2c5d8bad48f"); infoBean.setDeviceName("闊充箰鎾斁鍣�");//璁惧鍚嶅瓧 infoBean.setDeviceModel("MCLog.431");//璁惧鍨嬪彿 infoBean.setAccessMode("WIFI"); infoBean.setIPGateway("192.168.1.1"); - infoBean.setIPAddress("192.168.1.116"); - infoBean.setGateway_type("music.standard"); + infoBean.setIPAddress("192.168.1.103"); + infoBean.setGateway_type("screen.touch"); infoBean.setHw_version("HW2.0"); infoBean.setFw_version("Fw1.0"); infoBean.setOID("010105000000FE11");//姣忎釜璁惧oid閮借涓嶄竴鏍� infoBean.setSid("110105000000FE08110100000011");//姣忎釜璁惧鐨剆id閮借涓嶄竴鏍� + infoBean.setSupplier("JINMAOYUN"); HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean); + } + + 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 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() { + @Override + public void onError(HDLException e) { + tv.setText("鐢宠澶辫触"); + responseTv.setText(e.getMsg()); + secret = ""; + } + + @Override + public void onSuccess(String msg) { + tv.setText("鐢宠鎴愬姛"); + responseTv.setText(msg.toString()); + secret = msg.toString(); + } + }); + } /** @@ -95,29 +132,34 @@ */ void sendAuthenticateRequest() { tv.setText("寮�濮嬪叆缃戣璇�..."); + responseTv.setText(""); //璁よ瘉鎻愪氦鍙傛暟鍑嗗 // -// //娴嬭瘯鏈嶅姟 -// String spkStr = "ir.module";//浜у搧spk +// //娴嬭瘯鏈嶅姟鍣� +// String spkStr = "music.standard";//浜у搧spk // String macStr = "AA000000000000AF";//璁惧鍞竴MAC鍦板潃 // String secret = "44b360eb74b7ba64";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret // 姝e紡鏈嶅姟鍣� - String spkStr = "screen.mirror";//浜у搧spk - String macStr = "AA00000000000100";//璁惧鍞竴MAC鍦板潃 - String secret = "e186beeb7974998e";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret + String spkStr = "screen.touch";//浜у搧spk +// String macStr = "AA00000000000100";//璁惧鍞竴MAC鍦板潃 + String macStr = editText.getText().toString();//璁惧鍞竴MAC鍦板潃 +// String secret = "e186beeb7974998e";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret String mac_key = stringToMD5(stringToMD5(macStr + secret)); String versionString = "HDL_V1.0.1";// String time = String.valueOf(System.currentTimeMillis()); HDLLinkConfig.getInstance().getDeviceInfoBean().setDeviceMAC(macStr); +// HDLLinkConfig.getInstance().setCurrentGateway(infoBean); //1.璁剧疆璁よ瘉淇℃伅 AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean(); requestBean.setMAC(macStr); - requestBean.setSupplier("WISE"); + requestBean.setSupplier("JINMAOYUN"); requestBean.setFirmwareVersion(versionString); requestBean.setHardwareModel("1956F"); +// HDLLinkConfig.getInstance().setRequestBean(requestBean); + AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean(); authbean.setSpk(spkStr); authbean.setMACKey(mac_key); @@ -127,9 +169,9 @@ HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() { @Override public void onError(HDLLinkException e) { - tv.setText("璁よ瘉澶辫触"); + tv.setText("璁よ瘉澶辫触" + e.getCode()); responseTv.setText(e.getMsg()); - Log.i("TAG", "onError: 璁よ瘉澶辫触"); + Log.e(TAG, "onError: 璁よ瘉澶辫触 " + e.getCode()); } @Override @@ -140,16 +182,36 @@ }); } + public void UploadDeviceAuth(String mac, String result, String message, String auth_code) { + //灏嗚璇佺粨鏋滀笂鎶ョ粰缃戝叧 + HDLLink.getInstance().UploadDeviceAuth(mac, result, message, auth_code, new HDLLinkCallBack() { + @Override + public void onSuccess(String s) { + tv.setText("涓婃姤缁撴灉鎴愬姛"); + responseTv.setText(s.toString()); + } + + @Override + public void onError(HDLLinkException e) { + tv.setText("涓婃姤缁撴灉澶辫触"); + responseTv.setText(e.getMsg()); + Log.i(TAG, "onError: 涓婃姤缁撴灉澶辫触"); + } + }); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); responseTv = findViewById(R.id.response_tv); + editText = findViewById(R.id.edittext); tv = findViewById(R.id.state_tv); rv = findViewById(R.id.rv); rv.setLayoutManager(new LinearLayoutManager(this)); + selectnetwork(); checkIfCertified(); initDeviceInfo();//鍒濆鍖� registerAllTopicsListener(); @@ -163,9 +225,11 @@ launcher.launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}); - final List<DemoBean> beans = new ArrayList<>(); + final List<DemoBean> beans = new ArrayList<DemoBean>(); + beans.add(new DemoBean("鐢宠璁惧瀵嗛挜")); beans.add(new DemoBean("鍏ョ綉璁よ瘉")); beans.add(new DemoBean("鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎")); + beans.add(new DemoBean("涓婃姤oid鍒楄〃")); beans.add(new DemoBean("鑾峰彇鍔熻兘鍒楄〃")); beans.add(new DemoBean("鍔熻兘灞炴�ц鍙�")); beans.add(new DemoBean("璁惧鎺у埗")); @@ -177,6 +241,7 @@ beans.add(new DemoBean("TCP鍙戦��")); beans.add(new DemoBean("UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�")); beans.add(new DemoBean("TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�")); + beans.add(new DemoBean("閫�缃�")); demoAdapter = new DemoAdapter(beans); rv.setAdapter(demoAdapter); @@ -192,56 +257,68 @@ public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) { switch (position) { case 0: + //鐢宠璁惧瀵嗛挜 + applyDeviceSecret(); + break; + case 1: //鍏ョ綉璁よ瘉 sendAuthenticateRequest(); break; - case 1: + case 2: //鎼滅储缃戝叧 searchGatewayBroadcast(); break; - case 2: + case 3: + //涓婃姤oid鍒楄〃 + UploadOidList(); + break; + case 4: //鑾峰彇鍔熻兘鍒楄〃 getFunctionList(); break; - case 3: + case 5: //鍔熻兘灞炴�ц鍙� getFunctionAttribute(); break; - case 4: + case 6: //璁惧鎺у埗 controlDecide(); break; - case 5: + case 7: //璇诲彇鐘舵�� propertyRead(); break; - case 6: + case 8: //鑾峰彇鍦烘櫙鍒楄〃 getSceneList(); break; - case 7: - //鑾峰彇鍦烘櫙鍒楄〃 + case 9: + //鍦烘櫙鎺у埗 controlScene(); break; - case 8: + case 10: //鍔熻兘鍒楄〃 startDevicesListActivity(); break; - case 9: + case 11: //UDP鍙戦�� udpSend(); break; - case 10: + case 12: //TCP鍙戦�� tcpSend(); break; - case 11: + case 13: //UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� udpSendWithCallback(); break; - case 12: + case 14: //TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� tcpSendWithCallback(); + break; + case 15: + //閫�缃� + deleteNetwork(); break; } } @@ -251,6 +328,10 @@ public void showToast(String text) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); + } + + void selectnetwork() { + HDLLink.getInstance().selectNetwork("wlan0"); } /** @@ -320,7 +401,6 @@ } - String stringToMD5(String text) { byte[] hash; try { @@ -349,6 +429,7 @@ */ void searchGatewayBroadcast() { tv.setText("鎼滅储缃戝叧涓�..."); + responseTv.setText(""); HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() { @Override public void onError(HDLLinkException e) { @@ -391,6 +472,7 @@ */ void getFunctionAttribute() { tv.setText("鍔熻兘灞炴�ц鍙�"); + responseTv.setText(""); List<String> sids = new ArrayList<>(); sids.add(testLightSid); HDLLink.getInstance().getFunctionAttribute(sids, new HDLLinkCallBack() { @@ -412,6 +494,7 @@ */ void propertyRead() { tv.setText("璇诲彇鐘舵�佷腑..."); + responseTv.setText(""); List<String> list = new ArrayList<>(); list.add(testLightSid);//瑕佽鍙栬澶囩殑sid HDLLink.getInstance().propertyRead(list, new HDLLinkCallBack() { @@ -435,6 +518,7 @@ */ void controlDecide() { tv.setText("鎺у埗璁惧"); + responseTv.setText(""); isOn = !isOn; List<DeviceControlRequest> requestList = new ArrayList<>(); DeviceControlRequest request = new DeviceControlRequest(); @@ -464,6 +548,7 @@ */ void getSceneList() { tv.setText("璇诲彇鍦烘櫙鍒楄〃"); + responseTv.setText(""); HDLLink.getInstance().getSceneList(new HDLLinkCallBack() { @Override public void onSuccess(String msg) { @@ -486,6 +571,7 @@ */ void controlScene() { tv.setText("鍦烘櫙鎺у埗"); + responseTv.setText(""); //鎺у埗鍦烘櫙sid鍒楄〃锛屾敮鎸佹壒閲忔帶鍒� List<String> sids = new ArrayList<>(); isOn = !isOn; @@ -518,6 +604,7 @@ */ private void tcpSend() { tv.setText("TCP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�"); + responseTv.setText(""); String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); String bodyStr = getPropertyDownBodyStr(); HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr); @@ -528,6 +615,7 @@ */ private void udpSend() { tv.setText("UDP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�"); + responseTv.setText(""); String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); String bodyStr = getPropertyDownBodyStr(); HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr); @@ -560,6 +648,7 @@ */ private void udpSendWithCallback() { tv.setText("UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�"); + responseTv.setText(""); String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); String bodyStr = getPropertyDownBodyStr(); HDLLink.getInstance().udpSendMsg(propertyDownTopic, bodyStr, new HDLLinkResponseCallBack() { @@ -581,6 +670,7 @@ */ private void tcpSendWithCallback() { tv.setText("TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�"); + responseTv.setText(""); String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); String bodyStr = getPropertyDownBodyStr(); HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr, new HDLLinkCallBack() { @@ -596,4 +686,54 @@ } }); } + + private void deleteNetwork() { + tv.setText("閫�缃�"); + responseTv.setText(""); + String oid = HDLLinkConfig.getInstance().getDeviceInfoBean().getOID(); + HDLLink.getInstance().deleteNetwork(oid, false, new HDLLinkCallBack() { + @Override + public void onSuccess(String msg) { + responseTv.setText(msg); + //閫�缃戞垚鍔燂紝鍒犻櫎鏁版嵁 + } + + @Override + public void onError(HDLLinkException e) { + responseTv.setText(e.getMsg()); + } + }); + } + + /** + * 涓婃姤oid鍒楄〃 + */ + private void UploadOidList() { + tv.setText("涓婃姤oid鍒楄〃..."); + responseTv.setText(""); + + //1.璁剧疆璁惧淇℃伅 + AuthenticateRequest.AuthenticateDeviceInfoBean authenticateDeviceInfoBean = HDLLinkConfig.getInstance().getDeviceInfoBean(); + + ListUploadRequest request = new ListUploadRequest(); + request.setOid(authenticateDeviceInfoBean.getOID()); + request.setDevice_name(authenticateDeviceInfoBean.getDeviceName()); + request.setDevice_model(authenticateDeviceInfoBean.getDeviceModel()); + request.setDevice_mac(authenticateDeviceInfoBean.getDeviceMAC()); + request.setSrc("LINK");//鏉ヨ嚜鍝釜椹卞姩锛屽Link璁惧娌℃湁鍒欏~"LINK" + request.setFrom(authenticateDeviceInfoBean.getOID());//鏉ヨ嚜鍝釜缃戝叧锛屽鏋滄椂Link缃戠粶璁惧锛屽垯濉嚜韬玱id锛涘鏋滄槸椹卞姩鍒欎笉闇�瑕佹瀛楁 + + HDLLink.getInstance().UploadOidList(request, new HDLLinkCallBack() { + @Override + public void onError(HDLLinkException error) { + tv.setText(error.getMsg()); + } + + @Override + public void onSuccess(String data) { + tv.setText("涓婃姤oid鍒楄〃鎴愬姛"); + responseTv.setText(data); + } + }); + } } \ No newline at end of file -- Gitblit v1.8.0