Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color changing #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,6 @@ ASALocalRun/

# MFractors (Xamarin productivity tool) working folder
.mfractor/

# Mac specific
.DS_Store
12 changes: 12 additions & 0 deletions ArgumentParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
39 changes: 23 additions & 16 deletions HueBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class HueBridgeDiscovery
{
public List<HueBridgeObject> GetBridges()
{
List<HueBridgeObject> hueBridges = new List<HueBridgeObject>();
List<HueBridgeObject> hueBridges = new List<HueBridgeObject>();

HttpClient nupnpClient = new HttpClient();
HttpResponseMessage responseMessage = nupnpClient.GetAsync("https://www.meethue.com/api/nupnp").Result;
Expand All @@ -51,10 +51,10 @@ public List<HueBridgeObject> 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\"}}]"))
{
Expand All @@ -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;
Expand All @@ -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)
Expand All @@ -110,49 +110,56 @@ 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.");
}
}

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;
}
}
}
15 changes: 13 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
Expand Down Expand Up @@ -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;
Expand Down