Added thermostat, not complete
This commit is contained in:
parent
d979050306
commit
7cfb2ffc73
2 changed files with 67 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
public interface ThermostatRepository extends DeviceRepository<Thermostat> {}
|
Loading…
Reference in a new issue