diff --git a/wash/src/wash/control/TemperatureController.java b/wash/src/wash/control/TemperatureController.java index 2a7da82..cbda2d9 100644 --- a/wash/src/wash/control/TemperatureController.java +++ b/wash/src/wash/control/TemperatureController.java @@ -2,17 +2,90 @@ package wash.control; import actor.ActorThread; import wash.io.WashingIO; +import wash.control.WashingMessage.Order; -public class TemperatureController extends ActorThread { - - // TODO: add attributes +public final class TemperatureController extends ActorThread { + private WashingIO io; + private Heater heater; + private WashingMessage m; public TemperatureController(WashingIO io) { - // TODO + this.io = io; + heater = new Heater(40); + } + + protected void sendAck() { + m.sendAck(this); } @Override public void run() { - // TODO + heater.start(); + + while (true) { + m = poll(60000 / Settings.SPEEDUP).orElse(new WashingMessage(this, Order.NOOP)); + + switch (m.order()) { + case Order.TEMP_SET_40 -> heater.setTarget(40); + case Order.TEMP_SET_60 -> heater.setTarget(60); + case Order.TEMP_IDLE -> heater.setTarget(0); + default -> { + continue; + } + } + + } + } + + /** + * Heater class that extends Thread and controls the temperature + * of the washing machine. + * + * Note that the heater has access to local variables in the + * TemperatureController class. + */ + class Heater extends Thread { + double target; + boolean hovering; + + Heater(double target) { + this.target = target; + this.hovering = false; + } + + synchronized public void setTarget(double target) { + this.target = target; + this.hovering = false; + } + + @Override + public void run() { + double current; + while (!isInterrupted()) { + current = io.getTemperature(); + + if (io.getWaterLevel() == 0) { + io.heat(false); + usleep(60000 / Settings.SPEEDUP); + continue; + } + + if (current < target) { + io.heat(true); + } + + else if (current >= target) { + io.heat(false); + if(!hovering) { + sendAck(); + hovering = true; + usleep(60000 / Settings.SPEEDUP); + continue; + } + } + + usleep(60000 / Settings.SPEEDUP); + } + } } }