FakeHardware.cs 5.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. private readonly FakeGate _gate;
  62. public HardwareUser Owner { get; }
  63. public ISerialChannel Serial { get; }
  64. public ICamera Camera => null;
  65. public bool Disposed;
  66. public FakeLease(FakeGate gate, ISerialChannel s, HardwareUser u) { _gate = gate; Serial = s; Owner = u; }
  67. public void Dispose() { Disposed = true; _gate.ResumeCapture(); }
  68. }
  69. public class FakeGate : IHouseGate
  70. {
  71. private readonly ISerialChannel _serial;
  72. public bool CanAcquire = true;
  73. public FakeLease LastLease;
  74. public FakeGate(int sn, ISerialChannel s) { HouseSn = sn; _serial = s; }
  75. public int HouseSn { get; }
  76. public bool IsCapturePaused { get; private set; }
  77. #pragma warning disable 67
  78. public event Action OnPauseCapture;
  79. public event Action OnResumeCapture;
  80. #pragma warning restore 67
  81. public IHardwareLease Acquire(HardwareUser u, int t = 30000)
  82. {
  83. if (!CanAcquire) return null;
  84. IsCapturePaused = true;
  85. LastLease = new FakeLease(this, _serial, u);
  86. return LastLease;
  87. }
  88. public bool TryAcquire(HardwareUser u, out IHardwareLease l) { l = Acquire(u); return l != null; }
  89. public void PauseCapture() => IsCapturePaused = true;
  90. public void ResumeCapture() => IsCapturePaused = false;
  91. }
  92. }