Skip to content

Commit

Permalink
Merge pull request #2 from kisielewski/devel
Browse files Browse the repository at this point in the history
Stable server
  • Loading branch information
kisielewski authored Apr 26, 2019
2 parents 4c747dc + 116bd95 commit 8da9bd7
Show file tree
Hide file tree
Showing 13 changed files with 611 additions and 48 deletions.
22 changes: 16 additions & 6 deletions SmartHome-Server/Actions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ public Actions(Serial serial, SmartHome home)
this.home = home;
}

public void OnlySendControl(string name, int val)
public void SendControl(string name, int val)
{
serial.Write(name + val);
home.AddMessage("n", name + val);
}

public void OnlyAddMessage(string name, int val)
{
home.AddMessage("n", name + val);
}

public void SendAlarm(string name, int val)
{
serial.Write("AL" + val);
Expand Down Expand Up @@ -50,7 +55,7 @@ public void DetectAlarm(string name, int val)
home.AddMessage("p", "AB1");
}
}
else if(home.Controls["AB"] == 1 && val == 0)
else if(val == 0)
{
home.SetControl("AB", 0);
}
Expand All @@ -59,22 +64,27 @@ public void DetectAlarm(string name, int val)

public void DetectDoor(string name, int val)
{
if(val < 20 && home.Controls["S2"] == 0)
if(home.Controls["APO"] == 0)
{
return;
}
if(val < 20)
{
home.SetControl("S2", 1);
}
else if(val > 25 && home.Controls["S2"] == 1)
else if(val > 25)
{
home.SetControl("S2", 0);
}
}

public void DetectLight(string name, int val)
{
if(home.Controls["L3"] != val)
if(home.Controls["APR"] == 0)
{
home.SetControl("L3", val);
return;
}
home.SetControl("L3", val);
}
}
}
69 changes: 54 additions & 15 deletions SmartHome-Server/HttpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ namespace SmartHome_Server
public class HttpServer
{
private SmartHome home;
private string prefix;
private Thread HttpThread;

public HttpServer(SmartHome home)
public HttpServer(string prefix, SmartHome home)
{
this.prefix = prefix;
this.home = home;
}

Expand All @@ -32,7 +34,7 @@ public void Start()
private void Loop()
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://+:80/");
listener.Prefixes.Add(prefix);
listener.Start();
while (true)
{
Expand All @@ -48,34 +50,71 @@ private void NewRequest(object _context)
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
Response controller = new Response(home);
NameValueCollection collection;
string query = request.Url.Query;
string responseString = "";
byte[] responseBytes = null;
string path = "files"+request.Url.AbsolutePath;
if(request.Url.AbsolutePath == "/")
{
path += "index.html";
}

switch (request.Url.AbsolutePath)
{
case "/controls/get":
responseString = controller.GetControls();
case "/api/content/get":
collection = HttpUtility.ParseQueryString(query);
if(collection["lastchange"] != null)
{
string dateString = collection["lastchange"].Replace(' ', '+').Replace("\"", "");
if (DateTime.TryParse(dateString, out DateTime date))
{
responseString = controller.GetAllContent(date);
break;
}
}
responseString = controller.GetAllContent();
break;
case "/controls/set":
string query = request.Url.Query;
NameValueCollection collection = HttpUtility.ParseQueryString(query);
case "/api/controls/set":
collection = HttpUtility.ParseQueryString(query);
controller.SetControls(collection);
break;
case "/messages/get":
case "/api/controls/get":
responseString = controller.GetControls();
break;
case "/api/messages/get":
responseString = controller.GetMessages();
break;
case "/sensors/get":
case "/api/sensors/get":
responseString = controller.GetSensors();
break;
default:
response.StatusCode = 404;
if (File.Exists(path))
{
responseBytes = File.ReadAllBytes(path);
response.ContentType = MimeMapping.GetMimeMapping(path);
}
else
{
response.StatusCode = 404;
}
break;
}

byte[] buffer = Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
response.AddHeader("Access-Control-Allow-Origin", "*");
byte[] buffer = null;
Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
if (responseBytes != null && responseBytes.Length > 0)
{
response.ContentLength64 = responseBytes.Length;
output.Write(responseBytes, 0, responseBytes.Length);
}
else
{
buffer = Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
response.AddHeader("Access-Control-Allow-Origin", "*");
output.Write(buffer, 0, buffer.Length);
}

output.Close();
}
}
Expand Down
6 changes: 4 additions & 2 deletions SmartHome-Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ static void Main(string[] args)
Actions actions = new Actions(serial, home);

