diff --git a/HueUWP/APIHandler.cs b/HueUWP/APIHandler.cs new file mode 100755 index 0000000..b037081 --- /dev/null +++ b/HueUWP/APIHandler.cs @@ -0,0 +1,36 @@ +using Newtonsoft.Json.Linq; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.Storage; + +namespace HueUWP +{ + class APIHandler + { + + + NetworkHandler nwh; + public APIHandler(NetworkHandler nwh) + { + this.nwh = nwh; + } + + private JObject ObjectifyfJson(string json) + { + json = json.Replace("[", "").Replace("]", ""); + return JObject.Parse(json); + } + + public async void Register() + { + var json = await nwh.RegisterName("Hue", "Kenneth&Yorick"); + JObject o = ObjectifyfJson(json); + string id= o["success"]["username"].ToString(); + MainPage.LOCAL_SETTINGS.Values["id"] = id; + } + } +} diff --git a/HueUWP/App.xaml b/HueUWP/App.xaml index 74c682e..80724e5 100644 --- a/HueUWP/App.xaml +++ b/HueUWP/App.xaml @@ -1,5 +1,5 @@  /// Provides application-specific behavior to supplement the default Application class. diff --git a/HueUWP/HueUWP.csproj b/HueUWP/HueUWP.csproj index 4ce3100..7e815c2 100644 --- a/HueUWP/HueUWP.csproj +++ b/HueUWP/HueUWP.csproj @@ -93,6 +93,7 @@ + App.xaml @@ -133,11 +134,11 @@ 14.0 - \ No newline at end of file diff --git a/HueUWP/MainPage.xaml b/HueUWP/MainPage.xaml index fbec180..ed6b860 100644 --- a/HueUWP/MainPage.xaml +++ b/HueUWP/MainPage.xaml @@ -1,5 +1,5 @@  - + diff --git a/HueUWP/MainPage.xaml.cs b/HueUWP/MainPage.xaml.cs index 85d1d05..2ec8a73 100644 --- a/HueUWP/MainPage.xaml.cs +++ b/HueUWP/MainPage.xaml.cs @@ -1,4 +1,3 @@ -using HueUWP; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -8,6 +7,7 @@ using System.Linq; using System.Runtime.InteropServices.WindowsRuntime; using Windows.Foundation; using Windows.Foundation.Collections; +using Windows.Storage; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; @@ -18,7 +18,7 @@ using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 -namespace BindingToCommandsUWP +namespace HueUWP { @@ -28,6 +28,8 @@ namespace BindingToCommandsUWP /// public sealed partial class MainPage : Page { + public static ApplicationDataContainer LOCAL_SETTINGS = ApplicationData.Current.LocalSettings; + private ObservableCollection _lightsViewModel = LightDataSource.GetLights(); public ObservableCollection LightsViewModel @@ -38,8 +40,13 @@ namespace BindingToCommandsUWP public MainPage() { this.InitializeComponent(); - Debug.WriteLine(new NetworkHandler().RegisterName("Kaas", "Henk")); - Debug.WriteLine(new NetworkHandler().Test()); + LOCAL_SETTINGS.Values["ip"] = "localhost"; + LOCAL_SETTINGS.Values["port"] = 8000; + NetworkHandler nwh = new NetworkHandler(); + APIHandler api = new APIHandler(nwh); + //api.Register(); + Debug.WriteLine(LOCAL_SETTINGS.Values["id"]); + } private void ColorChanged(object sender, RoutedEventArgs e) @@ -57,5 +64,6 @@ namespace BindingToCommandsUWP light.UpdateState(button.IsOn); } + } } diff --git a/HueUWP/NetworkHandler.cs b/HueUWP/NetworkHandler.cs index a6c3b56..0bd041c 100755 --- a/HueUWP/NetworkHandler.cs +++ b/HueUWP/NetworkHandler.cs @@ -11,7 +11,15 @@ namespace HueUWP { class NetworkHandler { - private async Task Put(string ip, int port, string path, string json) + string ip; + int port; + public NetworkHandler() + { + this.ip = (string) MainPage.LOCAL_SETTINGS.Values["ip"]; + this.port = (int)MainPage.LOCAL_SETTINGS.Values["port"]; + } + + private async Task Put(string path, string json) { var cts = new CancellationTokenSource(); cts.CancelAfter(5000); @@ -31,7 +39,7 @@ namespace HueUWP string jsonResponse = await response.Content.ReadAsStringAsync(); - System.Diagnostics.Debug.WriteLine(jsonResponse); + //System.Diagnostics.Debug.WriteLine(jsonResponse); return jsonResponse; } @@ -43,7 +51,7 @@ namespace HueUWP } - private async Task Post(string ip, int port, string path, string json) + private async Task Post(string path, string json) { var cts = new CancellationTokenSource(); cts.CancelAfter(5000); @@ -63,7 +71,7 @@ namespace HueUWP string jsonResponse = await response.Content.ReadAsStringAsync(); - System.Diagnostics.Debug.WriteLine(jsonResponse); + //System.Diagnostics.Debug.WriteLine(jsonResponse); return jsonResponse; } @@ -74,7 +82,7 @@ namespace HueUWP } } - private async Task Get(string ip, int port, string path) + private async Task Get(string path) { var cts = new CancellationTokenSource(); cts.CancelAfter(5000); @@ -94,7 +102,7 @@ namespace HueUWP string jsonResponse = await response.Content.ReadAsStringAsync(); - System.Diagnostics.Debug.WriteLine(jsonResponse); + //System.Diagnostics.Debug.WriteLine(jsonResponse); return jsonResponse; } @@ -106,18 +114,20 @@ namespace HueUWP } - public async Task RegisterName(string AppName, string UserName) + public async Task RegisterName(string AppName, string UserName) { - var response = await Post("localhost",8000,"",$"{{\"devicetype\":\"{AppName}#{UserName}\"}}"); + var response = await Post("",$"{{\"devicetype\":\"{AppName}#{UserName}\"}}"); if (string.IsNullOrEmpty(response)) await new MessageDialog("Error while setting username. ….").ShowAsync(); + return response; } - public async Task Test() + public async Task Test() { - var response = await Get("localhost", 8000, "/111bb033202ac68b5812245c22f77eb/lights"); + var response = await Get( "/111bb033202ac68b5812245c22f77eb/lights"); if (string.IsNullOrEmpty(response)) await new MessageDialog("Error while setting username. ….").ShowAsync(); + return response; } diff --git a/HueUWP/project.json b/HueUWP/project.json index c594939..593d68f 100644 --- a/HueUWP/project.json +++ b/HueUWP/project.json @@ -1,16 +1,17 @@ -{ - "dependencies": { - "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0" - }, - "frameworks": { - "uap10.0": {} - }, - "runtimes": { - "win10-arm": {}, - "win10-arm-aot": {}, - "win10-x86": {}, - "win10-x86-aot": {}, - "win10-x64": {}, - "win10-x64-aot": {} - } +{ + "dependencies": { + "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0", + "Newtonsoft.Json": "7.0.1" + }, + "frameworks": { + "uap10.0": {} + }, + "runtimes": { + "win10-arm": {}, + "win10-arm-aot": {}, + "win10-x86": {}, + "win10-x86-aot": {}, + "win10-x64": {}, + "win10-x64-aot": {} + } } \ No newline at end of file diff --git a/HueUWP/project.lock.json b/HueUWP/project.lock.json index a7d5c41..a977d58 100755 --- a/HueUWP/project.lock.json +++ b/HueUWP/project.lock.json @@ -201,6 +201,14 @@ "lib/dotnet/Microsoft.Win32.Primitives.dll": {} } }, + "Newtonsoft.Json/7.0.1": { + "compile": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + }, + "runtime": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + } + }, "System.AppContext/4.0.0": { "dependencies": { "System.Collections": "[4.0.0, )", @@ -1779,6 +1787,14 @@ "lib/dotnet/Microsoft.Win32.Primitives.dll": {} } }, + "Newtonsoft.Json/7.0.1": { + "compile": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + }, + "runtime": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + } + }, "System.AppContext/4.0.0": { "dependencies": { "System.Collections": "[4.0.0, )", @@ -3390,6 +3406,14 @@ "lib/dotnet/Microsoft.Win32.Primitives.dll": {} } }, + "Newtonsoft.Json/7.0.1": { + "compile": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + }, + "runtime": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + } + }, "System.AppContext/4.0.0": { "dependencies": { "System.Collections": "[4.0.0, )", @@ -5006,6 +5030,14 @@ "lib/dotnet/Microsoft.Win32.Primitives.dll": {} } }, + "Newtonsoft.Json/7.0.1": { + "compile": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + }, + "runtime": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + } + }, "System.AppContext/4.0.0": { "dependencies": { "System.Collections": "[4.0.0, )", @@ -6617,6 +6649,14 @@ "lib/dotnet/Microsoft.Win32.Primitives.dll": {} } }, + "Newtonsoft.Json/7.0.1": { + "compile": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + }, + "runtime": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + } + }, "System.AppContext/4.0.0": { "dependencies": { "System.Collections": "[4.0.0, )", @@ -8233,6 +8273,14 @@ "lib/dotnet/Microsoft.Win32.Primitives.dll": {} } }, + "Newtonsoft.Json/7.0.1": { + "compile": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + }, + "runtime": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + } + }, "System.AppContext/4.0.0": { "dependencies": { "System.Collections": "[4.0.0, )", @@ -9844,6 +9892,14 @@ "lib/dotnet/Microsoft.Win32.Primitives.dll": {} } }, + "Newtonsoft.Json/7.0.1": { + "compile": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + }, + "runtime": { + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll": {} + } + }, "System.AppContext/4.0.0": { "dependencies": { "System.Collections": "[4.0.0, )", @@ -11868,6 +11924,29 @@ "ref/xamarinmac20/_._" ] }, + "Newtonsoft.Json/7.0.1": { + "sha512": "q3V4KLetMLnt1gpAVWgtXnHjKs0UG/RalBc29u2ZKxd5t5Ze4JBL5WiiYIklJyK/5CRiIiNwigVQUo0FgbsuWA==", + "type": "Package", + "files": [ + "[Content_Types].xml", + "_rels/.rels", + "lib/net20/Newtonsoft.Json.dll", + "lib/net20/Newtonsoft.Json.xml", + "lib/net35/Newtonsoft.Json.dll", + "lib/net35/Newtonsoft.Json.xml", + "lib/net40/Newtonsoft.Json.dll", + "lib/net40/Newtonsoft.Json.xml", + "lib/net45/Newtonsoft.Json.dll", + "lib/net45/Newtonsoft.Json.xml", + "lib/portable-net40+sl5+wp80+win8+wpa81/Newtonsoft.Json.dll", + "lib/portable-net40+sl5+wp80+win8+wpa81/Newtonsoft.Json.xml", + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.dll", + "lib/portable-net45+wp80+win8+wpa81+dnxcore50/Newtonsoft.Json.xml", + "Newtonsoft.Json.nuspec", + "package/services/metadata/core-properties/49e907fc018c4c7ab632e6d4ad4766f8.psmdcp", + "tools/install.ps1" + ] + }, "System.AppContext/4.0.0": { "sha512": "gUoYgAWDC3+xhKeU5KSLbYDhTdBYk9GssrMSCcWUADzOglW+s0AmwVhOUGt2tL5xUl7ZXoYTPdA88zCgKrlG0A==", "type": "Package", @@ -14566,7 +14645,8 @@ }, "projectFileDependencyGroups": { "": [ - "Microsoft.NETCore.UniversalWindowsPlatform >= 5.0.0" + "Microsoft.NETCore.UniversalWindowsPlatform >= 5.0.0", + "Newtonsoft.Json >= 7.0.1" ], "UAP,Version=v10.0": [] }