From 7cfb2ffc7322f0ece96a48301d3eb8e0e375c175 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Thu, 9 Apr 2020 11:30:05 +0200 Subject: [PATCH] Added thermostat, not complete --- .../smarthut/models/Thermostat.java | 64 +++++++++++++++++++ .../smarthut/models/ThermostatRepository.java | 3 + 2 files changed, 67 insertions(+) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatRepository.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java new file mode 100644 index 0000000..338fb29 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java @@ -0,0 +1,64 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import java.math.BigDecimal; +import javax.persistence.Column; +import javax.validation.constraints.NotNull; + +/** A thermostat capable of controlling cooling and heating. */ +public class Thermostat extends InputDevice { + + enum ThermostatState { + OFF, + IDLE, + COOLING, + HEATING + } + + @Column @NotNull private BigDecimal targetTemperature; + + @Column @NotNull BigDecimal averageTemperature; + + @Column @NotNull private final Sensor temperatureSensor; + + @Column @NotNull private ThermostatState state; + + /** Creates a thermostat with a temperature sensor and its initial OFF state */ + public Thermostat() { + super("thermostat"); + this.temperatureSensor = new Sensor(); + this.temperatureSensor.setSensor(Sensor.SensorType.TEMPERATURE); + this.state = ThermostatState.OFF; + } + + /** Setter method for the thermostat state */ + public void setState(ThermostatState state) { + this.state = state; + } + + /** + * Sets the target temperature to be reached. Changes the thermostat state accordingly and waits + * until such temperature is reached by the embedded sensor to become idle again. + * + * @param targetTemperature - the temperature to be reached by the thermostat + */ + public void setTargetTemperature(BigDecimal targetTemperature) { + if (this.state == ThermostatState.OFF) { + this.state = ThermostatState.IDLE; + } + + this.targetTemperature = targetTemperature; + BigDecimal actualTemperature = this.temperatureSensor.getValue(); + + if (actualTemperature.compareTo(targetTemperature) == -1) { + this.setState(ThermostatState.HEATING); + } else { + this.setState(ThermostatState.COOLING); + } + + while (!(this.temperatureSensor.getValue().equals(this.targetTemperature))) { + /** Do nothing, wait for the target temperature to be reached */ + } + + this.setState(ThermostatState.IDLE); + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatRepository.java new file mode 100644 index 0000000..a9c45f1 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ThermostatRepository.java @@ -0,0 +1,3 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +public interface ThermostatRepository extends DeviceRepository {}