| | |
| | | } |
| | | List<GatewayBean> gatewayBeanList = new ArrayList<>(); |
| | | for (GatewayBean gatewayBean : this.getGatewayList()) { |
| | | if (spkList.contains(gatewayBean.getGatewayType()) && !"MIR01R-LK.10".equals(gatewayBean.getDevice_model())) |
| | | if ((spkList.contains(gatewayBean.getGatewayType())) |
| | | && !"MIR01R-LK.10".equals(gatewayBean.getDevice_model()) |
| | | && !"MSMWP-LK.30".equals(gatewayBean.getDevice_model())) |
| | | gatewayBeanList.add(gatewayBean); |
| | | } |
| | | return gatewayBeanList; |
| | |
| | | } |
| | | for (GatewayBean gatewayBean : this.getGatewayList()) { |
| | | if (macAndSid.equals(gatewayBean.getDevice_mac()) |
| | | || macAndSid.equals(gatewayBean.getSid()) |
| | | || macAndSid.equals(gatewayBean.getOid())) { |
| | | || macAndSid.equals(gatewayBean.getSid())) { |
| | | return gatewayBean; |
| | | } |
| | | } |
| | |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 判断住宅是否有主网关 |
| | | * |
| | | * @param homeId |
| | | * @return |
| | | */ |
| | | public boolean hasMainGateway(String homeId) { |
| | | boolean hasMain = false; |
| | | for (GatewayBean gatewayBean : this.getGatewayList()) { |
| | | if (gatewayBean.getMaster().equals("true") |
| | | && gatewayBean.getHomeId().equals(homeId) |
| | | && gatewayBean.getGatewayType().equals("AGATEWAY")) { |
| | | hasMain = true; |
| | | } |
| | | } |
| | | return hasMain; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | public void refreshGatewayByHome(String homeId, boolean needEmptyHomeIdGateway, GatewayCallBack callBack) { |
| | | refreshGatewayByHomeIdAndSpk(homeId, this.getGatewayTypeList(), needEmptyHomeIdGateway, callBack); |
| | | } |
| | | |
| | | /** |
| | | * 根据住宅id获取网关 |
| | | * |
| | | * @param homeId 过滤的住宅id |
| | | * @param needEmptyHomeIdGateway 网关的住宅id为空时,是否可以加入网关列表 |
| | | * @param callBack 回调方法 |
| | | */ |
| | | public void refreshWifiMillimeterZT(boolean needEmptyHomeIdGateway, GatewayCallBack callBack) { |
| | | refreshGatewayBySpk(this.getWifiMillimeterZTTypeList(), needEmptyHomeIdGateway, callBack); |
| | | } |
| | | |
| | | /** |
| | | * 获取网络设备,包括网关及毫米波等网络设备 |
| | | * |
| | | * @param spk 网络设备spk |
| | | * @param needEmptyHomeIdGateway 是否需要空住宅Id设备 |
| | | * @param callBack 回调 |
| | | */ |
| | | public void refreshGatewayBySpk(List<String> spk, boolean needEmptyHomeIdGateway, GatewayCallBack callBack) { |
| | | String topicReply = TopicConstant.GATEWAY_SEARCH_REPLY; |
| | | final List<GatewayBean> tempGatewayBeanList = new ArrayList<>(); |
| | | EventListener eventListener = getSearchGatewayEvent(spk, needEmptyHomeIdGateway, tempGatewayBeanList); |
| | | EventDispatcher.getInstance().register(topicReply, eventListener); |
| | | |
| | | ThreadToolUtils.getInstance().newFixedThreadPool(1).execute(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | int count = 5; |
| | | while (0 < count--) { |
| | | try { |
| | | //搜索网关 |
| | | serchGatewayOneTime(); |
| | | Thread.sleep(300L); |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | //超出次数后移除监听事件 |
| | | EventDispatcher.getInstance().remove(topicReply, eventListener); |
| | | if (callBack != null) { |
| | | if (tempGatewayBeanList.size() == 0) { |
| | | callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GET_GATEWAY_FAILURE_ERROR)); |
| | | } else { |
| | | callBack.onSuccess(tempGatewayBeanList); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param callBack 回调 |
| | | */ |
| | | public void refreshGateway(GatewayCallBack callBack) { |
| | | refreshGateway(callBack,this.getGatewayTypeList()); |
| | | } |
| | | |
| | | |
| | | public void refreshGateway(GatewayCallBack callBack, List<String> spks) { |
| | | String topicReply = TopicConstant.GATEWAY_SEARCH_REPLY; |
| | | final List<GatewayBean> tempGatewayBeanList = new ArrayList<>(); |
| | | EventListener eventListener = getSearchGatewayEvent(this.getGatewayTypeList(), tempGatewayBeanList); |
| | | EventListener eventListener = getSearchGatewayEvent(spks, tempGatewayBeanList); |
| | | EventDispatcher.getInstance().register(topicReply, eventListener); |
| | | ThreadToolUtils.getInstance().newFixedThreadPool(1).execute(new Runnable() { |
| | | @Override |
| | |
| | | /** |
| | | * 获取搜索网关事件 |
| | | * |
| | | * @return |
| | | */ |
| | | private EventListener getSearchGatewayEvent(List<String> spk, boolean needEmptyHomeIdGateway, final List<GatewayBean> tempGatewayBeanList) { |
| | | //注册搜索网关监听 |
| | | return new EventListener() { |
| | | @Override |
| | | public void onMessage(Object msg) { |
| | | if (!(msg instanceof LinkResponse)) { |
| | | return; |
| | | } |
| | | GatewayBean gateway = getGatewayBeanByResponse((LinkResponse) msg); |
| | | if (gateway == null) { |
| | | return; |
| | | } |
| | | gateway.setOnline(true); |
| | | gateway.setIsLocalGateWay(true);//本地搜索到的网关标识为本地网关 |
| | | if ("av.zk.aiks".equals(gateway.getGatewayType())) { |
| | | /** 将主网关的homeid设置到埃克斯主机 |
| | | */ |
| | | // gateway.setHomeId(homeId); |
| | | } |
| | | //只加载住宅一样的或者网关还没有配置过的,或者不需要住宅id为空的网关 |
| | | //更新缓存网关,会记录所有收到的网关,以为了保存网关的IP信息 |
| | | updateGatewayList(gatewayBeanList, gateway); |
| | | //非搜索的网关类似返回 |
| | | if (spk.contains(gateway.getGatewayType())) { |
| | | //更新当前读取网关的列表,这个列表每次都是清空再读取 |
| | | updateGatewayList(tempGatewayBeanList, gateway); |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 获取搜索网关事件 |
| | | * |
| | | * @param homeId |
| | | * @return |
| | | */ |
| | |
| | | } |
| | | gateway.setOnline(true); |
| | | gateway.setIsLocalGateWay(true);//本地搜索到的网关标识为本地网关 |
| | | if ("av.zk.aiks".equals(gateway.getGatewayType())) { |
| | | /** 将主网关的homeid设置到埃克斯主机 |
| | | */ |
| | | // gateway.setHomeId(homeId); |
| | | } |
| | | //只加载住宅一样的或者网关还没有配置过的,或者不需要住宅id为空的网关 |
| | | if (homeId.equals(gateway.getHomeId()) || (needEmptyHomeIdGateway == true && TextUtils.isEmpty(gateway.getHomeId()))) { |
| | | //更新缓存网关,会记录所有收到的网关,以为了保存网关的IP信息 |
| | |
| | | /** |
| | | * 更新收到的网关到列表 |
| | | * |
| | | * @param gatewayBeanList |
| | | * @param gateway 当前收到的网关 |
| | | * @param gatewayBeans |
| | | * @param gateway 当前收到的网关 |
| | | */ |
| | | void updateGatewayList(final List<GatewayBean> gatewayBeanList, GatewayBean gateway) { |
| | | synchronized void updateGatewayList(final List<GatewayBean> gatewayBeans, GatewayBean gateway) { |
| | | |
| | | if (TextUtils.isEmpty(gateway.getOid()) || TextUtils.isEmpty(gateway.getDevice_mac())) { |
| | | LogUtils.e("网关Mac或者Oid为空,无效网关"); |
| | |
| | | |
| | | boolean isFound = false;//是否在内存中找到网关对象 |
| | | //找出mac一样的网关更新最新数据 |
| | | for (int i = 0; i < gatewayBeanList.size(); i++) { |
| | | if (gatewayBeanList.get(i).getDevice_mac().equals(gateway.getDevice_mac())) { |
| | | for (int i = 0; i < gatewayBeans.size(); i++) { |
| | | GatewayBean tempGatewayBean = gatewayBeans.get(i); |
| | | if (tempGatewayBean.getDevice_mac().equals(gateway.getDevice_mac())) { |
| | | /** |
| | | * 如果已经有uid 需要设置 |
| | | */ |
| | | if(null!=tempGatewayBean.getUids()&&tempGatewayBean.getUids().size()>0){ |
| | | gateway.setUids(tempGatewayBean.getUids()); |
| | | } |
| | | isFound = true; |
| | | gatewayBeanList.set(i, gateway); |
| | | gatewayBeans.set(i, gateway); |
| | | gateway.setAesKey(tempGatewayBean.getAesKey());//保留云端获取的aeskey |
| | | |
| | | // break; |
| | | } |
| | | } |
| | | |
| | | //清除oid一样,mac不一样的网关缓存。一般是在网关替换的情况空间出现 |
| | | for (int i = 0; i < gatewayBeanList.size(); i++) { |
| | | if (gatewayBeanList.get(i).getDevice_mac().equals(gateway.getDevice_mac()) && !gatewayBeanList.get(i).getOid().equals(gateway.getOid())) { |
| | | gatewayBeanList.remove(i--); |
| | | for (int i = 0; i < gatewayBeans.size(); i++) { |
| | | if (gatewayBeans.get(i).getDevice_mac().equals(gateway.getDevice_mac()) && !gatewayBeans.get(i).getOid().equals(gateway.getOid())) { |
| | | gatewayBeans.remove(i--); |
| | | } |
| | | } |
| | | |
| | | //没有找到就添加 |
| | | if (!isFound) { |
| | | gatewayBeanList.add(gateway); |
| | | gatewayBeans.add(gateway); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取网关对象 |
| | |
| | | |
| | | /* |
| | | * 绑定网关 |
| | | * */ |
| | | public void SetGatewayRemoteInfo(String homeId, String master, String mac, String regionUrl, String ip, boolean isEncrypt, HDLLinkCallBack callBack) { |
| | | String topic = String.format(TopicConstant.GATEWAY_EDIT_REMOTE, mac); |
| | | |
| | | JsonObject jObject = new JsonObject(); |
| | | if (master.equals("true")) { |
| | | jObject.addProperty("server_addr", regionUrl); |
| | | jObject.addProperty("remote", "true"); |
| | | } else { |
| | | jObject.addProperty("server_addr", ""); |
| | | jObject.addProperty("remote", "false"); |
| | | } |
| | | jObject.addProperty("homeId", homeId); |
| | | JsonObject sendJsonObj = new JsonObject(); |
| | | sendJsonObj.add("objects", jObject); |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | sendJsonObj.addProperty("time_stamp", time); |
| | | sendJsonObj.addProperty("id", IdUtils.getUUId()); |
| | | String sendStr = sendJsonObj.toString(); |
| | | |
| | | LinkRequest message = new LinkRequest(topic, sendStr, isEncrypt); |
| | | new HDLConnectHelper(ip, message, new HDLConnectHelper.HdlSocketListener() { |
| | | @Override |
| | | public void onSucceed(Object msg) { |
| | | if (callBack == null) return; |
| | | callBack.onSuccess("1"); |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLLinkCode hdlLinkCode) { |
| | | if (callBack == null) return; |
| | | callBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode)); |
| | | } |
| | | }, true).send(); |
| | | } |
| | | |
| | | /* |
| | | * 绑定网关 |
| | | * 编辑homeId给网关 |
| | | * */ |
| | | public void bindGateway(String master, String mac, String homeId, String regionUrl, String ip, boolean isEncrypt, HDLLinkCallBack callBack) { |
| | |
| | | public void onSucceed(Object msg) { |
| | | if (callBack == null) return; |
| | | callBack.onSuccess("1"); |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLLinkCode hdlLinkCode) { |
| | | if (callBack == null) return; |
| | | callBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode)); |
| | | } |
| | | }, true).send(); |
| | | } |
| | | |
| | | /* |
| | | * 绑定毫米波 |
| | | * 编辑homeId给毫米波 |
| | | * */ |
| | | public void bindMillimeterGateway(String homeId, String regionUrl, String ip, String mac, String master, boolean isEncrypt, HDLLinkCallBack callBack) { |
| | | String topic = String.format(TopicConstant.GATEWAY_EDIT_LOCAL, mac); |
| | | |
| | | JsonObject jObject = new JsonObject(); |
| | | jObject.addProperty("master", master); |
| | | |
| | | JsonObject sendJsonObj = new JsonObject(); |
| | | sendJsonObj.add("objects", jObject); |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | sendJsonObj.addProperty("time_stamp", time); |
| | | sendJsonObj.addProperty("id", IdUtils.getUUId()); |
| | | String sendStr = sendJsonObj.toString(); |
| | | |
| | | LinkRequest message = new LinkRequest(topic, sendStr, isEncrypt); |
| | | new HDLConnectHelper(ip, message, new HDLConnectHelper.HdlSocketListener() { |
| | | @Override |
| | | public void onSucceed(Object msg) { |
| | | if ("true".equals(master)) { |
| | | String longStr1 = SPUtils.getString("longitude"); |
| | | String latStr1 = SPUtils.getString("latitude"); |
| | | if (!TextUtils.isEmpty(longStr1) && !TextUtils.isEmpty(latStr1)) { |
| | | HDLLinkLocalSdk.getInstance().gatewayLocation(mac, longStr1, latStr1, isEncrypt, new HDLLinkTCallBack<String>() { |
| | | @Override |
| | | public void onSuccess(String data) { |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | } |
| | | }); |
| | | } |
| | | } |
| | | if (callBack == null) return; |
| | | callBack.onSuccess("1"); |
| | | SetGatewayRemoteInfo(homeId, master, mac, regionUrl, ip, isEncrypt, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | |
| | | }, true).send(); |
| | | } |
| | | |
| | | /** |
| | | * 设置网关入网从机模式 网关338可以使用 |
| | | */ |
| | | public void AuthGatewayNewVersion(String ip, String mac, String slaveMac, HDLLinkCallBack callBack) { |
| | | String topic = String.format(TopicConstant.GATEWAY_AUTH, mac); |
| | | |
| | | JsonObject sendJsonObj = new JsonObject(); |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | sendJsonObj.addProperty("time_stamp", time); |
| | | sendJsonObj.addProperty("id", IdUtils.getUUId()); |
| | | JsonObject jObject = new JsonObject(); |
| | | jObject.addProperty("spk", "LINKDEVICE"); |
| | | jObject.addProperty("mac", slaveMac); |
| | | jObject.addProperty("time", "120"); |
| | | sendJsonObj.add("objects", jObject); |
| | | |
| | | |
| | | String sendStr = sendJsonObj.toString(); |
| | | LinkRequest message = new LinkRequest(topic, sendStr, false); |
| | | new HDLConnectHelper(ip, message, new HDLConnectHelper.HdlSocketListener() { |
| | | @Override |
| | | public void onSucceed(Object msg) { |
| | | callBack.onSuccess(""); |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLLinkCode hdlLinkCode) { |
| | | callBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode)); |
| | | } |
| | | }, true).send(); |
| | | } |
| | | |
| | | /* |
| | | * 获取网关信息 |
| | | * */ |
| | |
| | | /** |
| | | * 发送数据到Link网关 |
| | | * |
| | | * @param ip 网关IP |
| | | * @param mac 网关mac |
| | | * @param isEncrypt 是否加密 |
| | | * @param topic 请求主题 |
| | | * @param jObject 负载数据<没有填null></> |
| | | * @param sendPath 发送路径<类名+方法名>class->methodName</> |
| | | */ |
| | | public void sendDataToLinkGateway(String ip, String mac, boolean isEncrypt, |
| | | String topic, Object jObject, String sendPath, HDLLinkCallBack callBack) { |
| | | String topicSend = topic.replace("%s", mac); |
| | | //组装需要发送的数据 |
| | | String sendStr = createSendData(jObject); |
| | | // LogUtils.i("sendDataToLinkGateway->" + sendPath + "->本地发送\r\n" + topicSend + "\r\n" + sendStr); |
| | | LinkRequest message = new LinkRequest(topicSend, sendStr, isEncrypt); |
| | | GatewayBean gatewayBean = this.getLocalGateway(mac); |
| | | if (gatewayBean != null) { |
| | | message.setCloudTopic(topic.replace("%s", gatewayBean.getGatewayId())); |
| | | } |
| | | new HDLConnectHelper(ip, message, new HDLConnectHelper.HdlSocketListener() { |
| | | @Override |
| | | public void onSucceed(Object msg) { |
| | | if (callBack == null) return; |
| | | try { |
| | | JSONObject jsonObject = new JSONObject(new Gson().toJson(msg)); |
| | | if (jsonObject.has("data")) { |
| | | callBack.onSuccess(jsonObject.getString("data")); |
| | | } |
| | | } catch (Exception e) { |
| | | callBack.onSuccess(e.getMessage()); |
| | | } |
| | | // LogUtils.i("sendDataToLinkGateway->"+sendPath+"->本地接收数据\r\n"+msg.toString()); |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLLinkCode hdlLinkCode) { |
| | | if (callBack == null) return; |
| | | callBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode)); |
| | | // LogUtils.i("sendDataToLinkGateway->" + sendPath + "->本地接收数据->失败(-200)"); |
| | | } |
| | | }, true, mac).send(); |
| | | } |
| | | |
| | | /** |
| | | * 发送数据到Link网关 |
| | | * |
| | | * @param mac 网关mac |
| | | * @param isEncrypt 是否加密 |
| | | * @param topic 请求主题 |
| | |
| | | String topic, Object jObject, String sendPath, HDLLinkCallBack callBack) { |
| | | GatewayBean gatewayBean = this.getLocalGateway(mac); |
| | | if (gatewayBean == null) { |
| | | callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_FOUND_LOCALLY)); |
| | | callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST)); |
| | | return; |
| | | } |
| | | String topicSend = topic.replace("%s", gatewayBean.getOid()); |
| | | String topicSend = topic.replace("%s", mac); |
| | | //组装需要发送的数据 |
| | | String sendStr = createSendData(jObject); |
| | | // LogUtils.i("sendDataToLinkGateway->" + sendPath + "->本地发送\r\n" + topicSend + "\r\n" + sendStr); |
| | |
| | | }, true, mac).send(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送数据到Link网关 |
| | | * |
| | |
| | | String topic, Object jObject, String sendPath, HDLLinkCallBack callBack) { |
| | | GatewayBean gatewayBean = this.getLocalGateway(mac); |
| | | if (gatewayBean == null) { |
| | | callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_FOUND_LOCALLY)); |
| | | callBack.onError(HDLLinkException.getErrorWithCode(HDLLinkCode.HDL_GATEWAY_NOT_EXIST)); |
| | | return; |
| | | } |
| | | String topicSend = topic.replace("%s", gatewayBean.getOid()); |
| | |
| | | // LogUtils.i("sendDataToLinkGateway->"+sendPath+"->本地接收数据\r\n"+msg.toString()); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onFailure(HDLLinkCode hdlLinkCode) { |
| | | if (callBack == null) return; |
| | |
| | | } |
| | | }, true, mac).send(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 组装需要发送的数据 |
| | |
| | | private List<String> getGatewayTypeList() { |
| | | List<String> typeList = new ArrayList<>(); |
| | | typeList.add("AGATEWAY");//网关 |
| | | typeList.add("av.zk.aiks");//埃克斯影音主机 |
| | | typeList.add("sensor.mmv_sleep");//毫米波spk |
| | | typeList.add("sensor.mmv_pose");//毫米波spk |
| | | typeList.add("sensor.hdl_mmw_pose");//Wi-Fi毫米波ZT版本spk |
| | | return typeList; |
| | | } |
| | | |
| | | private List<String> getWifiMillimeterZTTypeList() { |
| | | List<String> typeList = new ArrayList<>(); |
| | | typeList.add("sensor.hdl_mmw_pose");//Wi-Fi毫米波ZT版本spk |
| | | return typeList; |
| | | } |
| | | } |