Skip to content

Commit

Permalink
Merge pull request #3 from rmmlr/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
rmmlr authored Oct 10, 2018
2 parents 0da5f29 + 167d572 commit 2945c9d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 38 deletions.
95 changes: 75 additions & 20 deletions HueHookServer/HookReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using SmartHttpServer;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -41,57 +42,67 @@ public override void HandleGetRequest(HttpProcessor p)
{
try
{
if (p.HttpUrl.StartsWith("/light.hue"))
if (p.HttpUrl.StartsWith("/favicon.ico")) //many browsers ask for favicon.ico
{
p.WriteFailure();

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("/favicon.ico");
Console.ResetColor();
Console.WriteLine("HTTP/1.0 404 File not found");

return;
}
else if (p.HttpUrl.StartsWith("/light.hue"))
{
//parsing GET parameters
var parameters = HttpUtility.ParseQueryString(p.HttpUrl.Split('?').Last());

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Call for hue-light with id: {0}", parameters["id"]);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("/light.hue");
Console.ResetColor();

var cmd = new LightCommand();
if (parameters.AllKeys.Contains("on"))
cmd.On = bool.Parse(parameters["on"]);
if (parameters.AllKeys.Contains("sat"))
cmd.Saturation = int.Parse(parameters["sat"]);
if (parameters.AllKeys.Contains("hue"))
cmd.Hue = int.Parse(parameters["hue"]);
if (parameters.AllKeys.Contains("bri"))
cmd.Brightness = byte.Parse(parameters["bri"]);
var cmd = parameters.ToLightCommand();

Hue.Client.SendCommandAsync(cmd, new List<string>() { parameters["id"] });
}
else if (p.HttpUrl.StartsWith("/group.hue"))
{
throw new NotImplementedException();
//parsing GET parameters
var parameters = HttpUtility.ParseQueryString(p.HttpUrl.Split('?').Last());

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Call for hue-group with id: {0}", parameters["id"]);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("/group.hue");
Console.ResetColor();

var cmd = parameters.ToLightCommand();

Hue.Client.SendGroupCommandAsync(cmd, parameters["id"]);
}
else if (p.HttpUrl.StartsWith("/scene.hue"))
{
throw new NotImplementedException();
//parsing GET parameters
var parameters = HttpUtility.ParseQueryString(p.HttpUrl.Split('?').Last());

Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Call for hue-scene with id: {0}", parameters["id"]);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("/scene.hue");
Console.ResetColor();


Hue.Client.RecallSceneAsync(parameters["id"]);
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Invalid call!");
Console.WriteLine("Invalid call: {0}", p.HttpUrl);
Console.ResetColor();

p.OutputStream.Write("failure");
p.WriteFailure();
return;
}

p.WriteSuccess();
p.OutputStream.Write("success");

}
Expand All @@ -117,12 +128,56 @@ public override void HandlePostRequest(HttpProcessor p, StreamReader inputData)
#region Internal services



#endregion Internal services

#region Events


#endregion Events
}

public static class NameValueCollectionExtensions
{
public static LightCommand ToLightCommand(this NameValueCollection parameters)
{
Console.ForegroundColor = ConsoleColor.Magenta;

Console.WriteLine("Id: {0}", parameters["id"]);

var cmd = new LightCommand()
{
Effect = Effect.None
};

if (parameters.AllKeys.Contains("on"))
{
cmd.On = bool.Parse(parameters["on"]);
Console.WriteLine("On: {0}", cmd.On);
}
if (parameters.AllKeys.Contains("sat"))
{
cmd.Saturation = int.Parse(parameters["sat"]);
Console.WriteLine("Saturation: {0}", cmd.Saturation);
}
if (parameters.AllKeys.Contains("hue"))
{
cmd.Hue = int.Parse(parameters["hue"]);
Console.WriteLine("Hue: {0}", cmd.Hue);
}
if (parameters.AllKeys.Contains("bri"))
{
cmd.Brightness = byte.Parse(parameters["bri"]);
Console.WriteLine("Brightness: {0}", cmd.Brightness);
}
if (parameters.AllKeys.Contains("ct"))
{
cmd.ColorTemperature = byte.Parse(parameters["ct"]);
Console.WriteLine("ColorTemperature: {0}", cmd.ColorTemperature);
}

Console.ResetColor();

return cmd;
}
}
}
9 changes: 5 additions & 4 deletions HueHookServer/Client.cs → HueHookServer/Hue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Rca.HueHookServer
Expand All @@ -17,7 +18,6 @@ public class Hue

#region Properties


#endregion Properties

#region Constructor
Expand All @@ -40,9 +40,10 @@ public async void ConnectBridge(IPAddress ip, string appKey)

var bridge = await Client.GetBridgeAsync();

//Console.WriteLine("Hue bridge is connected.");
//Console.WriteLine(bridge.Config.Name);

Console.WriteLine("Hue bridge is connected.");
Console.WriteLine("Name: {0}", bridge.Config.Name);
Console.WriteLine();
Console.WriteLine();
}

#endregion Services
Expand Down
2 changes: 1 addition & 1 deletion HueHookServer/HueHookServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="HookReceiver.cs" />
<Compile Include="Client.cs" />
<Compile Include="Hue.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Expand Down
38 changes: 35 additions & 3 deletions HueHookServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using SmartHttpServer;
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Threading;

namespace Rca.HueHookServer
Expand All @@ -13,9 +15,33 @@ static int Main(string[] args)
//Default-Port (8008 HTTP-Alternativ)
const int port = 8008;

