WJC
2020-04-02 94e4e5b9fd3da964c44b7b14227d6fe2bbb426d7
ZigbeeApp/Shared/Phone/Device/Light/OnOffControl.cs
@@ -20,7 +20,7 @@
        /// <summary>
        /// The action.
        /// </summary>
        public Action action;
        public Action<CommonDevice, Common.Room> action;
        /// <summary>
        /// The light image.
@@ -33,7 +33,7 @@
        /// <summary>
        /// 传过来的设备
        /// </summary>
        private DeviceUI device;
        private CommonDevice device;
        /// <summary>
        /// 传过来的房间
        /// </summary>
@@ -78,31 +78,7 @@
        #endregion
        #region ◆ 接口__________________________
        /// <summary>
        /// 处理变化事件 --将弃用 改用DeviceInfoChange()
        /// </summary>
        /// <returns>The changed.</returns>
        /// <param name="common">Common.</param>
        public void Changed(CommonDevice common)
        {
        }
        /// <summary>
        /// Changeds the IL ogic status.
        /// </summary>
        /// <param name="logic">Logic.</param>
        public void ChangedILogicStatus(ZigBee.Device.Logic logic)
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// Changeds the IS cene status.
        /// </summary>
        /// <param name="scene">Scene.</param>
        public void ChangedISceneStatus(Scene scene)
        {
            //throw new NotImplementedException();
        }
        /// <summary>
        /// 设备状态更新接口
        /// <para>type:如果为 DeviceInComingRespon:设备新上报</para>
@@ -123,20 +99,20 @@
                    {
                        var deviceUI = device;
                        //设备为空
                        if (deviceUI.CommonDevice == null)
                        if (deviceUI == null)
                        {
                            return;
                        }
                        //是否为当前设备
                        if (deviceUI.CommonDevice.DeviceEpoint != common.DeviceEpoint || deviceUI.CommonDevice.DeviceAddr != common.DeviceAddr)
                        if (deviceUI.DeviceEpoint != common.DeviceEpoint || deviceUI.DeviceAddr != common.DeviceAddr)
                        {
                            return;
                        }
                        if (deviceUI.CommonDevice.Type == DeviceType.OnOffOutput)
                        if (deviceUI.Type == DeviceType.OnOffOutput)
                        {
                            if (common.DeviceStatusReport.CluterID == 6)
                            {
                                var light = deviceUI.CommonDevice as ToggleLight;
                                var light = deviceUI as ToggleLight;
                                light.DeviceStatusReport = common.DeviceStatusReport;
                                //记录、更新状态
                                if (light.DeviceStatusReport.AttriBute == null || light.DeviceStatusReport.AttriBute.Count == 0)
@@ -144,8 +120,22 @@
                                    return;
                                }
                                light.OnOffStatus = light.DeviceStatusReport.AttriBute[0].AttriButeData;
                                SetONOFFStatu(light.OnOffStatus == 1);
                                StatuBtn.Text = $"{Language.StringByID(R.MyInternationalizationString.Current)} {device.GetDeviceStatu()}";
                                StatuBtn.Text = Language.StringByID(R.MyInternationalizationString.Current) + "   " + UserCenter.HdlDeviceOtherLogic.Current.GetDeviceStatu(device);
                                if (device.DfunctionType == DeviceFunctionType.A开关)
                                {
                                    SetONOFFStatu(light.OnOffStatus == 1);
                                }
                                else if (device.DfunctionType == DeviceFunctionType.A插座)
                                {
                                    deviceIMG.IsSelected = switchBtn.IsSelected = light.OnOffStatus == 1;
                                }
                                else
                                {
                                    deviceIMG.IsSelected = switchBtn.IsSelected = light.OnOffStatus == 1;
                                }
                                light.LastDateTime = DateTime.Now;
                            }
                        }
