using Lab505.Script.Facades;
namespace Lab505.Script.Sim;
///
/// Per-user in-memory conveyor simulation.
/// Coil commands set state immediately then fire "feedback" after a short delay
/// (simulating the physical conveyor responding). WaitCx methods watch for feedback.
///
public sealed class SimConveyor : IConveyorFacade
{
// Feedback state (what WaitCx methods watch)
private bool? _c1In, _c1Out, _c1IMRin, _c1IMRout, _c1Ready, _c2Ready;
private bool? _c2In, _c2Out;
private bool? _c1HasGoods, _c1NotGoods, _c2HasGoods, _c2NotGoods;
// Read-only accessors for state snapshot
public bool? StateC1In => _c1In;
public bool? StateC1Out => _c1Out;
public bool? StateC1IMRin => _c1IMRin;
public bool? StateC1IMRout => _c1IMRout;
public bool? StateC1Ready => _c1Ready;
public bool? StateC2Ready => _c2Ready;
public bool? StateC2In => _c2In;
public bool? StateC2Out => _c2Out;
public bool? StateC1HasGoods => _c1HasGoods;
public bool? StateC1NotGoods => _c1NotGoods;
public bool? StateC2HasGoods => _c2HasGoods;
public bool? StateC2NotGoods => _c2NotGoods;
public event Action? OnChanged;
private const int FeedbackDelayMs = 600;
// ── Coil commands ──────────────────────────────────────────────────────────
public bool C1In(bool v) => FireFeedback(v, x => _c1In = x);
public bool C1Out(bool v) => FireFeedback(v, x => _c1Out = x);
public bool C1IMRin(bool v) => FireFeedback(v, x => _c1IMRin = x);
public bool C1IMRout(bool v) => FireFeedback(v, x => _c1IMRout = x);
public bool C1Ready(bool v) => FireFeedback(v, x => _c1Ready = x);
public bool C2Ready(bool v) => FireFeedback(v, x => _c2Ready = x);
public bool C2In(bool v) => FireFeedback(v, x => _c2In = x);
public bool C2Out(bool v) => FireFeedback(v, x => _c2Out = x);
public bool C1HasGoods(bool v) => FireFeedback(v, x => _c1HasGoods = x);
public bool C1NotGoods(bool v) => FireFeedback(v, x => _c1NotGoods = x);
public bool C2HasGoods(bool v) => FireFeedback(v, x => _c2HasGoods = x);
public bool C2NotGoods(bool v) => FireFeedback(v, x => _c2NotGoods = x);
public void StopAll()
{
C1In(false); C1Out(false); C1IMRin(false); C1IMRout(false);
C2In(false); C2Out(false);
}
// ── Internal helpers ───────────────────────────────────────────────────────
private bool FireFeedback(bool value, Action setter)
{
_ = Task.Run(async () =>
{
await Task.Delay(FeedbackDelayMs);
setter(value);
OnChanged?.Invoke();
});
return true;
}
}