diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java index 4065f12..9fbf704 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java @@ -64,8 +64,8 @@ public class CurtainsController { .findByIdAndUsername(deviceId, principal.getName()) .orElseThrow(NotFoundException::new); State s = c.cloneState(); - sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); - s.setSceneId(sceneId); + final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); + s.setSceneId(sc.getId()); if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) throw new DuplicateStateException(); return stateRepository.save(s); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java index b53b0af..1221043 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DeviceController.java @@ -5,6 +5,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.BadDataException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService; import java.security.Principal; @@ -40,11 +41,12 @@ public class DeviceController { .orElseThrow(NotFoundException::new); // check if roomId is valid - roomRepository - .findByIdAndUsername(deviceSaveRequest.getRoomId(), principal.getName()) - .orElseThrow(() -> new BadDataException("roomId is not a valid room id")); + final Room r = + roomRepository + .findByIdAndUsername(deviceSaveRequest.getRoomId(), principal.getName()) + .orElseThrow(() -> new BadDataException("roomId is not a valid room id")); - d.setRoomId(deviceSaveRequest.getRoomId()); + d.setRoomId(r.getId()); d.setName(deviceSaveRequest.getName()); deviceService.saveAsOwner(d, principal.getName()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 28648c1..e315ff1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -98,8 +98,8 @@ public class DimmableLightController extends GuestEnabledController s = d.cloneState(); - sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); - s.setSceneId(sceneId); + final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); + s.setSceneId(sc.getId()); if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) throw new DuplicateStateException(); return stateRepository.save(s); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index 145aa04..3768cb4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -110,8 +110,8 @@ public class RegularLightController extends GuestEnabledController .findByIdAndUsername(deviceId, principal.getName()) .orElseThrow(NotFoundException::new); State s = d.cloneState(); - sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); - s.setSceneId(sceneId); + final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); + s.setSceneId(sc.getId()); if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) throw new DuplicateStateException(); return stateRepository.save(s); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index 790aee2..88a78f6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -78,8 +78,8 @@ public class SmartPlugController { .findByIdAndUsername(deviceId, principal.getName()) .orElseThrow(NotFoundException::new); State s = d.cloneState(); - sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); - s.setSceneId(sceneId); + final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); + s.setSceneId(sc.getId()); if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) throw new DuplicateStateException(); return stateRepository.save(s); 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 49c31f2..8ab96f5 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 @@ -75,8 +75,8 @@ public class ThermostatController { .findByIdAndUsername(deviceId, principal.getName()) .orElseThrow(NotFoundException::new); State s = d.cloneState(); - sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); - s.setSceneId(sceneId); + final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new); + s.setSceneId(sc.getId()); if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0) throw new DuplicateStateException(); return stateRepository.save(s); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index d2231d8..aa88d09 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -7,7 +7,6 @@ import io.swagger.annotations.ApiModelProperty; import java.util.HashSet; import java.util.Set; import javax.persistence.*; -import javax.validation.constraints.NotNull; /** Generic abstraction for a smart home device */ @Entity @@ -46,11 +45,9 @@ public abstract class Device { * a REST call. */ @Column(name = "room_id", nullable = false) - @NotNull private Long roomId; /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ - @NotNull @Column(nullable = false) private String name; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java index 19e179c..5fdc119 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RangeTrigger.java @@ -43,7 +43,6 @@ public class RangeTrigger extends Trigger { * @return an optional big decimal, empty if none found */ @Query( - "SELECT AVG(s.value) FROM Sensor s JOIN s.room r WHERE s.sensor = 'TEMPERATURE' AND r.id = ?1") - Optional getAverageTemperature(Long thermostatRoomId); + "SELECT AVG(s.value) FROM Sensor s JOIN s.room r WHERE s.sensor = ?2 AND r.id = ?1") + Optional getAverageTemperature(Long thermostatRoomId, Sensor.SensorType sensorType); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java index 77010c6..9b7f990 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Trigger.java @@ -37,7 +37,6 @@ public abstract class Trigger { * from a REST call. */ @Column(name = "device_id", nullable = false) - @NotNull private Long deviceId; @ManyToOne 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 b1a9c9e..bf16b88 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 @@ -48,7 +48,11 @@ public class DeviceService { } public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException { - roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new); + final Room r = + roomRepository + .findByIdAndUsername(roomId, username) + .orElseThrow(NotFoundException::new); + if (!r.getId().equals(roomId)) throw new IllegalStateException(); } private void triggerTriggers(Device device, final String username) { @@ -140,9 +144,7 @@ public class DeviceService { User host = null; if (hostId == null) { if (roomId != null) { - roomRepository - .findByIdAndUsername(roomId, username) - .orElseThrow(NotFoundException::new); + throwIfRoomNotOwned(roomId, username); devices = deviceRepository.findByRoomId(roomId); } else { devices = deviceRepository.findAllByUsername(username); 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 5cdee37..100d77d 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 @@ -77,7 +77,8 @@ public class ThermostatService { Optional average; if (thermostat.isUseExternalSensors()) { - average = thermostatRepository.getAverageTemperature(thermostat.getRoomId()); + average = thermostatRepository.getAverageTemperature(thermostat.getRoomId(), Sensor.SensorType.TEMPERATURE); + } else { return thermostat.getInternalSensorTemperature(); }