黄学彪
2019-12-17 5986f63b75bd81c6cef262c670e9251c038cbf5d
ZigbeeApp/Shared/Phone/UserCenter/Device/Bind/ChooseKeyModePage.cs
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Com.Amap.Api.Location;
using Shared.Common;
using ZigBee.Device;
using static ZigBee.Device.BindObj;
@@ -14,14 +13,8 @@
        /// <param name="common">Common.</param>
        public void Changed(CommonDevice common)
        {
            //if (common.Type != ZigBee.Device.DeviceType.OnOffSwitch)
            //{
            //    return;
            //}
            Shared.Application.RunOnMainThread(() =>
            {
                //var dev = common as Panel;
            });
        }
@@ -32,32 +25,32 @@
        {
            this.currentKey = key;
            this.currentModeID = key.panelMode;
            this.oldModeID = key.panelMode;
            this.curDeviceBindType = deviceBindType;
            this.targetList = targetSelectList;
        }
        #region ◆ 变量申明__________________________
        ZigBee.Device.Panel currentKey;
        ZigBee.Device.Panel currentKey;//当前按键
        //显示被绑定设备或场景的view
        VerticalScrolViewLayout midVerticalScrolViewLayout;
        HorizontalScrolViewLayout btnHorizontalScrolViewLayout;
        VerticalScrolViewLayout midVerticalScrolViewLayout;//中部刷新列表
        HorizontalScrolViewLayout btnHorizontalScrolViewLayout;//房间列表左右滑动布局
        /// <summary>
        /// 按键配置的目标列表
        /// </summary>
        List<CommonDevice> targetList = new List<CommonDevice>();
        List<CommonDevice> targetList = new List<CommonDevice>();//本地临时选中列表
        /// <summary>
        /// 新的按键配置目标列表中,目标实际存在按键中,此时要删掉的数据
        /// </summary>
        List<CommonDevice> oldTargetList = new List<CommonDevice>();
        List<Room> roomList = new List<Room>();
        int currentClusterID = 6;
        public Action<List<BindListResponseObj>> action;
        int currentModeID = 0;
        int oldModeID = 0;
        string curDeviceBindType = "";
        List<CommonDevice> oldTargetList = new List<CommonDevice>();//本地旧的选中列表
        int currentClusterID = 6;//当前簇ID
        public Action<List<BindListResponseObj>> action;//回调绑定目标页面刷新
        int currentModeID = 0;//当前模式ID
        string curDeviceBindType = "";//当前设备绑定类型
        #endregion
        /// <summary>
        /// 显示按键模式页
        /// </summary>
        public void Show()
        {
            this.TopFrameLayout(this, Language.StringByID(R.MyInternationalizationString.ChooseKeyMode));
@@ -73,6 +66,9 @@
            MidFrameLayoutContent();
        }
        /// <summary>
        /// 中部布局
        /// </summary>
        async void MidFrameLayoutContent()
        {
            #region UI
@@ -104,11 +100,7 @@
            #endregion
            #region 数据处理
            roomList = Shared.Common.Room.Lists;
            Shared.Common.Room curRoom = roomList[0];
            int index = 0;
            KeyModeList(roomList[index]);
            KeyModeList();
            btnFinifh.MouseUpEventHandler += async (sender, e) =>
            {
                oldTargetList.Clear();
@@ -122,17 +114,15 @@
                    if (currentKey.bindList != null)
                    {
                        var bindResult = await currentKey.ClearBindInfoAsync();
                        if (bindResult != null && bindResult.clearBindInfoResponseData != null)
                        {
                            if (bindResult.clearBindInfoResponseData.Result != 0)
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    CommonPage.Loading.Hide();
                                    new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                                });
                                CommonPage.Loading.Hide();
                                this.RemoveFromParent();
                                btnFinifh.Enable = true;
                                btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                                return;
@@ -146,52 +136,14 @@
                        {
                            Application.RunOnMainThread(() =>
                            {
                                CommonPage.Loading.Hide();
                                new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                            });
                            CommonPage.Loading.Hide();
                            btnFinifh.Enable = true;
                            btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                            return;
                        }
                    }
                    //if (oldModeID != 65535 && oldModeID != currentModeID)
                    //{
                    //var bindResult = await currentKey.ClearBindInfoAsync();
                    //if (bindResult.clearBindInfoResponseData != null)
                    //{
                    //    if (bindResult.clearBindInfoResponseData.Result != 0)
                    //    {
                    //        Application.RunOnMainThread(() =>
                    //        {
                    //            new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                    //        });
                    //        CommonPage.Loading.Hide();
                    //        this.RemoveFromParent();
                    //        btnFinifh.Enable = true;
                    //        return;
                    //    }
                    //    else
                    //    {
                    //        currentKey.bindList.Clear();
                    //    }
                    //}
                    //}
                    //else
                    //{
                    //    foreach (var bindedDev in currentKey.bindList)
                    //    {
                    //        for (int i = 0; i < targetList.Count; i++)
                    //        {
                    //            var de = targetList[i];
                    //            if (de.DeviceAddr == bindedDev.BindMacAddr && de.DeviceEpoint == bindedDev.BindEpoint)
                    //            {
                    //                oldTargetList.Add(de);
                    //                targetList.RemoveAt(i);
                    //                i--;
                    //            }
                    //        }
                    //    }
                    //}
                    //2、配置模式
                    Panel.SetWritableValueResponAllData temp = null;
@@ -237,177 +189,148 @@
                            temp = await currentKey.ConfigureHdlKeyValueAsync(Panel.KeyMode.Wcd_Down_Stop);
                            break;
                    }
                    if (temp != null && temp.setWritableValueResponData != null && temp.setWritableValueResponData.Status == 0)
                    if (temp != null && temp.setWritableValueResponData != null)
                    {
                        ////删除被勾掉的数据
                        //foreach (var delDev in oldTargetList)
                        //{
                        //    var delDev1 = new BindObj.BindListResponseObj();
                        //    delDev1.BindType = 0;
                        //    delDev1.BindCluster = currentClusterID;
                        //    delDev1.BindMacAddr = delDev.DeviceAddr;
                        //    delDev1.BindEpoint = delDev.DeviceEpoint;
                        //    var result = await BindInfo.RemoveTargets(delDev1, currentKey);
                        //    if (result == 0)
                        //    {
                        //        var bindSc = currentKey.bindList.Find(obj =>
                        //        (obj.BindType == 0
                        //        && obj.BindCluster == currentClusterID
                        //        && obj.BindMacAddr == delDev.DeviceAddr
                        //        && obj.BindEpoint == delDev.DeviceEpoint
                        //        ));
                        //        currentKey.bindList.Remove(bindSc);
                        //    }
                        //}
                        //3、绑定新的数据
                        var addBindeDev = new AddBindData();
                        addBindeDev.DeviceAddr = currentKey.DeviceAddr;
                        addBindeDev.Epoint = currentKey.DeviceEpoint;
                        foreach (var de in targetList)
                        if (temp.setWritableValueResponData.Status == 0)
                        {
                            if (de.DeviceAddr != null)
                            //3、绑定新的数据
                            var addBindeDev = new AddBindData();
                            addBindeDev.DeviceAddr = currentKey.DeviceAddr;
                            addBindeDev.Epoint = currentKey.DeviceEpoint;
                            foreach (var de in targetList)
                            {
                                var addBindInfo = new AddBindListObj();
                                addBindInfo.BindType = 0;
                                addBindInfo.BindCluster = currentClusterID;
                                addBindInfo.BindMacAddr = de.DeviceAddr;
                                addBindInfo.BindEpoint = de.DeviceEpoint;
                                addBindeDev.BindList.Add(addBindInfo);
                                if (de.DeviceAddr != null)
                                {
                                    var addBindInfo = new AddBindListObj();
                                    addBindInfo.BindType = 0;
                                    addBindInfo.BindCluster = currentClusterID;
                                    addBindInfo.BindMacAddr = de.DeviceAddr;
                                    addBindInfo.BindEpoint = de.DeviceEpoint;
                                    addBindeDev.BindList.Add(addBindInfo);
                                }
                            }
                        }
                        var dev = new AddedDeviceBindResponseAllData();
                        dev = await currentKey.AddDeviceBindAsync(addBindeDev);
                        if (dev.addedDeviceBindResponseData == null)
                        {
                            Application.RunOnMainThread(() =>
                            //是亮度再次发6命令
                            if (currentClusterID == 8)
                            {
                                new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                            });
                            btnFinifh.Enable = true;
                            btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                                foreach (var de in targetList)
                                {
                                    if (de.DeviceAddr != null)
                                    {
                                        var addBindInfo = new AddBindListObj();
                                        addBindInfo.BindType = 0;
                                        addBindInfo.BindCluster = 6;
                                        addBindInfo.BindMacAddr = de.DeviceAddr;
                                        addBindInfo.BindEpoint = de.DeviceEpoint;
                                        addBindeDev.BindList.Add(addBindInfo);
                                    }
                                }
                            }
                            var dev = new AddedDeviceBindResponseAllData();
                            dev = await currentKey.AddDeviceBindAsync(addBindeDev);
                            if (dev.addedDeviceBindResponseData == null)
                            {
                                Application.RunOnMainThread(() =>
                                {
                                    CommonPage.Loading.Hide();
                                    new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                                });
                                btnFinifh.Enable = true;
                                btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                                return;
                            }
                            else
                            {
                                if (dev.addedDeviceBindResponseData.Result == 0)
                                {
                                    if (currentClusterID == 8)
                                    {
                                        foreach (var de in dev.addedDeviceBindResponseData.BindList)
                                        {
                                            if (de.BindCluster == 8)
                                            {
                                                currentKey.bindList.Add(de);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        currentKey.bindList = dev.addedDeviceBindResponseData.BindList;
                                    }
                                    if (action != null)
                                    {
                                        action(currentKey.bindList);
                                    }
                                    this.RemoveFromParent();
                                    targetList.Clear();
                                    CommonPage.Loading.Hide();
                                    btnFinifh.Enable = true;
                                    btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                                }
                                else if (dev.addedDeviceBindResponseData.Result == 1)
                                {
                                    Application.RunOnMainThread(() =>
                                    {
                                        CommonPage.Loading.Hide();
                                        new Tip()
                                        {
                                            MaxWidth = 150,
                                            Text = Language.StringByID(R.MyInternationalizationString.BindFailed),
                                            Direction = AMPopTipDirection.Up,
                                            CloseTime = 1
                                        }.Show(btnFinifh);
                                    });
                                    btnFinifh.Enable = true;
                                    btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                                    return;
                                }
                                else
                                {
                                    Application.RunOnMainThread(() =>
                                    {
                                        CommonPage.Loading.Hide();
                                        new Tip()
                                        {
                                            MaxWidth = 150,
                                            Text = Language.StringByID(R.MyInternationalizationString.BindUnknownError),
                                            Direction = AMPopTipDirection.Up,
                                            CloseTime = 1
                                        }.Show(btnFinifh);
                                    });
                                    btnFinifh.Enable = true;
                                    btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                                    return;
                                }
                            }
                        }
                        else
                        {
                            foreach (var ab in dev.addedDeviceBindResponseData?.BindList)
                            Application.RunOnMainThread(() =>
                            {
                                switch (ab.Result)
                                {
                                    case 0:
                                        currentKey.bindList.Add((BindListResponseObj)ab);
                                        for (int i = 0; i < targetList.Count; i++)
                                        {
                                            var de = targetList[i];
                                            var sc1 = new SceneUI();
                                            if (de.DeviceAddr == null)
                                            {
                                                sc1 = (SceneUI)de;
                                                if (sc1.Id == ab.BindScenesId)
                                                {
                                                    targetList.RemoveAt(i);
                                                    i--;
                                                }
                                            }
                                        }
                                        isFinish = true;
                                        break;
                                    case 1:
                                        Application.RunOnMainThread(() =>
                                        {
                                            new Tip()
                                            {
                                                MaxWidth = 150,
                                                Text = Language.StringByID(R.MyInternationalizationString.BindErrorAbsence),
                                                Direction = AMPopTipDirection.Up,
                                                CloseTime = 1
                                            }.Show(btnFinifh);
                                        });
                                        break;
                                    case 2:
                                        var abr = dev.addBindResultResponseData?.Result;
                                        switch (dev.addBindResultResponseData?.Result)
                                        {
                                            case 0:
                                                currentKey.bindList.Add((BindListResponseObj)ab);
                                                for (int i = 0; i < targetList.Count; i++)
                                                {
                                                    var de = targetList[i];
                                                    if (de.DeviceAddr == ab.BindMacAddr && de.DeviceEpoint == ab.BindEpoint)
                                                    {
                                                        targetList.RemoveAt(i);
                                                        i--;
                                                    }
                                                }
                                                isFinish = true;
                                                break;
                                            case 140:
                                                Application.RunOnMainThread(() =>
                                                {
                                                    new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.BindFull), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                                                });
                                                break;
                                            default:
                                                Application.RunOnMainThread(() =>
                                                {
                                                    new Tip() { MaxWidth = 150, Text = ab.ESName + Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                                                });
                                                break;
                                        }
                                        break;
                                    case 3:
                                        Application.RunOnMainThread(() =>
                                        {
                                            new Tip() { MaxWidth = 150, Text = ab.ESName + Language.StringByID(R.MyInternationalizationString.BindErrorExisted), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                                        });
                                        break;
                                    case 4:
                                        Application.RunOnMainThread(() =>
                                        {
                                            new Tip() { MaxWidth = 150, Text = ab.ESName + Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                                        });
                                        break;
                                    default:
                                        Application.RunOnMainThread(() =>
                                        {
                                            new Tip() { MaxWidth = 150, Text = ab.ESName + Language.StringByID(R.MyInternationalizationString.BindFailed), Direction = AMPopTipDirection.Up, CloseTime = 1 }.Show(btnFinifh);
                                        });
                                        break;
                                }
                            }
                                CommonPage.Loading.Hide();
                                new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.ChangeBindMode), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnFinifh);
                            });
                            btnFinifh.Enable = true;
                            btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                            return;
                        }
                    }
                    else
                    {
                        Application.RunOnMainThread(() =>
                        {
                            new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.ChangeBindMode), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnFinifh);
                            CommonPage.Loading.Hide();
                            new Tip() { MaxWidth = 150, Text = Language.StringByID(R.MyInternationalizationString.GwResponseOvertime), Direction = AMPopTipDirection.Up, CloseTime = 2 }.Show(btnFinifh);
                        });
                        btnFinifh.Enable = true;
                        btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
                        return;
                    }
                }
                catch { }
                finally
                catch
                {
                    if (isFinish)
                    {
                        if (action != null)
                        {
                            action(currentKey.bindList);
                        }
                        this.RemoveFromParent();
                    }
                    else
                    {
                        this.RemoveFromParent();
                    }
                    CommonPage.Loading.Hide();
                    btnFinifh.Enable = true;
                    btnFinifh.BackgroundColor = Shared.Common.ZigbeeColor.Current.XMBlack;
@@ -421,10 +344,9 @@
        /// </summary>
        /// <param name="gateway">Gateway.</param>
        /// <param name="key">Key.</param>
        void KeyModeList(Shared.Common.Room curRoom)
        private void KeyModeList()
        {
            midVerticalScrolViewLayout.RemoveAll();
            switch (curDeviceBindType)
            {
                case "AddSwitch":
@@ -442,7 +364,7 @@
            }
        }
        public void AddSwitchMode()
        private void AddSwitchMode()
        {
            Button oldMode = null;
            for (int i = 0; i < 3; i++)
@@ -520,6 +442,15 @@
                    btnChoose.Visible = true;
                    oldMode = btnChoose;
                }
                else if ((currentKey.panelMode == 65535) && i == 0)
                {
                    //默认第一个
                    btnChoose.IsSelected = true;
                    btnChoose.Visible = true;
                    oldMode = btnChoose;
                    currentModeID = 100;
                    currentKey.panelMode = currentModeID;
                }
                EventHandler<MouseEventArgs> hander = async (sender, e) =>
                 {
@@ -558,7 +489,7 @@
            }
        }
        public void AddDimmerMode()
        private void AddDimmerMode()
        {
            Button oldMode = null;
            for (int i = 0; i < 5; i++)
@@ -656,6 +587,15 @@
                    btnChoose.Visible = true;
                    oldMode = btnChoose;
                }
                else if ((currentKey.panelMode == 65535) && i == 0)
                {
                    //默认第一个
                    btnChoose.IsSelected = true;
                    btnChoose.Visible = true;
                    oldMode = btnChoose;
                    currentModeID = 200;
                    currentKey.panelMode = currentModeID;
                }
                EventHandler<MouseEventArgs> hander = (sender, e) =>
                {
@@ -704,7 +644,7 @@
            }
        }
        public void AddCurtainMode()
        private void AddCurtainMode()
        {
            Button oldMode = null;
            for (int i = 0; i < 5; i++)
@@ -802,6 +742,15 @@
                    btnChoose.Visible = true;
                    oldMode = btnChoose;
                }
                else if ((currentKey.panelMode == 65535) && i == 0)
                {
                    //默认第一个
                    btnChoose.IsSelected = true;
                    btnChoose.Visible = true;
                    oldMode = btnChoose;
                    currentModeID = 300;
                    currentKey.panelMode = currentModeID;
                }
                EventHandler<MouseEventArgs> hander = (sender, e) =>
                {
@@ -849,6 +798,11 @@
            }
        }
        private void FinisfhEventHandler(object sender, MouseEventArgs e)
        {
        }
        public void DeviceInfoChange(CommonDevice common, string typeTag)
        {
        }