Skip to content

Commit

Permalink
Merge pull request #1 from davidwallis3101/feature/custom-port
Browse files Browse the repository at this point in the history
Added ability to use custom HG port.
  • Loading branch information
David Wallis authored Feb 17, 2018
2 parents de363ee + ab82f71 commit 29a2eb0
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 329 deletions.
6 changes: 1 addition & 5 deletions EchoBridge/Devices/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace HGEchoBridge
{
public class Device
{

public Device()
{
_id = Guid.NewGuid();
Expand All @@ -33,16 +32,13 @@ public string id
}
}
}

public String name {get; set;}
public String deviceType {get; set;}
public String offUrl {get; set;}
public String onUrl {get; set;}
public String httpVerb {get; set;}
public String contentType{get; set;}
public String contentBody {get; set;}




}
}
1 change: 1 addition & 0 deletions EchoBridge/HGEchoBridge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
<Compile Include="Devices\DeviceState.cs" />
<Compile Include="Devices\HueApiResponse.cs" />
<Compile Include="SSDP\SSDPService.cs" />
<Compile Include="SSDP\UdpStateInfo.cs" />
<Compile Include="Webserver\Globals.cs" />
<Compile Include="Controllers\LightsController.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
118 changes: 49 additions & 69 deletions EchoBridge/SSDP/SSDPService.cs
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using NLog;

namespace HGEchoBridge
{
public class UdpStateInfo
{
public UdpStateInfo(UdpClient c, IPEndPoint ep )
{
client = c;
endpoint = ep;
}
public UdpClient client;
public IPEndPoint endpoint;
}

public class SSDPService
{
private static Logger logger = LogManager.GetCurrentClassLogger();

private static bool running;
private static string MulticastIP;
private static string MulticastLocalIP;
private static int MulticastPort;
private static bool _running;
private static string _multicastIp;
private static string _multicastLocalIp;
private static int _multicastPort;

public static string UUID;
public static int WebServerPort;

private static UdpClient MulticastClient;

private static byte[] byteDiscovery;
private static UdpClient _multicastClient;
private static byte[] _byteDiscovery;
public static string DiscoveryResponse;

//{0}=IPAddress {1}=Port {2}=RandomUUID
private static string discoveryTemplate = "HTTP/1.1 200 OK\r\n" +
private const string DiscoveryTemplate = "HTTP/1.1 200 OK\r\n" +
"CACHE-CONTROL: max-age=86400\r\n" +
"EXT:\r\n" +
"LOCATION: http://{0}:{1}/api/setup.xml\r\n" +
Expand All @@ -48,35 +34,36 @@ public class SSDPService


//239.255.255.250 port 1900 10.10.26
public SSDPService(string multicastIP, int multicastPort, string LocalIP, int webPort, string uuid)
public SSDPService(string multicastIp, int multicastPort, string localIp, int webPort, string uuid)
{
logger.Info("New SSDP Service initiated on IP [{0}], port [{1}]", multicastIP, multicastPort);
MulticastIP = multicastIP;
MulticastPort = multicastPort;
MulticastLocalIP = LocalIP;
logger.Info("New SSDP Service initiated on IP [{0}], port [{1}]", multicastIp, multicastPort);
_multicastIp = multicastIp;
_multicastPort = multicastPort;
_multicastLocalIp = localIp;
WebServerPort =webPort;
UUID = uuid;
DiscoveryResponse = string.Format(discoveryTemplate, MulticastLocalIP, WebServerPort, UUID);
byteDiscovery = Encoding.ASCII.GetBytes(DiscoveryResponse);
running = false;
DiscoveryResponse = string.Format(DiscoveryTemplate, _multicastLocalIp, WebServerPort, UUID);
_byteDiscovery = Encoding.ASCII.GetBytes(DiscoveryResponse);
_running = false;
}

public bool Start()
{
try
{
logger.Info("Starting SSDP Service on IP [{0}], port [{1}]...", MulticastIP, MulticastPort);
MulticastClient = new UdpClient(MulticastPort);
IPAddress ipSSDP = IPAddress.Parse(MulticastIP);
logger.Info("Starting SSDP Service on IP [{0}], port [{1}]...", _multicastIp, _multicastPort);
_multicastClient = new UdpClient(_multicastPort);
var ssdpIp = IPAddress.Parse(_multicastIp);

logger.Info("Joining multicast group on IP [{0}]...", MulticastLocalIP);
MulticastClient.JoinMulticastGroup(ipSSDP, IPAddress.Parse(MulticastLocalIP));
logger.Info("Joining multicast group on IP [{0}]...", _multicastLocalIp);
_multicastClient.JoinMulticastGroup(ssdpIp, IPAddress.Parse(_multicastLocalIp));

running = true;
_running = true;

UdpStateInfo udpListener = new UdpStateInfo(MulticastClient, new IPEndPoint(ipSSDP, MulticastPort));
var udpListener = new UdpStateInfo(_multicastClient, new IPEndPoint(ssdpIp, _multicastPort));

logger.Info("Starting Multicast Receiver...");
MulticastClient.BeginReceive(new AsyncCallback(MulticastReceiveCallback), udpListener);
_multicastClient.BeginReceive(MulticastReceiveCallback, udpListener);
logger.Info("SSDP Service started.");
}
catch (Exception ex)
Expand All @@ -90,75 +77,72 @@ public bool Start()

public bool IsRunning()
{
return running;
return _running;
}

public void Stop()
{
logger.Info("Stopping SSDP Service...");
running = false;
MulticastClient.Client.Shutdown(SocketShutdown.Both);
MulticastClient.Close();
_running = false;
_multicastClient.Client.Shutdown(SocketShutdown.Both);
_multicastClient.Close();
logger.Info("SSDP Service stopped.");

}
public static void MulticastReceiveCallback(IAsyncResult ar)

private static void MulticastReceiveCallback(IAsyncResult ar)
{
try
{

UdpStateInfo udpListener = (UdpStateInfo)(ar.AsyncState);
UdpClient client = udpListener.client;
IPEndPoint endpoint = udpListener.endpoint;
var udpListener = (UdpStateInfo)ar.AsyncState;
var client = udpListener.client;
var endpoint = udpListener.endpoint;

if (client != null)
{
//logger.Info("Received a UDP multicast from IP [{0}], on port [{1}].", endpoint.Address.ToString(), endpoint.Port);
Byte[] receiveBytes = client.EndReceive(ar, ref endpoint);
byte[] receiveBytes = client.EndReceive(ar, ref endpoint);
string receiveString = Encoding.ASCII.GetString(receiveBytes);

//todo dw

//discovery has occured, send our response
if (IsSSDPDiscoveryPacket(receiveString))
if (IsSsdpDiscoveryPacket(receiveString))
{
logger.Debug("Sending SSDP setup information to {0}",endpoint);

//MulticastClient.Send(byteDiscovery, byteDiscovery.Length, endpoint);
Socket WinSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

WinSocket.Connect(endpoint);
WinSocket.Send(byteDiscovery);
WinSocket.Shutdown(SocketShutdown.Both);
WinSocket.Close();

using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
{
socket.Connect(endpoint);
socket.Send(_byteDiscovery);
socket.Shutdown(SocketShutdown.Both);
socket.Close();
}
}


}
if (running)

if (_running)
{
//logger.Info("Restarted Multicast Receiver.");
MulticastClient.BeginReceive(new AsyncCallback(MulticastReceiveCallback), udpListener);
_multicastClient.BeginReceive(MulticastReceiveCallback, udpListener);
}

}
catch (Exception ex)
{

if (running)
if (_running)
{
logger.Warn(ex, "Error occured in MulticastReceiveCallBack.");
}
else
{
logger.Debug(ex, "Ignoring an Error occured in MulticastReceiveCallBack as SSDP service is not running.");
}

}
}

private static bool IsSSDPDiscoveryPacket(string message)
private static bool IsSsdpDiscoveryPacket(string message)
{
//logger.Info("Testing if message is SSDP Discovery Packet...");
//logger.Info("Examing message [{0}]", message);
Expand All @@ -170,10 +154,6 @@ private static bool IsSSDPDiscoveryPacket(string message)
}
//logger.Info("SSDP Discovery Packet not detected.");
return false;

}

}


}
17 changes: 17 additions & 0 deletions EchoBridge/SSDP/UdpStateInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Net;
using System.Net.Sockets;

