From d90b8630d753e26e7abd6ae8c30db479958875a4 Mon Sep 17 00:00:00 2001 From: Jonas Oksvold Frich Date: Sat, 20 Jun 2020 03:10:56 +0200 Subject: [PATCH] Added functionality for changing light color using Gamut triangle x and y coordinates. (0.0-1.0) --- .gitignore | 3 +++ ArgumentParser.cs | 12 ++++++++++++ HueBridge.cs | 39 +++++++++++++++++++++++---------------- Program.cs | 15 +++++++++++++-- 4 files changed, 51 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 3e759b7..bdefbba 100644 --- a/.gitignore +++ b/.gitignore @@ -328,3 +328,6 @@ ASALocalRun/ # MFractors (Xamarin productivity tool) working folder .mfractor/ + +# Mac specific +.DS_Store \ No newline at end of file diff --git a/ArgumentParser.cs b/ArgumentParser.cs index fc22ecd..dc258ca 100644 --- a/ArgumentParser.cs +++ b/ArgumentParser.cs @@ -122,6 +122,18 @@ public bool SetTemperatureCheckEnoughArguments() } } + public bool SetColorCheckEnoughArguments() + { + if (this.arguments.Length == 6) + { + return true; + } + else + { + return false; + } + } + public ArgumentParser() { arguments = Environment.GetCommandLineArgs(); diff --git a/HueBridge.cs b/HueBridge.cs index 5ba5d94..5a4ec1f 100644 --- a/HueBridge.cs +++ b/HueBridge.cs @@ -32,7 +32,7 @@ class HueBridgeDiscovery { public List GetBridges() { - List hueBridges = new List(); + List hueBridges = new List(); HttpClient nupnpClient = new HttpClient(); HttpResponseMessage responseMessage = nupnpClient.GetAsync("https://www.meethue.com/api/nupnp").Result; @@ -51,10 +51,10 @@ public List GetBridges() public string CreateBridgeLink(String alias, String localipaddress) { HttpClient apiClient = new HttpClient(); - StringContent content = new StringContent("{\"devicetype\": \"huecli#"+alias+"\"}", Encoding.UTF8, "application/json"); + StringContent content = new StringContent("{\"devicetype\": \"huecli#" + alias + "\"}", Encoding.UTF8, "application/json"); while (true) { - HttpResponseMessage responseMessage = apiClient.PostAsync("http://"+localipaddress+"/api", content).Result; + HttpResponseMessage responseMessage = apiClient.PostAsync("http://" + localipaddress + "/api", content).Result; String returnedMessage = responseMessage.Content.ReadAsStringAsync().Result; if (!returnedMessage.Contains("\"description\":\"link button not pressed\"}}]")) { @@ -72,7 +72,7 @@ public string CreateBridgeLink(String alias, String localipaddress) public bool RemoveBridgeLink(String localipaddress, String username) { HttpClient apiClient = new HttpClient(); - HttpResponseMessage responseMessage = apiClient.DeleteAsync("http://"+localipaddress+"/api/"+username+"/config/whitelist/"+username).Result; + HttpResponseMessage responseMessage = apiClient.DeleteAsync("http://" + localipaddress + "/api/" + username + "/config/whitelist/" + username).Result; if (responseMessage.IsSuccessStatusCode) { return true; @@ -86,8 +86,8 @@ public bool RemoveBridgeLink(String localipaddress, String username) class HueBridge { - public string HueBridgeAddress { get; set; } - public string HueBridgeAlias { get; set; } + public string HueBridgeAddress { get; set; } + public string HueBridgeAlias { get; set; } public string HueBridgeUsername { get; set; } // public HueBridge(String hueBridgeAlias, String hueBridgeAddress = null) @@ -110,20 +110,20 @@ class HueBridge public HueBridge(String hueBridgeAlias, String hueBridgeAddress, String hueBridgeUsername) { - this.HueBridgeAddress = hueBridgeAddress; - this.HueBridgeAlias = hueBridgeAlias; + this.HueBridgeAddress = hueBridgeAddress; + this.HueBridgeAlias = hueBridgeAlias; this.HueBridgeUsername = hueBridgeUsername; } public void GetBridgeLighting() { HttpClient apiClient = new HttpClient(); - HttpResponseMessage responseMessage = apiClient.GetAsync("http://"+this.HueBridgeAddress+"/api/"+this.HueBridgeUsername).Result; + HttpResponseMessage responseMessage = apiClient.GetAsync("http://" + this.HueBridgeAddress + "/api/" + this.HueBridgeUsername).Result; String returnedMessage = responseMessage.Content.ReadAsStringAsync().Result; var jsonObject = JObject.Parse(returnedMessage); foreach (JProperty lightObject in jsonObject["lights"]) { - Console.WriteLine("Light ID "+lightObject.Name+" with name "+lightObject.First["name"]+" found."); + Console.WriteLine("Light ID " + lightObject.Name + " with name " + lightObject.First["name"] + " found."); } } @@ -131,28 +131,35 @@ public void TurnOnLighting(String lightID) { HttpClient apiClient = new HttpClient(); StringContent content = new StringContent("{\"on\": true}", Encoding.UTF8, "application/json"); - HttpResponseMessage responseMessage = apiClient.PutAsync("http://"+this.HueBridgeAddress+"/api/"+this.HueBridgeUsername+"/lights/"+lightID+"/state", content).Result; + HttpResponseMessage responseMessage = apiClient.PutAsync("http://" + this.HueBridgeAddress + "/api/" + this.HueBridgeUsername + "/lights/" + lightID + "/state", content).Result; } public void TurnOffLighting(String lightID) { HttpClient apiClient = new HttpClient(); StringContent content = new StringContent("{\"on\": false}", Encoding.UTF8, "application/json"); - HttpResponseMessage responseMessage = apiClient.PutAsync("http://"+this.HueBridgeAddress+"/api/"+this.HueBridgeUsername+"/lights/"+lightID+"/state", content).Result; + HttpResponseMessage responseMessage = apiClient.PutAsync("http://" + this.HueBridgeAddress + "/api/" + this.HueBridgeUsername + "/lights/" + lightID + "/state", content).Result; } public void SetLightingBrightness(String lightID, String brightness) { HttpClient apiClient = new HttpClient(); - StringContent content = new StringContent("{\"bri\": "+brightness+"}", Encoding.UTF8, "application/json"); - HttpResponseMessage responseMessage = apiClient.PutAsync("http://"+this.HueBridgeAddress+"/api/"+this.HueBridgeUsername+"/lights/"+lightID+"/state", content).Result; + StringContent content = new StringContent("{\"bri\": " + brightness + "}", Encoding.UTF8, "application/json"); + HttpResponseMessage responseMessage = apiClient.PutAsync("http://" + this.HueBridgeAddress + "/api/" + this.HueBridgeUsername + "/lights/" + lightID + "/state", content).Result; } public void SetLightingTemperature(String lightID, String temperature) { HttpClient apiClient = new HttpClient(); - StringContent content = new StringContent("{\"ct\": "+temperature+"}", Encoding.UTF8, "application/json"); - HttpResponseMessage responseMessage = apiClient.PutAsync("http://"+this.HueBridgeAddress+"/api/"+this.HueBridgeUsername+"/lights/"+lightID+"/state", content).Result; + StringContent content = new StringContent("{\"ct\": " + temperature + "}", Encoding.UTF8, "application/json"); + HttpResponseMessage responseMessage = apiClient.PutAsync("http://" + this.HueBridgeAddress + "/api/" + this.HueBridgeUsername + "/lights/" + lightID + "/state", content).Result; + } + + public void SetLightingColor(String lightID, String x, String y) + { + HttpClient apiClient = new HttpClient(); + StringContent content = new StringContent("{\"xy\": [" + x + "," + y + "]}", Encoding.UTF8, "application/json"); + HttpResponseMessage responseMessage = apiClient.PutAsync("http://" + this.HueBridgeAddress + "/api/" + this.HueBridgeUsername + "/lights/" + lightID + "/state", content).Result; } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index bd4282f..474a639 100644 --- a/Program.cs +++ b/Program.cs @@ -32,7 +32,7 @@ static void Main(string[] args) { foreach (HueBridgeObject hueBridge in hueBridges) { - Console.WriteLine("Found hub at address "+hueBridge.internalipaddress); + Console.WriteLine("Found hub at address " + hueBridge.internalipaddress); } } else @@ -44,7 +44,7 @@ static void Main(string[] args) case "get-hubs": foreach (var hub in settings.GetHubs()) { - Console.WriteLine("Hub "+hub["alias"]+" at "+hub["localipaddress"]); + Console.WriteLine("Hub " + hub["alias"] + " at " + hub["localipaddress"]); } break; case "remove-hub": @@ -130,6 +130,17 @@ static void Main(string[] args) Console.WriteLine("Invalid syntax, use huecli set-temperature hubalias lightid 154-500"); } break; + case "set-color": + if (argParser.SetColorCheckEnoughArguments()) + { + HueBridge hueBridge = new HueBridge(argParser.arguments[2], settings.GetIPAddress(argParser.arguments[2]), settings.GetUsername(argParser.arguments[2])); + hueBridge.SetLightingColor(argParser.arguments[3], argParser.arguments[4], argParser.arguments[5]); + } + else + { + Console.WriteLine("Invalid syntax, use huecli set-color hubalias lightid 0.0-1.0 0.0-1.0"); + } + break; default: argParser.ShowHelp(); break;