diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java index c29e92f..0ab66ff 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java @@ -8,6 +8,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.security.Principal; import java.util.*; import java.util.List; +import java.util.stream.StreamSupport; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; @@ -22,7 +23,15 @@ public class ThermostatController { @GetMapping public List findAll() { - return toList(thermostatRepository.findAll()); + Iterable thermostats = thermostatRepository.findAll(); + StreamSupport.stream(thermostats.spliterator(), false) + .forEach( + s -> + s.setExternalSensorsAvailable( + thermostatRepository.getTemperatureSensorCount( + s.getRoomId()) + > 0)); + return toList(thermostats); } @GetMapping("/{id}") @@ -33,7 +42,6 @@ public class ThermostatController { private Thermostat save(Thermostat newT, ThermostatSaveRequest t) { newT.setTargetTemperature(t.getTargetTemperature()); newT.setState(t.getState()); - newT.setAverageTemperature(t.getAverageTemperature()); newT.setTargetTemperature(t.getTargetTemperature()); newT.setId(t.getId()); newT.setName(t.getName()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java index 0fac272..b897a92 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ThermostatSaveRequest.java @@ -29,9 +29,6 @@ public class ThermostatSaveRequest { /** Temperature to be reached */ @NotNull private BigDecimal targetTemperature; - /** Average room temperature given by all the temperature sensors in the room */ - @NotNull BigDecimal averageTemperature; - /** Embedded temperature sensor */ @NotNull private Sensor temperatureSensor; @@ -46,14 +43,6 @@ public class ThermostatSaveRequest { return this.state; } - public void setAverageTemperature(BigDecimal averageTemperature) { - this.averageTemperature = averageTemperature; - } - - public BigDecimal getAverageTemperature() { - return this.averageTemperature; - } - public Sensor getTemperatureSensor() { return this.temperatureSensor; } 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 index 6835646..e9d4375 100644 --- 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 @@ -1,39 +1,45 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import com.google.gson.annotations.SerializedName; import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.OneToOne; +import javax.persistence.Transient; import javax.validation.constraints.NotNull; /** A thermostat capable of controlling cooling and heating. */ @Entity -public class Thermostat extends InputDevice { +public class Thermostat extends OutputDevice { public enum ThermostatState { + @SerializedName("OFF") OFF, + @SerializedName("IDLE") IDLE, + @SerializedName("COOLING") COOLING, + @SerializedName("HEATING") HEATING } /** Temperature to be reached */ @Column @NotNull private BigDecimal targetTemperature; - /** Average room temperature given by all the temperature sensors in the room */ - @Column @NotNull BigDecimal averageTemperature; - - /** Embedded temperature sensor */ - @Column @NotNull @OneToOne private final Sensor temperatureSensor; + /** The temperature detected by the embedded sensor */ + @Column(nullable = false, precision = 4, scale = 1) + private BigDecimal sensorTemperature; /** State of this thermostat */ @Column @NotNull private ThermostatState state; + @Column boolean useInternalSensor = true; + + /** True if there are any other sensors in the room */ + @Transient boolean externalSensorsAvaliable; + /** 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; } @@ -45,29 +51,12 @@ public class Thermostat extends InputDevice { return this.state; } - public void setAverageTemperature(BigDecimal averageTemperature) { - this.averageTemperature = averageTemperature; - } - - public BigDecimal getAverageTemperature() { - return this.averageTemperature; - } - - public Sensor getTemperatureSensor() { - return this.temperatureSensor; - } - public BigDecimal getTargetTemperature() { return this.targetTemperature; } - /** - * Takes all of the temperature sensors in the room inculding its own embedded sensor and set - * this average temperature to the average of all the temperatures registered. - */ - public void calcAverageTemperature() { - // TODO: write actual method body - BigDecimal average = new BigDecimal(0); + public void setExternalSensorsAvailable(boolean externalSensorsAvaliable) { + this.externalSensorsAvaliable = externalSensorsAvaliable; } /** @@ -76,7 +65,7 @@ public class Thermostat extends InputDevice { * * @param targetTemperature - the temperature to be reached by the thermostat */ - public void setTargetTemperature(BigDecimal targetTemperature) { + /*public void setTargetTemperature(BigDecimal targetTemperature) { if (this.state == ThermostatState.OFF) { this.setState(ThermostatState.IDLE); } @@ -91,9 +80,9 @@ public class Thermostat extends InputDevice { } while (!(this.temperatureSensor.getValue().equals(this.targetTemperature))) { - /** Do nothing, wait for the target temperature to be reached */ + // 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 index a9c45f1..186276c 100644 --- 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 @@ -1,3 +1,27 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface ThermostatRepository extends DeviceRepository {} +import java.math.BigDecimal; +import java.util.Optional; +import org.springframework.data.jpa.repository.Query; + +public interface ThermostatRepository extends DeviceRepository { + /** + * Computes the average temperature of all temperature sensors in the room + * + * @param thermostatRoomId room ID of the thermostat + * @return an optional big decimal, empty if none found + */ + @Query( + "SELECT AVG(s.temperature) FROM Sensor s JOIN s.room r WHERE s.sensor = 'TEMPERATURE' AND r.id = ?1") + Optional getAverageTemperature(Long thermostatRoomId); + + /** + * Computes the average temperature of all temperature sensors in the room + * + * @param thermostatRoomId room ID of the thermostat + * @return an optional big decimal, empty if none found + */ + @Query( + "SELECT COUNT(*) FROM Sensor s JOIN s.room r WHERE s.sensor = 'TEMPERATURE' AND r.id = ?1") + Integer getTemperatureSensorCount(Long thermostatRoomId); +}