private void Btn_Save_Tapped(object sender, TappedRoutedEventArgs e)
        {
            if (LB_Rooms.Items.Count > 0 && LB_Rooms.SelectedIndex != -1)
            {
                // Update Room Detail
                Library.Core.Room SelectedRoom = (Library.Core.Room)LB_Rooms.SelectedItem;

                SelectedRoom.Name = Txt_RoomName.Text;

                int             NewAddress = 0;
                IFormatProvider _Culture   = new CultureInfo("en-US");
                int.TryParse(Txt_RoomI2CAddress.Text.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber, _Culture, out NewAddress);
                SelectedRoom.I2C_Slave_Address = NewAddress;

                SelectedRoom.ImagePath = ((ImageListClass)LV_RoomImage.SelectedItem).ImagePath;

                // Update Device Detail
                if (LB_Devices.Items.Count > 0)
                {
                    if (LB_Devices.SelectedIndex != -1)
                    {
                        Library.Core.Device SelectedDevice = (Library.Core.Device)LB_Devices.SelectedItem;

                        SelectedDevice.Name = txt_DeviceName.Text;
                        SelectedDevice.Pin  = (Library.Core.Device.PinsEnum)Enum.Parse(typeof(Library.Core.Device.PinsEnum), ((ComboBoxItem)cmb_DevicePin.SelectedItem).Content.ToString());

                        SelectedDevice.ImagePath = ((ImageListClass)LV_DeviceImage.SelectedItem).ImagePath;
                    }
                }
            }

            // Save
            Library.Core.Home.SaveHome(MainPage.MyHome);
        }
        private void LV_Devices_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            DeviceInfo _SelDevInfo = (DeviceInfo)LV_Devices.SelectedItem;

            Library.Core.Device SelectedDevice = new Library.Core.Device();

            foreach (var Device in SelectedRoom.Devices)
            {
                if (_SelDevInfo.Id == Device.Id)
                {
                    SelectedDevice = Device;
                    break;
                }
            }


            if (SelectedDevice.Status == Library.Core.Device.StatusEnum.Off)
            {
                Task.Factory.StartNew(() =>
                {
                    SelectedDevice.TurnOn();
                }).Wait(1000);
                SelectedDevice.Status = Library.Core.Device.StatusEnum.On;
            }
            else
            {
                Task.Factory.StartNew(() =>
                {
                    SelectedDevice.TurnOff();
                }).Wait(1000);
                SelectedDevice.Status = Library.Core.Device.StatusEnum.Off;
            }

            LoadDevices();
        }
        private void Btn_AddDevice_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Library.Core.Device NewDevice = new Library.Core.Device();

            NewDevice.Name      = "My Device";
            NewDevice.ImagePath = "ms-appx:///Resources/Image/Tiles/Device/Plug_130.png";
            NewDevice.Pin       = Library.Core.Device.PinsEnum.D0;

            MainPage.MyHome.Rooms[LB_Rooms.SelectedIndex].AddDevice(NewDevice);

            // Only update device listboc
            UpdateListBox(2);
        }
        private void ListView_Devices_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            Library.Core.Device SelectedDevice = null;

            foreach (var _Dev in Devices)
            {
                if (((_Device)(ListView_Devices.SelectedItem)).Id == _Dev.Id)
                {
                    SelectedDevice = _Dev;
                    break;
                }
            }

            if (SelectedDevice.Status == Library.Core.Device.StatusEnum.On)
            {
                SelectedDevice.TurnOff();
            }
            else if (SelectedDevice.Status == Library.Core.Device.StatusEnum.Off)
            {
                SelectedDevice.TurnOn();
            }

            ListView_Devices.Items.Clear();

            foreach (var _Device in Devices)
            {
                _Device _Dev = new _Device();
                _Dev.DeviceImagePath = _Device.ImagePath;
                _Dev.DeviceName      = _Device.Name;

                if (_Device.Status == Library.Core.Device.StatusEnum.On)
                {
                    _Dev.DeviceStatusColor = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 99, 0));
                }
                else
                {
                    _Dev.DeviceStatusColor = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 128, 128, 128));
                }

                _Dev.Id            = _Device.Id;
                _Dev.DeviceToolTip = "R" + RoomNumber.ToString() + "\\Dev" + _Device.Id.ToString();

                ListView_Devices.Items.Add(_Dev);
            }
        }
        private void UpdateDeviceDetailPane(Library.Core.Room SelectedRoom, bool SelectFirst = false, bool SkipClear = false)
        {
            if (SkipClear == false)
            {
                ClearDeviceDetailPane();
            }
            if (SelectedRoom.Devices.Count > 0)
            {
                if (SkipClear == false)
                {
                    foreach (var _Device in SelectedRoom.Devices)
                    {
                        LB_Devices.Items.Add(_Device);
                    }
                }

                if (SelectFirst)
                {
                    LB_Devices.SelectedIndex = 0;
                }

                Library.Core.Device SelectedDevice = (Library.Core.Device)LB_Devices.SelectedItem;

                txt_DeviceName.Text = SelectedDevice.Name;

                foreach (ComboBoxItem item in cmb_DevicePin.Items)
                {
                    if ((string)item.Content == SelectedDevice.Pin.ToString())
                    {
                        cmb_DevicePin.SelectedItem = item;
                        break;
                    }
                }

                foreach (var _Image in LV_DeviceImage.Items)
                {
                    if (((ImageListClass)_Image).ImagePath == SelectedDevice.ImagePath)
                    {
                        LV_DeviceImage.SelectedItem = _Image;
                        break;
                    }
                }
            }
        }
        private void LV_Devices_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
            DeviceInfo _SelDevInfo = (DeviceInfo)LV_Devices.SelectedItem;
            Library.Core.Device SelectedDevice = new Library.Core.Device();

            foreach (var Device in SelectedRoom.Devices)
            {
                if (_SelDevInfo.Id == Device.Id)
                {
                    SelectedDevice = Device;
                    break;
                }
            }


            if (SelectedDevice.Status == Library.Core.Device.StatusEnum.Off)
            {
                Task.Factory.StartNew(() =>
                {
                    SelectedDevice.TurnOn();
                }).Wait(1000);
                SelectedDevice.Status = Library.Core.Device.StatusEnum.On;
            }
            else
            {
                Task.Factory.StartNew(() =>
                {
                    SelectedDevice.TurnOff();
                }).Wait(1000);
                SelectedDevice.Status = Library.Core.Device.StatusEnum.Off;
            }

            LoadDevices();
        }
        private void Btn_AddDevice_Tapped(object sender, TappedRoutedEventArgs e)
        {
            Library.Core.Device NewDevice = new Library.Core.Device();

            NewDevice.Name = "My Device";
            NewDevice.ImagePath = "ms-appx:///Resources/Image/Tiles/Device/Plug_130.png";
            NewDevice.Pin = Library.Core.Device.PinsEnum.D0;

            MainPage.MyHome.Rooms[LB_Rooms.SelectedIndex].AddDevice(NewDevice);

            // Only update device listboc
            UpdateListBox(2);
        }