namespace HGEchoBridge
{
public class UdpStateInfo
{
public UdpStateInfo(UdpClient c, IPEndPoint ep )
{
client = c;
endpoint = ep;
}

public UdpClient client;
public IPEndPoint endpoint;
}
}
24 changes: 6 additions & 18 deletions EchoBridge/Webserver/WebServer.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Hosting;
using System.Net.Http;
using Owin;
using System.Web.Http;
using NLog;

namespace HGEchoBridge
{
public class WebServer
public class WebServer
{
private static Logger logger = LogManager.GetCurrentClassLogger();

private static Logger logger = LogManager.GetCurrentClassLogger();

private IDisposable webApplication;

public WebServer(){
logger.Info("New webserver initiated.");
}

public WebServer(string ipAddress, int port, string uuid, int defaultIntensity, List<Device>deviceObj)
public WebServer(string ipAddress, int port, string uuid, int defaultIntensity, List<Device> deviceObj)
{
logger.Info("New webserver initiated.");
Globals.IPAddress = ipAddress;
Expand All @@ -32,14 +22,12 @@ public WebServer(string ipAddress, int port, string uuid, int defaultIntensity,

Globals.DeviceList = new Devices(deviceObj);
logger.Info("Webserver created. DeviceConfig holds [{0}] device(s)", Globals.DeviceList.Count());

}


public void Start()
{
logger.Info("Webserver starting up, listening on {0}", Globals.BaseAddress);
webApplication = WebApp.Start<WebServerStartup>(url: Globals.BaseAddress);
webApplication = WebApp.Start<WebServerStartup>(Globals.BaseAddress);
logger.Info("Webserver started.");
}

Expand All @@ -49,5 +37,5 @@ public void Stop()
webApplication.Dispose();
logger.Info("Webserver stopped.");
}
}
}
}
Loading

0 comments on commit 29a2eb0

Please sign in to comment.