DisplayChannel.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using System.Collections.Generic;
  3. namespace fis.Vid
  4. {
  5. public class DisplayChannel:IDisposable
  6. {
  7. private static readonly Dictionary<VidType, Func<string, IDisplayEngine>> EngineCreators = new Dictionary<VidType, Func<string, IDisplayEngine>>();
  8. private bool _disposed;
  9. public event EventHandler Disposing;
  10. public IDisplayEngine Engine { get; }
  11. public string ChannelId { get; }
  12. public byte[] BasicData { get; }
  13. public int ImageCount { get; }
  14. public byte[] ExtendedData { get; }
  15. static DisplayChannel()
  16. {
  17. EngineCreators.Add(VidType.VinnoVidSingle, s => new VidDisplayEngine(s));
  18. EngineCreators.Add(VidType.VinnoVidMovie, s => new VidDisplayEngine(s));
  19. EngineCreators.Add(VidType.ThirdVidSingle, s => new ThirdPartVidDisplayEngine(s));
  20. EngineCreators.Add(VidType.ThirdVidMovie, s => new ThirdPartVidDisplayEngine(s));
  21. }
  22. public DisplayChannel(VidType dataType, string filePath)
  23. {
  24. ChannelId = Guid.NewGuid().ToString("N").ToUpper();
  25. if (!EngineCreators.ContainsKey(dataType))
  26. {
  27. throw new KeyNotFoundException($"Can not find engine creator of {dataType}");
  28. }
  29. var engine = EngineCreators[dataType](filePath);
  30. Engine = engine;
  31. ImageCount = Engine.ImageCount;
  32. BasicData = Engine.BasicData;
  33. ExtendedData = Engine.ExtendedData;
  34. }
  35. ~DisplayChannel()
  36. {
  37. Dispose(false);
  38. }
  39. public byte[] GetImageData(int index)
  40. {
  41. return Engine.GetImageData(index);
  42. }
  43. protected void Dispose(bool disposing)
  44. {
  45. if (!_disposed)
  46. {
  47. Engine?.Close();
  48. }
  49. _disposed = true;
  50. }
  51. public void Dispose()
  52. {
  53. OnDisposing();
  54. Dispose(true);
  55. GC.SuppressFinalize(this);
  56. }
  57. public override string ToString()
  58. {
  59. return ChannelId;
  60. }
  61. protected virtual void OnDisposing()
  62. {
  63. Disposing?.Invoke(this, EventArgs.Empty);
  64. }
  65. }
  66. }