| | |
| | | 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.hdl.sdk.common.config.AuthenticateConfig; |
| | | 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.socket.HDLAuthSocket; |
| | | import com.hdl.sdk.connect.socket.HDLSocket; |
| | | import com.hdl.sdk.connect.bean.DeviceControlRequest; |
| | |
| | | import com.hdl.sdk.socket.SocketOptions; |
| | | import com.hdl.sdk.socket.codec.MessagePipeLine; |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.security.MessageDigest; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | |
| | | tv = findViewById(R.id.state_tv); |
| | | rv = findViewById(R.id.rv); |
| | | rv.setLayoutManager(new LinearLayoutManager(this)); |
| | | AuthenticateConfig.getInstance().setLocalEncrypt(true); |
| | | AuthenticateConfig.getInstance().setLocalSecret("5648d9b9cafd30dd"); |
| | | |
| | | checkIfCertified(); |
| | | |
| | | ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { |
| | | @Override |
| | |
| | | beans.add(new DemoBean("状态上报")); |
| | | beans.add(new DemoBean("读取状态")); |
| | | beans.add(new DemoBean("入网认证")); |
| | | beans.add(new DemoBean("搜索指定网关是否在线")); |
| | | demoAdapter = new DemoAdapter(beans); |
| | | rv.setAdapter(demoAdapter); |
| | | |
| | |
| | | // HDLSocket.getInstance().propertyRead(); |
| | | break; |
| | | case 6: |
| | | //入网认证 |
| | | HDLAuthSocket.getInstance().sendAuthenticateRequest(new HDLAuthSocket.CallBack() { |
| | | // //入网认证 |
| | | sendAuthenticateRequest(); |
| | | break; |
| | | case 7: |
| | | // //如果已经认证后,搜索指定网关是否在线 |
| | | HDLLink.getInstance().searchGateway(new HDLAuthSocket.SearchGatewayCallBack() { |
| | | @Override |
| | | public void onError(String error) { |
| | | Log.i("TAG", "onError: 认证失败"); |
| | | // tv.setText("认证失败"); |
| | | // responseTv.setText(error); |
| | | public void onEnd(String error) { |
| | | tv.setText("网关不在线"); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | tv.setText("认证成功"); |
| | | responseTv.setText(data); |
| | | public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | | tv.setText("网关在线"); |
| | | responseTv.setText("网关id:"+gatewaySearchBean.getGatewayId()); |
| | | // Log.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId()); |
| | | } |
| | | }); |
| | | |
| | | // HDLAuthSocket.getInstance().searchGateway("1406844230123372545", new HDLAuthSocket.SearchGatewayCallBack() { |
| | | // @Override |
| | | // public void onEnd(String error) { |
| | | // |
| | | // } |
| | | // |
| | | // @Override |
| | | // public void onSuccess(GatewaySearchBean gatewaySearchBean) { |
| | | // Log.i("TAG", "onSuccess: 搜索成功:"+gatewaySearchBean.getGatewayId()); |
| | | // } |
| | | // }); |
| | | |
| | | |
| | | break; |
| | |
| | | } |
| | | }); |
| | | } |
| | | public void showToast(String text) { |
| | | Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); |
| | | } |
| | | |
| | | |
| | | void checkIfCertified(){ |
| | | boolean isCertified = HDLLink.getInstance().checkIfCertified(); |
| | | showToast(isCertified?"已经认证过":"未认证"); |
| | | } |
| | | |
| | | /** |
| | | * 入网认证 |
| | | */ |
| | | void sendAuthenticateRequest(){ |
| | | |
| | | String macStr = "AA000000000000BB"; |
| | | String secret = "87ae414b7a853f65"; |
| | | String mac_key = stringToMD5(stringToMD5(macStr + secret)); |
| | | |
| | | String versionString = "HDL_V1.0.1"; |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | |
| | | //1.设置认证信息 |
| | | AuthenticateRequest.RequestBean requestBean = new AuthenticateRequest.RequestBean(); |
| | | requestBean.setMAC(macStr); |
| | | requestBean.setSupplier("HDL"); |
| | | requestBean.setFirmwareVersion(versionString); |
| | | requestBean.setHardwareModel("1956F"); |
| | | AuthenticateRequest.AuthBean authbean = new AuthenticateRequest.AuthBean(); |
| | | authbean.setSpk("ir.module"); |
| | | 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() { |
| | | @Override |
| | | public void onError(String error) { |
| | | Log.i("TAG", "onError: 认证失败"); |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | tv.setText("认证成功"); |
| | | responseTv.setText(data.toString()); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | String stringToMD5(String text) { |
| | | byte[] hash; |
| | | try { |
| | | hash = MessageDigest.getInstance("MD5").digest(text.getBytes("UTF-8")); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } catch (UnsupportedEncodingException e) { |
| | | e.printStackTrace(); |
| | | return null; |
| | | } |
| | | |
| | | StringBuilder hex = new StringBuilder(hash.length * 2); |
| | | for (byte b : hash) { |
| | | if ((b & 0xFF) < 0x10) |
| | | hex.append("0"); |
| | | hex.append(Integer.toHexString(b & 0xFF)); |
| | | } |
| | | |
| | | return hex.toString(); |
| | | } |
| | | } |