@@ -164,18 +154,18 @@
                    {
                        var deviceUI = device;
                        //设备为空
                        if (deviceUI.CommonDevice == null)
                        if (deviceUI == null)
                        {
                            return;
                        }
                        //是否为当前设备
                        if (deviceUI.CommonDevice.DeviceEpoint != common.DeviceEpoint || deviceUI.CommonDevice.DeviceAddr != common.DeviceAddr)
                        if (deviceUI.DeviceEpoint != common.DeviceEpoint || deviceUI.DeviceAddr != common.DeviceAddr)
                        {
                            return;
                        }
                        deviceUI.CommonDevice.IsOnline = common.IsOnline;
                        deviceUI.IsOnline = common.IsOnline;
                        //OnBtn.IsSelected = OffBtn.IsSelected= deviceUI.CommonDevice.IsOnline == 1;
                        deviceUI.CommonDevice.LastDateTime = DateTime.Now;
                        deviceUI.LastDateTime = DateTime.Now;
                    }
                    catch (Exception ex)
                    {
@@ -193,8 +183,8 @@
        public override void RemoveFromParent()
        {
            ZbGateway.StatusList.Remove(this);
            //action();
            //action = null;
            action(device, room);
            action = null;
            RemoveUpdateControlDeviceStatuAction();
            if (IsDrawerLockMode)
            {
@@ -216,10 +206,10 @@
        /// </summary>
        /// <param name="dev">Device.</param>
        /// <param name="room">Room.</param>
        public void Show(DeviceUI dev, Shared.Common.Room room)
        public void Show(CommonDevice dev, Shared.Common.Room room)
        {
            device = dev;
            zbGateway = this.device.CommonDevice.Gateway;
            zbGateway = this.device.Gateway;
            this.room = room;
            AddTop();
@@ -227,7 +217,7 @@
            AddBodyView(device);
            var light = dev.CommonDevice as ToggleLight;
            var light = dev as ToggleLight;
            //补上非远程
            if (light.Gateway == null)
            {
@@ -237,7 +227,7 @@
            {
                UserHomeView.ReadStatus(light, () =>
                {
                    ReadDeviceAttributeLogic.Instance.SendLightStatuComand(device.CommonDevice);
                    ReadDeviceAttributeLogic.Instance.SendLightStatuComand(device);
                });
            }
            else
@@ -245,12 +235,11 @@
                //防止短时间内多次读取状态
                if (CommonPage.ReadDeviceStatuSpan < (DateTime.Now - light.LastDateTime).TotalSeconds)
                {
                    ReadDeviceAttributeLogic.Instance.SendLightStatuComand(device.CommonDevice);
                    ReadDeviceAttributeLogic.Instance.SendLightStatuComand(device);
                }
            }
            var de = Shared.Common.Room.LoveRoomDeviceUIFilePathList.Find((obj) => obj == device.FileName);
            if (de == null)
            if(UserCenter.HdlRoomLogic.Current.IsCollectInRoom(device) == false)
            {
                collectionBtn.IsSelected = false;
            }
@@ -297,7 +286,7 @@
        /// <summary>
        /// AddBodyView
        /// </summary>
        public void AddBodyView(DeviceUI device)
        public void AddBodyView(CommonDevice device)
        {
            bodyFrameLayout = new FrameLayout()
            {
@@ -335,7 +324,7 @@
                Width = Application.GetRealWidth(500),
                Height = Application.GetRealHeight(60),
                Gravity = Gravity.CenterHorizontal,
                Text = device.CommonDevice.DeviceEpointName,
                Text = Common.LocalDevice.Current.GetDeviceEpointName(device),
                TextColor = ZigbeeColor.Current.GXCTextBlackColor,
                TextSize = 15,
                IsBold=true
@@ -349,44 +338,10 @@
                Height = Application.GetRealHeight(60),
                Gravity = Gravity.CenterHorizontal,
                TextColor = ZigbeeColor.Current.GXCTextGrayColor,
                Text = $"{Language.StringByID(R.MyInternationalizationString.Current)} {device.GetDeviceStatu()}",
                Text = Language.StringByID(R.MyInternationalizationString.Current) + "   " + UserCenter.HdlDeviceOtherLogic.Current.GetDeviceStatu(device),
                TextSize = 10
            };
            itemView.AddChidren(StatuBtn);
            deviceIMG = new Button()
            {
                Y = Application.GetRealHeight(340),
                Width = Application.GetMinRealAverage(579),
                Height = Application.GetMinRealAverage(579),
                Gravity = Gravity.CenterHorizontal,
                UnSelectedImagePath = "Light/OnOff.png",
            };
            itemView.AddChidren(deviceIMG);
            OffBtn = new Button
            {
                X = Application.GetRealWidth(294),
                Y = Application.GetRealHeight(588),
                Width = Application.GetMinRealAverage(81),
                Height = Application.GetMinRealAverage(81),
                UnSelectedImagePath = "Light/OFF.png",
                SelectedImagePath = "Light/OFFSelected.png",
                IsSelected = (device.CommonDevice as ToggleLight).OnOffStatus == 0
            };
            itemView.AddChidren(OffBtn);
            OnBtn = new Button
            {
                X = Application.GetRealWidth(590),
                Y = Application.GetRealHeight(588),
                Width = Application.GetMinRealAverage(81),
                Height = Application.GetMinRealAverage(81),
                UnSelectedImagePath = "Light/ON.png",
                SelectedImagePath = "Light/ONSelected.png",
                IsSelected = (device.CommonDevice as ToggleLight).OnOffStatus == 1
            };
            itemView.AddChidren(OnBtn);
            var roomBG = new Button
            {
@@ -427,6 +382,138 @@
                TextSize = 12
            };
            itemView.AddChidren(roomName);
            AddByFunctionType(itemView);
        }
        /// <summary>
        /// AddByFunctionType
        /// </summary>
        private void AddByFunctionType(FrameLayout itemView)
        {
            if(device.DfunctionType == DeviceFunctionType.A开关)
            {
                AddOnOff(itemView);
            }
            else if(device.DfunctionType == DeviceFunctionType.A插座)
            {
                AddPlug(itemView);
            }
            else
            {
                AddLight(itemView);
            }
        }
        /// <summary>
        /// AddOnOff
        /// </summary>
        private void AddOnOff(FrameLayout itemView)
        {
            deviceIMG = new Button()
            {
                Y = Application.GetRealHeight(340),
                Width = Application.GetMinRealAverage(579),
                Height = Application.GetMinRealAverage(579),
                Gravity = Gravity.CenterHorizontal,
                UnSelectedImagePath = "Light/OnOff.png",
            };
            itemView.AddChidren(deviceIMG);
            OffBtn = new Button
            {
                X = Application.GetRealWidth(294),
                Y = Application.GetRealHeight(588),
                Width = Application.GetMinRealAverage(81),
                Height = Application.GetMinRealAverage(81),
                UnSelectedImagePath = "Light/OFF.png",
                SelectedImagePath = "Light/OFFSelected.png",
                IsSelected = (device as ToggleLight).OnOffStatus == 0
            };
            itemView.AddChidren(OffBtn);
            OnBtn = new Button
            {
                X = Application.GetRealWidth(590),
                Y = Application.GetRealHeight(588),
                Width = Application.GetMinRealAverage(81),
                Height = Application.GetMinRealAverage(81),
                UnSelectedImagePath = "Light/ON.png",
                SelectedImagePath = "Light/ONSelected.png",
                IsSelected = (device as ToggleLight).OnOffStatus == 1
            };
            itemView.AddChidren(OnBtn);
            OnBtn.MouseUpEventHandler += ON;
            OffBtn.MouseUpEventHandler += OFF;
        }
        /// <summary>
        /// light
        /// </summary>
        /// <param name="itemView"></param>
        private void AddLight(FrameLayout itemView)
        {
            deviceIMG = new Button()
            {
                Y = Application.GetRealHeight(389),
                Width = Application.GetMinRealAverage(377),
                Height = Application.GetMinRealAverage(435),
                Gravity = Gravity.CenterHorizontal,
                UnSelectedImagePath = "Light/DeskLamp.png",
                SelectedImagePath = "Light/DeskLampSelected.png",
                IsSelected = (device as ToggleLight).OnOffStatus == 1
            };
            itemView.AddChidren(deviceIMG);
            switchBtn = new Button()
            {
                Y = Application.GetRealHeight(996),
                Width = Application.GetMinRealAverage(81),
                Height = Application.GetMinRealAverage(81),
                Gravity = Gravity.CenterHorizontal,
                UnSelectedImagePath = "Item/Switch.png",
                SelectedImagePath = "Item/SwitchSelected.png",
                IsSelected = (device as ToggleLight).OnOffStatus == 1
            };
            itemView.AddChidren(switchBtn);
            switchBtn.MouseUpEventHandler += Switch;
            deviceIMG.MouseUpEventHandler += Switch;
        }
        /// <summary>
        /// plug
        /// </summary>
        /// <param name="itemView"></param>
        private void AddPlug(FrameLayout itemView)
        {
            deviceIMG = new Button()
            {
                Y = Application.GetRealHeight(344),
                Width = Application.GetMinRealAverage(567),
                Height = Application.GetMinRealAverage(567),
                Gravity = Gravity.CenterHorizontal,
                UnSelectedImagePath = "Light/Plug.png",
                SelectedImagePath = "Light/PlugSelected.png",
                IsSelected = (device as ToggleLight).OnOffStatus == 1
            };
            itemView.AddChidren(deviceIMG);
            switchBtn = new Button()
            {
                Y = Application.GetRealHeight(996),
                Width = Application.GetMinRealAverage(81),
                Height = Application.GetMinRealAverage(81),
                Gravity = Gravity.CenterHorizontal,
                UnSelectedImagePath = "Item/Switch.png",
                SelectedImagePath = "Item/SwitchSelected.png",
                IsSelected = (device as ToggleLight).OnOffStatus == 1
            };
            itemView.AddChidren(switchBtn);
            switchBtn.MouseUpEventHandler += Switch;
            deviceIMG.MouseUpEventHandler += Switch;
        }
        #endregion
@@ -437,8 +524,6 @@
        /// </summary>
        private void BindEvent()
        {
            OnBtn.MouseUpEventHandler += ON;
            OffBtn.MouseUpEventHandler += OFF;
            collectionBtn.MouseUpEventHandler += Collection;
        }
@@ -451,35 +536,43 @@
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="eventArgs">Event arguments.</param>
        //private void Switch(object sender, MouseEventArgs eventArgs)
        //{
        //    sendedControlCommand = false;
        //    zbGateway.ReportAction += UpdateDeviceControllStatu;
        //    switchBtn.IsSelected = !switchBtn.IsSelected;
        //    deviceIMG.IsSelected = !deviceIMG.IsSelected;
        //    StatuBtn.Text = CommonFormResouce.GetSwitchStatu(deviceIMG.IsSelected);
        //    if (switchBtn.IsSelected == true)
        //    {
        //        (device.CommonDevice as ToggleLight).SwitchControl(1);
        //    }
        //    else
        //    {
        //        (device.CommonDevice as ToggleLight).SwitchControl(0);
        //    }
        //    //控制延时回调
        //    DeviceUI.SendCommandDelayAction(device.CommonDevice, () =>
        //    {
        //        if (Parent == null)
        //        {
        //            return;
        //        }
        //        RemoveUpdateControlDeviceStatuAction();
        //        if (sendedControlCommand == false)
        //        {
        //            DeviceUI.ShowStatuTip(R.MyInternationalizationString.FAIL);
        //        }
        //    });
        //}
        private void Switch(object sender, MouseEventArgs eventArgs)
        {
            sendedControlCommand = false;
            zbGateway.ReportAction += UpdateDeviceControllStatu;
            switchBtn.IsSelected = !switchBtn.IsSelected;
            deviceIMG.IsSelected = !deviceIMG.IsSelected;
            StatuBtn.Text = Language.StringByID(R.MyInternationalizationString.Current) + "   ";
            if (deviceIMG.IsSelected == true)
            {
                StatuBtn.Text += Language.StringByID(R.MyInternationalizationString.uOpen1);
            }
            else
            {
                StatuBtn.Text += Language.StringByID(R.MyInternationalizationString.Close);
            }
            if (switchBtn.IsSelected == true)
            {
                (device as ToggleLight).SwitchControl(1);
            }
            else
            {
                (device as ToggleLight).SwitchControl(0);
            }
            //控制延时回调
            UserCenter.HdlDeviceOtherLogic.Current.SendCommandDelayAction(device, () =>
            {
                if (Parent == null)
                {
                    return;
                }
                RemoveUpdateControlDeviceStatuAction();
                if (sendedControlCommand == false)
                {
                    UserCenter.HdlDeviceOtherLogic.Current.ShowStatuTip(R.MyInternationalizationString.FAIL);
                }
            });
        }
        /// <summary>
        /// ON
@@ -489,10 +582,10 @@
        private void ON(object sender, MouseEventArgs eventArgs)
        {
            SetONOFFStatu(true);
            (device.CommonDevice as ToggleLight).SwitchControl(1);
            (device as ToggleLight).SwitchControl(1);
            //控制延时回调
            zbGateway.ReportAction += UpdateDeviceControllStatu;
            DeviceUI.SendCommandDelayAction(device.CommonDevice, () =>
            UserCenter.HdlDeviceOtherLogic.Current.SendCommandDelayAction(device, () =>
            {
                if (Parent == null)
                {
@@ -501,7 +594,7 @@
                RemoveUpdateControlDeviceStatuAction();
                if (sendedControlCommand == false)
                {
                    DeviceUI.ShowStatuTip(R.MyInternationalizationString.FAIL);
                    UserCenter.HdlDeviceOtherLogic.Current.ShowStatuTip(R.MyInternationalizationString.FAIL);
                }
            });
        }
@@ -513,10 +606,10 @@
        private void OFF(object sender, MouseEventArgs eventArgs)
        {
            SetONOFFStatu(false);
            (device.CommonDevice as ToggleLight).SwitchControl(0);
            (device as ToggleLight).SwitchControl(0);
            //控制延时回调
            zbGateway.ReportAction += UpdateDeviceControllStatu;
            DeviceUI.SendCommandDelayAction(device.CommonDevice, () =>
            UserCenter.HdlDeviceOtherLogic.Current.SendCommandDelayAction(device, () =>
            {
                if (Parent == null)
                {
@@ -525,7 +618,7 @@
                RemoveUpdateControlDeviceStatuAction();
                if (sendedControlCommand == false)
                {
                    DeviceUI.ShowStatuTip(R.MyInternationalizationString.FAIL);
                    UserCenter.HdlDeviceOtherLogic.Current.ShowStatuTip(R.MyInternationalizationString.FAIL);
                }
            });
        }
@@ -535,18 +628,20 @@
        /// </summary>
        /// <param name="statu"></param>
        private void SetONOFFStatu(bool statu)
        {
        {
            StatuBtn.Text = Language.StringByID(R.MyInternationalizationString.Current) + "   ";
            if (statu)
            {
                OnBtn.IsSelected = true;
                OffBtn.IsSelected = false;
                StatuBtn.Text += Language.StringByID(R.MyInternationalizationString.uOpen1);
            }
            else
            {
                OnBtn.IsSelected = false;
                OffBtn.IsSelected = true;
                OffBtn.IsSelected = true;
                StatuBtn.Text += Language.StringByID(R.MyInternationalizationString.Close);
            }
            StatuBtn.Text = statu ? $"{Language.StringByID(R.MyInternationalizationString.Current)} {Language.StringByID(R.MyInternationalizationString.Open)}" : $"{Language.StringByID(R.MyInternationalizationString.Current)} {Language.StringByID(R.MyInternationalizationString.Shut)}";
        }
@@ -564,9 +659,9 @@
            UserView.HomePage.Instance.AddChidren(detailInfo);
            UserView.HomePage.Instance.PageIndex += 1;
            detailInfo.Show(device, room);
            detailInfo.action = () =>
            detailInfo.EditAction += (curDev, curRoom) =>
            {
                Show(device, room);
                Show(curDev, curRoom);
            };
        }
@@ -583,12 +678,12 @@
            //collection
            if ((sender as Button).IsSelected)
            {
                Shared.Common.Room.CurrentRoom.GetLoveRoom().DeleteDevice(device.FileName);
                UserCenter.HdlRoomLogic.Current.DeleteLoveDevice(device);
                (sender as Button).IsSelected = false;
            }
            else
            {
                Shared.Common.Room.CurrentRoom.GetLoveRoom().AddDevice(device.FileName);
                UserCenter.HdlRoomLogic.Current.AddLoveDevice(device);
                (sender as Button).IsSelected = true;
            }
        }
@@ -613,7 +708,7 @@
                return;
            }
            var tempDevice = (CommonDevice)objValue;
            if (tempDevice.DeviceEpoint != this.device.CommonDevice.DeviceEpoint || tempDevice.DeviceAddr != this.device.CommonDevice.DeviceAddr)
            if (tempDevice.DeviceEpoint != this.device.DeviceEpoint || tempDevice.DeviceAddr != this.device.DeviceAddr)
            {
                //不是当前设备的推送,则不处理
                return;