More sonarqube fixes

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-09 14:05:43 +02:00
parent 6e98972a5b
commit 191870d3c6
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.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<State<?>> stateRepository;

View file

@ -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<Device> 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 DevicePropagationService deviceService;
@Autowired private DeviceService deviceService;
@Autowired private ThermostatPopulationService thermostatPopulationService;
@Autowired private ThermostatRepository thermostatRepository;
@ -41,12 +43,12 @@ public class ThermostatService {
public List<Thermostat> findAll(String username) {
Iterable<Thermostat> 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<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));
}
}