From b1072a799774a18b19856d10c9ecfdda96a8b48d Mon Sep 17 00:00:00 2001
From: 562935844@qq.com
Date: 星期五, 15 九月 2023 09:44:38 +0800
Subject: [PATCH] 代码同步
---
HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java | 834 ++++++++++++++++++++++++++++++++++++++++++++++++-----------
1 files changed, 679 insertions(+), 155 deletions(-)
diff --git a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
index f4566ee..595c20b 100644
--- a/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
+++ b/HDLSDK/hdl-connect/src/main/java/com/hdl/sdk/connect/socket/HDLAuthSocket.java
@@ -1,101 +1,234 @@
package com.hdl.sdk.connect.socket;
+import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
+
import android.text.TextUtils;
import android.util.Log;
import com.google.gson.Gson;
+import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
-import com.hdl.sdk.connect.config.HDLLinkConfig;
+import com.hdl.sdk.common.HDLSdk;
import com.hdl.sdk.common.config.TopicConstant;
+import com.hdl.sdk.common.event.EventDispatcher;
+import com.hdl.sdk.common.event.EventListener;
+import com.hdl.sdk.common.exception.HDLLinkCode;
+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.LogUtils;
+import com.hdl.sdk.common.utils.ThreadToolUtils;
import com.hdl.sdk.common.utils.gson.GsonConvert;
-import com.hdl.sdk.connect.bean.AuthenticateRequest;
-import com.hdl.sdk.connect.bean.AuthenticateRequest.AuthenticateDeviceInfoBean;
-import com.hdl.sdk.connect.bean.BaseLocalResponse;
-import com.hdl.sdk.connect.bean.DeviceControlRequest;
-import com.hdl.sdk.connect.bean.GatewaySearchBean;
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.DeviceControlRequest;
+import com.hdl.sdk.connect.bean.request.GatewayInfoRequest;
+import com.hdl.sdk.connect.bean.response.AuthenticateResponse;
+import com.hdl.sdk.connect.bean.response.BaseLocalResponse;
+import com.hdl.sdk.connect.bean.response.GatewaySearchBean;
+import com.hdl.sdk.connect.bean.response.NetworkAccessBroadcastResponse;
+import com.hdl.sdk.connect.callback.BaseCallBack;
+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.protocol.LinkMessageDecoder;
import com.hdl.sdk.connect.protocol.LinkMessageEncoder;
-import com.hdl.sdk.socket.SocketBoot;
-import com.hdl.sdk.socket.SocketOptions;
import com.hdl.sdk.socket.client.UdpClient;
import com.hdl.sdk.socket.codec.MessagePipeLine;
+import com.hdl.sdk.socket.udp.UdpSocketBoot;
+import com.hdl.sdk.socket.udp.UdpSocketOptions;
-import java.io.UnsupportedEncodingException;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
import java.util.List;
-
-import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by jlchen on 11/11/21.
*
- * @Description : HDLAuthSocket
+ * @Description : HDLAuthSocket 鐢变簬鍓嶆湡宸茬粡鍛藉悕濂斤紝涓嶅仛鏇存敼锛屽彲鐢ㄤ綔Udp鏈嶅姟绔娇鐢�
*/
public class HDLAuthSocket {
+ private static final String TAG = "HDLAuth";
/**
* udp榛樿绔彛
*/
private static final int UDP_PORT = 8585;
- private static SocketBoot updBoot;
+ /**
+ * 鍥犱负鑰冭檻鍒颁娇鐢ㄤ竴涓鍙o紝瑕佹敮鎸佹帴鏀跺缃戝叧鐨勬暟鎹紝鎵�浠ュ彧鍏佽浣跨敤涓�涓�
+ */
+ private static UdpSocketBoot udpSocketBoot;
+ // private EventListener authEvent;
+ //鎼滅储缃戝叧
+ private EventListener searchGatewayEvent;
+ /**
+ * udp榛樿缁勬挱ip
+ */
+ private static final String UDP_GROUP_IP = "239.0.168.188";
+
/**
* instance
*/
- private volatile static HDLAuthSocket instance;
+ private static final HDLAuthSocket instance = new HDLAuthSocket();
- public interface CallBack {
-
- void onError(String error);
-
- void onSuccess(String data);
-
+ private HDLAuthSocket() {
+ initListenerGatewayEvent();
+ initSearchGatewayEvent();
}
+
+// public interface CallBack extends BaseCallBack {
+// void onSuccess(String msg);
+// }
/**
* getInstance
*
* @return HDLAuthSocket
*/
- public static synchronized HDLAuthSocket getInstance() {
- if (instance == null) {
- synchronized (HDLLinkConfig.class) {
- if (instance == null) {
- instance = new HDLAuthSocket();
- }
- }
- }
+ public static HDLAuthSocket getInstance() {
+// if (instance == null) {
+// synchronized (HDLAuthSocket.class) {
+// if (instance == null) {
+// instance = new HDLAuthSocket();
+// }
+// }
+// }
return instance;
}
- private SocketOptions getUdpOptions() {
- final SocketOptions options = new SocketOptions();
+ private UdpSocketOptions getUdpOptions() {
+ final UdpSocketOptions options = new UdpSocketOptions();
final MessagePipeLine pipeLine = new MessagePipeLine();
pipeLine.add(new LinkMessageDecoder());
pipeLine.add(new LinkMessageEncoder());
options.setHandleMessage(pipeLine);
- options.setEnabledHeartbeat(false);
return options;
}
- private SocketBoot getUdpBoot(String ip) {
- if (updBoot == null) {
- updBoot = UdpClient.init(ip, UDP_PORT, getUdpOptions());
- updBoot.connect();
+
+ /**
+ * 鑾峰彇褰撳墠udp瀵硅薄锛屽鏋滀笉瀛樺湪灏卞垱寤�
+ *
+ * @return 杩斿洖褰撳墠瀵硅薄
+ */
+ private synchronized UdpSocketBoot getUdpBoot() {
+ try {
+ if (udpSocketBoot == null) {
+ udpSocketBoot = UdpClient.init(UDP_PORT, getUdpOptions());
+ udpSocketBoot.bind();
+
+ LogUtils.i("鍒濆鍖朣ocket udp");
+ }
+ } catch (Exception e) {
+ LogUtils.e("鍒濆鍖朣ocket udp寮傚父" + e.getMessage());
}
- return updBoot;
+
+ return udpSocketBoot;
}
- private String getOid() {
- return "000101EA9B8C1C22";
+ public void init() {
+ getUdpBoot();
}
- private String getSid() {
- return "000101EA9B8C1C2202010001021A";
+ /**
+ * 寮�濮嬬洃鍚拰鍙戣捣鍏ョ綉鍙婅璇佽姹�
+ *
+ * @param request 璁よ瘉璇锋眰淇℃伅
+ * @param callBack 缁撴灉鍥炶皟
+ */
+ public void startAuthenticateRequest(AuthenticateRequest request, HDLLinkCallBack callBack) {
+ HDLLinkConfig.getInstance().clearConfig();
+ //1.鍚姩Socket 寮�鍚洃鍚�
+ getUdpBoot();
+ //2.鏋勫缓鐩戝惉Listener
+// authEvent =
+ //3.鐩戝惉缃戝叧骞挎挱鐨勫叆缃戞寚浠�
+ EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
+ EventDispatcher.getInstance().register(TopicConstant.GATEWAY_AUTH_BROADCAST, new EventListener() {
+ @Override
+ public void onMessage(Object msg) {
+ LogUtils.i("鏀跺埌缃戝叧璁よ瘉淇℃伅: ");
+ NetworkAccessBroadcastResponse bean = getNetworkAccessBroadcastResponse(msg);
+ if (bean != null) {
+ //绉婚櫎鐩戝惉
+ EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
+ LogUtils.i("缃戝叧鍏ョ綉骞挎挱IP: " + bean.getIPAddress());
+ HDLLinkConfig.getInstance().setIpAddress(bean.getIPAddress());
+ String ipStr = bean.getIPAddress();
+ if (!TextUtils.isEmpty(ipStr)) {
+ sendAuthenticateRequest(ipStr, request, callBack);
+ }
+ }
+
+ }
+ });
+ }
+
+ /**
+ * 缁撴潫鐩戝惉鍏ョ綉鍙婅璇佸箍鎾�
+ */
+ public void endAuthenticateRequest() {
+ //绉婚櫎鐩戝惉
+ EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_AUTH_BROADCAST);
+ }
+
+ /**
+ * 涓婃姤浠庣綉鍏充俊鎭�
+ *
+ * @param callBack 鍥炶皟
+ */
+ public void UploadGatewayInfo(HDLLinkCallBack callBack) {
+ String time = String.valueOf(System.currentTimeMillis());
+ final BaseLocalResponse<GatewayInfoRequest> data = new BaseLocalResponse<>();
+ data.setId(IdUtils.getUUId());
+ data.setTime_stamp(time);
+
+ final GatewayInfoRequest request = new GatewayInfoRequest();
+ AuthenticateRequest.AuthenticateDeviceInfoBean authenticateDeviceInfoBean = HDLLinkConfig.getInstance().getDeviceInfoBean();
+ request.setDevice_model(authenticateDeviceInfoBean.getDeviceModel());
+ request.setDevice_mac(authenticateDeviceInfoBean.getDeviceMAC());
+ request.setDevice_name(authenticateDeviceInfoBean.getDeviceName());
+ request.setGateway_type(authenticateDeviceInfoBean.getGateway_type());
+ request.setAccess_mode(authenticateDeviceInfoBean.getAccessMode());
+ request.setOid(authenticateDeviceInfoBean.getOID());
+ request.setSid(authenticateDeviceInfoBean.getSid());
+ request.setSupplier(authenticateDeviceInfoBean.getSupplier());
+
+ if (authenticateDeviceInfoBean.getSupplier() == null) {
+ AuthenticateRequest.RequestBean requestBean = HDLLinkConfig.getInstance().getRequestBean();
+ request.setSupplier(requestBean.getSupplier());
+ }
+
+ request.setHw_version(authenticateDeviceInfoBean.getHw_version());
+ request.setFw_version(HDLSdk.getInstance().getVersion());//sdk鐗堟湰
+ request.setIp_address(authenticateDeviceInfoBean.getIPAddress());
+ request.setMaster("false");
+ request.setLocalEncrypt(false);
+
+ data.setObjects(request);
+
+ String topic = HDLLinkConfig.getInstance().getFullTopic(TopicConstant.GATEWAY_SEARCH_REPLY);
+ LinkRequest message = new LinkRequest(topic,
+ GsonConvert.getGson().toJson(data));
+
+ String ip = IpUtils.getBroadcastAddress();
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
+ @Override
+ public void onSucceed(Object msg) {
+ if (callBack == null) return;
+ callBack.onSuccess("涓婃姤鎴愬姛");
+ LogUtils.i("UploadGatewayInfo onSucceed = " + msg);
+ }
+
+ @Override
+ public void onFailure() {
+ if (callBack == null) return;
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UPLOAD_GATEWAYINFO_FAILURE_ERROR));
+ LogUtils.i("UploadGatewayInfo onFailure");
+ }
+ });
+
}
/**
@@ -105,80 +238,191 @@
* @param request 璁よ瘉璇锋眰淇℃伅
* @param callBack 缁撴灉鍥炶皟
*/
- public void sendAuthenticateRequest(String ip, AuthenticateRequest request, CallBack callBack) {
+ public void sendAuthenticateRequest(String ip, AuthenticateRequest request, HDLLinkCallBack callBack) {
if (request == null) {
- callBack.onError("request null");
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR));
}
String topic = DEIVCE_AUTH_REQUEST;
Gson gs = new Gson();
String requestStr = gs.toJson(request);
LinkRequest message = new LinkRequest(topic, requestStr);
- HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() {
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
@Override
public void onSucceed(Object msg) {
- callBack.onSuccess(msg.toString());
+ if (callBack == null) return;
+ try {
+ AuthenticateResponse bean = getAuthenticateResponseBean(msg);
+ if (bean != null) {
+ if (bean.getCode().equals("200") || bean.getCode().equals("0")) {
+ 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 (ipAddress == null) {
+ ipAddress = bean.getIp_address();
+ }
+ HDLLinkConfig.getInstance().setAuthBean(request.getAuth());
+
+ //鍒ゆ柇缃戝叧鏄惁宸茬粡娉ㄥ唽鍒颁簯绔�
+ Log.d("panlili", "璁よ瘉鎴愬姛----->localSecret= " + localSecret);
+ 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);
+ HDLLinkConfig.getInstance().setRequestBean(request.getAuth().getRequest());
+ callBack.onSuccess("璁よ瘉鎴愬姛");
+
+// UploadDeviceAuth(request.getObjects().getDeviceMAC(), "0", "success", bean.getCode(), null);
+ }
+ } else if (bean.getCode().equals("14013")) {
+ //璁よ瘉澶辫触锛岃MAC瀵瑰簲鐨勮澶囧瘑閽ヤ笉瀛樺湪
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_MAC_KEY_ERROR_));
+
+// UploadDeviceAuth(request.getObjects().getDeviceMAC(), "-1", "fail", bean.getCode(), null);
+ } else if (bean.getCode().equals("124013")) {
+ //璁よ瘉澶辫触锛岃MAC瀵瑰簲鐨勮澶囧瘑閽ヤ笉瀛樺湪
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_124013_ERROR));
+
+ } else if (bean.getCode().equals("124015")) {
+ //璁よ瘉澶辫触锛岃澶囧巶瀹朵笉鍖归厤
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_124015_ERROR));
+
+ } else {
+ //璁よ瘉澶辫触锛岄敊璇爜锛�
+ LogUtils.e("璁よ瘉澶辫触锛岄敊璇爜锛�" + bean.getCode());
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_AUTH_ERROR));
+
+// UploadDeviceAuth(request.getObjects().getDeviceMAC(), "-1", "fail", bean.getCode(), null);
+ }
+ } else {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
+ }
+ } catch (Exception e) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
+ }
}
@Override
public void onFailure() {
- Log.i("TAG", "onFailure: ");
- callBack.onError("瓒呮椂");
+ LogUtils.i(TAG, "onFailure: ");
+ if (callBack == null) return;
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
}
});
}
- /**
- * 鍙戦�佸叆缃戝強璁よ瘉璇锋眰
- *
- * @param callBack 缁撴灉鍥炶皟
- */
- public void sendAuthenticateRequest(CallBack callBack) {
- String macStr = "AA000000000000BB";
- String secret = "87ae414b7a853f65";
- String mac_key = stringToMD5(stringToMD5(macStr + secret));
+ public void UploadDeviceAuth(String mac, String result, String message, String auth_code, HDLLinkCallBack callBack) {
+ HDLSocket.getInstance().UploadDeviceAuth(mac, result, message, auth_code, new HDLLinkCallBack() {
+ @Override
+ public void onSuccess(String msg) {
+ if (callBack != null) {
+ callBack.onSuccess(msg);
+ }
+ LogUtils.i(TAG, "onSuccess: " + msg);
+ }
- 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);
+ @Override
+ public void onError(HDLLinkException e) {
+ if (callBack != null) {
+ callBack.onError(e);
+ }
+ LogUtils.i(TAG, "onError: " + e.getMsg() + " " + e.getCode());
+ }
+ });
}
- public interface SearchGatewayCallBack {
- /**
- * 鎼滅储缁撴潫
- *
- * @param error
- */
- void onEnd(String error);
+ public void linkBroadcast(String oid, HDLLinkCallBack callBack) {
+ if (TextUtils.isEmpty(oid)) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR));
+ }
+
+ Gson gs = new Gson();
+ JsonArray jsonArray = new JsonArray();
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("oid", oid);
+ jsonArray.add(jsonObject);
+ String requestStr = gs.toJson(jsonArray);
+
+ String topic = String.format(TopicConstant.LINK_BROADCAST, HDLLinkConfig.getInstance().getDeviceInfoBean().getOID());
+ LinkRequest message = new LinkRequest(topic, requestStr);
+
+ String ip = IpUtils.getBroadcastAddress();
+
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
+ @Override
+ public void onSucceed(Object msg) {
+ if (callBack == null) return;
+ try {
+ callBack.onSuccess("鏇存柊鎴愬姛");
+ LogUtils.i(TAG, "onSucceed: msg= " + msg);
+ } catch (Exception e) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_ERROR));
+ }
+ }
+
+ @Override
+ public void onFailure() {
+ LogUtils.i(TAG, "onFailure: ");
+ if (callBack == null) return;
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
+ }
+ });
+ }
+
+
+// /**
+// * 鍙戦�佸叆缃戝強璁よ瘉璇锋眰
+// *
+// * @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 extends BaseCallBack {
/**
* 鎼滅储缃戝叧鎴愬姛
*
@@ -204,36 +448,128 @@
}
/**
- * 鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎锛屾悳绱㈠埌鍒欒繑鍥炴寚瀹氱殑缃戝叧瀵硅薄
+ * 缁勬挱鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎锛屾悳绱㈠埌鍒欒繑鍥炴寚瀹氱殑缃戝叧瀵硅薄
*
- * @param gatewayId 缃戝叧id
- * @param callBack 鍥炶皟
+ * @param callBack 鍥炶皟
*/
- public void searchGateway(String gatewayId, SearchGatewayCallBack callBack) {
- String time = String.valueOf(System.currentTimeMillis());
- JsonObject jsonObject = new JsonObject();
- jsonObject.addProperty("id", IdUtils.getUUId());
- jsonObject.addProperty("time_stamp", time);
- String ip = IpUtils.getBroadcastAddress();
- LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
- jsonObject.toString());
+ public void searchGatewayMulticast(SearchGatewayCallBack callBack) {
+ searchGateway(HDLLinkConfig.getInstance().getGatewayId(), UDP_GROUP_IP, UDP_PORT, callBack);
+ }
- HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() {
+ /**
+ * 缁勬挱鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎锛屾悳绱㈠埌鍒欒繑鍥炴寚瀹氱殑缃戝叧瀵硅薄
+ *
+ * @param callBack 鍥炶皟
+ */
+ public void searchGatewayBroadcast(SearchGatewayCallBack callBack) {
+ if (!HDLLinkConfig.getInstance().checkIfCertified()) {
+ if (callBack != null) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
+ }
+ return;
+ }
+ String ip = IpUtils.getBroadcastAddress();
+ searchGateway(HDLLinkConfig.getInstance().getGatewayId(), ip, UDP_PORT, callBack);
+ }
+
+
+ /**
+ * 璁惧鎺у埗
+ */
+ public void propertyDown(List<DeviceControlRequest> request, HDLLinkCallBack callBack) {
+ 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 = HDLLinkConfig.getInstance().getFullTopic(TopicConstant.PROPERTY_DOWN);
+ LinkRequest message = new LinkRequest(topic,
+ GsonConvert.getGson().toJson(data));
+
+ String ip = IpUtils.getBroadcastAddress();
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
@Override
public void onSucceed(Object msg) {
- GatewaySearchBean searchBean = getGatewaySearchBean(msg);
- if (searchBean != null && searchBean.getGatewayId() == gatewayId) {
- Log.i("TAG", "onSuccess: ");
- callBack.onSuccess(searchBean);
- }
+ if (callBack == null) return;
+ callBack.onSuccess("鎺у埗鎴愬姛");
}
@Override
public void onFailure() {
- Log.i("TAG", "onFailure: ");
- callBack.onEnd("瓒呮椂");
+ if (callBack == null) return;
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_CONTROL_FAILURE_ERROR));
}
});
+
+ }
+
+ /**
+ * 閫氱敤鍙戦�佹寚浠�
+ * 1绉掓病鍝嶅簲灏辫浠栭噸鏂板彂閫�,閲嶈瘯3娆�
+ *
+ * @param topic 鍙戦�佹暟鎹�
+ * @param bodyStr 鍥炲鐨勪富棰�
+ * @param broadcast 鏄惁瑕佸箍鎾�
+ * @param callBack 鍥炶皟
+ */
+ public void udpSendMsg(String topic, String bodyStr, boolean broadcast, HDLLinkResponseCallBack callBack) {
+ if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
+ if (callBack != null) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_DATA_NULL_ERROR));
+ }
+ return;
+ }
+
+ if (!HDLLinkConfig.getInstance().checkIfCertified()) {
+ if (callBack != null) {
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_UNAUTHORIZED_ERROR));
+ }
+ return;
+ }
+
+ LinkRequest message = new LinkRequest(topic, bodyStr);
+ String ip = HDLLinkConfig.getInstance().getIpAddress();
+ if (broadcast) {
+ ip = IpUtils.getBroadcastAddress();
+ }
+ HdlSocketHelper.sendUdp(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() {
+ @Override
+ public void onSucceed(Object msg) {
+ if (callBack == null) return;
+ callBack.onSuccess((LinkResponse) msg);
+ }
+
+ @Override
+ public void onFailure() {
+ if (callBack == null) return;
+ callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR));
+ }
+ }
+ );
+ }
+
+ /**
+ * 閫氱敤鍙戦�佹寚浠� 鍙彂涓�娆★紝涓嶇洃鍚洖澶嶏紝涓嶉噸鍙�
+ *
+ * @param topic 鍙戦�佹暟鎹�
+ * @param bodyStr 鍥炲鐨勪富棰�
+ * @param broadcast 鏄惁骞挎挱
+ */
+ public void udpSendMsg(String topic, String bodyStr, boolean broadcast) {
+ if (TextUtils.isEmpty(topic) || TextUtils.isEmpty(bodyStr)) {
+ LogUtils.e("鍙傛暟涓嶈兘涓虹┖锛屼笉鑳藉彂閫乁DP鏁版嵁");
+ return;
+ }
+ if (!HDLLinkConfig.getInstance().checkIfCertified()) {
+ LogUtils.e("鏈璇侊紝璇峰厛璁よ瘉鍐嶈皟鐢║DP鍙戦�佹柟娉�");
+ return;
+ }
+ LinkRequest message = new LinkRequest(topic, bodyStr);
+ String ip = HDLLinkConfig.getInstance().getIpAddress();
+ if (broadcast) {
+ ip = IpUtils.getBroadcastAddress();
+ }
+ HdlSocketHelper.sendUdpOne(getUdpBoot(), ip, UDP_PORT, message);
}
private GatewaySearchBean getGatewaySearchBean(Object msg) {
@@ -251,55 +587,243 @@
return searchBean;
}
+ private AuthenticateResponse getAuthenticateResponseBean(Object msg) {
+ AuthenticateResponse mBean = null;
+ if (msg instanceof LinkResponse) {
+ LinkResponse linkResponse = (LinkResponse) msg;
+ String data = linkResponse.getData();
+ if (!TextUtils.isEmpty(data)) {
+ AuthenticateResponse response = GsonConvert.getGson().fromJson(data, new TypeToken<AuthenticateResponse>() {
+ }.getType());
+ mBean = response;
+ }
+
+ }
+ return mBean;
+ }
+
+ private NetworkAccessBroadcastResponse getNetworkAccessBroadcastResponse(Object msg) {
+ NetworkAccessBroadcastResponse mBean = null;
+ if (msg instanceof LinkResponse) {
+ LinkResponse linkResponse = (LinkResponse) msg;
+ String data = linkResponse.getData();
+ if (!TextUtils.isEmpty(data)) {
+ NetworkAccessBroadcastResponse response = GsonConvert.getGson().fromJson(data, new TypeToken<NetworkAccessBroadcastResponse>() {
+ }.getType());
+ mBean = response;
+ }
+
+ }
+ return mBean;
+ }
+
/**
- * 璁惧鎺у埗
+ * 缃戝叧鎼滅储鐩稿叧
*/
- public void propertyDown(List<DeviceControlRequest> request, HDLSocket.CallBack callBack) {
+ private static final int MAX_SEARCH_COUNT = 10;//鎬诲叡鎼滅储娴嬭瘯
+ private final AtomicInteger searchGatewayCount = new AtomicInteger(0);
+ ;
+ private final AtomicBoolean isSearchGatewaySuccess = new AtomicBoolean(true);
+ private String searchGatewayId = "";
+ private SearchGatewayCallBack mSearchGatewayCallBack;
-// if (!TextUtils.isEmpty(getGatewayId()) && !TextUtils.isEmpty(getTcpIp())) {
+ private void initSearchGatewayEvent() {
+ LogUtils.i("鎼滅储缃戝叧", "initSearchGatewayEvent");
+ searchGatewayEvent = new EventListener() {
+ @Override
+ public void onMessage(Object msg) {
+ try {
+ 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());
+ GatewaySearchBean searchBean = response.getObjects();
+
+ if (searchBean != null && !TextUtils.isEmpty(searchBean.getGatewayId())) {
+ searchBean.setIp_address(linkResponse.getSource_ipAddress());
+ if (searchBean.getGatewayId().contains(searchGatewayId) && !TextUtils.isEmpty(searchGatewayId)) {
+ removeSearchGatewayEvent();//绉婚櫎鎼滅储缃戝叧鐩戝惉
+ isSearchGatewaySuccess.set(true);//鎼滅储鎴愬姛鏍囪
+ searchGatewayCount.set(11);//娆℃暟鏍囪
+ HDLLinkConfig.getInstance().setCurrentGateway(searchBean);//璁剧疆褰撳墠缃戝叧
+ if (mSearchGatewayCallBack != null) {
+ mSearchGatewayCallBack.onSuccess(searchBean);
+ }
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ LogUtils.i("panlili锛�" + e.toString());
+ }
+ }
+ };
+ }
+
+ private void initListenerGatewayEvent() {
+ LogUtils.i("鍒濆鍖栨案涔呯洃鍚綉鍏冲箍鎾簨浠讹紝涓嶇敤绉婚櫎姝や簨浠�");
+ EventListener gatewayEvent = new EventListener() {
+ @Override
+ public void onMessage(Object msg) {
+ try {
+ if (msg instanceof LinkResponse) {
+ LinkResponse linkResponse = (LinkResponse) msg;
+ String data = linkResponse.getData();
+ LogUtils.i("鎺ユ敹鍒扮綉鍏充俊鎭細" + data);
+ if (!TextUtils.isEmpty(data)) {
+ final BaseLocalResponse<GatewaySearchBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewaySearchBean>>() {
+ }.getType());
+ GatewaySearchBean gateway = response.getObjects();
+ if (gateway != null && !TextUtils.isEmpty(gateway.getGatewayId())) {
+ //鍙兘缃戝叧甯﹁繃鏉ョ殑ip涓嶅
+ gateway.setIp_address(linkResponse.getSource_ipAddress());
+ //涓荤綉鍏冲苟涓旀槸褰撳墠缁戝畾鐨勭綉鍏�
+ if ("true".equals(gateway.getMaster().toLowerCase()) && gateway.getGatewayId().equals(HDLLinkConfig.getInstance().getGatewayId())) {
+ HDLLinkConfig.getInstance().setCurrentGateway(gateway);//璁剧疆褰撳墠缃戝叧
+ if (!TextUtils.isEmpty(gateway.getIp_address())) {
+ HDLLinkConfig.getInstance().setIpAddress(gateway.getIp_address());
+ LogUtils.i("鎺ユ敹鍒扮綉鍏充俊鎭細" + HDLLinkConfig.getInstance().getIpAddress());
+ }
+ //鏇存柊褰撳墠缃戝叧鐨勪俊鎭�
+ HDLLinkConfig.getInstance().reSaveConfig();
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ LogUtils.i("panlili锛�" + e.toString());
+ }
+ }
+ };
+ EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, gatewayEvent);
+ }
+
+ /**
+ * 鎼滅储鎸囧畾缃戝叧鏄惁鍦ㄧ嚎锛屾悳绱㈠埌鍒欒繑鍥炴寚瀹氱殑缃戝叧瀵硅薄
+ *
+ * @param gatewayId 缃戝叧id
+ * @param ipAddress 鐩爣鐨処P鍦板潃
+ * @param port 鐩爣鐨勭鍙�
+ * @param callBack 鍥炶皟
+ */
+ public void searchGateway(String gatewayId, String ipAddress, int port, SearchGatewayCallBack callBack) {
+ this.searchGatewayId = gatewayId;
+ this.mSearchGatewayCallBack = callBack;
+ //閲嶇疆鍙傛暟
+ searchGatewayCount.set(0);
+ isSearchGatewaySuccess.set(false);
String time = String.valueOf(System.currentTimeMillis());
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("id", IdUtils.getUUId());
+ jsonObject.addProperty("time_stamp", time);
+ LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
+ jsonObject.toString());
+ //娉ㄥ唽鎼滅储缃戝叧鐩戝惉
+ registerSearchGatewayEvent();
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
+ try {
+ //鎼滅储缃戝叧
+ searchGatewayCount.set(searchGatewayCount.get() + 1);
+ LogUtils.i("鎼滅储缃戝叧", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
+ getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
+ Thread.sleep(1000L);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
- 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");
- 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();
+ if (!isSearchGatewaySuccess.get()) {
+ try {
+ LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
+ //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
+ callBackSearchGatewayTimeout();
+ } catch (Exception e) {
+ }
+ }
+ }
+ }).start();
}
+ /**
+ * 娉ㄥ唽鎼滅储缃戝叧鐩戝惉
+ */
+ private void registerSearchGatewayEvent() {
+ LogUtils.i("鎼滅储缃戝叧", "娉ㄥ唽鎼滅储缃戝叧鐩戝惉");
+ EventDispatcher.getInstance().registerIo(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
+ }
+
+ /**
+ * 绉婚櫎鎼滅储缃戝叧鐩戝惉
+ */
+ private void removeSearchGatewayEvent() {
+ LogUtils.i("鎼滅储缃戝叧", "绉婚櫎鎼滅储缃戝叧鐩戝惉");
+ EventDispatcher.getInstance().remove(TopicConstant.GATEWAY_SEARCH_REPLY, searchGatewayEvent);
+ }
+
+ /**
+ * 鍥炶皟鎼滅储缃戝叧瓒呮椂
+ */
+ private void callBackSearchGatewayTimeout() {
+ removeSearchGatewayEvent();
+ ThreadToolUtils.getInstance().runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ if (mSearchGatewayCallBack != null) {
+ mSearchGatewayCallBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_SEARCH_GATEWAY_TIMEOUT_ERROR));
+ }
+ }
+ });
+ }
+ /**
+ * 鎼滅储鍦ㄧ嚎鎵�鏈夌殑缃戝叧锛屽寘鎷綋鍓嶄綇瀹呯殑鍙婃病鏈夌粦瀹氳繃鍏跺畠浣忓畢鐨勭綉鍏�
+ *
+ * @param ipAddress 鐩爣鐨処P鍦板潃
+ * @param port 鐩爣鐨勭鍙�
+ * @param callBack 鍥炶皟
+ */
+ public void searchGateway(String ipAddress, int port, SearchGatewayCallBack callBack) {
+// this.searchGatewayId = gatewayId;
+// this.mSearchGatewayCallBack = callBack;
+// //閲嶇疆鍙傛暟
+// searchGatewayCount.set(0);
+// isSearchGatewaySuccess.set(false);
+ String time = String.valueOf(System.currentTimeMillis());
+ JsonObject jsonObject = new JsonObject();
+ jsonObject.addProperty("id", IdUtils.getUUId());
+ jsonObject.addProperty("time_stamp", time);
+ LinkRequest message = new LinkRequest(TopicConstant.GATEWAY_SEARCH,
+ jsonObject.toString());
+ //娉ㄥ唽鎼滅储缃戝叧鐩戝惉
+ registerSearchGatewayEvent();
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ while (searchGatewayCount.get() < 10 && (!isSearchGatewaySuccess.get())) {
+ try {
+ //鎼滅储缃戝叧
+ searchGatewayCount.set(searchGatewayCount.get() + 1);
+ LogUtils.i("鎼滅储缃戝叧", "鎼滅储缃戝叧绗�" + searchGatewayCount.get() + "娆�");
+ getUdpBoot().sendMsg(ipAddress, port, message.getSendBytes());
+ Thread.sleep(1000L);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
+ if (!isSearchGatewaySuccess.get()) {
+ //鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�
+ callBackSearchGatewayTimeout();
+ LogUtils.e("鎼滅储缃戝叧", "鎼滅储10娆★紝鎸囧畾缃戝叧閮芥病鍥炲锛屽洖璋冭秴鏃�");
+ }
+ }
+ }).start();
+ }
}
--
Gitblit v1.8.0