HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -13,9 +13,15 @@ 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.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; @@ -25,6 +31,9 @@ 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; @@ -46,8 +55,8 @@ tv = findViewById(R.id.state_tv); rv = findViewById(R.id.rv); rv.setLayoutManager(new LinearLayoutManager(this)); HDLLinkConfig.getInstance().setLocalEncrypt(true); HDLLinkConfig.getInstance().setLocalSecret("5648d9b9cafd30dd"); checkIfCertified(); ActivityResultLauncher<String[]> launcher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { @Override @@ -66,6 +75,7 @@ beans.add(new DemoBean("状态上报")); beans.add(new DemoBean("读取状态")); beans.add(new DemoBean("入网认证")); beans.add(new DemoBean("搜索指定网关是否在线")); demoAdapter = new DemoAdapter(beans); rv.setAdapter(demoAdapter); @@ -148,33 +158,24 @@ // 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; @@ -182,5 +183,93 @@ } }); } 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(); } } HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/HDLLink.java
@@ -67,4 +67,12 @@ HDLAuthSocket.getInstance().sendAuthenticateRequest(ip,request,callBack); } /** * 搜索指定网关是否在线,搜索到则返回指定的网关对象 * * @param callBack 回调 */ public void searchGateway(HDLAuthSocket.SearchGatewayCallBack callBack) { HDLAuthSocket.getInstance().searchGateway(HDLLinkConfig.getInstance().getGatewayId(), callBack); } } HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/AuthenticateRequest.java
@@ -89,12 +89,12 @@ private String device_mac;//设备的Mac private String device_name;// private String device_model; private String access_mode = "WIFI"; private String access_mode; private String sid; private String ip_mac; private String ip_address = "192.16.10.104"; private String ip_address; private String netmask = "255.255.255.0"; private String ip_gateway = "192.16.10.1"; private String ip_gateway = "192.168.10.1"; private String dns1 = "114.114.114.114"; private String dns2 = "8.8.8.8"; private VersionBean[] versions; HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/bean/AuthenticateResponseBean.java
@@ -9,7 +9,7 @@ */ public class AuthenticateResponseBean implements Serializable { private String id; private String timeStamp; private String time_stamp; private String code; private GatewayObjects objects; private Auth auth; @@ -17,8 +17,8 @@ public String getID() { return id; } public void setID(String value) { this.id = value; } public String getTimeStamp() { return timeStamp; } public void setTimeStamp(String value) { this.timeStamp = value; } public String getTimeStamp() { return time_stamp; } public void setTimeStamp(String value) { this.time_stamp = value; } public String getCode() { return code; } public void setCode(String value) { this.code = value; } HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/config/HDLLinkConfig.java
@@ -4,6 +4,7 @@ import com.hdl.sdk.common.config.TopicConstant; import com.hdl.sdk.common.utils.SPUtils; import com.hdl.sdk.connect.bean.GatewaySearchBean; /** @@ -20,6 +21,7 @@ private String gatewayId; private String ipAddress; private boolean isLocalEncrypt;//网关是否需要加密通讯 private GatewaySearchBean currentGateway;//当前网关 /** * instance @@ -83,8 +85,10 @@ * @return */ public boolean checkIfCertified(){ //localSecret不为空并且长度等于16 return !TextUtils.isEmpty(localSecret) && localSecret.length() == 16; //localSecret不为空并且长度等于16 网关id不能为空 return !TextUtils.isEmpty(localSecret) && localSecret.length() == 16 && !TextUtils.isEmpty(gatewayId); } public void setLocalSecret(String localSecret) { @@ -112,6 +116,14 @@ isLocalEncrypt = localEncrypt; } public GatewaySearchBean getCurrentGateway() { return currentGateway; } public void setCurrentGateway(GatewaySearchBean currentGateway) { this.currentGateway = currentGateway; } /** * 判断当前主题数据是否需要加密 * @param topicStr 当前主题 @@ -126,6 +138,13 @@ ); } /** * 拼接网关ID获取完整的主题 * @param topicStr * @return */ public String getFullTopic(String topicStr){ return String.format(topicStr, gatewayId); } } HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -6,6 +6,9 @@ import com.google.gson.Gson; import com.google.gson.JsonObject; import com.google.gson.reflect.TypeToken; import com.hdl.sdk.common.event.EventDispatcher; import com.hdl.sdk.common.event.EventListener; import com.hdl.sdk.connect.bean.AuthenticateResponseBean; import com.hdl.sdk.connect.config.HDLLinkConfig; import com.hdl.sdk.common.config.TopicConstant; import com.hdl.sdk.common.utils.IdUtils; @@ -52,7 +55,7 @@ void onError(String error); void onSuccess(String data); void onSuccess(String msg); } @@ -90,12 +93,25 @@ return updBoot; } private String getOid() { return "000101EA9B8C1C22"; } /** * 开始入网及认证请求 * * @param request 认证请求信息 * @param callBack 结果回调 */ public void startAuthenticateRequest(AuthenticateRequest request, CallBack callBack) { //1.启动Socket 开启监听 getUdpBoot(IpUtils.getBroadcastAddress()); //监听网关广播的入网指令 EventDispatcher.getInstance().register(TopicConstant.GATEWAY_AUTH_BROADCAST, new EventListener() { @Override public void onMessage(Object msg) { private String getSid() { return "000101EA9B8C1C2202010001021A"; //移除监听 // EventDispatcher.getInstance().remove(this); } }); } /** @@ -117,59 +133,71 @@ HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() { @Override public void onSucceed(Object msg) { callBack.onSuccess(msg.toString()); if(callBack == null) return; AuthenticateResponseBean bean = getAuthenticateResponseBean(msg); if(bean != null){ String localSecret = bean.getAuth().getLocalSecret(); String gatewayId = bean.getObjects().getGatewayID(); String ipAddress = bean.getObjects().getIPAddress(); HDLLinkConfig.getInstance().saveConfig(localSecret,gatewayId,ipAddress); callBack.onSuccess("Success"); }else{ callBack.onSuccess("参数异常"); } } @Override public void onFailure() { Log.i("TAG", "onFailure: "); if(callBack == null) return; callBack.onError("超时"); } }); } /** * 发送入网及认证请求 * * @param callBack 结果回调 */ public void sendAuthenticateRequest(CallBack callBack) { 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.设置设备信息 AuthenticateDeviceInfoBean infoBean = new AuthenticateDeviceInfoBean(); infoBean.setDeviceMAC(macStr); infoBean.setIPMAC(macStr); infoBean.setDeviceName("HDL面板"); infoBean.setAccessMode("HDL"); infoBean.setOID(getOid()); infoBean.setSid(getSid()); // 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"; sendAuthenticateRequest(ip, request, callBack); } // /** // * 发送入网及认证请求 // * // * @param callBack 结果回调 // */ // public void sendAuthenticateRequest(CallBack callBack) { // 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.设置设备信息 // AuthenticateDeviceInfoBean infoBean = new AuthenticateDeviceInfoBean(); // infoBean.setDeviceMAC(macStr); // infoBean.setIPMAC(macStr); // infoBean.setDeviceName("HDL面板"); // infoBean.setAccessMode("HDL"); // infoBean.setOID(getOid()); // infoBean.setSid(getSid()); //// 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"; // sendAuthenticateRequest(ip, request, callBack); // } public interface SearchGatewayCallBack { /** @@ -222,9 +250,13 @@ @Override public void onSucceed(Object msg) { GatewaySearchBean searchBean = getGatewaySearchBean(msg); if (searchBean != null && searchBean.getGatewayId() == gatewayId) { if (searchBean != null && searchBean.getGatewayId().contains(gatewayId)) { Log.i("TAG", "onSuccess: "); HDLLinkConfig.getInstance().setCurrentGateway(searchBean); HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt()); callBack.onSuccess(searchBean); }else { } } @@ -251,53 +283,43 @@ return searchBean; } private AuthenticateResponseBean getAuthenticateResponseBean(Object msg) { AuthenticateResponseBean mBean = null; if (msg instanceof LinkResponse) { LinkResponse linkResponse = (LinkResponse) msg; String data = linkResponse.getData(); if (!TextUtils.isEmpty(data)) { AuthenticateResponseBean response = GsonConvert.getGson().fromJson(data, new TypeToken<AuthenticateResponseBean>() { }.getType()); mBean = response; } } return mBean; } /** * 设备控制 */ public void propertyDown(List<DeviceControlRequest> request, HDLSocket.CallBack callBack) { // if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) { String time = String.valueOf(System.currentTimeMillis()); final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>(); data.setId(IdUtils.getUUId()); data.setTime_stamp(time); data.setObjects(request); String topic = String.format(TopicConstant.PROPERTY_DOWN, "1406844230123372545"); String topic = HDLLinkConfig.getInstance().getFullTopic(TopicConstant.PROPERTY_DOWN); LinkRequest message = new LinkRequest(topic, GsonConvert.getGson().toJson(data)); // Log.i("TAG", "propertyDown: " + message.toString()); String ip = IpUtils.getBroadcastAddress(); HdlSocketHelper.send(getUdpBoot(ip), message, null); // } } 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(); }