package com.hdl.sdk.hdl_core.HDLDeviceManger.Core;
|
|
import android.content.Context;
|
import android.util.Log;
|
import android.widget.Toast;
|
|
import com.hdl.sdk.hdl_core.Config.Configuration;
|
import com.hdl.sdk.hdl_core.Config.ZigbeeDeviceID;
|
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.Bean.ZigbeeBean.ZigbeeDeviceListBean;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeDeviceSaveBean;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeGatewayInfoBean;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeSceneListBean;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Bean.ZigbeeBean.ZigbeeSceneSaveBean;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Callback.PushCallback;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.DevicesInfoEvent;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.EventBusEvent.ZigbeeDeviceInfoEvent;
|
import com.hdl.sdk.hdl_core.HDLDeviceManger.Parser.DeviceParser;
|
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.sql.Time;
|
import java.util.ArrayList;
|
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 boolean isZigbee;//开放zigbee家居选择
|
|
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;//组播
|
private static List<ZigbeeDeviceSaveBean> zigbeeDeviceSaveBeans = new ArrayList<>();//设备列表
|
private static List<ZigbeeSceneSaveBean> zigbeeSceneSaveBeans = new ArrayList<>();//设备场景列表
|
private static Context mContext;
|
|
/**
|
* 酒店搜索
|
* <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);
|
}
|
|
/**
|
* 家居搜索
|
* 获取Bus模块设备 isZigbee标注进入zigbee模式
|
*/
|
public static void getHomeDevices(final Context context, boolean isZigbee) {
|
if (isZigbee){
|
HDLUdpCore.closeSocket6008();
|
HDLUdpCore.closeSocket6000();
|
if (HDLDeviceManager.devicesDataList != null){
|
HDLDeviceManager.devicesDataList.clear();
|
}
|
mContext = context;
|
HDLZigbeeMqtt.setIsCancel(true);
|
PushCallback.setIsGateway(true);
|
//SearchZigbeeGateway.release();
|
HDLCommandText.reSearch();
|
searchGateways(context);
|
}else {
|
SPUtils.setParam(context, SPUtils.KEY_RCU_IP_, "");
|
curSearchMode = GET_BUS_DEVICES;
|
rcuIp = "";
|
searchDevices(GET_BUS_DEVICES, SEARCH_BROACAST, Configuration.PORT);//家居搜索
|
}
|
}
|
|
public static void reSearch(){
|
HDLZigbeeMqtt.setIsCancel(true);
|
HDLCommandText.reSearch();
|
searchGateways(mContext);
|
}
|
|
/**
|
* 搜索zigbee网关
|
*/
|
private static void searchGateways(final Context context){
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
SearchZigbeeGateway.sendSearchGateway(new SearchZigbeeGateway.OnSearchListener() {
|
@Override
|
public void onSuccess(final List<ZigbeeGatewayInfoBean> zigbeeGatewayInfoBeanList) {
|
final int size = zigbeeGatewayInfoBeanList.size();
|
Log.d("11113", "网关数量为: "+size);
|
if (zigbeeDeviceSaveBeans != null){
|
zigbeeDeviceSaveBeans.clear();
|
}
|
if (zigbeeSceneSaveBeans != null){
|
zigbeeSceneSaveBeans.clear();
|
}
|
if (zigbeeGatewayInfoBeanList.size() != 0){
|
searchZigbeeDevices(context,zigbeeGatewayInfoBeanList, zigbeeGatewayInfoBeanList.get(0).getIpAddress(),
|
zigbeeGatewayInfoBeanList.get(0).getGatewayId(),0,size);
|
|
}else {
|
HDLLog.info("搜不到网关请,再搜索");
|
}
|
}
|
|
@Override
|
public void onEorror(Exception e) {
|
HDLLog.info("搜索网关失败"+e.toString());
|
}
|
|
});
|
}
|
}).start();
|
}
|
|
/**
|
* 搜索所有网关的设备
|
*/
|
private static void searchZigbeeDevices(Context context,final List<ZigbeeGatewayInfoBean> zigbeeGatewayInfoBeanList,final String ipAddr,final String gatewayId,final int subnetId,final int gatewaySize){
|
HDLCommandText.initData();
|
HDLZigbeeMqtt.connect1(ipAddr);
|
HDLCommandText hdlCommandText = new HDLCommandText();
|
hdlCommandText.setIpAddr(ipAddr);
|
HDLZigbeeMqtt.sendMqttData1(Configuration.SEARCH_SCENE,Configuration.SCENE_JSON);//搜索该网关下所有的场
|
new Thread(new Runnable() {
|
@Override
|
public void run() {
|
try {
|
Thread.sleep(1000);
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
}
|
}).start();
|
HDLZigbeeMqtt.sendMqttData1(Configuration.SEARCH_DEVICE,Configuration.SEARVH_JSON);//搜索该网关下所有设备
|
handleDeviceCallbackData(context,zigbeeGatewayInfoBeanList, ipAddr, gatewayId,subnetId,gatewaySize);
|
}
|
|
private static void handleDeviceCallbackData(final Context context, final List<ZigbeeGatewayInfoBean> zigbeeGatewayInfoBeanList, final String ipAddr, final String gatewayId, final int subnetId, final int gatewaySize) {
|
HDLCommandText.handleDeviceData(new HDLCommandText.DataCallBack() {
|
@Override
|
public void onSuccess(List<ZigbeeDeviceListBean> zigbeeDeviceListBeans, ZigbeeSceneListBean zigbeeSceneListBean) {
|
Log.d("11113", "传输的数据数量: "+zigbeeDeviceListBeans.size());
|
Log.d("11113", "subnetId: "+subnetId);
|
Log.d("11113", "gatewaySize: "+gatewaySize);
|
Log.d("11113", "传输过来场景的数量: "+zigbeeSceneListBean.getData().getScenesSum());
|
//处理设备数据
|
for (int i = 0; i < zigbeeDeviceListBeans.size(); i++){
|
Log.d("11113", "handleDeviceCallbackData: 执行到这"+i);
|
ZigbeeDeviceSaveBean objectBeans = new ZigbeeDeviceSaveBean() ;
|
objectBeans.setIpAddress(ipAddr);
|
objectBeans.setGatewayId(gatewayId);
|
Log.d("11113", "setGatewayId: "+gatewayId);
|
objectBeans.setmSubnetID(subnetId);//标注子网号,也就是网关
|
objectBeans.setDevice_ID(zigbeeDeviceListBeans.get(i).getDevice_ID());
|
objectBeans.setDeviceAddr(zigbeeDeviceListBeans.get(i).getDeviceAddr());
|
objectBeans.setDeviceName(zigbeeDeviceListBeans.get(i).getData().getDeviceName());
|
objectBeans.setEpoint(zigbeeDeviceListBeans.get(i).getEpoint());
|
objectBeans.setMacName(zigbeeDeviceListBeans.get(i).getData().getMacName());
|
objectBeans.setInClusterList(zigbeeDeviceListBeans.get(i).getData().getInClusterList());
|
zigbeeDeviceSaveBeans.add(objectBeans);
|
}
|
//处理场景数据
|
if (zigbeeSceneListBean.getData().getScenesSum() != 0){
|
for (int i = 0 ; i < zigbeeSceneListBean.getData().getScenesSum(); i++){
|
ZigbeeSceneSaveBean zigbeeSceneSaveBean = new ZigbeeSceneSaveBean();
|
zigbeeSceneSaveBean.setIpAddr(ipAddr);
|
zigbeeSceneSaveBean.setSubnetId(subnetId);
|
zigbeeSceneSaveBean.setScenesId(zigbeeSceneListBean.getData().getScenesLists().get(i).getScenesId());
|
zigbeeSceneSaveBean.setScenesName(zigbeeSceneListBean.getData().getScenesLists().get(i).getScenesName());
|
zigbeeSceneSaveBean.setIsOpen(zigbeeSceneListBean.getData().getScenesLists().get(i).getIsOpen());
|
zigbeeSceneSaveBeans.add(zigbeeSceneSaveBean);
|
}
|
}
|
//遍历完再次发起搜索
|
if (gatewaySize > 1 && subnetId != gatewaySize-1){//网关为两个
|
searchZigbeeDevices(context,zigbeeGatewayInfoBeanList, zigbeeGatewayInfoBeanList.get(1).getIpAddress(),
|
zigbeeGatewayInfoBeanList.get(1).getGatewayId(),1,gatewaySize);
|
}
|
// else if (gatewaySize > 2 && subnetId != gatewaySize-1 && subnetId != 1){//网关为三个
|
// searchZigbeeDevices(context,zigbeeGatewayInfoBeanList, zigbeeGatewayInfoBeanList.get(2).getIpAddress(),
|
// zigbeeGatewayInfoBeanList.get(2).getGatewayId(),2,gatewaySize);
|
// }
|
else {
|
handleZigbee2Bus(context,zigbeeSceneSaveBeans);
|
}
|
}
|
});
|
}
|
//新建对应的BUS数据
|
private static void handleZigbee2Bus( Context context,List<ZigbeeSceneSaveBean> zigbeeSceneSaveBeans){
|
for (int i = 0; i < zigbeeDeviceSaveBeans.size(); i++){
|
Log.d("11113", "handleZigbee2Bus: "+i);
|
int deviceId = zigbeeDeviceSaveBeans.get(i).getDevice_ID();
|
int mSubnetID = zigbeeDeviceSaveBeans.get(i).getmSubnetID();
|
int mChannelNum = zigbeeDeviceSaveBeans.get(i).getEpoint();
|
String mChannelRamark = zigbeeDeviceSaveBeans.get(i).getDeviceName();
|
String mparentRemarks = zigbeeDeviceSaveBeans.get(i).getMacName();
|
if (deviceId == ZigbeeDeviceID.OnOffOutput){
|
//继电器
|
DeviceParser.addDevicesListWithoutSearching(Configuration.LIGTH_BIG_TYPE, 1,
|
mSubnetID, i, mChannelNum, mChannelRamark,"继电器("+ mparentRemarks+")",false);
|
zigbeeDeviceSaveBeans.get(i).setDeviceMark(i);
|
}
|
if (deviceId == ZigbeeDeviceID.DimmableLight){
|
//调光设备
|
DeviceParser.addDevicesListWithoutSearching(Configuration.LIGTH_BIG_TYPE, 0,
|
mSubnetID, i, mChannelNum, mChannelRamark, "调光设备("+mparentRemarks+")",false);
|
zigbeeDeviceSaveBeans.get(i).setDeviceMark(i);
|
}
|
if (deviceId == ZigbeeDeviceID.WindowCoveringDevice){
|
//窗帘设备
|
DeviceParser.addDevicesListWithoutSearching(Configuration.CURTAIN_BIG_TYPE,0,
|
mSubnetID, i, mChannelNum, mChannelRamark, "窗帘设备("+mparentRemarks+")",false);
|
zigbeeDeviceSaveBeans.get(i).setDeviceMark(i);
|
}
|
if (deviceId == ZigbeeDeviceID.Thermostat){
|
//恒温器(空调)
|
DeviceParser.addDevicesListWithoutSearching(Configuration.AIR_BIG_TYPE,0,
|
mSubnetID,i,mChannelNum,mChannelRamark,"空调设备("+mparentRemarks+")",false);
|
zigbeeDeviceSaveBeans.get(i).setDeviceMark(i);
|
}
|
if (deviceId == ZigbeeDeviceID.AirPanel){
|
//新风
|
DeviceParser.addDevicesListWithoutSearching(Configuration.FRESH_AIR_BIG_TYPE,0,
|
mSubnetID,i,mChannelNum,mChannelRamark,"新风设备("+mparentRemarks+")",false);
|
zigbeeDeviceSaveBeans.get(i).setDeviceMark(i);
|
}
|
}
|
//生成场景数据
|
if (zigbeeSceneSaveBeans.size()!=0){
|
for (int i = 0 , k = 200; i < zigbeeSceneSaveBeans.size(); i++,k++){
|
zigbeeSceneSaveBeans.get(i).setDeviceid(k);
|
DeviceParser.addScenesDevicesListWithoutSearching(zigbeeSceneSaveBeans.get(i).getSubnetId(), k,
|
zigbeeSceneSaveBeans.get(i).getScenesId(), zigbeeSceneSaveBeans.get(i).getScenesId(),
|
zigbeeSceneSaveBeans.get(i).getScenesName(), zigbeeSceneSaveBeans.get(i).getScenesName(),
|
false);
|
}
|
}
|
DeviceParser.addDevicesListWithoutSearching(Configuration.GLOBAL_LOGIC_BIG_TYPE,0,
|
30,254,20,"结束标志(无作用)","结束标志(无作用)",true);
|
//存储场景数据
|
Log.d("11115", "数据大小为: "+zigbeeSceneSaveBeans.size());
|
SPUtils.setZigbeeSceneSaveBean(context,SPUtils.KEY_ZIGBEE_SCENE_IP,zigbeeSceneSaveBeans);
|
//存储设备数据
|
SPUtils.setZigbeeDeviceSaveBean(context,SPUtils.KEY_ZIGBEE_IP,zigbeeDeviceSaveBeans);
|
EventBus.getDefault().post(new ZigbeeDeviceInfoEvent(zigbeeDeviceSaveBeans, true));
|
if (zigbeeDeviceSaveBeans != null){
|
zigbeeDeviceSaveBeans.clear();
|
}
|
}
|
|
|
|
|
|
|
/**
|
* 初始化搜索
|
* @param searchMode 搜索模式
|
* @param baseSearchType 基础搜索类型
|
* @param port
|
* @param
|
*/
|
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(boolean isZigbee) {
|
if(isZigbee){
|
return;
|
}
|
|
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);
|
|
}
|
}
|