Hue m_HueClient = new Hue();
Hue m_HueClient = new Hue();


#region Startup

var versionInfo = FileVersionInfo.GetVersionInfo(Assembly.GetEntryAssembly().Location);
var attribute = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false)
.Cast<AssemblyDescriptionAttribute>().FirstOrDefault();

Console.WriteLine();
Console.BackgroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.Black;
//Console.WriteLine("{0} v{1}", typeof(Program).Assembly.GetName().Name, typeof(Program).Assembly.GetName().Version);
Console.WriteLine("{0} v{1}", versionInfo.ProductName, versionInfo.ProductVersion);
Console.ResetColor();
if (attribute != null)
Console.WriteLine(attribute.Description);
Console.WriteLine();
Console.WriteLine(versionInfo.LegalCopyright);
for (int i = 0; i < 70; i++)
Console.Write("-");
Console.WriteLine();
Console.WriteLine();

#endregion

#region Init hue client
if (args.Length != 2)
{
Expand All @@ -34,7 +60,9 @@ static int Main(string[] args)

if (IPAddress.TryParse(args[0], out bridgeIp))
{
Console.WriteLine("Connect hue bridge at: {0}", bridgeIp);
m_HueClient.ConnectBridge(bridgeIp, args[1]);
Thread.Sleep(2500);
}
else
{
Expand All @@ -46,12 +74,13 @@ static int Main(string[] args)
}
#endregion


#region init server
Console.WriteLine("Start local HTTP server.");

IPAddress[] ipv4Addresses = Array.FindAll(Dns.GetHostEntry(string.Empty).AddressList, a => a.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
var ip = IPAddress.Parse("127.0.0.1");

Console.WriteLine("{0} v{1}", typeof(Program).Assembly.GetName().Name, typeof(Program).Assembly.GetName().Version);
Console.WriteLine();

if (ipv4Addresses.Count() == 0)
{
Expand Down Expand Up @@ -94,6 +123,9 @@ static int Main(string[] args)
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("http://{0}:{1}/", ip, port);
Console.ResetColor();
for (int i = 0; i < 70; i++)
Console.Write("-");
Console.WriteLine();
Console.WriteLine();
#endregion

Expand Down
2 changes: 1 addition & 1 deletion HueHookServer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern,
// die einer Assembly zugeordnet sind.
[assembly: AssemblyTitle("HueHookServer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("Control your Philips Hue system with simple HTTP GET requests.\nFor more informations visit: https://github.com/rmmlr/HueHook")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("RC-Art Solutions")]
[assembly: AssemblyProduct("HueHookServer")]
Expand Down
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This project is in a early develop phase (10/09/2018).
---

# HueHook
Control your Philips Hue system with simple url calls, POST parameters are not needed.
Control your Philips Hue system with simple HTTP GET requests. POST parameters are not needed!


## How to use
Expand All @@ -15,20 +15,27 @@ Run the HueHookServer.exe with 2 start parameters.
* parameter 1: IP of the hue-bridge
* parameter 2: authorized user-id

To obtain this parameters you can start the program with a shortcut. Append the parameters to the target path, e.g. `C:\path-to-program\HueHookServer.exe 192.168.0.1 my-app-key`.

After successfully initialization the programm prints the server address (ip and port).

### Use the program

Call the urls described below, each url starts with the address (ip and port) and have some optional parameters:

| |Value |Light |Group |Scene |
|---|---------|:----------------:|:----------------:|:----------------:|
|url| |`/light.hue` |`/group.hue` |`/scene.hue` |
|id |0 - 254 |:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|
|on |0, 1 |:heavy_check_mark:|:heavy_check_mark:|:x: |
|hue|0 - 65535|:heavy_check_mark:|:heavy_check_mark:|:x: |
|sat|0 - 254 |:heavy_check_mark:|:heavy_check_mark:|:x: |
|bri|0 - 254 |:heavy_check_mark:|:heavy_check_mark:|:x: |
|Description |Name|Value |Light |Group |Scene |
|------------------|----|-----------|:----------------:|:----------------:|:----------------:|
|:warning: URL | |`/*.hue` |`/light.hue` |`/group.hue` |`/scene.hue` |
|:warning: ID |id |0 - 254 |:heavy_check_mark:|:heavy_check_mark:|:heavy_check_mark:|
|On state |on |0, 1 |:heavy_check_mark:|:heavy_check_mark:|:x: |
|Hue |hue |0 - 65535 |:heavy_check_mark:|:heavy_check_mark:|:x: |
|Saturation |sat |0 - 254 |:heavy_check_mark:|:heavy_check_mark:|:x: |
|Brightness |bri |0 - 254 |:heavy_check_mark:|:heavy_check_mark:|:x: |
|Color Temperature |ct |153 - 500 |:heavy_check_mark:|:heavy_check_mark:|:x: |

:warning: required parameter &nbsp; :heavy_check_mark: parameter allowed &nbsp; :x: parameter not allowed

The URL must at least be made up of the required parameters (:warning:). In addition, further allowed parameters (:heavy_check_mark:) can be appended. The parameters are appended to the URL as a query string (name/value pairs), see the example below.

#### Example
The url `http://192.168.0.1/light.hue?id=1&on=1&bri=127` means, switch on the light with id 1 and setup the brightness to a value of 127.
Expand Down

0 comments on commit 2945c9d

Please sign in to comment.