InputProviderIntel.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using Emgu.CV;
  3. using Emgu.CV.Structure;
  4. using System.Timers;
  5. using System.Threading;
  6. namespace BBIWARG.Input.InputProviding
  7. {
  8. class InputProviderIntel : MarshalByRefObject, IInputProvider
  9. {
  10. IntelCameraWrapper wrapper;
  11. public int CurrentFrameID
  12. {
  13. get;
  14. set;
  15. }
  16. public float FieldOfViewHorizontal
  17. {
  18. get;
  19. set;
  20. }
  21. public float FieldOfViewVertical
  22. {
  23. get;
  24. set;
  25. }
  26. public int ImageWidth
  27. {
  28. get { return 640; }
  29. }
  30. public int ImageHeight
  31. {
  32. get { return 480; }
  33. }
  34. public bool IsActive
  35. {
  36. get;
  37. set;
  38. }
  39. public event DeviceStartedEventHandler DeviceStartedEvent;
  40. public event NewFrameEventHandler NewFrameEvent;
  41. public UInt16 lowConfidenceValue;
  42. private AppDomain domain;
  43. private System.Threading.Timer watchdogTimer;
  44. public void initialize()
  45. {
  46. CurrentFrameID = 0;
  47. watchdogTimer = new System.Threading.Timer(Timer_Elapsed, null, 1000, 1000);
  48. }
  49. private void Timer_Elapsed(object state)
  50. {
  51. checkExitErrorstate();
  52. if (wrapper != null)
  53. wrapper.errorstate = true;
  54. }
  55. public void start()
  56. {
  57. domain = System.AppDomain.CreateDomain(System.Guid.NewGuid().ToString());
  58. wrapper = (IntelCameraWrapper) domain.CreateInstanceAndUnwrap(typeof(IntelCameraWrapper).Assembly.GetName().ToString(), typeof(IntelCameraWrapper).FullName);
  59. wrapper.init(this);
  60. IsActive = true;
  61. if (DeviceStartedEvent != null) {
  62. DeviceStartedEvent(this, new EventArgs());
  63. DeviceStartedEvent = null; //only notify once...
  64. }
  65. wrapper.run();
  66. checkExitErrorstate();
  67. }
  68. private void checkExitErrorstate()
  69. {
  70. if (wrapper.errorstate)
  71. {
  72. //we have crashed. Kill the app domain and try again.
  73. Console.WriteLine("Killing AppDomain...");
  74. try
  75. {
  76. System.AppDomain.Unload(domain);
  77. Thread.Sleep(5000);
  78. }
  79. catch (Exception)
  80. {
  81. Console.WriteLine("Could not unload app domain... Still trying to start again...");
  82. }
  83. Console.WriteLine("Starting again...");
  84. start();
  85. }
  86. }
  87. public void stop()
  88. {
  89. IsActive = false;
  90. }
  91. internal bool hasNewFrameEvent()
  92. {
  93. return NewFrameEvent != null;
  94. }
  95. internal void killAndRestart()
  96. {
  97. throw new NotImplementedException();
  98. }
  99. internal void newFrame(int currentFrameID, Image<Gray, ushort> dImg)
  100. {
  101. NewFrameEvent(this, new NewFrameEventArgs(currentFrameID, dImg));
  102. }
  103. }
  104. }