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 {}