Quellcode durchsuchen

test(d2-02): 测试替身 FakeSerial/FakeLease/FakeGate

huangjie vor 2 Tagen
Ursprung
Commit
be88acf

+ 93 - 0
ivf_tl_operate_2.0/control/IvfTl.ControlHost.Tests/Fakes/FakeHardware.cs

@@ -0,0 +1,93 @@
+using System;
+using System.Collections.Generic;
+using IvfTl.Hardware;
+namespace IvfTl.ControlHost.Tests.Fakes
+{
+    public class FakeSerial : ISerialChannel
+    {
+        public List<string> Calls = new List<string>();
+        public bool IsOpen => true;
+        public string PortName => "COMTEST";
+        public Action<string> Log { get; set; }
+        public int MoveReadTimeoutMs { get; set; }
+        public int QueryReadTimeoutMs { get; set; }
+        public int MotorSettleMs { get; set; }
+        public bool Open() { Calls.Add("Open"); return true; }
+        public void Close() => Calls.Add("Close");
+        public byte[] SendWait(byte[] f, int e = 0) { Calls.Add("SendWait"); return null; }
+        public int ShakeHandsWait() { Calls.Add("ShakeHands"); return 5; }
+        public int ReadCcdSnWait() => -1;
+        public int ReadLightBrightnessWait() { Calls.Add("ReadLight"); return 500; }
+        public int ReadWellHorizontalPosWait(int w) => -1;
+        public int ReadWellFocusZeroWait(int w) => -1;
+        public int ReadScanStepWait() => -1;
+        public bool WriteWellHorizontalPosWait(int well, int p) { Calls.Add($"WriteWellHor({well},{p})"); return true; }
+        public bool WriteScanStepWait(int p) { Calls.Add($"WriteScanStep({p})"); return true; }
+        public bool WriteOpenIntakeTimeWait(int v, bool b = false) { Calls.Add($"WriteIntake({v})"); return true; }
+        public bool WriteOpenVentTimeWait(int v) { Calls.Add($"WriteVent({v})"); return true; }
+        public int ReadOpenVentTimeWait() { Calls.Add("ReadVent"); return 200; }
+        public bool WriteLightBrightnessWait(int v) { Calls.Add($"WriteLight({v})"); return true; }
+        public bool VerticalMoveToWait(int p, int d = -1) { Calls.Add($"VMoveTo({p})"); return true; }
+        public bool VerticalForwardWait(int p, int d = -1) { Calls.Add($"VFwd({p})"); return true; }
+        public bool VerticalBackwardWait(int p, int d = -1) { Calls.Add($"VBack({p})"); return true; }
+        public bool VerticalResetWait(int d = -1) { Calls.Add("VReset"); return true; }
+        public int ReadVerticalPositionWait() => 0;
+        public bool HorizontalMoveToWait(int p, int d = -1) { Calls.Add($"HMoveTo({p})"); return true; }
+        public bool HorizontalForwardWait(int p, int d = -1) { Calls.Add($"HFwd({p})"); return true; }
+        public bool HorizontalBackwardWait(int p, int d = -1) { Calls.Add($"HBack({p})"); return true; }
+        public bool HorizontalResetWait(int d = -1) { Calls.Add("HReset"); return true; }
+        public int ReadHorizontalPositionWait() => 0;
+        public decimal TemperatureWait() { Calls.Add("Temp"); return 37.0m; }
+        public decimal ShangTemperatureWait() => 37.0m;
+        public decimal BoLiTemperatureWait() => 37.0m;
+        public decimal PressureWait() { Calls.Add("Pressure"); return 1.0m; }
+        public (decimal, decimal, decimal) BufferBottleStateWait() => (1m, 37m, 37m);
+        public DoorState DoorStatusWait() { Calls.Add("Door"); return DoorState.关闭; }
+        public bool OpenLedWait() { Calls.Add("OpenLed"); return true; }
+        public bool CloseLedWait() { Calls.Add("CloseLed"); return true; }
+        public bool OpenIntakeValveWait() { Calls.Add("OpenIntake"); return true; }
+        public bool CloseIntakeValveWait() { Calls.Add("CloseIntake"); return true; }
+        public bool OpenExhaustValveWait() { Calls.Add("OpenExhaust"); return true; }
+        public bool CloseExhaustValveWait() { Calls.Add("CloseExhaust"); return true; }
+        public bool HouseAerationWait() { Calls.Add("Aeration"); return true; }
+        public bool HouseVentWait() { Calls.Add("Vent"); return true; }
+        public bool BufferBottleAerationWait() => true;
+        public bool AutoAirSwapWait(bool on) => true;
+        public object RawComBin => null;
+        public void Dispose() => Calls.Add("Dispose");
+    }
+
+    public class FakeLease : IHardwareLease
+    {
+        public HardwareUser Owner { get; }
+        public ISerialChannel Serial { get; }
+        public ICamera Camera => null;
+        public bool Disposed;
+        public FakeLease(ISerialChannel s, HardwareUser u) { Serial = s; Owner = u; }
+        public void Dispose() => Disposed = true;
+    }
+
+    public class FakeGate : IHouseGate
+    {
+        private readonly ISerialChannel _serial;
+        public bool CanAcquire = true;
+        public FakeLease LastLease;
+        public FakeGate(int sn, ISerialChannel s) { HouseSn = sn; _serial = s; }
+        public int HouseSn { get; }
+        public bool IsCapturePaused { get; private set; }
+#pragma warning disable 67
+        public event Action OnPauseCapture;
+        public event Action OnResumeCapture;
+#pragma warning restore 67
+        public IHardwareLease Acquire(HardwareUser u, int t = 30000)
+        {
+            if (!CanAcquire) return null;
+            IsCapturePaused = true;
+            LastLease = new FakeLease(_serial, u);
+            return LastLease;
+        }
+        public bool TryAcquire(HardwareUser u, out IHardwareLease l) { l = Acquire(u); return l != null; }
+        public void PauseCapture() => IsCapturePaused = true;
+        public void ResumeCapture() => IsCapturePaused = false;
+    }
+}