| | |
| | | |
| | | /** |
| | | * 获取当前udp对象,如果不存在就创建 |
| | | * |
| | | * @return 返回当前对象 |
| | | */ |
| | | private UdpSocketBoot getUdpBoot() { |
| | |
| | | String requestStr = gs.toJson(request); |
| | | LinkRequest message = new LinkRequest(topic, requestStr); |
| | | |
| | | HdlSocketHelper.send(getUdpBoot(), ip,UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { |
| | | HdlSocketHelper.send(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { |
| | | @Override |
| | | public void onSucceed(Object msg) { |
| | | if (callBack == null) return; |
| | |
| | | String localSecret = ""; |
| | | String gatewayId = ""; |
| | | String ipAddress = ""; |
| | | if(bean.getAuth() != null) { |
| | | if (bean.getAuth() != null) { |
| | | localSecret = bean.getAuth().getLocalSecret(); |
| | | } |
| | | if(bean.getObjects() != null){ |
| | | gatewayId = bean.getObjects().getGatewayID(); |
| | | ipAddress = bean.getObjects().getIPAddress(); |
| | | if (bean.getObjects() != null) { |
| | | gatewayId = bean.getObjects().getGatewayID(); |
| | | ipAddress = bean.getObjects().getIPAddress(); |
| | | } |
| | | //判断网关是否已经注册到云端 |
| | | if (TextUtils.isEmpty(localSecret) || TextUtils.isEmpty(gatewayId)) { |
| | |
| | | * @param callBack 回调 |
| | | */ |
| | | public void searchGatewayMulticast(SearchGatewayCallBack callBack) { |
| | | searchGateway(HDLLinkConfig.getInstance().getGatewayId(), UDP_GROUP_IP,UDP_PORT, callBack); |
| | | searchGateway(HDLLinkConfig.getInstance().getGatewayId(), UDP_GROUP_IP, UDP_PORT, callBack); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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); |
| | | searchGateway(HDLLinkConfig.getInstance().getGatewayId(), ip, UDP_PORT, callBack); |
| | | } |
| | | |
| | | |
| | |
| | | GsonConvert.getGson().toJson(data)); |
| | | |
| | | String ip = IpUtils.getBroadcastAddress(); |
| | | HdlSocketHelper.send(getUdpBoot(), ip,UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { |
| | | HdlSocketHelper.send(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { |
| | | @Override |
| | | public void onSucceed(Object msg) { |
| | | if (callBack == null) return; |
| | |
| | | } |
| | | 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(); |
| | | HdlSocketHelper.send(getUdpBoot(), ip, UDP_PORT, message, new HdlSocketHelper.HdlSocketListener() { |
| | |
| | | LogUtils.e("udpSendMsg", "参数不能为空"); |
| | | return; |
| | | } |
| | | if (!HDLLinkConfig.getInstance().checkIfCertified()) { |
| | | LogUtils.e("udpSendMsg", "未认证,请先认证"); |
| | | return; |
| | | } |
| | | LinkRequest message = new LinkRequest(topic, bodyStr); |
| | | String ip = HDLLinkConfig.getInstance().getIpAddress();// IpUtils.getBroadcastAddress(); |
| | | getUdpBoot().sendMsg( ip,UDP_PORT,message.getSendBytes()); |
| | | getUdpBoot().sendMsg(ip, UDP_PORT, message.getSendBytes()); |
| | | } |
| | | |
| | | private GatewaySearchBean getGatewaySearchBean(Object msg) { |
| | |
| | | } |
| | | |
| | | private void initListenerGatewayEvent() { |
| | | LogUtils.i( "初始化永久监听网关广播事件,不用移除此事件"); |
| | | EventListener gatewayEvent = new EventListener() { |
| | | LogUtils.i("初始化永久监听网关广播事件,不用移除此事件"); |
| | | EventListener gatewayEvent = new EventListener() { |
| | | @Override |
| | | public void onMessage(Object msg) { |
| | | try { |
| | |
| | | * 搜索指定网关是否在线,搜索到则返回指定的网关对象 |
| | | * |
| | | * @param gatewayId 网关id |
| | | * @param ipAddress 目标的IP地址 |
| | | * @param port 目标的端口 |
| | | * @param ipAddress 目标的IP地址 |
| | | * @param port 目标的端口 |
| | | * @param callBack 回调 |
| | | */ |
| | | public void searchGateway(String gatewayId, String ipAddress,int port, SearchGatewayCallBack callBack) { |
| | | public void searchGateway(String gatewayId, String ipAddress, int port, SearchGatewayCallBack callBack) { |
| | | this.searchGatewayId = gatewayId; |
| | | this.mSearchGatewayCallBack = callBack; |
| | | //重置参数 |
| | |
| | | }).start(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 注册搜索网关监听 |
| | | */ |
| | |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 搜索在线所有的网关,包括当前住宅的及没有绑定过其它住宅的网关 |
| | | * |
| | | * @param ipAddress 目标的IP地址 |
| | | * @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(); |
| | | } |
| | | } |