using Lab505.Script; namespace Lab505.Script.Sim; /// /// Per-user in-memory workflow simulation. /// Each X_A/X_B call runs a full 8-step cycle using SimContext robots with fixed delays. /// public sealed class SimFlow : IFlowFacade { private readonly SimImr _imr; private readonly SimAmr _amr; private readonly SimConveyor _conv; private readonly SimAsrs _asrs; private readonly SimDobot _dobot; public bool IsRunning { get; private set; } public string? LastError { get; private set; } public int CompletedCycles { get; private set; } public event Action? RunningStateChanged; internal SimFlow(SimContext ctx) { _imr = ctx.IMR; _amr = ctx.AMR; _conv = ctx.Conveyor; _asrs = ctx.ASRS; _dobot = ctx.Dobot; } // ── X_A: S34 pick → workflow → S34 return ──────────────────────────────── public Task X1A(int times, int ng, CancellationToken ct = default) => RunCycle(times, ng, ct => _asrs.X1_S34(ct), ct => _asrs.X1_R43(ct), ct); public Task X2A(int times, int ng, CancellationToken ct = default) => RunCycle(times, ng, ct => _asrs.X2_S34(ct), ct => _asrs.X2_R43(ct), ct); public Task X3A(int times, int ng, CancellationToken ct = default) => RunCycle(times, ng, ct => _asrs.X3_S34(ct), ct => _asrs.X3_R43(ct), ct); // ── X_B: S12 pick → workflow → S12 return ──────────────────────────────── public Task X1B(int times, int ng, CancellationToken ct = default) => RunCycle(times, ng, ct => _asrs.X1_S12(ct), ct => _asrs.X1_R12(ct), ct); public Task X2B(int times, int ng, CancellationToken ct = default) => RunCycle(times, ng, ct => _asrs.X2_S12(ct), ct => _asrs.X2_R12(ct), ct); public Task X3B(int times, int ng, CancellationToken ct = default) => RunCycle(times, ng, ct => _asrs.X3_S12(ct), ct => _asrs.X3_R12(ct), ct); // ── Run cycle (no ASRS) ─────────────────────────────────────────────────── public Task RunCycle(int times, int ng, CancellationToken ct = default) => RunCycle(times, ng, _ => Task.CompletedTask, _ => Task.CompletedTask, ct); // ── Core cycle loop ─────────────────────────────────────────────────────── private async Task RunCycle( int times, int ng, Func startOp, Func endOp, CancellationToken ct) { IsRunning = true; LastError = null; RunningStateChanged?.Invoke(); try { for (int i = 0; i < times; i++) { ct.ThrowIfCancellationRequested(); // Step 1: Init — IMR→C1 + AMR→Cap2 + ASRS pick (parallel) await Task.WhenAll(_imr.GoTo("C1", ct), _amr.GoTo("Cap2", ct), startOp(ct)); // Step 2: AMR load from C2 _conv.C2Out(true); await _amr.Load(ct); _conv.C2Out(false); // Step 3: AMR undock → Cap1 await _amr.Undock(ct); await _amr.GoTo("Cap1", ct); // Step 4: AMR→IMR exchange via C1 _conv.C1In(true); await _imr.Load(ct); _conv.C1In(false); _conv.C1Out(true); await Task.Delay(300, ct); _conv.C1Out(false); // Step 5: IMR process @ ABB await _imr.GoTo("ABB", ct); await _dobot.Pickup(1, ng, ct); await _imr.GoTo("C1", ct); // Step 6: IMR→AMR exchange via C1 _conv.C1IMRin(true); await _imr.Unload(ct); _conv.C1IMRin(false); await _amr.Load(ct); // Step 7: AMR → Cap2 await _amr.GoTo("Cap2", ct); // Step 8: AMR unload to C2 + ASRS return _conv.C2In(true); await _amr.Unload(ct); _conv.C2In(false); await endOp(ct); CompletedCycles++; } } catch (OperationCanceledException) { throw; } catch (Exception ex) { LastError = ex.Message; throw; } finally { IsRunning = false; RunningStateChanged?.Invoke(); } } }