这个bug改了我一天半的时间 真的是绞尽脑汁 首先是因为公司产品A8遥控器功能分为组1 组2 组1-2 三个组,而遥控器是属于离线设备无法上报数据 ,我们app在添加到遥控器的时候 给他设置三个groupID ,但是因为之前的机制太乱 会出现groupID给重复,引起的不同遥控器groupID却是相同的,解决方案 搜索到遥控器的时候,遍历一遍本地设备中所有的遥控器,看下哪些groupID没有被用过。再将其取出来给新遥控器赋值。
public enum GroupNumb {
GROUP0(new int[]{0x8011,0x8012,0x8013}),
GROUP1(new int[]{0x8014,0x8015,0x8016}),
GROUP2(new int[]{0x8017,0x8018 ,0x8019}),
GROUP3(new int[]{0x801a,0x801b,0x801c}),
GROUP4(new int[]{0x801d ,0x801e ,0x801f}),
GROUP5(new int[]{0x8020,0x8021,0x8022}),
GROUP6(new int[]{0x8023,0x8024,0x8025}),
GROUP7(new int[]{0x8026,0x8027,0x8028});
int[] groups;
GroupNumb(int[] groups){
this.groups = groups;
}
public int[] getGroups() {
return groups;
}
public void setGroups(int[] groups){
this.groups = groups;
}
}
给遥控分配分组ID
private boolean assignGroupIdToRemote(Device device){
if (device.getDeviceType() == ConstantDeviceType.Remote_control
|| device.getDeviceType() == ConstantDeviceType.Remote_control2) {
remoteCount++;
GroupNumb groupNumb = RemoteManage.getInstance().addRemote(device);
Log.e("weichongbin9", "分配:Remote_GroupId:" + "groupNumb:" + groupNumb );
if(groupNumb == null){
ToastHelper.makeText(getString(R.string.remote_limit_reached)).show();
//遥控器已达设备数量上限,将分组分配到任意不支持的分组值(不可为0和-1),避免拒绝遥控器加入时默认分组为-1可控所有灯
CtrlDeviceHandler.assignGroupIdToRemote(device, -10086, -10086, -10086);
return false;
}
int[] groups = groupNumb.getGroups();
Log.e("weichongbin9", "分配:Remote_GroupId:" + "groups:" + groups );
int group_first = groups[0];
int group_sec = groups[1];
int group_third = groups[2];
if(group_first == -1 || group_sec == -1 || group_third == -1){
return false;
}
//对遥控器发送分配组信息指令
CtrlDeviceHandler.assignGroupIdToRemote(device, group_first, group_sec, group_third);
Log.e("分配:Remote_GroupId",group_first+group_sec+group_third+"");
Log.e("weichongbin9", "分配:Remote_GroupId:" + "group_first:" + group_first + ",group_sec:" + group_sec+ ",group_third:" + group_third + "");
}
return true;
}
解析分组ID遍历所有的本地遥控器
/**
* 添加设备
*
* @param device
*/
public GroupNumb addRemote(Device device) {
ArrayList<Device> remoteDevices = DeviceMange.getInstance().getRemoteControls();
if(remoteDevices != null && remoteDevices.size() >= 8){
return null;
}
GroupNumb groupNumb = null;
if (!list.contains(device)) {
list.add(device);
} else {
list.set(list.indexOf(device), device);
String remoteGroupId = SharedPreferencesUtil.queryValue(Constant.BINDREMOTE + device.getMeshAddress());
groupNumb = new Gson().fromJson(remoteGroupId, GroupNumb.class);
}
remoteMap.put(device.getMeshAddress(), device);
if (groupNumb == null) {
if (unallocated == null || unallocated.size() <= 0) {
return null;
}
groupNumb = unallocated.get(0);
}
boolean isAllowAdd = true;
GroupNumb[] values = GroupNumb.values();
for (GroupNumb groups : values) {
isAllowAdd = true;
int group_first = groups.getGroups()[0];
Log.e("weichongbin11155","---准备遍历 group_first = " + group_first);
ArrayList<Device> list = DeviceMange.getInstance().getAllDevices();
for (int i = 0; i < list.size(); i++) {
Device oldDevice = list.get(i);
if(oldDevice.getGroupMap() != null && oldDevice.getGroupMap().length >= 3){
int remotegroup= oldDevice.getGroupMap()[0];
Log.e("weichongbin11155","---remotegroup----" + remotegroup + " group1 = " + oldDevice.getGroupMap()[0] + " group2 =" + oldDevice.getGroupMap()[1] + " group3 = " + oldDevice.getGroupMap()[2]);
if (remotegroup == group_first){
isAllowAdd = false;
break;
}
}
}
Log.e("weichongbin11155","---遍历结果 isAllowAdd = " + isAllowAdd);
if(isAllowAdd){
groupNumb = groups;
break;
}
}
if(!isAllowAdd){
return null;
}
// for (int j= 0; j<unallocated.size(); j++){
// groupNumb = unallocated.get(j);
// int[] groups = groupNumb.getGroups();
// Log.e("weichongbin9", "分配:Remote_GroupId:" + "groups:" + groups );
// int group_first = groups[0];
// int group_sec = groups[1];
// int group_third = groups[2];
// ArrayList<Device> list = DeviceMange.getInstance().getAllDevices();
// for (int i = 0; i < list.size(); i++) {
// device = list.get(i);
// if(device.getGroupMap() != null && device.getGroupMap().length >= 3){
// int remotegroup= device.getGroupMap()[0];
// Log.e("weichongbin11155","---remotegroup----" + remotegroup + " group1 = " + device.getGroupMap()[0] + " group2 =" + device.getGroupMap()[1] + " group3 = " + device.getGroupMap()[2]);
// if (remotegroup == group_first && remotegroup!=32086){
// group_first = group_first+3;
// group_sec = group_sec+3;
// group_third = group_third+3;
// }else if(remotegroup == group_first && remotegroup==32086){
// group_first = 32785;
// group_sec = 32786;
// group_third = 32787;
// }
// }
// }
// }
groupMap.put(device.getMeshAddress(), groupNumb);
Log.e("weichongbin11155", "groupNumb: " + groupNumb +" groupMap : " + groupMap );
allocated.add(groupNumb);
unallocated.remove(groupNumb);
String unbind = new Gson().toJson(this.unallocated);//未分配
Log.e("weichongbin9", "未分配:unbind:" + "unbind:" + unbind );
SharedPreferencesUtil.keepShared(Constant.UNALLOCATED + SharedPreferencesUtil.queryValue(Constant.LOGIN_USERNAME), unbind);
String bind = new Gson().toJson(this.allocated);//已分配
Log.e("weichongbin9", "已分配:bind:" + "bind:" + bind );
SharedPreferencesUtil.keepShared(Constant.ALLOCATED + SharedPreferencesUtil.queryValue(Constant.LOGIN_USERNAME), bind);
//将已分配的通过设备mesh保存
String remoteGroupId = new Gson().toJson(groupNumb.getGroups());
device.setGroupMap(groupNumb.getGroups());
Log.e("weichongbin11155","---准备存储 remoteGroupId = " + remoteGroupId + " 对应的设备为 = " + (Constant.BINDREMOTE + device.getMeshAddress()));
SharedPreferencesUtil.keepShared(Constant.BINDREMOTE + device.getMeshAddress(), remoteGroupId);
// if (!list.contains(device)) {
// list.add(device);
// remoteMap.put(device.getMeshAddress(), device);
// if(unallocated == null || unallocated.size() <= 0){
// return null;
// }
// groupNumb = unallocated.get(0);
// groupMap.put(device.getMeshAddress(), groupNumb);
//
// allocated.add(groupNumb);
// unallocated.remove(groupNumb);
//
// String unbind = new Gson().toJson(this.unallocated);//未分配
// SharedPreferencesUtil.keepShared(Constant.UNALLOCATED, unbind);
// String bind = new Gson().toJson(this.allocated);//已分配
// SharedPreferencesUtil.keepShared(Constant.ALLOCATED, bind);
//
// //将已分配的通过设备mesh保存
// String remoteGroupId = new Gson().toJson(groupNumb.getGroups());
// device.setGroupMap(groupNumb.getGroups());
// SharedPreferencesUtil.keepShared(Constant.BINDREMOTE + device.getMeshAddress(), remoteGroupId);
// } else {
// list.set(list.indexOf(device), device);
// String remoteGroupId = SharedPreferencesUtil.queryValue(Constant.BINDREMOTE + device.getMeshAddress());
// groupNumb = new Gson().fromJson(remoteGroupId, GroupNumb.class);
// }
return groupNumb;
}