From c3d6590507a62c9fb9a8a0a4f2e3debd73b15442 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Sat, 2 May 2020 13:58:28 +0200 Subject: [PATCH] Thermostats fixed (for scenes) --- .../smarthut/controller/SceneController.java | 2 +- .../smarthut/models/Thermostat.java | 8 +++++++ .../smarthut/service/DeviceService.java | 22 +++++++++++-------- .../smarthut/service/SceneService.java | 7 +++--- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java index 0cddba2..4de0b89 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java @@ -68,7 +68,7 @@ public class SceneController { .findByIdAndUsername(id, principal.getName()) .orElseThrow(NotFoundException::new); - return sceneService.apply(newScene); + return sceneService.apply(newScene, principal.getName()); } @PutMapping("/{id}") 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 c02c4e4..e501ba4 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 @@ -6,9 +6,11 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Transient; import javax.validation.constraints.NotNull; +import org.springframework.stereotype.Component; /** A thermostat capable of controlling cooling and heating. */ @Entity +@Component public class Thermostat extends Switchable implements BooleanTriggerable { @Override @@ -34,6 +36,12 @@ public class Thermostat extends Switchable implements BooleanTriggerable { BigDecimal measured = this.getMeasuredTemperature(); BigDecimal target = this.getTargetTemperature(); + + if (measured == null) { + this.setMode(Thermostat.Mode.IDLE); + return true; + } + BigDecimal delta = target.subtract(measured); if (delta.abs().doubleValue() < 0.25) { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java index 0bc2a6e..379b86f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/DeviceService.java @@ -37,7 +37,7 @@ public class DeviceService { } } - private void triggerTriggers(Device device) { + private void triggerTriggers(Device device, final String username) { final long deviceId = device.getId(); @@ -57,7 +57,7 @@ public class DeviceService { sceneRepository .findById(t.getSceneId()) .orElseThrow(IllegalStateException::new)) - .forEach(sceneService::apply); + .forEach((s) -> sceneService.apply(s, username)); } public List findAll(Long hostId, String username) throws NotFoundException { @@ -97,11 +97,7 @@ public class DeviceService { } } - for (Device d : devices) { - if (d instanceof Thermostat) { - thermostatService.populateMeasuredTemperature((Thermostat) d); - } - } + populateComputedFields(devices); return toList(devices); } catch (NotFoundException e) { @@ -110,6 +106,14 @@ public class DeviceService { } } + public void populateComputedFields(Iterable devices) { + for (Device d : devices) { + if (d instanceof Thermostat) { + thermostatService.populateMeasuredTemperature((Thermostat) d); + } + } + } + public T saveAsGuest(T device, String guestUsername, Long hostId) throws NotFoundException { final User currentUser = userRepository.findByUsername(guestUsername); @@ -160,7 +164,7 @@ public class DeviceService { devices.forEach((d) -> propagateUpdateAsOwner(d, username)); if (!fromScene) { - devices.forEach(this::triggerTriggers); + devices.forEach((d) -> triggerTriggers(d, username)); } return toList(devices); @@ -172,7 +176,7 @@ public class DeviceService { propagateUpdateAsOwner(device, username); if (!fromScene) { - triggerTriggers(device); + triggerTriggers(device, username); } return device; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java index 826d896..5b6dc7f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java @@ -13,16 +13,17 @@ import org.springframework.stereotype.Component; public class SceneService { @Autowired private DeviceRepository deviceRepository; + @Autowired private DeviceService deviceService; - public List apply(Scene newScene) { + public List apply(Scene newScene, String username) { final List updated = new ArrayList<>(); for (final State s : newScene.getStates()) { s.apply(); updated.add(s.getDevice()); } - deviceRepository.saveAll(updated); - + deviceService.saveAllAsOwner(updated, username, true); + deviceService.populateComputedFields(updated); return updated; } }