Created
May 30, 2016 14:57
-
-
Save aevitas/105dd7db8ca86718b9e9900fecf12901 to your computer and use it in GitHub Desktop.
LIFX REST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Net; | |
using System.Threading.Tasks; | |
using Lifx.Core.Internal; | |
using Lifx.Core.Protocol; | |
using RestSharp; | |
using RestSharp.Authenticators; | |
using RestSharp.Serializers; | |
namespace Lifx.Core | |
{ | |
public class LifxClient | |
{ | |
private readonly RestClient _client; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="LifxClient" /> class, using the specified LIFX authentication token. | |
/// </summary> | |
/// <param name="authenticationToken">The authentication token.</param> | |
public LifxClient(string authenticationToken) | |
{ | |
Requires.NotNullOrEmpty(authenticationToken, nameof(authenticationToken)); | |
_client = new RestClient(Current.BaseUrl) | |
{ | |
Authenticator = new HttpBasicAuthenticator(authenticationToken, string.Empty) | |
}; | |
} | |
/// <summary> | |
/// Gets lights belonging to the authenticated account. Filter the lights using selectors. Properties such as id, | |
/// label, group and location can be used in selectors. | |
/// </summary> | |
/// <param name="selector">The selector.</param> | |
/// <returns></returns> | |
public async Task<IEnumerable<Light>> GetLightsAsync(Selector selector) | |
{ | |
var request = CreateRequest($"lights/{selector}", Method.GET); | |
request.AddParameter("selector", selector); | |
var response = await _client.ExecuteGetTaskAsync<List<Light>>(request); | |
return response.Data; | |
} | |
/// <summary> | |
/// Sets the state of the lights within the selector. All parameters (except for the selector) are optional. If you | |
/// don't supply a parameter, the API will leave that value untouched. | |
/// </summary> | |
/// <param name="selector">The selector.</param> | |
/// <param name="powerState">State of the power.</param> | |
/// <param name="color">The color.</param> | |
/// <param name="brightness">The brightness.</param> | |
/// <param name="duration">The duration in seconds.</param> | |
/// <returns></returns> | |
public async Task SetState(Selector selector, PowerState powerState, Color color, double brightness, double duration) | |
{ | |
var request = CreateRequest($"lights/{selector}/state", Method.PUT); | |
request.AddParameter("selector", selector); | |
request.AddParameter("power", powerState.ToString().ToLowerInvariant()); | |
request.AddParameter("color", color.ToString()); | |
request.AddParameter("brightness", brightness); | |
request.AddParameter("duration", duration); | |
await _client.ExecuteTaskAsync(request); | |
} | |
/// <summary> | |
/// Turn off lights if any of them are on, or turn them on if they are all off. All lights matched by the selector will | |
/// share the same power state after this action. Physically powered off lights are ignored. | |
/// </summary> | |
/// <param name="selector">The selector.</param> | |
/// <param name="duration">The duration in seconds.</param> | |
/// <returns></returns> | |
public async Task TogglePower(Selector selector, double duration) | |
{ | |
var request = CreateRequest($"lights/{selector}/toggle", Method.POST); | |
request.AddParameter("selector", selector); | |
request.AddParameter("duration", duration); | |
await _client.ExecutePostTaskAsync(request); | |
} | |
/// <summary> | |
/// Performs a breathe effect by slowly fading between the given colors. Use the parameters to tweak the effect. | |
/// </summary> | |
/// <param name="selector">The selector.</param> | |
/// <param name="color">The color.</param> | |
/// <param name="startColor">The start color.</param> | |
/// <param name="period">The period.</param> | |
/// <param name="cycles">The cycles.</param> | |
/// <param name="persist">if set to <c>true</c> [persist].</param> | |
/// <param name="powerOn">if set to <c>true</c> [power on].</param> | |
/// <param name="peak">The peak.</param> | |
/// <returns></returns> | |
public async Task Breathe(Selector selector, Color color, Color startColor, double period, double cycles, bool persist, | |
bool powerOn, double peak) | |
{ | |
var request = CreateRequest($"lights/{selector}/effects/breathe", Method.POST); | |
request.AddParameter("selector", selector); | |
request.AddParameter("color", color.ToString()); | |
request.AddParameter("from_color", startColor.ToString()); | |
request.AddParameter("period", period); | |
request.AddParameter("cycles", cycles); | |
request.AddParameter("persist", persist); | |
request.AddParameter("power_on", powerOn); | |
request.AddParameter("peak", peak); | |
await _client.ExecutePostTaskAsync(request); | |
} | |
/// <summary> | |
/// Performs a pulse effect by quickly flashing between the given colors. Use the parameters to tweak the effect. | |
/// </summary> | |
/// <param name="selector">The selector.</param> | |
/// <param name="color">The color.</param> | |
/// <param name="startColor">The start color.</param> | |
/// <param name="period">The period.</param> | |
/// <param name="cycles">The cycles.</param> | |
/// <param name="persist">if set to <c>true</c> [persist].</param> | |
/// <param name="powerOn">if set to <c>true</c> [power on].</param> | |
/// <returns></returns> | |
public async Task Pulse(Selector selector, Color color, Color startColor, double period, double cycles, bool persist, | |
bool powerOn) | |
{ | |
var request = CreateRequest($"lights/{selector}/effects/pulse", Method.POST); | |
request.AddParameter("selector", selector); | |
request.AddParameter("color", color.ToString()); | |
request.AddParameter("from_color", startColor.ToString()); | |
request.AddParameter("period", period); | |
request.AddParameter("cycles", cycles); | |
request.AddParameter("persist", persist); | |
request.AddParameter("power_on", powerOn); | |
await _client.ExecutePostTaskAsync(request); | |
} | |
/// <summary> | |
/// Activates a scene from the users account | |
/// </summary> | |
/// <param name="sceneId">The scene identifier.</param> | |
/// <param name="duration">The duration.</param> | |
/// <returns></returns> | |
public async Task ActivateScene(string sceneId, double duration) | |
{ | |
var request = CreateRequest($"scenes/scene_id:{sceneId}/activate", Method.PUT); | |
request.AddParameter("scene_uuid", sceneId); | |
request.AddParameter("duration", duration); | |
await _client.ExecuteTaskAsync(request); | |
} | |
/// <summary> | |
/// This endpoint lets you validate a user's color string and return the hue, saturation, brightness and kelvin values | |
/// that the API will interpret as. | |
/// </summary> | |
/// <param name="color">The color.</param> | |
/// <returns></returns> | |
public async Task<Color> ValidateColor(string color) | |
{ | |
var request = CreateRequest("color", Method.GET); | |
request.AddParameter("string", color); | |
var result = await _client.ExecuteTaskAsync<Color>(request); | |
return result.Data; | |
} | |
private RestRequest CreateRequest(string resource, Method method) | |
{ | |
return new RestRequest(resource, method) | |
{ | |
JsonSerializer = new JsonSerializer(), | |
RequestFormat = DataFormat.Json, | |
Method = method | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment