| | |
| | | package com.hdl.sdk.connect.socket; |
| | | |
| | | import android.net.ipsec.ike.TunnelModeChildSessionParams; |
| | | import android.text.TextUtils; |
| | | |
| | | import com.google.gson.Gson; |
| | |
| | | import com.hdl.sdk.common.exception.HDLLinkCode; |
| | | import com.hdl.sdk.common.exception.HDLLinkException; |
| | | import com.hdl.sdk.common.utils.LogUtils; |
| | | import com.hdl.sdk.common.utils.ThreadToolUtils; |
| | | import com.hdl.sdk.connect.bean.response.AuthenticateResponse; |
| | | import com.hdl.sdk.connect.bean.response.NetworkAccessBroadcastResponse; |
| | | import com.hdl.sdk.connect.callback.BaseCallBack; |
| | |
| | | |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.util.List; |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | import static com.hdl.sdk.common.config.TopicConstant.DEIVCE_AUTH_REQUEST; |
| | | |
| | |
| | | private static final int UDP_PORT = 8585; |
| | | private static SocketBoot updBoot; |
| | | // private EventListener authEvent; |
| | | //搜索网关 |
| | | private EventListener searchGatewayEvent; |
| | | /** |
| | | * udp默认组播ip |
| | | */ |
| | |
| | | * instance |
| | | */ |
| | | private volatile static HDLAuthSocket instance; |
| | | |
| | | private HDLAuthSocket() { |
| | | initSearchGatewayEvent(); |
| | | } |
| | | |
| | | // public interface CallBack extends BaseCallBack { |
| | | // void onSuccess(String msg); |
| | |
| | | |
| | | /** |
| | | * 组播搜索指定网关是否在线,搜索到则返回指定的网关对象 |
| | | * |
| | | * @param callBack 回调 |
| | | */ |
| | | public void searchGatewayMulticast(SearchGatewayCallBack callBack) { |
| | |
| | | |
| | | /** |
| | | * 组播搜索指定网关是否在线,搜索到则返回指定的网关对象 |
| | | * |
| | | * @param callBack 回调 |
| | | */ |
| | | public void searchGatewayBroadcast(SearchGatewayCallBack callBack) { |
| | |
| | | searchGateway(HDLLinkConfig.getInstance().getGatewayId(), ip, callBack); |
| | | } |
| | | |
| | | /** |
| | | * 搜索指定网关是否在线,搜索到则返回指定的网关对象 |
| | | * |
| | | * @param gatewayId 网关id |
| | | * @param ip 接收目标的ip地址 |
| | | * @param callBack 回调 |
| | | */ |
| | | public void searchGateway(String gatewayId, String ip, SearchGatewayCallBack callBack) { |
| | | 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()); |
| | | |
| | | HdlSocketHelper.send(getUdpBoot(ip), message, new HdlSocketHelper.HdlSocketListener() { |
| | | @Override |
| | | public void onSucceed(Object msg) { |
| | | GatewaySearchBean searchBean = getGatewaySearchBean(msg); |
| | | if (searchBean != null && searchBean.getGatewayId().contains(gatewayId)) { |
| | | LogUtils.i(TAG, "onSuccess: "); |
| | | HDLLinkConfig.getInstance().setCurrentGateway(searchBean); |
| | | HDLLinkConfig.getInstance().setLocalEncrypt(searchBean.isLocalEncrypt()); |
| | | callBack.onSuccess(searchBean); |
| | | }else { |
| | | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure() { |
| | | LogUtils.i(TAG, "onFailure: "); |
| | | callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_TIMEOUT_ERROR)); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | |
| | |
| | | /** |
| | | * 通用发送指令 |
| | | * 1秒没响应就让他重新发送,重试3次 |
| | | * |
| | | * @param topic 发送数据 |
| | | * @param bodyStr 回复的主题 |
| | | * @param callBack 回调 |
| | |
| | | return mBean; |
| | | } |
| | | |
| | | /** |
| | | * 网关搜索相关 |
| | | */ |
| | | 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; |
| | | 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())) { |
| | | if (searchBean.getGatewayId().contains(searchGatewayId)) { |
| | | removeSearchGatewayEvent();//移除搜索网关监听 |
| | | isSearchGatewaySuccess.set(true);//搜索成功标记 |
| | | searchGatewayCount.set(11);//次数标记 |
| | | if(mSearchGatewayCallBack != null){ |
| | | mSearchGatewayCallBack.onSuccess(searchBean); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 搜索指定网关是否在线,搜索到则返回指定的网关对象 |
| | | * |
| | | * @param gatewayId 网关id |
| | | * @param ip 接收目标的ip地址 |
| | | * @param callBack 回调 |
| | | */ |
| | | public void searchGateway(String gatewayId, String ip, 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(ip).sendMsg(message.getSendBytes()); |
| | | Thread.sleep(1000L); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | if(!isSearchGatewaySuccess.get()){ |
| | | //搜索10次,指定网关都没回复,回调超时 |
| | | callBackSearchGatewayTimeout(); |
| | | LogUtils.e("搜索网关--","搜索10次,指定网关都没回复,回调超时"); |
| | | } |
| | | } |
| | | }).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)); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | } |