Merge branch 'sonar-fix' into 'dev'

More sonarqube fixes

See merge request sa4-2020/the-sanmarinoes/backend!125
This commit is contained in:
Claudio Maggioni 2020-05-09 14:06:01 +02:00
commit 62d2c8d027
4 changed files with 43 additions and 26 deletions

View file

@ -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.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; 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.DeviceService;
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatPopulationService;
import java.security.Principal; import java.security.Principal;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -19,7 +19,7 @@ public class ThermostatController {
@Autowired private DeviceService deviceService; @Autowired private DeviceService deviceService;
@Autowired private ThermostatRepository thermostatRepository; @Autowired private ThermostatRepository thermostatRepository;
@Autowired private ThermostatService thermostatService; @Autowired private ThermostatPopulationService thermostatService;
@Autowired private SceneRepository sceneRepository; @Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository; @Autowired private StateRepository<State<?>> stateRepository;

View file

@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
@Component @Component
public class DevicePopulationService { public class DevicePopulationService {
@Autowired private ThermostatService thermostatService; @Autowired private ThermostatPopulationService thermostatService;
public void populateComputedFields(Iterable<Device> devices) { public void populateComputedFields(Iterable<Device> devices) {
for (Device d : devices) { for (Device d : devices) {

View file

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

View file

@ -16,7 +16,9 @@ public class ThermostatService {
@Autowired private SensorSocketEndpoint endpoint; @Autowired private SensorSocketEndpoint endpoint;
@Autowired private DevicePropagationService deviceService; @Autowired private DeviceService deviceService;
@Autowired private ThermostatPopulationService thermostatPopulationService;
@Autowired private ThermostatRepository thermostatRepository; @Autowired private ThermostatRepository thermostatRepository;
@ -41,12 +43,12 @@ public class ThermostatService {
public List<Thermostat> findAll(String username) { public List<Thermostat> findAll(String username) {
Iterable<Thermostat> all = thermostatRepository.findAllByUsername(username); Iterable<Thermostat> all = thermostatRepository.findAllByUsername(username);
all.forEach(this::populateMeasuredTemperature); all.forEach(thermostatPopulationService::populateMeasuredTemperature);
return Utils.toList(all); return Utils.toList(all);
} }
public void computeState(Thermostat t) { public void computeState(Thermostat t) {
populateMeasuredTemperature(t); thermostatPopulationService.populateMeasuredTemperature(t);
t.computeState(); t.computeState();
} }
@ -67,28 +69,9 @@ public class ThermostatService {
if (t.isPresent()) { if (t.isPresent()) {
Thermostat u = t.get(); Thermostat u = t.get();
populateMeasuredTemperature(u); thermostatPopulationService.populateMeasuredTemperature(u);
t = Optional.of(u); t = Optional.of(u);
} }
return t; return t;
} }
private BigDecimal measureTemperature(final Thermostat thermostat) {
Optional<BigDecimal> 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));
}
} }