//bind actions
home.DefaultCotrolsAction = new SmartHome.TypeControlsAction(actions.OnlySendControl);
home.DefaultCotrolsAction = new SmartHome.TypeControlsAction(actions.SendControl);
home.ControlsActions.Add("AL", new SmartHome.TypeControlsAction(actions.SendAlarm));
home.ControlsActions.Add("APR", new SmartHome.TypeControlsAction(actions.OnlyAddMessage));
home.ControlsActions.Add("APO", new SmartHome.TypeControlsAction(actions.OnlyAddMessage));
home.SensorsActions.Add("PT", new SmartHome.TypeSensorsAction(actions.SetTemperature));
home.SensorsActions.Add("PR", new SmartHome.TypeSensorsAction(actions.DetectAlarm));
home.SensorsActions.Add("PO", new SmartHome.TypeSensorsAction(actions.DetectDoor));

HttpServer server = new HttpServer(home);
HttpServer server = new HttpServer(args[1], home);
server.Start();

home.AddMessage("p", "http");
Expand Down
44 changes: 38 additions & 6 deletions SmartHome-Server/Response.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Specialized;
using System;
using System.Threading;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace SmartHome_Server
Expand All @@ -14,25 +16,55 @@ public Response(SmartHome home)

public string GetControls()
{
return JsonConvert.SerializeObject(home.Controls);
home.Semaphore.WaitOne();
string result = JsonConvert.SerializeObject(home.Controls);
home.Semaphore.Release();
return result;
}

public void SetControls(NameValueCollection collection)
{
foreach(string name in collection)
foreach (string name in collection)
{
home.SetControl(name, int.Parse(collection[name]));
if (int.TryParse(collection[name], out int val)){
home.Semaphore.WaitOne();
home.SetControl(name, val);
home.Semaphore.Release();
}
}
}

public string GetMessages()
{
return JsonConvert.SerializeObject(home.Messages);
home.Semaphore.WaitOne();
string result = JsonConvert.SerializeObject(home.Messages);
home.Semaphore.Release();
return result;
}

public string GetSensors()
{
return JsonConvert.SerializeObject(home.Sensors);
home.Semaphore.WaitOne();
string result = JsonConvert.SerializeObject(home.Sensors);
home.Semaphore.Release();
return result;
}

public string GetAllContent()
{
home.Semaphore.WaitOne();
string result = JsonConvert.SerializeObject(home);
home.Semaphore.Release();
return result;
}

public string GetAllContent(DateTime date)
{
while (home.LastChange.Equals(date))
{
Thread.Sleep(100);
}
return GetAllContent();
}
}
}
14 changes: 6 additions & 8 deletions SmartHome-Server/Serial.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,18 @@ private void Read()
}
if (data.Equals("D"))
{
home.Semaphore.WaitOne();
home.AddMessage("p", "D");
home.Semaphore.Release();
continue;
}
string name = data.Substring(0, 2);
int val = 0;
try
{
val = int.Parse(data.Substring(2, data.Length - 2));
}
catch (Exception)
if(int.TryParse(data.Substring(2, data.Length-2), out int val))
{
continue;
home.Semaphore.WaitOne();
home.SetSensor(name, val);
home.Semaphore.Release();
}
home.SetSensor(name, val);
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions SmartHome-Server/SmartHome-Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,25 @@
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Content Include="files\index.html">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="files\main.css">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="files\script.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="files\smart_home.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="files\smart_home.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="files\smart_home_small.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading

0 comments on commit 8da9bd7

Please sign in to comment.