HDLSDK/app/src/main/java/com/hdl/hdlsdk/MainActivity.java
@@ -37,6 +37,7 @@ 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.config.HDLLinkConfig; import com.hdl.sdk.connect.socket.HDLAuthSocket; import com.hdl.sdk.connect.socket.HDLSocket; import com.hdl.sdk.connect.bean.request.DeviceControlRequest; @@ -246,9 +247,16 @@ tv.setText("开始入网认证..."); //认证提交参数准备 String spkStr = "screen.mirror";//产品spk String macStr = "AA000000000000AC";//设备唯一MAC地址 String secret = "ee62124c151b737c";//通过spk和mac提交云端认证后分配的secret //测试服务 String spkStr = "ir.module";//产品spk String macStr = "AA000000000000BB";//设备唯一MAC地址 String secret = "87ae414b7a853f65";//通过spk和mac提交云端认证后分配的secret //正式服务器 // String spkStr = "screen.mirror";//产品spk // String macStr = "AA000000000000AC";//设备唯一MAC地址 // String secret = "ee62124c151b737c";//通过spk和mac提交云端认证后分配的secret String mac_key = stringToMD5(stringToMD5(macStr + secret)); String versionString = "HDL_V1.0.1";// String time = String.valueOf(System.currentTimeMillis()); @@ -489,21 +497,39 @@ * TCP发送 只发一次,不监听回复,不重发 */ private void tcpSend() { String time = String.valueOf(System.currentTimeMillis()); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("id", IdUtils.getUUId()); jsonObject.addProperty("time_stamp", time); HDLLink.getInstance().tcpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString()); String propertyDownTopic = String.format(TopicConstant.PROPERTY_DOWN, HDLLinkConfig.getInstance().getGatewayId()); String bodyStr = getPropertyDownBodyStr(); HDLLink.getInstance().tcpSendMsg(propertyDownTopic, bodyStr); } /** * 只发一次,不监听回复,不重发 */ private void udpSend() { 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()); JsonObject jsonObject = new JsonObject(); jsonObject.addProperty("id", IdUtils.getUUId()); jsonObject.addProperty("time_stamp", time); HDLLink.getInstance().udpSendMsg(TopicConstant.GATEWAY_SEARCH, jsonObject.toString()); final BaseLocalResponse<List<DeviceControlRequest>> data = new BaseLocalResponse<>(); data.setId(IdUtils.getUUId()); data.setTime_stamp(time); data.setObjects(requestList); return GsonConvert.getGson().toJson(data); } } HDLSDK/hdl-common/src/main/java/com/hdl/sdk/common/exception/HDLLinkCode.java
@@ -15,6 +15,7 @@ HDL_UNAUTHORIZED_ERROR(-2005,"未认证,请先认证"), HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED(-2006,"认证失败,网关未注册到云端"), HDL_SEARCH_GATEWAY_TIMEOUT_ERROR(-2007,"搜索网关失败,超时"), HDL_AUTH_MAC_KEY_ERROR_(-2008,"认证失败,该MAC对应的设备密钥不存在"), HDL_GET_DEVICE_LIST_ERROR(-2100,"获取设备列表失败"), HDL_GET_FUNCTION_LIST_ERROR(-2101,"获取功能列表失败"), HDL_GET_FUNCTION_PROPERTIES_ERROR(-2102,"获取功能属性失败"), HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -178,30 +178,39 @@ String requestStr = gs.toJson(request); LinkRequest message = new LinkRequest(topic, requestStr); HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message,new HdlSocketHelper.HdlSocketListener() { HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { @Override public void onSucceed(Object msg) { if (callBack == null) return; try { AuthenticateResponse bean = getAuthenticateResponseBean(msg); if (bean != null) { String localSecret = ""; String gatewayId = ""; String ipAddress = ""; if (bean.getAuth() != null) { localSecret = bean.getAuth().getLocalSecret(); } if (bean.getObjects() != null) { gatewayId = bean.getObjects().getGatewayID(); ipAddress = bean.getObjects().getIPAddress(); } //判断网关是否已经注册到云端 if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) { //认证失败,网关未注册到云端 callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED)); } else { HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress); callBack.onSuccess("认证成功"); if (bean.getCode().equals("200")) { String localSecret = ""; String gatewayId = ""; String ipAddress = ""; if (bean.getAuth() != null) { localSecret = bean.getAuth().getLocalSecret(); } if (bean.getObjects() != null) { gatewayId = bean.getObjects().getGatewayID(); ipAddress = bean.getObjects().getIPAddress(); } //判断网关是否已经注册到云端 if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) { //认证失败,网关未注册到云端 callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR_GATEWAY_NOT_REGISTERED)); } else { HDLLinkConfig.getInstance().saveConfig(localSecret, gatewayId, ipAddress); callBack.onSuccess("认证成功"); } } else if (bean.getCode().equals("14013")){ //认证失败,该MAC对应的设备密钥不存在 callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_MAC_KEY_ERROR_)); } else{ //认证失败,错误码: LogUtils.e("认证失败,错误码:" + bean.getCode()); callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR)); } } else { callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR)); HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HdlSocketHelper.java
@@ -197,7 +197,9 @@ } } } }, sendAwaitTime, sendAwaitTime, TimeUnit.MILLISECONDS); }, 0, sendAwaitTime, TimeUnit.MILLISECONDS); //initialdelay - 首次执行的延迟时间 0 //delay - 一次执行终止和下一次执行开始之间的延迟 } /**