FakeHardware.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using System;
  2. using System.Collections.Generic;
  3. using IvfTl.Hardware;
  4. namespace IvfTl.ControlHost.Tests.Fakes
  5. {
  6. public class FakeSerial : ISerialChannel
  7. {
  8. public List<string> Calls = new List<string>();
  9. public bool IsOpen => true;
  10. public string PortName => "COMTEST";
  11. public Action<string> Log { get; set; }
  12. public int MoveReadTimeoutMs { get; set; }
  13. public int QueryReadTimeoutMs { get; set; }
  14. public int MotorSettleMs { get; set; }
  15. public bool Open() { Calls.Add("Open"); return true; }
  16. public void Close() => Calls.Add("Close");
  17. public byte[] SendWait(byte[] f, int e = 0) { Calls.Add("SendWait"); return null; }
  18. public int ShakeHandsWait() { Calls.Add("ShakeHands"); return 5; }
  19. public int ReadCcdSnWait() => -1;
  20. public int ReadLightBrightnessWait() { Calls.Add("ReadLight"); return 500; }
  21. public int ReadWellHorizontalPosWait(int w) => -1;
  22. public int ReadWellFocusZeroWait(int w) => -1;
  23. public int ReadScanStepWait() => -1;
  24. public bool WriteWellHorizontalPosWait(int well, int p) { Calls.Add($"WriteWellHor({well},{p})"); return true; }
  25. public bool WriteScanStepWait(int p) { Calls.Add($"WriteScanStep({p})"); return true; }
  26. public bool WriteOpenIntakeTimeWait(int v, bool b = false) { Calls.Add($"WriteIntake({v})"); return true; }
  27. public bool WriteOpenVentTimeWait(int v) { Calls.Add($"WriteVent({v})"); return true; }
  28. public int ReadOpenVentTimeWait() { Calls.Add("ReadVent"); return 200; }
  29. public bool WriteLightBrightnessWait(int v) { Calls.Add($"WriteLight({v})"); return true; }
  30. public bool VerticalMoveToWait(int p, int d = -1) { Calls.Add($"VMoveTo({p})"); return true; }
  31. public bool VerticalForwardWait(int p, int d = -1) { Calls.Add($"VFwd({p})"); return true; }
  32. public bool VerticalBackwardWait(int p, int d = -1) { Calls.Add($"VBack({p})"); return true; }
  33. public bool VerticalResetWait(int d = -1) { Calls.Add("VReset"); return true; }
  34. public int ReadVerticalPositionWait() => 0;
  35. public bool HorizontalMoveToWait(int p, int d = -1) { Calls.Add($"HMoveTo({p})"); return true; }
  36. public bool HorizontalForwardWait(int p, int d = -1) { Calls.Add($"HFwd({p})"); return true; }
  37. public bool HorizontalBackwardWait(int p, int d = -1) { Calls.Add($"HBack({p})"); return true; }
  38. public bool HorizontalResetWait(int d = -1) { Calls.Add("HReset"); return true; }
  39. public int ReadHorizontalPositionWait() => 0;
  40. public decimal TemperatureWait() { Calls.Add("Temp"); return 37.0m; }
  41. public decimal ShangTemperatureWait() => 37.0m;
  42. public decimal BoLiTemperatureWait() => 37.0m;
  43. public decimal PressureWait() { Calls.Add("Pressure"); return 1.0m; }
  44. public (decimal, decimal, decimal) BufferBottleStateWait() => (1m, 37m, 37m);
  45. public DoorState DoorStatusWait() { Calls.Add("Door"); return DoorState.关闭; }
  46. public bool OpenLedWait() { Calls.Add("OpenLed"); return true; }
  47. public bool CloseLedWait() { Calls.Add("CloseLed"); return true; }
  48. public bool OpenIntakeValveWait() { Calls.Add("OpenIntake"); return true; }
  49. public bool CloseIntakeValveWait() { Calls.Add("CloseIntake"); return true; }
  50. public bool OpenExhaustValveWait() { Calls.Add("OpenExhaust"); return true; }
  51. public bool CloseExhaustValveWait() { Calls.Add("CloseExhaust"); return true; }
  52. public bool HouseAerationWait() { Calls.Add("Aeration"); return true; }
  53. public bool HouseVentWait() { Calls.Add("Vent"); return true; }
  54. public bool BufferBottleAerationWait() => true;
  55. public bool AutoAirSwapWait(bool on) => true;
  56. public object RawComBin => null;
  57. public void Dispose() => Calls.Add("Dispose");
  58. }
  59. public class FakeLease : IHardwareLease
  60. {
  61. public HardwareUser Owner { get; }
  62. public ISerialChannel Serial { get; }
  63. public ICamera Camera => null;
  64. public bool Disposed;
  65. public FakeLease(ISerialChannel s, HardwareUser u) { Serial = s; Owner = u; }
  66. public void Dispose() => Disposed = true;
  67. }
  68. public class FakeGate : IHouseGate
  69. {
  70. private readonly ISerialChannel _serial;
  71. public bool CanAcquire = true;
  72. public FakeLease LastLease;
  73. public FakeGate(int sn, ISerialChannel s) { HouseSn = sn; _serial = s; }
  74. public int HouseSn { get; }
  75. public bool IsCapturePaused { get; private set; }
  76. #pragma warning disable 67
  77. public event Action OnPauseCapture;
  78. public event Action OnResumeCapture;
  79. #pragma warning restore 67
  80. public IHardwareLease Acquire(HardwareUser u, int t = 30000)
  81. {
  82. if (!CanAcquire) return null;
  83. IsCapturePaused = true;
  84. LastLease = new FakeLease(_serial, u);
  85. return LastLease;
  86. }
  87. public bool TryAcquire(HardwareUser u, out IHardwareLease l) { l = Acquire(u); return l != null; }
  88. public void PauseCapture() => IsCapturePaused = true;
  89. public void ResumeCapture() => IsCapturePaused = false;
  90. }
  91. }