Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Jacob Salvi 2020-05-09 11:11:11 +02:00
commit 5bfb7888c2
15 changed files with 27 additions and 31 deletions

View file

@ -64,8 +64,8 @@ public class CurtainsController {
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Dimmable> 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);

View file

@ -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
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());

View file

@ -98,8 +98,8 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Dimmable> 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);

View file

@ -110,8 +110,8 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> 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);

View file

@ -78,8 +78,8 @@ public class SmartPlugController {
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> 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);

View file

@ -75,8 +75,8 @@ public class ThermostatController {
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> 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);

View file

@ -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;

View file

@ -43,7 +43,6 @@ public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D
GREATER_EQUAL
}
@NotNull
@Column(nullable = false)
private Operator operator;

View file

@ -7,7 +7,6 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
/** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */
@Entity
@ -45,7 +44,6 @@ public class Sensor extends InputDevice implements RangeTriggerable {
/** The type of this sensor */
@Column(nullable = false)
@NotNull
@Enumerated(value = EnumType.STRING)
private SensorType sensor;

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
@ -20,7 +19,6 @@ public class SmartPlug extends Switchable implements BooleanTriggerable {
/** Whether the smart plug is on */
@Column(name = "smart_plug_on", nullable = false)
@NotNull
private boolean on;
public BigDecimal getTotalConsumption() {

View file

@ -64,7 +64,7 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
}
/** Temperature to be reached */
@Column @NotNull private BigDecimal targetTemperature;
@Column private BigDecimal targetTemperature;
/** The temperature detected by the embedded sensor */
@Column(nullable = false, precision = 4, scale = 1)

View file

@ -25,6 +25,6 @@ public interface ThermostatRepository extends DeviceRepository<Thermostat> {
* @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<BigDecimal> getAverageTemperature(Long thermostatRoomId);
"SELECT AVG(s.value) FROM Sensor s JOIN s.room r WHERE s.sensor = ?2 AND r.id = ?1")
Optional<BigDecimal> getAverageTemperature(Long thermostatRoomId, Sensor.SensorType sensorType);
}

View file

@ -37,7 +37,6 @@ public abstract class Trigger<D extends Device> {
* from a REST call.
*/
@Column(name = "device_id", nullable = false)
@NotNull
private Long deviceId;
@ManyToOne

View file

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

View file

@ -77,7 +77,8 @@ public class ThermostatService {
Optional<BigDecimal> average;
if (thermostat.isUseExternalSensors()) {
average = thermostatRepository.getAverageTemperature(thermostat.getRoomId());
average = thermostatRepository.getAverageTemperature(thermostat.getRoomId(), Sensor.SensorType.TEMPERATURE);
} else {
return thermostat.getInternalSensorTemperature();
}