Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Bounz committed Feb 4, 2019
1 parent 3b58774 commit ba7d802
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions EchoBridge/Controllers/LightsController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Text;
using System.Web.Http;
using System.Net.Http;
Expand All @@ -17,46 +17,44 @@ public class LightsController : ApiController
public HttpResponseMessage Get()
{
logger.Info("LightsController called. (// GET api/{userId}/lights)...");
string output = "{" ;
var output = "{" ;

foreach (Device d in Globals.DeviceList.List())
foreach (var d in Globals.DeviceList.List())
{
output += "\"" + d.id.ToString() + "\": \"" + d.name + "\",";
output += "\"" + d.id + "\": \"" + d.name + "\",";
}
//remove trailing comma
if (output.Length > 1) output = output.Substring(0, output.Length - 1);

output += "}";


return new HttpResponseMessage()
return new HttpResponseMessage
{
Content = new StringContent(output, Encoding.UTF8, "application/json")
};

}

// GET api/{userId}/lights/5
public HttpResponseMessage Get(string id)
{
logger.Info("LightsController called (// GET api/{userId}/lights/{id}) Retrieving light with id[" + id + "]...");
Device device = Globals.DeviceList.FindById(id);
var device = Globals.DeviceList.FindById(id);
if (device == null)
{
logger.Warn("LightsController GET: Could not locate a light with id [" + id + "].");
return new HttpResponseMessage()
return new HttpResponseMessage
{
StatusCode = System.Net.HttpStatusCode.NotFound,
StatusCode = HttpStatusCode.NotFound,
ReasonPhrase = "Could locate a device with that id."
};
}

logger.Info("LightsController GET: Returned DeviceResponse for device named[{0}], with id [{1}]", device.name, device.id);
DeviceResponse response = DeviceResponse.createResponse(device.name, device.id);
return Request.CreateResponse(System.Net.HttpStatusCode.OK, response);
var response = DeviceResponse.createResponse(device.name, device.id);
return Request.CreateResponse(HttpStatusCode.OK, response);
}


// PUT api/{userId}/lights/5/
public HttpResponseMessage Put(string id)
{
Expand Down Expand Up @@ -90,15 +88,13 @@ public HttpResponseMessage Put(string id)

if (Utilities.MakeHttpRequest(url, device.httpVerb, device.contentType, body))
{

logger.Info("LightsController PUT: Successfully updated state of device via HTTP request.");
var responseString = "[{\"success\":{\"/lights/" + device.id + "/state/on\":" + deviceState.on.ToString().ToLower() + "}}]";
return new HttpResponseMessage
{
Content = new StringContent(responseString, Encoding.UTF8, "application/json"),
StatusCode = HttpStatusCode.OK
};

}
else
{
Expand Down Expand Up @@ -144,9 +140,5 @@ private static string ReplaceIntensityValue(string request, DeviceState deviceSt
}
return request;
}




}
}

0 comments on commit ba7d802

Please # to comment.