/// <summary> /// Sets the light to a specific brightness level of <see cref="MinimumBrightness"/> and <see cref="MaximumBrightness"/>. /// </summary> /// <param name="level">The level amount to set the brightness.</param> /// <exception cref="ElgatoLightNotOnException">Thrown if trying to set the brightness and the device isn't turned on.</exception> /// <exception cref="ElgatoLightOutOfRangeException">Thrown if the new value is out of range. The minimum range is <see cref="MinimumBrightness"/> and maximum brightness is <see cref="MaximumBrightness"/>.</exception> public async Task SetBrightnessAsync(int level) { if (IsOn == false) { throw new ElgatoLightNotOnException("The light must be turned On in order to turn the set the brightness."); } await UpdateStatusAsync(); Debug.WriteLine($"SetBrightnessAsync Old Values: {ToString()}"); if (level < MinimumBrightness || level > MaximumBrightness) { throw new ElgatoLightOutOfRangeException($"Brightness Out of Range {level}. The minimum range is {MinimumBrightness} and the Maximum is {MaximumBrightness}."); } var targetStatus = LightsStatus.FromLight(this); targetStatus.Lights[0].Brightness = level; var targetStatusJson = targetStatus.ToJson(); var retreturnedJsonStatus = await UpdateDevice(targetStatusJson); if (string.IsNullOrWhiteSpace(retreturnedJsonStatus)) { Debug.WriteLine($"The network is either offline or the device returned null data."); return; } await ParseLightStatus(retreturnedJsonStatus); Debug.WriteLine($"SetBrightnessAsync New Values: {ToString()}"); }
/// <summary> /// Must be 2900-7000 /// </summary> /// <param name="temp"></param> /// <returns></returns> /// <exception cref="ElgatoLightNotOnException">Thrown if trying to set the brightness and the device isn't turned on.</exception> /// <exception cref="ElgatoLightOutOfRangeException">Thrown if the new value is out of range. The minimum range is <see cref="MinimumTemperature"/> and maximum tempature is <see cref="MaximumTemperature"/>.</exception> public async Task SetColorTemperatureAsync(int temp) { if (IsOn == false) { throw new ElgatoLightNotOnException("The light must be turned On in order to turn the set the tempature."); } await UpdateStatusAsync(); Debug.WriteLine($"SetColorTemperatureAsync Old Values: {ToString()}"); if (temp < MinimumTemperature || temp > MaximumTemperature) { throw new ElgatoLightOutOfRangeException($"Temperature Out of Range. {temp} is an invlaid temperature"); } var targetStatusStatus = LightsStatus.FromLight(this); targetStatusStatus.Lights[0].Temperature = temp; var targetStatusJson = targetStatusStatus.ToJson(); var retreturnedJsonStatus = await UpdateDevice(targetStatusJson); if (string.IsNullOrWhiteSpace(retreturnedJsonStatus)) { Debug.WriteLine($"The network is either offline or the device returned null data."); return; } await ParseLightStatus(retreturnedJsonStatus); Debug.WriteLine($"SetColorTemperatureAsync New Values: {ToString()}"); }
public async Task OffAsync() { await UpdateStatusAsync(); Debug.WriteLine($"OffAsync Old Values: {ToString()}"); // extract the current settings var targetSettings = LightsStatus.FromLight(this); targetSettings.Lights[0].On = 0; // convert the current status to json var targetStatusJson = targetSettings.ToJson(); var returnedJsonStatus = await UpdateDevice(targetStatusJson); if (string.IsNullOrWhiteSpace(returnedJsonStatus)) { Debug.WriteLine($"The network is either offline or the device returned null data."); return; } await ParseLightStatus(returnedJsonStatus); await UpdateSettings(); Debug.WriteLine($"OffAsync New Values: {ToString()}"); }
internal async Task UpdateStatusAsync() { var statusEndPoint = string.Format(StatusInfoEndPointTemplate, Address, Port); if (NetworkDiscoveryHelper.IsNetworkAvailable() == false) { return; } using (var client = new HttpClient()) { var streamTask = client.GetStreamAsync(statusEndPoint); LightsStatus status = await JsonSerializer.DeserializeAsync <LightsStatus>(await streamTask); if (status != null && status.Lights != null && status.Lights.Count > 0) { On = status.Lights[0].On; Brightness = status.Lights[0].Brightness; Temperature = status.Lights[0].Temperature; } } }