LightControllerBase.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. /// <summary>
  9. /// 光源控制器基类
  10. /// </summary>
  11. public abstract class LightControllerBase : ILightController
  12. {
  13. public int Id { get; }
  14. public string Name { get; set; }
  15. public LightModel Model { get; }
  16. public int ChannelCount { get; }
  17. public bool IsConnected { get; protected set; }
  18. protected List<ILightChannel> ChannelsInternal { get; }
  19. public IReadOnlyList<ILightChannel> Channels => ChannelsInternal;
  20. protected LightControllerBase(int id,int channelCount)
  21. {
  22. Id = id;
  23. ChannelCount = channelCount;
  24. ChannelsInternal = new List<ILightChannel>();
  25. InitializeChannels();
  26. }
  27. private void InitializeChannels()
  28. {
  29. for (int i = 0; i < ChannelCount; i++)
  30. {
  31. ChannelsInternal.Add(CreateChannel(i));
  32. }
  33. }
  34. protected abstract ILightChannel CreateChannel(int index);
  35. public abstract Task<bool> ConnectAsync();
  36. public abstract Task DisconnectAsync();
  37. public abstract Task<bool> InitializeAsync();
  38. public abstract Task<bool> TurnOnAllAsync();
  39. public abstract Task<bool> TurnOffAllAsync();
  40. protected virtual void Dispose(bool disposing)
  41. {
  42. if (disposing)
  43. {
  44. foreach (var channel in ChannelsInternal)
  45. {
  46. (channel as IDisposable)?.Dispose();
  47. }
  48. ChannelsInternal.Clear();
  49. }
  50. }
  51. public void Dispose()
  52. {
  53. Dispose(true);
  54. GC.SuppressFinalize(this);
  55. }
  56. }
  57. }