1. 程式人生 > >IOT-15893 多個遙控器時,在一個遙控器上設定控制裝置,使用另一個遙控器也可控制

IOT-15893 多個遙控器時,在一個遙控器上設定控制裝置,使用另一個遙控器也可控制

這個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;
    }