From b0210cbfbd0817f29a2ca0b4ab47c6abdca2588d Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sun, 3 Nov 2024 17:17:52 +0100 Subject: [PATCH] Bad water controller --- wash/src/wash/control/WaterController.java | 66 +++++++++++++++++++++- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/wash/src/wash/control/WaterController.java b/wash/src/wash/control/WaterController.java index e357157..36b9696 100644 --- a/wash/src/wash/control/WaterController.java +++ b/wash/src/wash/control/WaterController.java @@ -5,14 +5,74 @@ import wash.io.WashingIO; public class WaterController extends ActorThread { - // TODO: add attributes + WashingMessage.Order o; + WashingMessage m; + WashingIO io; public WaterController(WashingIO io) { - // TODO + this.io = io; } @Override public void run() { - // TODO + + boolean firstRun = true; + + while (true) { + try { + WashingMessage n = receiveWithTimeout(60000 / Settings.SPEEDUP); + + if (n != null) { + if (o != null && o != n.order()) + firstRun = true; + m = n; + o = m.order(); + } + + if (o != null) { + switch (o) { + case WATER_DRAIN -> { + if (io.getWaterLevel() > 0.0) { + io.fill(false); + io.drain(true); + } + else { + //io.drain(false); + if (firstRun) + //sendAck(); + firstRun = false; + } + } + + case WATER_FILL -> { + if (io.getWaterLevel() < 10.0) { + io.drain(false); + io.fill(true); + } else { + io.fill(false); + if (firstRun) + //sendAck(); + firstRun = false; + } + } + + case WATER_IDLE -> { + io.drain(false); + io.fill(false); + } + + } + } + + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + + // if m is null, it means a minute passed and no message was received + } + } + + public void sendAck() { + m.sendAck(this); } }