Skip to content

Commit

Permalink
Merge pull request #3 from Bounz/light-dim-fix
Browse files Browse the repository at this point in the history
Light dim fix
  • Loading branch information
Bounz authored Feb 4, 2019
2 parents 7b668e4 + ba7d802 commit 3e0067a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 35 deletions.
36 changes: 14 additions & 22 deletions EchoBridge/Controllers/LightsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 All @@ -79,26 +77,24 @@ public HttpResponseMessage Put(string id)
logger.Info($"Alexa request: {jsonContent}");
var deviceState=Newtonsoft.Json.JsonConvert.DeserializeObject<DeviceState>(jsonContent);

var url = string.IsNullOrEmpty(device.DimUrl)
? deviceState.on
var url = deviceState.bri.HasValue && !string.IsNullOrEmpty(device.DimUrl)
? device.DimUrl
: deviceState.on
? device.onUrl
: device.offUrl
: device.DimUrl;
: device.offUrl;

url = ReplaceIntensityValue(url, deviceState);
var body = ReplaceIntensityValue(device.contentBody, deviceState);

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 All @@ -119,7 +115,7 @@ private static string ReplaceIntensityValue(string request, DeviceState deviceSt
intensity.byte : 0-255 brightness. this is raw from the echo
intensity.percent : 0-100, adjusted for the vera
*/
var intensity = deviceState.bri;
var intensity = deviceState.bri ?? 0;
if (deviceState.on && intensity == 0)
{
logger.Info("DeviceState was on but brightness value was zero. Setting to default brightness.");
Expand All @@ -144,9 +140,5 @@ private static string ReplaceIntensityValue(string request, DeviceState deviceSt
}
return request;
}




}
}
22 changes: 9 additions & 13 deletions EchoBridge/Devices/DeviceState.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace HGEchoBridge
{
public class DeviceState {
public bool on {get; set;}
public int bri { get; set; }
public int? bri { get; set; }
public int hue { get; set; }
public int sat { get; set; }
public String effect { get; set; }
public string effect { get; set; }
public int ct { get; set; }
public String alert { get; set; }
public String colormode { get; set; }
public string alert { get; set; }
public string colormode { get; set; }
public bool reachable { get; set; }
public List<Double> xy { get; set; }
public List<double> xy { get; set; }



public override String ToString()
public override string ToString()
{
return "DeviceState{" +
"on=" + on.ToString() +
", bri=" + bri.ToString() +
"on=" + on +
", bri=" + bri +
'}';
}
}
Expand Down

0 comments on commit 3e0067a

Please # to comment.