using System; using System.Collections.Generic; namespace fis.Vid { public class DisplayChannel:IDisposable { private static readonly Dictionary> EngineCreators = new Dictionary>(); private bool _disposed; public event EventHandler Disposing; public IDisplayEngine Engine { get; } public string ChannelId { get; } public byte[] BasicData { get; } public int ImageCount { get; } public byte[] ExtendedData { get; } static DisplayChannel() { EngineCreators.Add(VidType.VinnoVidSingle, s => new VidDisplayEngine(s)); EngineCreators.Add(VidType.VinnoVidMovie, s => new VidDisplayEngine(s)); EngineCreators.Add(VidType.ThirdVidSingle, s => new ThirdPartVidDisplayEngine(s)); EngineCreators.Add(VidType.ThirdVidMovie, s => new ThirdPartVidDisplayEngine(s)); } public DisplayChannel(VidType dataType, string filePath) { ChannelId = Guid.NewGuid().ToString("N").ToUpper(); if (!EngineCreators.ContainsKey(dataType)) { throw new KeyNotFoundException($"Can not find engine creator of {dataType}"); } var engine = EngineCreators[dataType](filePath); Engine = engine; ImageCount = Engine.ImageCount; BasicData = Engine.BasicData; ExtendedData = Engine.ExtendedData; } ~DisplayChannel() { Dispose(false); } public byte[] GetImageData(int index) { return Engine.GetImageData(index); } protected void Dispose(bool disposing) { if (!_disposed) { Engine?.Close(); } _disposed = true; } public void Dispose() { OnDisposing(); Dispose(true); GC.SuppressFinalize(this); } public override string ToString() { return ChannelId; } protected virtual void OnDisposing() { Disposing?.Invoke(this, EventArgs.Empty); } } }