New file |
| | |
| | | package com.hdl.sdk.link.gateway; |
| | | |
| | | import android.text.TextUtils; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonArray; |
| | | import com.google.gson.JsonElement; |
| | | import com.google.gson.JsonObject; |
| | | import com.google.gson.JsonParser; |
| | | import com.google.gson.reflect.TypeToken; |
| | | import com.hdl.sdk.link.HDLLinkLocalSdk; |
| | | import com.hdl.sdk.link.R; |
| | | import com.hdl.sdk.link.common.config.TopicConstant; |
| | | import com.hdl.sdk.link.common.event.EventDispatcher; |
| | | import com.hdl.sdk.link.common.event.EventListener; |
| | | import com.hdl.sdk.link.common.exception.HDLLinkCode; |
| | | import com.hdl.sdk.link.common.exception.HDLLinkException; |
| | | import com.hdl.sdk.link.common.utils.IdUtils; |
| | | import com.hdl.sdk.link.common.utils.IpUtils; |
| | | import com.hdl.sdk.link.common.utils.LogUtils; |
| | | import com.hdl.sdk.link.common.utils.SPUtils; |
| | | import com.hdl.sdk.link.common.utils.ThreadToolUtils; |
| | | import com.hdl.sdk.link.common.utils.gson.GsonConvert; |
| | | |
| | | import com.hdl.sdk.link.core.bean.LinkRequest; |
| | | import com.hdl.sdk.link.core.bean.LinkResponse; |
| | | import com.hdl.sdk.link.core.bean.gateway.GatewayBean; |
| | | import com.hdl.sdk.link.core.bean.response.BaseLocalResponse; |
| | | import com.hdl.sdk.link.core.callback.GatewayCallBack; |
| | | import com.hdl.sdk.link.core.callback.HDLLinkCallBack; |
| | | import com.hdl.sdk.link.core.callback.HDLLinkTCallBack; |
| | | import com.hdl.sdk.link.core.config.HDLLinkConfig; |
| | | import com.hdl.sdk.link.core.connect.HDLConnectHelper; |
| | | |
| | | import org.json.JSONArray; |
| | | import org.json.JSONException; |
| | | import org.json.JSONObject; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * Created by hxb on 2021/12/23. |
| | | */ |
| | | public class HDLLinkLocalGateway { |
| | | //instance |
| | | private volatile static HDLLinkLocalGateway instance; |
| | | |
| | | //getInstance |
| | | public static synchronized HDLLinkLocalGateway getInstance() { |
| | | if (instance == null) { |
| | | synchronized (HDLLinkLocalGateway.class) { |
| | | if (instance == null) { |
| | | instance = new HDLLinkLocalGateway(); |
| | | } |
| | | } |
| | | } |
| | | return instance; |
| | | } |
| | | |
| | | /** |
| | | * 网关列表,记录所有搜索到的网关,可能包含断线的网关 |
| | | */ |
| | | private final List<GatewayBean> gatewayBeanList = new ArrayList(); |
| | | |
| | | /** |
| | | * 获取缓存的网关列表,可能包含断线的网关 |
| | | * |
| | | * @return 网关列表 |
| | | */ |
| | | public List<GatewayBean> getGatewayList() { |
| | | return gatewayBeanList; |
| | | } |
| | | |
| | | /** |
| | | * 通过oid或者网关ID或者AMC获取内存中的网关,注意,如果是远程通讯,gatewayBeanList要平台上绑定的主网关及从网关进去,要不获取不到网关信息 |
| | | * |
| | | * @param oidOrGatewayId |
| | | * @return |
| | | */ |
| | | public GatewayBean getGatewayByOidOrGatewayId(String oidOrGatewayId) { |
| | | if (TextUtils.isEmpty(oidOrGatewayId)) { |
| | | return null; |
| | | } |
| | | for (GatewayBean gatewayBean : gatewayBeanList) { |
| | | if (oidOrGatewayId.equals(gatewayBean.getOid()) |
| | | || oidOrGatewayId.equals(gatewayBean.getGatewayId()) |
| | | || oidOrGatewayId.equals(gatewayBean.getDevice_mac()) |
| | | || oidOrGatewayId.equals(gatewayBean.getIp_address()) |
| | | ) |
| | | return gatewayBean; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 通过IP获取网关信息 |
| | | * |
| | | * @param ipAddress |
| | | * @return |
| | | */ |
| | | public GatewayBean getGatewayByIpAddress(String ipAddress) { |
| | | if (TextUtils.isEmpty(ipAddress)) { |
| | | return null; |
| | | } |
| | | for (GatewayBean gatewayBean : gatewayBeanList) { |
| | | if (ipAddress.equals(gatewayBean.getIp_address())) |
| | | return gatewayBean; |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 通过spk获取网关,这个也支持获取毫米波 |
| | | * |
| | | * @param spkList 集合 |
| | | * @return 返回网关列表 |
| | | */ |
| | | public List<GatewayBean> getGatewayBySpk(List<String> spkList) { |
| | | if (spkList.size() == 0) { |
| | | return this.getGatewayList(); |
| | | } |
| | | List<GatewayBean> gatewayBeanList = new ArrayList<>(); |
| | | for (GatewayBean gatewayBean : this.getGatewayList()) { |
| | | if (spkList.contains(gatewayBean.getGatewayType())) |
| | | gatewayBeanList.add(gatewayBean); |
| | | } |
| | | return gatewayBeanList; |
| | | } |
| | | |
| | | /** |
| | | * 获取网关列表 去掉红外宝这种设备 |
| | | * |
| | | * @param spkList 集合 |
| | | * @return 返回网关列表 |
| | | */ |
| | | public List<GatewayBean> getGatewayByGatewayType(List<String> spkList) { |
| | | if (spkList.size() == 0) { |
| | | return this.getGatewayList(); |
| | | } |
| | | List<GatewayBean> gatewayBeanList = new ArrayList<>(); |
| | | for (GatewayBean gatewayBean : this.getGatewayList()) { |
| | | if (spkList.contains(gatewayBean.getGatewayType()) && !"MIR01R-LK.10".equals(gatewayBean.getDevice_model())) |
| | | gatewayBeanList.add(gatewayBean); |
| | | } |
| | | return gatewayBeanList; |
| | | } |
| | | |
| | | /** |
| | | * 获取本地网关对象(包括毫米波) |
| | | * |
| | | * @param macAndSid 设备mac |
| | | * @return 找到返回GatewayBean, 未找到返回null. |
| | | */ |
| | | public GatewayBean getLocalGateway(String macAndSid) { |
| | | if (TextUtils.isEmpty(macAndSid)) { |
| | | return null; |
| | | } |
| | | for (GatewayBean gatewayBean : this.getGatewayList()) { |
| | | if (macAndSid.equals(gatewayBean.getDevice_mac()) |
| | | || macAndSid.equals(gatewayBean.getSid())) { |
| | | return gatewayBean; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前住宅的主网关; |
| | | * |
| | | * @param homeId 住宅id |
| | | * @return 找到返回GatewayBean, 未找到返回null. |
| | | */ |
| | | public GatewayBean findMainGateway(String homeId) { |
| | | for (GatewayBean gatewayBean : this.getGatewayList()) { |
| | | if (gatewayBean.getMaster().equals("true") |
| | | && gatewayBean.getHomeId().equals(homeId) |
| | | && gatewayBean.getGatewayType().equals("AGATEWAY")) { |
| | | return gatewayBean; |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 搜索网关,只发一次,没有回调 |
| | | */ |
| | | public void serchGatewayOneTime() { |
| | | 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(), false); |
| | | |
| | | String ipAddress = IpUtils.getBroadcastAddress(); |
| | | new HDLConnectHelper(1, ipAddress, message, false).send(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 创新所有在线的网关,包括当前住宅的及没有绑定过的网关 |
| | | * |
| | | * @param callBack 回调 |
| | | */ |
| | | public void refreshGatewayByHome(String homeId, GatewayCallBack callBack) { |
| | | refreshGatewayByHome(homeId, true, callBack); |
| | | } |
| | | |
| | | /** |
| | | * 根据住宅id获取网关 |
| | | * |
| | | * @param homeId 过滤的住宅id |
| | | * @param needEmptyHomeIdGateway 网关的住宅id为空时,是否可以加入网关列表 |
| | | * @param callBack 回调方法 |
| | | */ |
| | | public void refreshGatewayByHome(String homeId, boolean needEmptyHomeIdGateway, GatewayCallBack callBack) { |
| | | refreshGatewayByHomeIdAndSpk(homeId, this.getGatewayTypeList(), needEmptyHomeIdGateway, callBack); |
| | | } |
| | | |
| | | /** |
| | | * 获取网络设备,包括网关及毫米波等网络设备 |
| | | * |
| | | * @param homeId 住宅Id |
| | | * @param spk 网络设备spk |
| | | * @param needEmptyHomeIdGateway 是否需要空住宅Id设备 |
| | | * @param callBack 回调 |
| | | */ |
| | | public void refreshGatewayByHomeIdAndSpk(String homeId, List<String> spk, boolean needEmptyHomeIdGateway, GatewayCallBack callBack) { |
| | | String topicReply = TopicConstant.GATEWAY_SEARCH_REPLY; |
| | | final List<GatewayBean> tempGatewayBeanList = new ArrayList<>(); |
| | | EventListener eventListener = getSearchGatewayEvent(homeId, 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) { |
| | | String topicReply = TopicConstant.GATEWAY_SEARCH_REPLY; |
| | | final List<GatewayBean> tempGatewayBeanList = new ArrayList<>(); |
| | | EventListener eventListener = getSearchGatewayEvent(this.getGatewayTypeList(), 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 homeId |
| | | // * @return |
| | | // */ |
| | | // private EventListener getSearchGatewayEvent(String homeId, final List<GatewayBean> tempGatewayBeanList) { |
| | | // return getSearchGatewayEvent(homeId,true,tempGatewayBeanList); |
| | | // } |
| | | |
| | | |
| | | // /** |
| | | // * 获取搜索网关事件 |
| | | // * |
| | | // * @param homeId |
| | | // * @return |
| | | // */ |
| | | // private EventListener getSearchGatewayEvent(String homeId, 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);//本地搜索到的网关标识为本地网关 |
| | | // |
| | | // //只加载住宅一样的或者网关还没有配置过的,或者不需要住宅id为空的网关 |
| | | // if (homeId.equals(gateway.getHomeId()) || (needEmptyHomeIdGateway == true && TextUtils.isEmpty(gateway.getHomeId()))) { |
| | | // //更新缓存网关,会记录所有收到的网关,以为了保存网关的IP信息 |
| | | // updateGatewayList(gatewayBeanList, gateway); |
| | | // //更新当前读取网关的列表,这个列表每次都是清空再读取 |
| | | // updateGatewayList(tempGatewayBeanList, gateway); |
| | | // } |
| | | // } |
| | | // }; |
| | | // } |
| | | |
| | | /** |
| | | * 获取搜索网关事件 |
| | | * |
| | | * @param homeId |
| | | * @return |
| | | */ |
| | | private EventListener getSearchGatewayEvent(String homeId, 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);//本地搜索到的网关标识为本地网关 |
| | | //只加载住宅一样的或者网关还没有配置过的,或者不需要住宅id为空的网关 |
| | | if (homeId.equals(gateway.getHomeId()) || (needEmptyHomeIdGateway == true && TextUtils.isEmpty(gateway.getHomeId()))) { |
| | | //更新缓存网关,会记录所有收到的网关,以为了保存网关的IP信息 |
| | | updateGatewayList(gatewayBeanList, gateway); |
| | | //非搜索的网关类似返回 |
| | | if (spk.contains(gateway.getGatewayType())) { |
| | | //更新当前读取网关的列表,这个列表每次都是清空再读取 |
| | | updateGatewayList(tempGatewayBeanList, gateway); |
| | | } |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 获取搜索网关事件 这边不过滤homeId |
| | | * |
| | | * @return |
| | | */ |
| | | private EventListener getSearchGatewayEvent(List<String> spk, 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.setIsLocalGateWay(true);//本地搜索到的网关标识为本地网关 |
| | | //升级网关驱动 需要显示所有网关 |
| | | //⚠️这边不需要添加到gatewayBeanList 因为这个是网关离线升级的时候的功能 |
| | | // updateGatewayList(gatewayBeanList, gateway); |
| | | //更新当前读取网关的列表,这个列表每次都是清空再读取 |
| | | if (spk.contains(gateway.getGatewayType())) { |
| | | //更新当前读取网关的列表,这个列表每次都是清空再读取 |
| | | updateGatewayList(tempGatewayBeanList, gateway); |
| | | } |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 更新收到的网关到列表 |
| | | * |
| | | * @param gatewayBeanList |
| | | * @param gateway 当前收到的网关 |
| | | */ |
| | | void updateGatewayList(final List<GatewayBean> gatewayBeanList, GatewayBean gateway) { |
| | | |
| | | if (TextUtils.isEmpty(gateway.getOid()) || TextUtils.isEmpty(gateway.getDevice_mac())) { |
| | | LogUtils.e("网关Mac或者Oid为空,无效网关"); |
| | | return; |
| | | } |
| | | |
| | | boolean isFound = false;//是否在内存中找到网关对象 |
| | | //找出mac一样的网关更新最新数据 |
| | | for (int i = 0; i < gatewayBeanList.size(); i++) { |
| | | if (gatewayBeanList.get(i).getDevice_mac().equals(gateway.getDevice_mac())) { |
| | | isFound = true; |
| | | gatewayBeanList.set(i, gateway); |
| | | // 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--); |
| | | } |
| | | } |
| | | |
| | | //没有找到就添加 |
| | | if (!isFound) { |
| | | gatewayBeanList.add(gateway); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取网关对象 |
| | | * |
| | | * @param linkResponse |
| | | * @return |
| | | */ |
| | | private GatewayBean getGatewayBeanByResponse(LinkResponse linkResponse) { |
| | | String data = linkResponse.getData(); |
| | | if (!TextUtils.isEmpty(data)) { |
| | | final BaseLocalResponse<GatewayBean> response = GsonConvert.getGson().fromJson(data, new TypeToken<BaseLocalResponse<GatewayBean>>() { |
| | | }.getType()); |
| | | return response.getObjects(); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | /* |
| | | * 绑定网关 |
| | | * */ |
| | | public void SetGatewayRemoteInfo(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"); |
| | | } |
| | | |
| | | 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) { |
| | | String topic = String.format(TopicConstant.GATEWAY_EDIT_REMOTE, mac); |
| | | |
| | | JsonObject jObject = new JsonObject(); |
| | | jObject.addProperty("homeId", homeId); |
| | | // if(master.equals("true")) { |
| | | // jObject.addProperty("server_addr", regionUrl); |
| | | // jObject.addProperty("remote", "true"); |
| | | // }else { |
| | | // jObject.addProperty("server_addr", ""); |
| | | // jObject.addProperty("remote", "false"); |
| | | // } |
| | | |
| | | 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(); |
| | | } |
| | | |
| | | /* |
| | | * 初始化网关 |
| | | * */ |
| | | public void initializeGateway(String mac, String oid, String ip, boolean isEncrypt, HDLLinkCallBack callBack) { |
| | | String topic = String.format(TopicConstant.GATEWAY_INITIALIZE_REMOTE, oid); |
| | | |
| | | 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("device_mac", mac); |
| | | sendJsonObj.add("objects", jObject); |
| | | 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(); |
| | | } |
| | | |
| | | /* |
| | | * 设置主从网关标记 |
| | | * */ |
| | | public void setMasterGateway(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(master, mac, regionUrl, ip, isEncrypt, new HDLLinkCallBack() { |
| | | @Override |
| | | public void onSuccess(String msg) { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onError(HDLLinkException e) { |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLLinkCode hdlLinkCode) { |
| | | if (callBack == null) return; |
| | | callBack.onError(HDLLinkException.getErrorWithCode(hdlLinkCode)); |
| | | } |
| | | }, true).send(); |
| | | } |
| | | |
| | | /** |
| | | * 设置网关入网从机模式 |
| | | */ |
| | | public void AuthGateway(String ip, String mac, 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("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(); |
| | | } |
| | | |
| | | /** |
| | | * 设置网关入网从机模式 |
| | | */ |
| | | public void AuthGateway(String ip, String mac) { |
| | | 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("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) { |
| | | } |
| | | |
| | | @Override |
| | | public void onFailure(HDLLinkCode hdlLinkCode) { |
| | | } |
| | | }, true).send(); |
| | | } |
| | | |
| | | /* |
| | | * 获取网关信息 |
| | | * */ |
| | | public void getGatewayInfo(String ip, String mac, boolean isEncrypt, HDLLinkCallBack callBack) { |
| | | String topic = String.format(TopicConstant.GATEWAY_GET, mac); |
| | | |
| | | JsonObject sendJsonObj = new JsonObject(); |
| | | 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(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 发送数据到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(); |
| | | } |
| | | |
| | | /** |
| | | * 组装需要发送的数据 |
| | | * |
| | | * @param jObject 负载数据<没有填null> |
| | | * @return 返回json字符串 |
| | | */ |
| | | private String createSendData(Object jObject) { |
| | | //topic = String.format(TopicConstant.GATEWAY_EDIT_LOCAL, mac); |
| | | JsonObject sendJsonObj = new JsonObject(); |
| | | if (jObject != null) { |
| | | if (jObject instanceof JsonObject) { |
| | | sendJsonObj.add("objects", (JsonObject) jObject); |
| | | } else if (jObject instanceof JsonArray) { |
| | | sendJsonObj.add("objects", (JsonArray) jObject); |
| | | } else if (jObject instanceof JSONArray) { |
| | | JsonArray array = GsonConvert.getGson().fromJson(jObject.toString(), new TypeToken<JsonArray>() { |
| | | }.getType()); |
| | | sendJsonObj.add("objects", array); |
| | | } |
| | | } |
| | | String time = String.valueOf(System.currentTimeMillis()); |
| | | sendJsonObj.addProperty("time_stamp", time); |
| | | sendJsonObj.addProperty("id", IdUtils.getUUId()); |
| | | return sendJsonObj.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 支持网关类型 |
| | | * |
| | | * @return 类型列表 |
| | | */ |
| | | private List<String> getGatewayTypeList() { |
| | | List<String> typeList = new ArrayList<>(); |
| | | typeList.add("AGATEWAY");//网关 |
| | | typeList.add("sensor.mmv_sleep");//毫米波spk |
| | | typeList.add("sensor.mmv_pose");//毫米波spk |
| | | return typeList; |
| | | } |
| | | } |