From 646672e3db85a0fbb2258bcfe6e803741be695ab Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Wed, 15 Apr 2020 17:55:04 +0200 Subject: [PATCH] Code review for Thermostat --- socket_test.html | 4 ++- .../smarthut/config/SpringFoxConfig.java | 1 + .../controller/ThermostatController.java | 8 ++--- .../smarthut/dto/ThermostatSaveRequest.java | 19 +++------- .../smarthut/models/Thermostat.java | 36 +++++-------------- .../smarthut/models/ThermostatRepository.java | 13 +++++++ .../smarthut/scheduled/UpdateTasks.java | 18 +++++++--- .../smarthut/service/ThermostatService.java | 19 ++++++++++ 8 files changed, 68 insertions(+), 50 deletions(-) diff --git a/socket_test.html b/socket_test.html index 91399e5..776248d 100644 --- a/socket_test.html +++ b/socket_test.html @@ -29,8 +29,10 @@ connection.onopen = function(evt) { connection.onmessage = function(evt) { console.log("***ONMESSAGE", evt); let data = JSON.parse(evt.data); + let a = document.getElementById("giovanni"); + if (a) a.remove(); - malusa.innerHTML += "

" + JSON.stringify(JSON.parse(evt.data), null, 2) + "

"; + malusa.innerHTML += "
" + JSON.stringify(JSON.parse(evt.data), null, 2) + "
"; }; connection.onerror = function(evt) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java index 971a7fb..09c7bdd 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/config/SpringFoxConfig.java @@ -68,6 +68,7 @@ public class SpringFoxConfig { .or(PathSelectors.regex("/room.*")::apply) .or(PathSelectors.regex("/device.*")::apply) .or(PathSelectors.regex("/buttonDimmer.*")::apply) + .or(PathSelectors.regex("/thermostat.*")::apply) .or(PathSelectors.regex("/dimmableLight.*")::apply) .or(PathSelectors.regex("/knobDimmer.*")::apply) .or(PathSelectors.regex("/regularLight.*")::apply) 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 063801e..57e1ca9 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 @@ -1,6 +1,5 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; - import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; @@ -40,12 +39,13 @@ public class ThermostatController { newT.setId(t.getId()); newT.setName(t.getName()); newT.setRoomId(t.getRoomId()); + newT.setUseExternalSensors(t.isUseExternalSensors()); - if (newT.getState() == Thermostat.ThermostatState.OFF - && t.getState() != Thermostat.ThermostatState.OFF) { + if (t.isTurnOn()) { + newT.setState(Thermostat.ThermostatState.IDLE); thermostatService.computeState(newT); } else { - newT.setState(t.getState()); + newT.setState(Thermostat.ThermostatState.OFF); } newT = thermostatRepository.save(newT); 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 6a541ba..5ac3402 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 @@ -1,19 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; -import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; import java.math.BigDecimal; import javax.validation.constraints.NotNull; public class ThermostatSaveRequest { - public enum ThermostatState { - OFF, - IDLE, - COOLING, - HEATING - } - /** Device identifier */ private long id; @@ -32,14 +23,14 @@ public class ThermostatSaveRequest { @NotNull private boolean useExternalSensors; /** State of this thermostat */ - @NotNull private Thermostat.ThermostatState state; + @NotNull private boolean turnOn; - public void setState(Thermostat.ThermostatState state) { - this.state = state; + public boolean isTurnOn() { + return turnOn; } - public Thermostat.ThermostatState getState() { - return this.state; + public void setTurnOn(boolean turnOn) { + this.turnOn = turnOn; } public boolean isUseExternalSensors() { 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 bc762fd..8524b8f 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 @@ -27,14 +27,15 @@ public class Thermostat extends OutputDevice { /** The temperature detected by the embedded sensor */ @Column(nullable = false, precision = 4, scale = 1) - private BigDecimal internalSensorTemperature; + private BigDecimal internalSensorTemperature = + Sensor.TYPICAL_VALUES.get(Sensor.SensorType.TEMPERATURE); /** State of this thermostat */ @Column @NotNull private ThermostatState state; @Transient private BigDecimal measuredTemperature; - @Column boolean useExternalSensors = false; + @Column private boolean useExternalSensors = false; /** Creates a thermostat with a temperature sensor and its initial OFF state */ public Thermostat() { @@ -74,30 +75,11 @@ public class Thermostat extends OutputDevice { this.targetTemperature = targetTemperature; } - /* - * 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.setState(ThermostatState.IDLE); - } + public void setInternalSensorTemperature(BigDecimal internalSensorTemperature) { + this.internalSensorTemperature = internalSensorTemperature; + } - 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); - }*/ + public void setUseExternalSensors(boolean useExternalSensors) { + this.useExternalSensors = useExternalSensors; + } } 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 5f673c0..896e0ad 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,10 +1,23 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import java.math.BigDecimal; +import java.util.List; import java.util.Optional; +import javax.transaction.Transactional; import org.springframework.data.jpa.repository.Query; public interface ThermostatRepository extends DeviceRepository { + + /** + * Finds all devices belonging to a user + * + * @param username the User's username + * @return all devices of that user + */ + @Transactional + @Query("SELECT t FROM Thermostat t JOIN t.room r JOIN r.user u WHERE u.username = ?1") + List findAllByUsername(String username); + /** * Computes the average temperature of all temperature sensors in the room * diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java index e902662..4fe293c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/scheduled/UpdateTasks.java @@ -1,11 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.scheduled; import ch.usi.inf.sa4.sanmarinoes.smarthut.controller.MotionSensorController; -import ch.usi.inf.sa4.sanmarinoes.smarthut.controller.SensorController; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SensorService; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; -import java.math.BigDecimal; import java.util.Collection; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -29,16 +28,24 @@ public class UpdateTasks { @Autowired private SensorService sensorService; + @Autowired private ThermostatService thermostatService; + @Autowired private MotionSensorController motionSensorController; @Autowired private SensorSocketEndpoint sensorSocketEndpoint; - /** Generates fake sensor updates every two seconds with a +/- 1.25% error */ + /** Generates fake sensor updates every two seconds with a +/- 2.5% error */ @Scheduled(fixedRate = 2000) public void sensorFakeUpdate() { sensorService.sensorFakeUpdate(); } + /** Generates fake sensor updates every two seconds with a +/- 2.5% error */ + @Scheduled(fixedRate = 2000) + public void thermostatInteralSensorFakeUpdate() { + thermostatService.fakeUpdateAll(); + } + /** * Generate fake motion detections in all motion detectors every 20 seconds for 2 seconds at * most @@ -64,7 +71,10 @@ public class UpdateTasks { public void smartPlugConsumptionFakeUpdate() { smartPlugRepository.updateTotalConsumption(SmartPlug.AVERAGE_CONSUMPTION_KW); final Collection c = smartPlugRepository.findByOn(true); - c.forEach(s -> sensorSocketEndpoint.queueDeviceUpdate(s, sensorRepository.findUser(s.getId()))); + c.forEach( + s -> + sensorSocketEndpoint.queueDeviceUpdate( + s, sensorRepository.findUser(s.getId()))); } /** Sends device updates through sensor socket in batch every one second */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java index 9bd41de..fca88ee 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatService.java @@ -1,5 +1,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; @@ -17,6 +18,24 @@ public class ThermostatService { @Autowired private ThermostatRepository thermostatRepository; + private void randomJitter(Thermostat thermostat) { + updateValueForThermostat( + thermostat, + Sensor.TYPICAL_VALUES + .get(Sensor.SensorType.TEMPERATURE) + .multiply(BigDecimal.valueOf(0.975 + Math.random() / 20))); + } + + private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) { + thermostat.setInternalSensorTemperature(value); + thermostatRepository.save(thermostat); + } + + public void fakeUpdateAll() { + thermostatRepository.findAll().forEach(this::randomJitter); + updateStates(); + } + public List findAll(String username) { Iterable all = thermostatRepository.findAllByUsername(username); all.forEach(this::populateMeasuredTemperature);