static async Task <Uri> SetLightAsync(KeyLightAir light, string path) { HttpResponseMessage response = await client.PutAsJsonAsync( path, light); response.EnsureSuccessStatusCode(); return(response.Headers.Location); }
static async Task <KeyLightAir> GetLightAsync(string path) { KeyLightAir light = null; HttpResponseMessage response = await client.GetAsync(path); if (response.IsSuccessStatusCode) { light = await response.Content.ReadAsAsync <KeyLightAir>(); } return(light); }
static async Task RunAsync() { client.BaseAddress = new Uri(Properties.Settings.Default["ip"].ToString()); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); try { var url = "elgato/lights"; KeyLightAir keylight = new KeyLightAir(); // Get the current light status keylight = await GetLightAsync(url); //Turn off if already on if (keylight.lights[0].on == 1) { Console.WriteLine("Turning off..."); keylight.lights[0].on = 0; } else { // Create a new light Console.WriteLine("Turning on..."); Light light = new Light { brightness = Convert.ToInt32(Properties.Settings.Default["brightness"]), temperature = Convert.ToInt32(Properties.Settings.Default["temperature"]), on = 1 }; keylight = new KeyLightAir(); keylight.lights = new List <Light>(); keylight.lights.Add(light); keylight.numberOfLights = keylight.lights.Count(); } var result = await SetLightAsync(keylight, url); } catch (Exception e) { Console.WriteLine(e.Message); } }