diff --git a/HueUWP/APIHandler.cs b/HueUWP/APIHandler.cs index 9ece594..a6935a5 100755 --- a/HueUWP/APIHandler.cs +++ b/HueUWP/APIHandler.cs @@ -44,7 +44,7 @@ namespace HueUWP { if(l.IsOn) { - var json = await nwh.SetLightInfo(l.ID, $"{{\"bri\": {l.Brightness},\"hue\": {l.Hue},\"sat\": {l.Saturation}}}"); + var json = await nwh.SetLightInfo(l.ID, $"{{\"bri\": {l.Brightness-1},\"hue\": {(l.Hue/360)*65535},\"sat\": {l.Saturation*254}}}"); Debug.WriteLine(json); } @@ -55,16 +55,14 @@ namespace HueUWP List lightlist = new List(); try { var json = await nwh.AllLights(); - Debug.WriteLine(json); JObject o = JObject.Parse(json); - Debug.WriteLine(o.ToString()); for (int i = 1; i <= o.Count; i++) { var light = o["" + i]; var state = light["state"]; - alllights.Add(new Light() { api = this,ID = i, Brightness = (int)state["bri"], IsOn = (string)state["on"] == "true" ? true : false, Hue = (int)state["hue"], Saturation = (int)state["sat"], Name = (string)light["name"], Type = (string)light["type"] }); - Debug.WriteLine("Added light number " + i); + alllights.Add(new Light() { api = this,ID = i, Brightness = (int)state["bri"], IsOn = ((string)state["on"]).ToLower() == "true" ? true : false, Hue = (int)state["hue"], Saturation = (int)state["sat"], Name = (string)light["name"], Type = (string)light["type"] }); + Debug.WriteLine("Added light number " + i + " " + state["on"]); } } catch(Exception e) { diff --git a/HueUWP/App.xaml.cs b/HueUWP/App.xaml.cs index 67a8269..3dd861e 100644 --- a/HueUWP/App.xaml.cs +++ b/HueUWP/App.xaml.cs @@ -7,6 +7,7 @@ using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.Foundation; using Windows.Foundation.Collections; +using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; @@ -22,6 +23,8 @@ namespace HueUWP /// sealed partial class App : Application { + Frame rootFrame; + /// /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). @@ -47,7 +50,7 @@ namespace HueUWP } #endif - Frame rootFrame = Window.Current.Content as Frame; + rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active @@ -57,6 +60,8 @@ namespace HueUWP rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; + rootFrame.Navigated += RootFrame_Navigated; + SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { @@ -78,6 +83,23 @@ namespace HueUWP Window.Current.Activate(); } + private void RootFrame_Navigated(object sender, NavigationEventArgs e) + { + SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = + rootFrame.CanGoBack ? + AppViewBackButtonVisibility.Visible : + AppViewBackButtonVisibility.Collapsed; + } + + private void OnBackRequested(object sender, BackRequestedEventArgs e) + { + if (rootFrame.CanGoBack) + { + e.Handled = true; + rootFrame.GoBack(); + } + } + /// /// Invoked when Navigation to a certain page fails /// diff --git a/HueUWP/Converters/BooleanToStringConverter.cs b/HueUWP/Converters/NullableBooleanConverter.cs similarity index 66% rename from HueUWP/Converters/BooleanToStringConverter.cs rename to HueUWP/Converters/NullableBooleanConverter.cs index 3e44fd3..32fbc1b 100644 --- a/HueUWP/Converters/BooleanToStringConverter.cs +++ b/HueUWP/Converters/NullableBooleanConverter.cs @@ -7,25 +7,25 @@ using Windows.UI.Xaml.Data; namespace HueUWP.Converters { - public class BooleanToStringConverter : IValueConverter + public class NullableBooleanConverter : IValueConverter { //From bool to nullable public object Convert(object value, Type targetType, object parameter, string language) { bool b = (bool)value; - return b.ToString(); + bool? be = b as bool?; + return be; } //From nullable to bool public object ConvertBack(object value, Type targetType, object parameter, string language) { - string s = (string)value; - bool b = false; + bool? b = (bool?)value; + bool be = false; + if (b.HasValue) + be = (bool)b; - if (s == "true") - b = true; - - return b; + return be; } } } diff --git a/HueUWP/DetailView.xaml b/HueUWP/DetailView.xaml index 82bc160..0dbe622 100644 --- a/HueUWP/DetailView.xaml +++ b/HueUWP/DetailView.xaml @@ -8,6 +8,15 @@ mc:Ignorable="d"> + + + + + + + + + diff --git a/HueUWP/DetailView.xaml.cs b/HueUWP/DetailView.xaml.cs index 73738b3..67ba713 100644 --- a/HueUWP/DetailView.xaml.cs +++ b/HueUWP/DetailView.xaml.cs @@ -22,9 +22,17 @@ namespace HueUWP /// public sealed partial class DetailView : Page { + Light l; + public DetailView() { this.InitializeComponent(); } + + protected override void OnNavigatedTo(NavigationEventArgs e) + { + l = e.Parameter as Light; + this.DataContext = l; + } } } diff --git a/HueUWP/HueUWP.csproj b/HueUWP/HueUWP.csproj index a183060..b8c36ff 100644 --- a/HueUWP/HueUWP.csproj +++ b/HueUWP/HueUWP.csproj @@ -98,7 +98,7 @@ App.xaml - + DetailView.xaml diff --git a/HueUWP/Light.cs b/HueUWP/Light.cs index 1000795..bb1ec28 100644 --- a/HueUWP/Light.cs +++ b/HueUWP/Light.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Input; +using Windows.UI.Xaml.Media; namespace HueUWP { @@ -22,7 +23,12 @@ namespace HueUWP public double Saturation { get; set; } public APIHandler api { get; set; } - + + public SolidColorBrush Color + { + get { return new SolidColorBrush(HueUWP.ColorUtil.HsvToRgb(Hue, Saturation, Brightness)); } + } + public Light() { } @@ -43,6 +49,8 @@ namespace HueUWP this.Hue = h; this.Saturation = s; this.Brightness = v; api.SetLightValues(this); Debug.WriteLine(r + "-" + g + "-" + b); + Debug.WriteLine(h + "-" + s + "-" + v); + NotifyPropertyChanged(nameof(UpdateColor)); } diff --git a/HueUWP/MainPage.xaml b/HueUWP/MainPage.xaml index 95b99bc..8a4752e 100644 --- a/HueUWP/MainPage.xaml +++ b/HueUWP/MainPage.xaml @@ -11,7 +11,7 @@ mc:Ignorable="d"> - + @@ -29,18 +29,18 @@ - + - + - + Off On diff --git a/HueUWP/MainPage.xaml.cs b/HueUWP/MainPage.xaml.cs index 1cab349..6f6a8b6 100644 --- a/HueUWP/MainPage.xaml.cs +++ b/HueUWP/MainPage.xaml.cs @@ -32,6 +32,8 @@ namespace HueUWP private ObservableCollection _lightsViewModel = LightDataSource.GetLights(); + private APIHandler api; + public ObservableCollection LightsViewModel { get { return this._lightsViewModel; } @@ -43,12 +45,9 @@ namespace HueUWP LOCAL_SETTINGS.Values["ip"] = "localhost"; LOCAL_SETTINGS.Values["port"] = 8000; NetworkHandler nwh = new NetworkHandler(); - APIHandler api = new APIHandler(nwh); - //api.Register(); + api = new APIHandler(nwh); + _lightsViewModel.Clear(); api.GetAllLights(_lightsViewModel); - - Debug.WriteLine(LOCAL_SETTINGS.Values["id"]); - } private void ColorChanged(object sender, RoutedEventArgs e) @@ -73,23 +72,27 @@ namespace HueUWP ToggleSwitch button = ((ToggleSwitch)sender); Light light = (Light)button.DataContext; light.UpdateState(button.IsOn); - } - - private void slider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e) - { - Slider slider = ((Slider)sender); - Light l = (Light)slider.DataContext; - l.UpdateColor((int)slider.Value, 255, 255); + light.UpdateColor(0, 38, 255); } private void RegisterButton_Click(object sender, RoutedEventArgs e) { - + api.Register(); + Debug.WriteLine(LOCAL_SETTINGS.Values["id"]); } private void RefreshButton_Click(object sender, RoutedEventArgs e) { + _lightsViewModel.Clear(); + api.GetAllLights(_lightsViewModel); + } + private void myListView_ItemClick(object sender, ItemClickEventArgs e) + { + Light l = e.ClickedItem as Light; + Frame rootframe = Window.Current.Content as Frame; + if (l != null) + rootframe.Navigate(typeof(DetailView), l); } } } diff --git a/HueUWP/SettingsView.xaml b/HueUWP/SettingsView.xaml index bcaca26..ac4e124 100644 --- a/HueUWP/SettingsView.xaml +++ b/HueUWP/SettingsView.xaml @@ -8,6 +8,13 @@ mc:Ignorable="d"> + + + + + + Settings +