KCSLightChannel.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace TeamAAS_VP.Core.Lights
  7. {
  8. public class KCSLightChannel : ILightChannel
  9. {
  10. private readonly KCSLightController _controller;
  11. private int _brightness;
  12. private bool _isOn;
  13. public int ChannelIndex { get; }
  14. public string ChannelName { get; set; }
  15. public int Brightness => _brightness;
  16. public bool IsOn => _isOn;
  17. internal int CurrentBrightness => _brightness;
  18. public KCSLightChannel(int index, KCSLightController controller)
  19. {
  20. ChannelIndex = index;
  21. ChannelName = $"Channel {index + 1}";
  22. _controller = controller;
  23. }
  24. public async Task<bool> SetBrightnessAsync(int brightness)
  25. {
  26. if (brightness < 0 || brightness > 255)
  27. throw new ArgumentOutOfRangeException(nameof(brightness));
  28. var result = await _controller.SetChannelBrightnessInternal(ChannelIndex, brightness);
  29. if (result)
  30. {
  31. _brightness = brightness;
  32. _isOn = brightness > 0;
  33. }
  34. return result;
  35. }
  36. public async Task<bool> TurnOnAsync()
  37. {
  38. return await SetBrightnessAsync(_brightness > 0 ? _brightness : 100);
  39. }
  40. public async Task<bool> TurnOffAsync()
  41. {
  42. return await SetBrightnessAsync(0);
  43. }
  44. public async Task<int> GetBrightnessAsync()
  45. {
  46. _brightness = await _controller.GetChannelBrightnessInternal(ChannelIndex);
  47. _isOn = _brightness > 0;
  48. return _brightness;
  49. }
  50. public async Task<bool> GetStatusAsync()
  51. {
  52. await GetBrightnessAsync();
  53. return _isOn;
  54. }
  55. }
  56. }