From b1569b789eef12cf5d4252620ca7e7d2d9c8b93a Mon Sep 17 00:00:00 2001 From: panlili2024 <14743743+panlili2024@user.noreply.gitee.com> Date: 星期四, 14 十一月 2024 14:26:03 +0800 Subject: [PATCH] sdk重连优化 --- HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java | 897 ++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 754 insertions(+), 143 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..94d2799 100644 --- a/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java +++ b/HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java @@ -1,5 +1,18 @@ package com.hdl.hdlsdk; +import android.Manifest; +import android.content.Context; +import android.content.Intent; +import android.content.pm.PackageInfo; +import android.content.pm.PackageManager; +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,28 +21,36 @@ import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import android.Manifest; -import android.os.Bundle; -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.reflect.TypeToken; +import com.hdl.hdlsdk.device.DevicesListActivity; +import com.hdl.sdk.common.config.TopicConstant; +import com.hdl.sdk.common.event.DeleteNetworkListener; +import com.hdl.sdk.common.event.EventListener; +import com.hdl.sdk.common.exception.HDLLinkException; +import com.hdl.sdk.common.utils.AllTopicManagerUtils; import com.hdl.sdk.common.utils.IdUtils; -import com.hdl.sdk.common.utils.IpUtils; +import com.hdl.sdk.common.utils.LogUtils; +import com.hdl.sdk.common.utils.gson.GsonConvert; 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.bean.LinkResponse; +import com.hdl.sdk.connect.bean.request.AttributesRequest; +import com.hdl.sdk.connect.bean.request.AuthenticateRequest; +import com.hdl.sdk.connect.bean.request.DeviceControlRequest; +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.DeviceInfoResponse; +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.cloud.CallBackListener; +import com.hdl.sdk.connect.cloud.CheckAppVersionListener; +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.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; @@ -40,11 +61,103 @@ public class MainActivity extends AppCompatActivity { + private static final String TAG = "MainActivity"; private DemoAdapter demoAdapter; private RecyclerView rv; private TextView tv; private TextView responseTv; + private TextView localSecret; + private EditText editText; boolean isOn; + private EventListener allTopicsListener; + private String testLightSid = "02010131D9C03D01020300010107"; + private String secret = ""; + + 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(); + } + }); + + } + + void checkAppVersion() { + 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() { + @Override + public void onSuccess(UpdateInfo info) { + tv.setText("鏈夋柊鏇存柊"); + responseTv.setText(info.toString()); + } + + @Override + public void onError(HDLException e) { + tv.setText("妫�娴嬫洿鏂板け璐�"); + responseTv.setText(e.getMsg()); + } + }); + + } + + 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 @@ -52,11 +165,29 @@ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); responseTv = findViewById(R.id.response_tv); + editText = findViewById(R.id.edittext); + localSecret = findViewById(R.id.local_secret); tv = findViewById(R.id.state_tv); rv = findViewById(R.id.rv); rv.setLayoutManager(new LinearLayoutManager(this)); + selectnetwork(); checkIfCertified(); + initDeviceInfo();//鍒濆鍖� + registerAllTopicsListener(); + HDLLink.getInstance().setDeleteNetworkListener(new DeleteNetworkListener() { + @Override + public void onSuccess(Object msg) { + LogUtils.i("setDeleteNetworkListener onSucceed = " + msg); + } + + @Override + public void onFailure() { + LogUtils.i("setDeleteNetworkListener onFailure"); + } + }); + + UploadSidList(); ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { @Override @@ -67,189 +198,259 @@ launcher.launch(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}); - final List<DemoBean> beans = new ArrayList<>(); - beans.add(new DemoBean("鎼滅储缃戝叧")); + 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("璁惧鎺у埗")); - 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("UDP鍙戦��")); + beans.add(new DemoBean("TCP鍙戦��")); + beans.add(new DemoBean("UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�")); + beans.add(new DemoBean("TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋�")); + beans.add(new DemoBean("閫�缃�")); + beans.add(new DemoBean("妫�娴嬫洿鏂�")); 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 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); - } - }); - + //鐢宠璁惧瀵嗛挜 + applyDeviceSecret(); 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); - } - }); - break; - case 2: - //鍔熻兘灞炴�ц鍙� - // HDLSocket.getInstance().getFunctionAttribute(); - - 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); - - - break; - case 4: - //鐘舵�佷笂鎶� - //HDLSocket.getInstance().propertyUp(); - break; - case 5: - //璇诲彇鐘舵�� - // HDLSocket.getInstance().propertyRead(); - break; - case 6: -// //鍏ョ綉璁よ瘉 + //鍏ョ綉璁よ瘉 sendAuthenticateRequest(); break; + case 2: + //鎼滅储缃戝叧 + searchGatewayBroadcast(); + break; + case 3: + //涓婃姤oid鍒楄〃 + UploadOidList(); + break; + case 4: + //鑾峰彇鍔熻兘鍒楄〃 + getFunctionList(); + break; + case 5: + //鍔熻兘灞炴�ц鍙� + getFunctionAttribute(); + break; + case 6: + //璁惧鎺у埗 + controlDecide(); + 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()); - } - }); - - + //璇诲彇鐘舵�� + propertyRead(); + break; + case 8: + //鑾峰彇鍦烘櫙鍒楄〃 + getSceneList(); + break; + case 9: + //鍦烘櫙鎺у埗 + getScene(); + break; + case 10: + //鍔熻兘鍒楄〃 + startDevicesListActivity(); + break; + case 11: + //UDP鍙戦�� + udpSend(); + break; + case 12: + //TCP鍙戦�� + tcpSend(); + break; + case 13: + //UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� + udpSendWithCallback(); + break; + case 14: + //TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� + tcpSendWithCallback(); + break; + case 15: + //閫�缃� + deleteNetwork(); + break; + case 16: + //妫�娴嬫洿鏂� + checkAppVersion(); break; } } }); } + + public void showToast(String text) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); } + void selectnetwork() { + HDLLink.getInstance().selectNetwork("wlan0"); + } - void checkIfCertified(){ + /** + * 妫�娴嬫槸鍚﹁璇佽繃 + */ + 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("onMessage", "response.getTopic():" + response.getTopic()); + if ("/user/all/custom/gateway/broadcast_reply".equals(response.getTopic())) { + localSecret.setText("瀵嗛挜锛�" + HDLLinkConfig.getInstance().getLocalSecret()); + + } + } + }; + 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; + } + + /** + * 绉婚櫎鎵�鏈変富棰樻暟鎹殑鐩戝惉 + */ + void removeAllTopicsListener() { + HDLLink.getInstance().removeAllTopicsListener(allTopicsListener); + } + + void initDeviceInfo() { + AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); + infoBean.setDeviceMAC("f2c5d8bad48f"); + infoBean.setIPMAC("f2c5d8bad48f"); + infoBean.setDeviceName("榄旈暅");//璁惧鍚嶅瓧 + infoBean.setDeviceModel("HDLSDK");//璁惧鍨嬪彿 + infoBean.setAccessMode("WIFI"); + 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"); + infoBean.setOID("010105000000FE11");//姣忎釜璁惧oid閮借涓嶄竴鏍� + infoBean.setSid("110105000000FE08110100000011");//姣忎釜璁惧鐨剆id閮借涓嶄竴鏍� + infoBean.setSupplier("JINMAOYUN"); + HDLLinkConfig.getInstance().setDeviceInfoBean(infoBean); + } /** * 鍏ョ綉璁よ瘉 */ - void sendAuthenticateRequest(){ + void sendAuthenticateRequest() { + tv.setText("寮�濮嬪叆缃戣璇�..."); + //璁よ瘉鎻愪氦鍙傛暟鍑嗗 - String macStr = "AA000000000000BB"; - String secret = "87ae414b7a853f65"; +// 娴嬭瘯鏈嶅姟 +// String spkStr = "ir.module";//浜у搧spk +// String macStr = "AA000000000000AF";//璁惧鍞竴MAC鍦板潃 +// String secret = "44b360eb74b7ba64";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret + +// 姝e紡鏈嶅姟鍣� + String spkStr = "screen.mirror";//浜у搧spk + String macStr = "f2c5d8bad48f";//璁惧鍞竴MAC鍦板潃 +// String secret = "e186beeb7974998e";//閫氳繃spk鍜宮ac鎻愪氦浜戠璁よ瘉鍚庡垎閰嶇殑secret + String mac_key = stringToMD5(stringToMD5(macStr + secret)); - - String versionString = "HDL_V1.0.1"; + String versionString = "HDL_V1.0.1";// String time = String.valueOf(System.currentTimeMillis()); + HDLLinkConfig.getInstance().getDeviceInfoBean().setDeviceMAC(macStr); //1.璁剧疆璁よ瘉淇℃伅 AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean(); requestBean.setMAC(macStr); - requestBean.setSupplier("HDL"); + requestBean.setSupplier("JINMAOYUN"); requestBean.setFirmwareVersion(versionString); requestBean.setHardwareModel("1956F"); AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean(); - authbean.setSpk("ir.module"); + authbean.setSpk(spkStr); authbean.setMACKey(mac_key); authbean.setRequest(requestBean); - - //2.璁剧疆璁惧淇℃伅 - AuthenticateRequest.AuthenticateDeviceInfoBean infoBean = new AuthenticateRequest.AuthenticateDeviceInfoBean(); - infoBean.setDeviceMAC(macStr); - infoBean.setIPMAC(macStr); - infoBean.setDeviceName("HDL闈㈡澘"); - infoBean.setDeviceModel("HDL"); - infoBean.setAccessMode("WIFI"); - infoBean.setIPAddress(IpUtils.getIP(this)); - - - infoBean.setOID("010105000000FE08"); - infoBean.setSid("010105000000FE08110100000000"); -// infoBean.set - AuthenticateRequest.VersionBean[] versionBeans = new AuthenticateRequest.VersionBean[]{new AuthenticateRequest.VersionBean("FW", versionString), new AuthenticateRequest.VersionBean("HW", "1956F")}; - infoBean.setVersions(versionBeans); - AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, infoBean, authbean); - - String ip = IpUtils.getBroadcastAddress(); -// ip = "192.168.10.102"; - - HDLLink.getInstance().sendAuthenticateRequest(ip, request, new HDLAuthSocket.CallBack() { + //HDLLinkConfig.getInstance().getDeviceInfoBean()杩欎釜鍒濆鍖栫殑鏃跺�欒鍏堣缃ソ + AuthenticateRequest request = new AuthenticateRequest(IdUtils.getUUId(), time, HDLLinkConfig.getInstance().getDeviceInfoBean(), authbean); + HDLLink.getInstance().startAuthenticateRequest(request, new HDLLinkCallBack() { @Override - public void onError(String error) { + 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 +473,414 @@ return hex.toString(); } + + /** + * 濡傛灉宸茬粡璁よ瘉鍚庯紝鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎 + * 骞挎挱鎼滅储璁よ瘉杩囩殑缃戝叧鏄惁鍦ㄧ嚎 + */ + void searchGatewayBroadcast() { + tv.setText("鎼滅储缃戝叧涓�..."); + responseTv.setText(""); + HDLLinkConfig.getInstance().getGatewayId(); + HDLLink.getInstance().searchGatewayBroadcast(new HDLAuthSocket.SearchGatewayCallBack() { + @Override + public void onError(HDLLinkException e) { + tv.setText("缃戝叧涓嶅湪绾�"); + responseTv.setText(e.getMsg()); + } + + @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(""); + HDLLink.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("鍔熻兘灞炴�ц鍙�"); + responseTv.setText(""); + List<String> sids = new ArrayList<>(); + sids.add(testLightSid); + HDLLink.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("璇诲彇鐘舵�佷腑..."); + responseTv.setText(""); + List<String> list = new ArrayList<>(); + list.add(testLightSid);//瑕佽鍙栬澶囩殑sid + HDLLink.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("鎺у埗璁惧"); + responseTv.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("璇诲彇鍦烘櫙鍒楄〃"); + responseTv.setText(""); + HDLLink.getInstance().getSceneList(new HDLLinkCallBack() { + @Override + public void onSuccess(String msg) { + responseTv.setText(msg); + Log.d("panlili", "MainActivity.java:getSceneList-----> " + 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 getScene() { + tv.setText("鍦烘櫙璇诲彇"); + responseTv.setText(""); + //鍦烘櫙sid鍒楄〃锛屾敮鎸佹壒閲忚鍙� + List<String> sids = new ArrayList<>(); + sids.add("0201034C50B53F0A0A0100011100"); + sids.add("0201037F50B53F0A0A0100021100"); + HDLLink.getInstance().getScene(sids, new HDLLinkCallBack() { + @Override + public void onSuccess(String msg) { + responseTv.setText(msg); + } + + @Override + public void onError(HDLLinkException e) { + responseTv.setText(e.getMsg()); + } + }); + } + + /** + * 鎺у埗鍦烘櫙 + * 鎵ц鎴愬姛鐨勮瘽 鍝嶅簲code涓�200 + */ + void controlScene() { + tv.setText("鍦烘櫙鎺у埗"); + responseTv.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()); + } + }); + } + + + void startDevicesListActivity() { + Intent intent = new Intent(this, DevicesListActivity.class); + startActivity(intent); + } + + /** + * TCP鍙戦�� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� + */ + 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); + } + + /** + * UDP鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙� + */ + 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); + } + + private String getPropertyDownBodyStr() { + 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); + String time = String.valueOf(System.currentTimeMillis()); + + final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>(); + data.setId(IdUtils.getUUId()); + data.setTime_stamp(time); + data.setObjects(requestList); + + return GsonConvert.getGson().toJson(data); + } + + /** + * UDP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� + */ + 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() { + @Override + public void onSuccess(LinkResponse msg) { + Log.i("udpSendWithCallback", "udpSendWithCallback"); + responseTv.setText(GsonConvert.getGson().toJson(msg)); + } + + @Override + public void onError(HDLLinkException e) { + responseTv.setText(e.getMsg()); + } + }); + } + + /** + * TCP鍙戦�侊紝鐩戝惉涓婚鍥炲锛屽甫閲嶅彂甯﹀洖璋� + */ + 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() { + @Override + public void onSuccess(String msg) { + Log.i("tcpSendWithCallback", "tcpSendWithCallback"); + responseTv.setText(msg); + } + + @Override + public void onError(HDLLinkException e) { + responseTv.setText(e.getMsg()); + } + }); + } + + 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); + } + }); + } + + private void UploadSidList() { + tv.setText("涓婃姤Sid鍒楄〃..."); + responseTv.setText(""); + + //1.璁剧疆璁惧淇℃伅 + AuthenticateRequest.AuthenticateDeviceInfoBean authenticateDeviceInfoBean = HDLLinkConfig.getInstance().getDeviceInfoBean(); + + ListSidRequest request = new ListSidRequest(); + request.setOid(authenticateDeviceInfoBean.getOID()); + request.setName("sid鍚嶇О"); + request.setOmodel(authenticateDeviceInfoBean.getDeviceModel()); + request.setSid("12345678");//褰撳墠sid鐨� + request.setSpk("light.switch");//褰撳墠spk鍚嶇О锛屾牴鎹疄闄呰澶囨潵瀹氫箟 + request.setFrom(authenticateDeviceInfoBean.getOID());//鏉ヨ嚜鍝釜缃戝叧锛屽鏋滄椂Link缃戠粶璁惧锛屽垯濉嚜韬玱id锛涘鏋滄槸椹卞姩鍒欎笉闇�瑕佹瀛楁 + + AttributesRequest attributes = new AttributesRequest(); + attributes.setKey("on_off"); + attributes.setData_type("string"); + List<Object> valueList = new ArrayList<>(); + valueList.add("on"); + valueList.add("off"); + attributes.setValue(valueList); + //娣诲姞鏀寔鐨勫睘鎬у垪琛� + List<AttributesRequest> attributesList = new ArrayList<>(); + request.setAttributes(attributesList); + List<ListSidRequest> sidRequestList = new ArrayList<>(); + sidRequestList.add(request); + HDLLink.getInstance().UploadSidList(sidRequestList, new HDLLinkCallBack() { + @Override + public void onError(HDLLinkException error) { + tv.setText(error.getMsg()); + } + + @Override + public void onSuccess(String data) { + tv.setText("涓婃姤sid鍒楄〃鎴愬姛"); + responseTv.setText(data); + } + }); + } + + /** + * 鑾峰彇APP鐗堟湰 + * + * @param context + * @return + */ + public static String getAppVersionName(Context context) { + String versionName = "0"; + try { + PackageManager packageManager = context.getPackageManager(); + PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0); + versionName = packageInfo.versionName; + if (TextUtils.isEmpty(versionName)) { + versionName = "0"; + } + } catch (Exception e) { + e.printStackTrace(); + } + return versionName; + } + + @Override + protected void onDestroy() { + super.onDestroy(); + removeAllTopicsListener(); + } + } \ No newline at end of file -- Gitblit v1.8.0