wip, to fix

This commit is contained in:
Tommaso Rodolfo Masera 2020-04-13 16:42:48 +02:00
parent 0b1170404d
commit 4c4297dfdf
4 changed files with 55 additions and 45 deletions

View file

@ -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<Thermostat> findAll() {
return toList(thermostatRepository.findAll());
Iterable<Thermostat> 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());

View file

@ -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;
}

View file

@ -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);
}
}*/
}

View file

@ -1,3 +1,27 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface ThermostatRepository extends DeviceRepository<Thermostat> {}
import java.math.BigDecimal;
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
public interface ThermostatRepository extends DeviceRepository<Thermostat> {
/**
* 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<BigDecimal> 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);
}