| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TeamAAS_VP.Core.Lights
- {
- /// <summary>
- /// 光源控制器基类
- /// </summary>
- public abstract class LightControllerBase : ILightController
- {
- public int Id { get; }
- public string Name { get; set; }
- public LightModel Model { get; }
- public int ChannelCount { get; }
- public bool IsConnected { get; protected set; }
- protected List<ILightChannel> ChannelsInternal { get; }
- public IReadOnlyList<ILightChannel> Channels => ChannelsInternal;
- protected LightControllerBase(int id,int channelCount)
- {
- Id = id;
- ChannelCount = channelCount;
- ChannelsInternal = new List<ILightChannel>();
- InitializeChannels();
- }
- private void InitializeChannels()
- {
- for (int i = 0; i < ChannelCount; i++)
- {
- ChannelsInternal.Add(CreateChannel(i));
- }
- }
- protected abstract ILightChannel CreateChannel(int index);
- public abstract Task<bool> ConnectAsync();
- public abstract Task DisconnectAsync();
- public abstract Task<bool> InitializeAsync();
- public abstract Task<bool> TurnOnAllAsync();
- public abstract Task<bool> TurnOffAllAsync();
- protected virtual void Dispose(bool disposing)
- {
- if (disposing)
- {
- foreach (var channel in ChannelsInternal)
- {
- (channel as IDisposable)?.Dispose();
- }
- ChannelsInternal.Clear();
- }
- }
- public void Dispose()
- {
- Dispose(true);
- GC.SuppressFinalize(this);
- }
- }
- }
|