From 191870d3c6c6d870f76575393c3e12bd58caddc6 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 9 May 2020 14:05:43 +0200 Subject: [PATCH] More sonarqube fixes --- .../controller/ThermostatController.java | 4 +-- .../service/DevicePopulationService.java | 2 +- .../service/ThermostatPopulationService.java | 34 +++++++++++++++++++ .../smarthut/service/ThermostatService.java | 29 ++++------------ 4 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationService.java 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 8ab96f5..386e1ca 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 @@ -5,7 +5,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.DuplicateStateException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; -import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService; +import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatPopulationService; import java.security.Principal; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -19,7 +19,7 @@ public class ThermostatController { @Autowired private DeviceService deviceService; @Autowired private ThermostatRepository thermostatRepository; - @Autowired private ThermostatService thermostatService; + @Autowired private ThermostatPopulationService thermostatService; @Autowired private SceneRepository sceneRepository; @Autowired private StateRepository> stateRepository; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationService.java index 4d3d490..9b3e7d2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DevicePopulationService.java @@ -8,7 +8,7 @@ import org.springframework.stereotype.Component; @Component public class DevicePopulationService { - @Autowired private ThermostatService thermostatService; + @Autowired private ThermostatPopulationService thermostatService; public void populateComputedFields(Iterable devices) { for (Device d : devices) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationService.java new file mode 100644 index 0000000..d8e1878 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/ThermostatPopulationService.java @@ -0,0 +1,34 @@ +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 java.math.BigDecimal; +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class ThermostatPopulationService { + + @Autowired private ThermostatRepository thermostatRepository; + + private BigDecimal measureTemperature(final Thermostat thermostat) { + Optional average; + + if (thermostat.isUseExternalSensors()) { + average = + thermostatRepository.getAverageTemperature( + thermostat.getRoomId(), Sensor.SensorType.TEMPERATURE); + + } else { + return thermostat.getInternalSensorTemperature(); + } + + return average.orElse(null); + } + + public void populateMeasuredTemperature(Thermostat thermostat) { + thermostat.setMeasuredTemperature(measureTemperature(thermostat)); + } +} 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 d11e76e..5eeec6c 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 @@ -16,7 +16,9 @@ public class ThermostatService { @Autowired private SensorSocketEndpoint endpoint; - @Autowired private DevicePropagationService deviceService; + @Autowired private DeviceService deviceService; + + @Autowired private ThermostatPopulationService thermostatPopulationService; @Autowired private ThermostatRepository thermostatRepository; @@ -41,12 +43,12 @@ public class ThermostatService { public List findAll(String username) { Iterable all = thermostatRepository.findAllByUsername(username); - all.forEach(this::populateMeasuredTemperature); + all.forEach(thermostatPopulationService::populateMeasuredTemperature); return Utils.toList(all); } public void computeState(Thermostat t) { - populateMeasuredTemperature(t); + thermostatPopulationService.populateMeasuredTemperature(t); t.computeState(); } @@ -67,28 +69,9 @@ public class ThermostatService { if (t.isPresent()) { Thermostat u = t.get(); - populateMeasuredTemperature(u); + thermostatPopulationService.populateMeasuredTemperature(u); t = Optional.of(u); } return t; } - - private BigDecimal measureTemperature(final Thermostat thermostat) { - Optional average; - - if (thermostat.isUseExternalSensors()) { - average = - thermostatRepository.getAverageTemperature( - thermostat.getRoomId(), Sensor.SensorType.TEMPERATURE); - - } else { - return thermostat.getInternalSensorTemperature(); - } - - return average.orElse(null); - } - - public void populateMeasuredTemperature(Thermostat thermostat) { - thermostat.setMeasuredTemperature(measureTemperature(thermostat)); - } }