package com.hdl.sdk.hdl_core.HDLDeviceManger.Core;
|
|
import android.content.Context;
|
|
import com.hdl.sdk.hdl_core.Config.Configuration;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.DevicesData;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.RemarkTimes;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.SearchCountBean;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.DevicesInfoEvent;
|
import com.hdl.sdk.hdl_core.Util.LogUtil.HDLLog;
|
import com.hdl.sdk.hdl_core.Util.NetUtil.NetWorkUtil;
|
import com.hdl.sdk.hdl_core.Util.SPUtil.SPUtils;
|
|
import org.greenrobot.eventbus.EventBus;
|
|
import java.util.List;
|
import java.util.Random;
|
import java.util.Timer;
|
import java.util.TimerTask;
|
|
|
public class HandleSearch {
|
public static boolean isSearching = false;
|
public static int curSearchMode = 0;
|
public static String rcuIp = "";//主动传入的rcu ip地址
|
// private static Timer getRcuIpTimer = null;//获取Rcuip Timer
|
// private static Timer getRcuIpFailorSuccessTimer = null;//获取Rcuip Timer
|
public static boolean isGetRcuIp = false;
|
private static boolean isRefreshAllDevicesState = false;
|
|
public static Timer searchTimer = null;//发送第一次搜索Timer
|
public static Timer searchTwiceTimer = null;//发送第二次搜索Timer
|
public static Timer searchFailTimer = null;//发送搜索失败Timer
|
private static Timer remarkTimer = null;//搜索备注Timer
|
private static Timer refreshTimer = null;
|
|
private static int curDevSize;//记录设备总数。作用:比较搜索出来的总设备数
|
public static byte random1 = 0;//随机数1
|
public static byte random2 = 0;//随机数2
|
|
public static final int GET_RCU_DEVICES = 0;//获取Rcu模块设备
|
public static final int GET_BUS_DEVICES = 1;//获取家居模块 设备
|
|
private static final int SEARCH_POINT_TO_POINT = 100;//点对点
|
private static final int SEARCH_BROACAST = 101;//广播
|
private static final int SEARCH_MULTICAST = 102;//组播
|
|
/**
|
* 酒店搜索
|
* <p>
|
* <p>
|
* 这方法暂时不提供,获取备注当中也没有处理此类型。
|
* <p>
|
* 获取Rcu ip地址
|
*/
|
// private static void getRcuIp() {
|
// isGetRcuIp = true;
|
// HDLUdpCore.closeMulticast6000();
|
// HDLUdpCore.initMulticastSocket6008();
|
// if (getRcuIpFailorSuccessTimer != null) {
|
// getRcuIpFailorSuccessTimer.cancel();
|
// getRcuIpFailorSuccessTimer = null;
|
// }
|
// if (getRcuIpTimer != null) {
|
// getRcuIpTimer.cancel();
|
// getRcuIpTimer = null;
|
// }
|
//
|
// getRcuIpTimer = new Timer();
|
// getRcuIpFailorSuccessTimer = new Timer();
|
// HDLDeviceManager.isGetRcuIpSuccess = false;
|
// HDLDeviceManager.rcuIpList.clear();
|
//
|
// getRcuIpTimer.schedule(new TimerTask() {
|
// @Override
|
// public void run() {
|
//// 每400毫秒发送一次
|
// if (!HDLDeviceManager.isGetRcuIpSuccess) {
|
// baseSearch(SEARCH_MULTICAST, Configuration.RCU_SEND_PORT);
|
// }
|
//
|
//
|
// }
|
// }, 1, 200);
|
//
|
//
|
// getRcuIpFailorSuccessTimer.schedule(new TimerTask() {
|
// @Override
|
// public void run() {
|
// isGetRcuIp = false;
|
// if (getRcuIpTimer != null) {
|
// getRcuIpTimer.cancel();
|
// getRcuIpTimer = null;
|
// }
|
//// 若没有回复则失败,开始家居搜索。若有回复则保存Rcu ip地址,开始酒店协议搜索第二步
|
// if (HDLDeviceManager.isGetRcuIpSuccess) {
|
// HDLDeviceManager.isGetRcuIpSuccess = false;
|
// //返回ip地址列表,如果只有一个不返回,直接搜索。
|
// switch (HDLDeviceManager.rcuIpList.size()) {
|
// case 0:
|
// searchDevices(GET_BUS_DEVICES, SEARCH_BROACAST, Configuration.PORT);//家居搜索
|
// break;
|
// case 1:
|
// rcuIp = HDLDeviceManager.rcuIpList.get(0);
|
// HDLLog.info("搜索到的rcu ip地址:" + rcuIp);
|
// searchDevices(GET_RCU_DEVICES, SEARCH_POINT_TO_POINT, Configuration.RCU_SEND_PORT);//RCU模块搜索
|
// break;
|
// default:
|
// //返回用户选择哪个rcuIp
|
// EventBus.getDefault().post(new RcuIpListEvent(HDLDeviceManager.rcuIpList));
|
// break;
|
// }
|
//
|
// } else {
|
// HDLLog.info("开始家居协议搜索");
|
// searchDevices(GET_BUS_DEVICES, SEARCH_BROACAST, Configuration.PORT);//家居搜索
|
// }
|
// }
|
// }, 1000);
|
// }
|
|
/**
|
* 酒店搜索
|
* 获取Rcu模块设备
|
*/
|
public static void getRcuDevices(Context context, String newRcuIp) {
|
curSearchMode = GET_RCU_DEVICES;
|
rcuIp = newRcuIp;
|
SPUtils.setParam(context, SPUtils.KEY_RCU_IP_, newRcuIp);
|
searchDevices(GET_RCU_DEVICES, SEARCH_POINT_TO_POINT, Configuration.RCU_SEND_PORT);
|
}
|
|
public static void getHomeDevices(Context context) {
|
SPUtils.setParam(context, SPUtils.KEY_RCU_IP_, "");
|
curSearchMode = GET_BUS_DEVICES;
|
rcuIp = "";
|
searchDevices(GET_BUS_DEVICES, SEARCH_BROACAST, Configuration.PORT);//家居搜索
|
}
|
|
/**
|
* 初始化搜索
|
*
|
* @param searchMode 搜索模式
|
* @param baseSearchType 基础搜索类型
|
* @param port
|
*/
|
private static void searchDevices(final int searchMode, final int baseSearchType, final int port) {
|
isSearching = true;
|
isRefreshAllDevicesState = false;
|
String newIpAddress = "";
|
switch (searchMode) {
|
case GET_BUS_DEVICES:
|
HDLUdpCore.closeSocket6008();
|
HDLUdpCore.init6000();
|
newIpAddress = NetWorkUtil.getLocalBroadCast();
|
break;
|
case GET_RCU_DEVICES:
|
HDLUdpCore.closeSocket6000();
|
HDLUdpCore.init6008();
|
newIpAddress = rcuIp;
|
break;
|
default:
|
break;
|
}
|
|
final String secondSearchAddress = newIpAddress;//仅作为内部类需要定义为final
|
|
if (searchTimer != null) {
|
searchTimer.cancel();
|
searchTimer.purge();
|
searchTimer = null;
|
}
|
|
if (searchFailTimer != null) {
|
searchFailTimer.cancel();
|
searchFailTimer.purge();
|
searchFailTimer = null;
|
}
|
if (remarkTimer != null) {
|
remarkTimer.cancel();
|
remarkTimer.purge();
|
remarkTimer = null;
|
}
|
|
searchTimer = new Timer();
|
searchFailTimer = new Timer();
|
|
int max = 254;
|
int min = 1;
|
random1 = (byte) (new Random().nextInt(max + 1) % (max - min + 1) + min);
|
random2 = (byte) (new Random().nextInt(max + 1) % (max - min + 1) + min);
|
|
baseSearch(baseSearchType, port);
|
|
// 初始化搜索超时,5000毫秒后,无搜索设备数据返回则搜索超时
|
searchFailTimer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
if (HDLDeviceManager.devicesDataList.size() == 0) {
|
// 返回搜索超时
|
if (searchTimer != null) {
|
searchTimer.cancel();
|
searchTimer.purge();
|
searchTimer = null;
|
}
|
|
switch (searchMode) {
|
case GET_RCU_DEVICES:
|
HDLLog.info("Rcu设备搜索超时");
|
// searchDevices(GET_BUS_DEVICES, SEARCH_BROACAST, Configuration.PORT);//家居搜索
|
EventBus.getDefault().post(new DevicesInfoEvent(false));//不进行家居搜索,直接停止
|
break;
|
case GET_BUS_DEVICES:
|
//家居协议搜索超时
|
HDLLog.info("家居设备搜索超时");
|
EventBus.getDefault().post(new DevicesInfoEvent(false));
|
break;
|
}
|
}
|
}
|
}, 5000);
|
|
if (searchTimer == null) {
|
return;
|
}
|
|
//TODO 可能会为空
|
searchTimer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
// 每隔500毫秒判断当前的设备数与搜索出来的设备数是否一致
|
if (curDevSize != HDLDeviceManager.realDevicesDataList.size() || HDLDeviceManager.realDevicesDataList.size() == 0) {
|
//有新设备,继续等待500毫秒
|
if (searchTwiceTimer != null) {
|
searchTwiceTimer.cancel();
|
searchTwiceTimer.purge();
|
searchTwiceTimer = null;
|
HDLLog.info("有新设备阻断发送搜索命令");
|
}
|
if (HDLDeviceManager.realDevicesDataList.size() == 0) {
|
baseSearch(baseSearchType, port);
|
} else {
|
curDevSize = HDLDeviceManager.realDevicesDataList.size();
|
|
HDLLog.info("有新设备,继续等待500毫秒");
|
|
}
|
|
} else {
|
if (searchTwiceTimer == null) {
|
searchTwiceTimer = new Timer();
|
final SearchCountBean bean = new SearchCountBean();
|
bean.setCount(0);
|
searchTwiceTimer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
if (bean.getCount() < 3) {
|
HDLLog.info("没有收到新设备,发送第 " + (bean.getCount() + 1) + " 次搜索命令");
|
secondTimeSearch(random1, random2, HDLDeviceManager.realDevicesDataList, secondSearchAddress, port);
|
} else {
|
if (searchTimer != null) {
|
searchTimer.cancel();
|
searchTimer.purge();
|
searchTimer = null;
|
}
|
if (searchTwiceTimer != null) {
|
searchTwiceTimer.cancel();
|
searchTwiceTimer.purge();
|
searchTwiceTimer = null;
|
}
|
|
isSearching = false;
|
HDLLog.info("开始获取每个模块的回路状态与备注");
|
getDevRemarks();//开始获取第一备注,逐一获取。
|
}
|
bean.setCount(bean.getCount() + 1);
|
}
|
}, 1, 800);
|
}
|
|
|
}
|
}
|
}, 500, 500);
|
}
|
|
/**
|
* 发送第一次搜索
|
*
|
* @param type 搜索类型
|
* @param port 端口号
|
*/
|
private static void baseSearch(int type, int port) {
|
// 设备列表、设备备注列表初始化清空
|
if (remarkTimer != null) {
|
remarkTimer.cancel();
|
remarkTimer.purge();
|
remarkTimer = null;
|
}
|
isRefreshAllDevicesState = false;
|
HDLDeviceManager.devicesDataList.clear();
|
HDLDeviceManager.realDevicesDataList.clear();
|
HDLDeviceManager.listRemarks.clear();
|
curDevSize = 0;
|
|
// 发送第一次搜索命令
|
switch (type) {
|
case SEARCH_POINT_TO_POINT:
|
HDLLog.info("发送第一次RCU搜索");
|
HDLCommand.cusSendCommand(Configuration.DEVICES_SEARCH_COMMAND,
|
255, 255, new byte[]{random1, random2}, port, rcuIp);
|
break;
|
case SEARCH_BROACAST:
|
HDLLog.info("发送第一次家居搜索");
|
HDLCommand.cusSendCommand(Configuration.DEVICES_SEARCH_COMMAND,
|
255, 255, new byte[]{random1, random2},
|
port, NetWorkUtil.getLocalBroadCast());
|
break;
|
case SEARCH_MULTICAST:
|
HDLCommand.cusSendMulticastCommand(Configuration.DEVICES_SEARCH_COMMAND,
|
255, 255
|
, new byte[]{random1, random2}, port);
|
break;
|
default:
|
break;
|
}
|
|
|
}
|
|
/**
|
* 发送第二次搜索
|
*
|
* @param arg1 随机数1
|
* @param arg2 随机数2
|
* @param devicesDataList 所有设备信息集
|
* @param port 端口号
|
*/
|
public static void secondTimeSearch(byte arg1, byte arg2, List<DevicesData> devicesDataList, String ipAddress, int port) {
|
if (devicesDataList == null || devicesDataList.size() == 0) {
|
return;
|
}
|
int devicesCount = 32;
|
int count = devicesDataList.size() / devicesCount;
|
int remainder = devicesDataList.size() % devicesCount;
|
if (remainder != 0) {
|
count++;
|
}
|
outter:
|
for (int j = 0; j < count; j++) {
|
byte[] bytes;
|
int remainder2;
|
if (j != count - 1
|
|| (count == 1 && remainder == 0)
|
) {
|
remainder2 = devicesCount;
|
} else {
|
remainder2 = remainder;
|
}
|
|
bytes = new byte[2 + remainder2 * 2];
|
bytes[0] = arg1;
|
bytes[1] = arg2;
|
for (int k = 0; k < remainder2; k++) {
|
if ((devicesCount * j + k >= devicesDataList.size()) || ((2 + 2 * k + 1) >= bytes.length)) {
|
break outter;
|
}
|
bytes[2 + 2 * k] = (byte) devicesDataList.get(devicesCount * j + k).getSourceSubnetID();
|
bytes[2 + 2 * k + 1] = (byte) devicesDataList.get(devicesCount * j + k).getSourceDeviceID();
|
}
|
|
String sendData = "";
|
for (int i = 0; i < bytes.length; i++) {
|
if (i % 2 != 0) {
|
if ((i / 2) == 0) {
|
sendData += (bytes[i] & 0xff) + "(随机数)";
|
} else {
|
sendData += (bytes[i] & 0xff) + "(第" + (i / 2) + "个模块),";
|
}
|
|
} else {
|
sendData += (bytes[i] & 0xff) + ",";
|
}
|
|
|
}
|
HDLLog.info("第二次发送的搜索数据:" + sendData);
|
HDLCommand.cusSendCommand(Configuration.DEVICES_SEARCH_COMMAND, 255, 255, bytes, port, ipAddress);
|
}
|
}
|
|
/**
|
* 返回设备列表
|
* 2019-10-14
|
*/
|
public static void OnDeviceListGetSuccessCallBack() {
|
// byte[] debugBytes = new byte[6 + HDLDeviceManager.devicesDataList.size() * 2];
|
// debugBytes[0] = 1;
|
// debugBytes[1] = 1;
|
// debugBytes[2] = 1;
|
// debugBytes[3] = 1;
|
// debugBytes[4] = 1;
|
// debugBytes[5] = 1;
|
//
|
// for (int i = 0; i < HDLDeviceManager.devicesDataList.size(); i++) {
|
//
|
// debugBytes[6 + i + i] = (byte) HDLDeviceManager.devicesDataList.get(i).getSourceSubnetID();
|
// debugBytes[6 + i + i + 1] = (byte) HDLDeviceManager.devicesDataList.get(i).getSourceDeviceID();
|
// String device = "第 " + (i + 1) + " 个模块:模块备注名称:" + HDLDeviceManager.devicesDataList.get(i).getRemark()
|
// + ",子网号:" + HDLDeviceManager.devicesDataList.get(i).getSourceSubnetID()
|
// + ",设备号:" + HDLDeviceManager.devicesDataList.get(i).getSourceDeviceID()
|
// + ",回路数:" + HDLDeviceManager.devicesDataList.get(i).getAppliancesInfoList().size() + "\n";
|
// HDLLog.info(device);
|
// }
|
|
EventBus.getDefault().post(new DevicesInfoEvent(HDLDeviceManager.devicesDataList, true));
|
/**搜索设备成功,执行一次保存到本地*/
|
HDLDeviceManager.saveDevicesDataList();
|
}
|
|
|
// /**
|
// * 返回设备列表
|
// */
|
// public static void deviceListCallBack() {
|
// byte[] debugBytes = new byte[6 + HDLDeviceManager.devicesDataList.size() * 2];
|
// debugBytes[0] = 1;
|
// debugBytes[1] = 1;
|
// debugBytes[2] = 1;
|
// debugBytes[3] = 1;
|
// debugBytes[4] = 1;
|
// debugBytes[5] = 1;
|
//
|
// for (int i = 0; i < HDLDeviceManager.devicesDataList.size(); i++) {
|
//
|
// debugBytes[6 + i + i] = (byte) HDLDeviceManager.devicesDataList.get(i).getSourceSubnetID();
|
// debugBytes[6 + i + i + 1] = (byte) HDLDeviceManager.devicesDataList.get(i).getSourceDeviceID();
|
// String device = "第 " + (i + 1) + " 个模块:模块备注名称:" + HDLDeviceManager.devicesDataList.get(i).getRemark()
|
// + ",子网号:" + HDLDeviceManager.devicesDataList.get(i).getSourceSubnetID()
|
// + ",设备号:" + HDLDeviceManager.devicesDataList.get(i).getSourceDeviceID()
|
// + ",回路数:" + HDLDeviceManager.devicesDataList.get(i).getAppliancesInfoList().size() + "\n";
|
// HDLLog.info(device);
|
// }
|
//
|
//// HDLUdpCore.sendTestCMD(debugBytes, HDLTest.SEND_TEST_PORT);
|
// EventBus.getDefault().post(new DevicesInfoEvent(HDLDeviceManager.devicesDataList, true));
|
//
|
//
|
// }
|
|
/**
|
* 刷新所有设备状态和备注
|
* 2019-10-15
|
*/
|
public static void refreshAllDevicesStateAndRemarks() {
|
|
if (isSearching) {
|
isRefreshAllDevicesState = false;
|
return;
|
}
|
isRefreshAllDevicesState = true;
|
for (int i = 0; i < HDLDeviceManager.listRemarks.size(); i++) {
|
HDLDeviceManager.listRemarks.get(i).setCallBack(false);
|
}
|
getDevRemarks();
|
}
|
|
/**
|
* 1S
|
* @param delayTime 单位秒
|
*/
|
public static void refreshAllDevicesState(int delayTime) {
|
if (refreshTimer != null) {
|
refreshTimer.cancel();
|
refreshTimer.purge();
|
refreshTimer = null;
|
}
|
if (isSearching) {
|
isRefreshAllDevicesState = false;
|
return;
|
}
|
|
refreshTimer = new Timer();
|
refreshTimer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
isRefreshAllDevicesState = true;
|
for (int i = 0; i < HDLDeviceManager.listRemarks.size(); i++) {
|
HDLDeviceManager.listRemarks.get(i).setCallBack(false);
|
}
|
getDevRemarks();
|
}
|
}, 1000 * delayTime);
|
}
|
|
|
/**
|
* 获取备注
|
*
|
* @param
|
*/
|
|
|
public static void getDevRemarks() {
|
if (remarkTimer != null) {
|
remarkTimer.cancel();
|
// remarkTimer.purge();
|
remarkTimer = null;
|
}
|
if ((HDLDeviceManager.listRemarks != null && HDLDeviceManager.listRemarks.size() == 0) || isSearching) {
|
isRefreshAllDevicesState = false;
|
if(HDLDeviceManager.devicesDataList != null && HDLDeviceManager.devicesDataList.size() > 0){
|
HandleSearch.OnDeviceListGetSuccessCallBack();
|
}
|
return;
|
}
|
|
|
int pos = -1;
|
for (int i = 0; i < HDLDeviceManager.listRemarks.size(); i++) {
|
if (!HDLDeviceManager.listRemarks.get(i).isCallBack()) {
|
pos = i;
|
break;
|
}
|
}
|
|
//判断是否获取设备备注完全,若完全则返回所有设备列表。
|
if (HDLDeviceManager.listRemarks != null && HDLDeviceManager.listRemarks.size() != 0 && pos == -1) {
|
if (!isRefreshAllDevicesState) {
|
HandleSearch.OnDeviceListGetSuccessCallBack();
|
} else {
|
//刷新完毕
|
// TODO: 刷新完毕可能要做某些事情
|
HDLLog.info("刷新设备状态完毕");
|
isRefreshAllDevicesState = false;
|
HandleSearch.OnDeviceListGetSuccessCallBack();
|
// /**刷新设备状态完毕,执行一次保存到本地*/
|
// HDLDeviceManager.saveDevicesDataList();
|
}
|
|
return;
|
}
|
remarkTimer = new Timer();
|
final int newPos = pos;
|
final RemarkTimes remarkTimes = new RemarkTimes();
|
remarkTimes.setCount(0);
|
|
if (remarkTimer == null) {
|
return;
|
}
|
remarkTimer.schedule(new TimerTask() {
|
@Override
|
public void run() {
|
if (remarkTimes.getCount() < 3) {
|
remarkTimes.setCount(remarkTimes.getCount() + 1);
|
if (newPos < HDLDeviceManager.listRemarks.size()) {
|
int port = 0;
|
String ipAddress = "";
|
switch (curSearchMode) {
|
case HandleSearch.GET_RCU_DEVICES:
|
ipAddress = rcuIp;
|
port = Configuration.RCU_SEND_PORT;
|
break;
|
case HandleSearch.GET_BUS_DEVICES:
|
ipAddress = HDLCommand.getLocalBroadCastIp();//2019-10-16
|
// ipAddress = HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getIpAddress();
|
port = Configuration.PORT;
|
break;
|
}
|
HDLLog.info("----->发送获取备注命令。共" + HDLDeviceManager.listRemarks.size() + "个模块。第"
|
+ newPos + "个模块。第 " + (HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getChannelNum())
|
+ " 回路,子网号:" + HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getDeviceSubnetID()
|
+ ",设备号:" + HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getDeviceDeviceID()
|
+ " 第 " + remarkTimes.getCount() + " 次" + " ip = " + ipAddress);
|
HDLCommand.cusSendCommand(Configuration.DEVICES_READ_COMMAND,
|
HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getDeviceSubnetID()
|
, HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getDeviceDeviceID(),
|
new byte[]{
|
(byte) HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getBigType(),
|
(byte) HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getLittleType(),
|
(byte) HDLDeviceManager.listRemarks.get(newPos).getAppliancesInfo().getChannelNum()},
|
port, ipAddress);
|
}
|
} else {
|
|
if (HDLDeviceManager.listRemarks != null
|
&& HDLDeviceManager.listRemarks.size() != 0
|
&& newPos < HDLDeviceManager.listRemarks.size()) {
|
HDLDeviceManager.listRemarks.get(newPos).setCallBack(true);
|
}
|
getDevRemarks();
|
}
|
}
|
}, 1, 1200);
|
|
}
|
}
|