Fixed user story 5 and 7
This commit is contained in:
parent
7ebf74a4c8
commit
3da04ccaef
21 changed files with 193 additions and 100 deletions
|
@ -20,7 +20,6 @@ public class ButtonDimmerController
|
||||||
|
|
||||||
private DeviceService deviceService;
|
private DeviceService deviceService;
|
||||||
private ButtonDimmerRepository buttonDimmerRepository;
|
private ButtonDimmerRepository buttonDimmerRepository;
|
||||||
private DimmableRepository<Dimmable> dimmableRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected ButtonDimmerController(
|
protected ButtonDimmerController(
|
||||||
|
@ -30,12 +29,14 @@ public class ButtonDimmerController
|
||||||
super(inputRepository, outputRepository, DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR);
|
super(inputRepository, outputRepository, DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR);
|
||||||
this.deviceService = deviceService;
|
this.deviceService = deviceService;
|
||||||
this.buttonDimmerRepository = inputRepository;
|
this.buttonDimmerRepository = inputRepository;
|
||||||
this.dimmableRepository = outputRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ButtonDimmer create(
|
public ButtonDimmer create(
|
||||||
@Valid @RequestBody final GenericDeviceSaveReguest bd, final Principal principal) {
|
@Valid @RequestBody final GenericDeviceSaveReguest bd, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(bd.getRoomId(), principal.getName());
|
||||||
|
|
||||||
ButtonDimmer newBD = new ButtonDimmer();
|
ButtonDimmer newBD = new ButtonDimmer();
|
||||||
newBD.setName(bd.getName());
|
newBD.setName(bd.getName());
|
||||||
newBD.setRoomId(bd.getRoomId());
|
newBD.setRoomId(bd.getRoomId());
|
||||||
|
@ -61,7 +62,7 @@ public class ButtonDimmerController
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
dimmableRepository.saveAll(buttonDimmer.getOutputs());
|
deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName(), false);
|
||||||
|
|
||||||
return buttonDimmer.getOutputs();
|
return buttonDimmer.getOutputs();
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,9 @@ public class CurtainsController {
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Curtains create(
|
public Curtains create(
|
||||||
@Valid @RequestBody DimmableSaveRequest curtain, final Principal principal) {
|
@Valid @RequestBody DimmableSaveRequest curtain, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(curtain.getRoomId(), principal.getName());
|
||||||
return save(new Curtains(), curtain, principal);
|
return save(new Curtains(), curtain, principal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,6 +57,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
|
||||||
public DimmableLight create(
|
public DimmableLight create(
|
||||||
@Valid @RequestBody DimmableSaveRequest dl, final Principal principal)
|
@Valid @RequestBody DimmableSaveRequest dl, final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(dl.getRoomId(), principal.getName());
|
||||||
return save(new DimmableLight(), dl, principal.getName(), null);
|
return save(new DimmableLight(), dl, principal.getName(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,19 +1,20 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||||
|
|
||||||
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 com.google.gson.Gson;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.DeviceService;
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An abstract controller for an input device that has output connected to it. Aids to create the
|
* An abstract controller for an input device that has output connected to it. Aids to create the
|
||||||
* output add and output remove route
|
* output add and output remove route
|
||||||
|
@ -26,14 +27,16 @@ public abstract class InputDeviceConnectionController<
|
||||||
|
|
||||||
private class Connection {
|
private class Connection {
|
||||||
private final I input;
|
private final I input;
|
||||||
private final List<O> output;
|
private final List<O> outputs;
|
||||||
|
|
||||||
private Connection(I input, List<O> output) {
|
private Connection(I input, List<O> outputs) {
|
||||||
this.input = input;
|
this.input = input;
|
||||||
this.output = output;
|
this.outputs = outputs;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Autowired private DeviceService deviceService;
|
||||||
|
|
||||||
private DeviceRepository<I> inputRepository;
|
private DeviceRepository<I> inputRepository;
|
||||||
|
|
||||||
private DeviceRepository<O> outputReposiory;
|
private DeviceRepository<O> outputReposiory;
|
||||||
|
@ -56,17 +59,17 @@ public abstract class InputDeviceConnectionController<
|
||||||
this.connector = connector;
|
this.connector = connector;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Connection checkConnectionIDs(Long inputId, List<Long> outputs)
|
private Connection checkConnectionIDs(Long inputId, List<Long> outputs, String username)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
final I input =
|
final I input =
|
||||||
inputRepository
|
inputRepository
|
||||||
.findById(inputId)
|
.findByIdAndUsername(inputId, username)
|
||||||
.orElseThrow(() -> new NotFoundException("input device"));
|
.orElseThrow(() -> new NotFoundException("input device"));
|
||||||
final List<O> outputDevices = new ArrayList<>();
|
final List<O> outputDevices = new ArrayList<>();
|
||||||
for (final Long outputId : outputs) {
|
for (final Long outputId : outputs) {
|
||||||
outputDevices.add(
|
outputDevices.add(
|
||||||
outputReposiory
|
outputReposiory
|
||||||
.findById(outputId)
|
.findByIdAndUsername(outputId, username)
|
||||||
.orElseThrow(() -> new NotFoundException("output device")));
|
.orElseThrow(() -> new NotFoundException("output device")));
|
||||||
}
|
}
|
||||||
return new Connection(input, outputDevices);
|
return new Connection(input, outputDevices);
|
||||||
|
@ -76,19 +79,19 @@ public abstract class InputDeviceConnectionController<
|
||||||
* Implements the output device connection creation (add) route
|
* Implements the output device connection creation (add) route
|
||||||
*
|
*
|
||||||
* @param inputId input device id
|
* @param inputId input device id
|
||||||
* @param outputId output device id list
|
* @param outputs output device id list
|
||||||
* @return the list of output devices attached to the input device of id inputId
|
* @return the list of output devices attached to the input device of id inputId
|
||||||
* @throws NotFoundException if inputId or outputId are not valid
|
* @throws NotFoundException if inputId or outputId are not valid
|
||||||
*/
|
*/
|
||||||
protected Set<? extends OutputDevice> addOutput(Long inputId, List<Long> outputId)
|
protected Set<? extends OutputDevice> addOutput(
|
||||||
throws NotFoundException {
|
Long inputId, List<Long> outputs, String username) throws NotFoundException {
|
||||||
final Connection pair = checkConnectionIDs(inputId, outputId);
|
final Connection pair = checkConnectionIDs(inputId, outputs, username);
|
||||||
|
|
||||||
for (final O o : pair.output) {
|
for (final O o : pair.outputs) {
|
||||||
connector.connect(pair.input, o, true);
|
connector.connect(pair.input, o, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
outputReposiory.saveAll(pair.output);
|
deviceService.saveAllAsOwner(pair.outputs, username, false);
|
||||||
return pair.input.getOutputs();
|
return pair.input.getOutputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,33 +99,37 @@ public abstract class InputDeviceConnectionController<
|
||||||
* Implements the output device connection destruction (remove) route
|
* Implements the output device connection destruction (remove) route
|
||||||
*
|
*
|
||||||
* @param inputId input device id
|
* @param inputId input device id
|
||||||
* @param outputId output device id list
|
* @param outputs output device id list
|
||||||
* @return the list of output devices attached to the input device of id inputId
|
* @return the list of output devices attached to the input device of id inputId
|
||||||
* @throws NotFoundException if inputId or outputId are not valid
|
* @throws NotFoundException if inputId or outputId are not valid
|
||||||
*/
|
*/
|
||||||
protected Set<? extends OutputDevice> removeOutput(Long inputId, List<Long> outputId)
|
protected Set<? extends OutputDevice> removeOutput(
|
||||||
throws NotFoundException {
|
Long inputId, List<Long> outputs, String username) throws NotFoundException {
|
||||||
final Connection pair = checkConnectionIDs(inputId, outputId);
|
final Connection pair = checkConnectionIDs(inputId, outputs, username);
|
||||||
|
|
||||||
for (final O o : pair.output) {
|
for (final O o : pair.outputs) {
|
||||||
connector.connect(pair.input, o, false);
|
connector.connect(pair.input, o, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
outputReposiory.saveAll(pair.output);
|
deviceService.saveAllAsOwner(pair.outputs, username, false);
|
||||||
return pair.input.getOutputs();
|
return pair.input.getOutputs();
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/{id}/lights")
|
@PostMapping("/{id}/lights")
|
||||||
public List<OutputDevice> addLight(
|
public List<OutputDevice> addLight(
|
||||||
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
|
@PathVariable("id") long inputId,
|
||||||
|
@RequestBody List<Long> lightId,
|
||||||
|
final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
return toList(addOutput(inputId, lightId));
|
return toList(addOutput(inputId, lightId, principal.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}/lights")
|
@DeleteMapping("/{id}/lights")
|
||||||
public List<OutputDevice> removeLight(
|
public List<OutputDevice> removeLight(
|
||||||
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
|
@PathVariable("id") long inputId,
|
||||||
|
@RequestBody List<Long> lightId,
|
||||||
|
final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
return toList(removeOutput(inputId, lightId));
|
return toList(removeOutput(inputId, lightId, principal.getName()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,19 +19,19 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
|
||||||
|
|
||||||
@Autowired private DeviceService deviceService;
|
@Autowired private DeviceService deviceService;
|
||||||
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
||||||
@Autowired private DimmableRepository<Dimmable> dimmableRepository;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected KnobDimmerController(
|
protected KnobDimmerController(
|
||||||
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
|
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
|
||||||
super(inputRepository, outputRepository, Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR);
|
super(inputRepository, outputRepository, Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR);
|
||||||
this.knobDimmerRepository = inputRepository;
|
this.knobDimmerRepository = inputRepository;
|
||||||
this.dimmableRepository = outputRepository;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public KnobDimmer create(
|
public KnobDimmer create(
|
||||||
@Valid @RequestBody GenericDeviceSaveReguest kd, final Principal principal) {
|
@Valid @RequestBody GenericDeviceSaveReguest kd, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(kd.getRoomId(), principal.getName());
|
||||||
KnobDimmer newKD = new KnobDimmer();
|
KnobDimmer newKD = new KnobDimmer();
|
||||||
newKD.setName(kd.getName());
|
newKD.setName(kd.getName());
|
||||||
newKD.setRoomId(kd.getRoomId());
|
newKD.setRoomId(kd.getRoomId());
|
||||||
|
@ -49,7 +49,7 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
|
||||||
.orElseThrow(NotFoundException::new);
|
.orElseThrow(NotFoundException::new);
|
||||||
|
|
||||||
dimmer.setLightIntensity(bd.getIntensity());
|
dimmer.setLightIntensity(bd.getIntensity());
|
||||||
dimmableRepository.saveAll(dimmer.getOutputs());
|
deviceService.saveAllAsOwner(dimmer.getOutputs(), principal.getName(), false);
|
||||||
|
|
||||||
return dimmer.getOutputs();
|
return dimmer.getOutputs();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
||||||
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.MotionSensorService;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
||||||
import java.security.Principal;
|
import java.security.Principal;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
@ -18,14 +19,15 @@ import org.springframework.web.bind.annotation.*;
|
||||||
public class MotionSensorController {
|
public class MotionSensorController {
|
||||||
|
|
||||||
@Autowired private DeviceService deviceService;
|
@Autowired private DeviceService deviceService;
|
||||||
|
@Autowired private MotionSensorService motionSensorService;
|
||||||
@Autowired private MotionSensorRepository motionSensorService;
|
@Autowired private MotionSensorRepository motionSensorRepository;
|
||||||
|
|
||||||
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public MotionSensor create(
|
public MotionSensor create(
|
||||||
@Valid @RequestBody GenericDeviceSaveReguest ms, final Principal principal) {
|
@Valid @RequestBody GenericDeviceSaveReguest ms, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(ms.getRoomId(), principal.getName());
|
||||||
MotionSensor newMS = new MotionSensor();
|
MotionSensor newMS = new MotionSensor();
|
||||||
newMS.setName(ms.getName());
|
newMS.setName(ms.getName());
|
||||||
newMS.setRoomId(ms.getRoomId());
|
newMS.setRoomId(ms.getRoomId());
|
||||||
|
@ -33,23 +35,6 @@ public class MotionSensorController {
|
||||||
return deviceService.saveAsOwner(newMS, principal.getName());
|
return deviceService.saveAsOwner(newMS, principal.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Updates detection status of given motion sensor and propagates update throgh socket
|
|
||||||
*
|
|
||||||
* @param sensor the motion sensor to update
|
|
||||||
* @param detected the new detection status
|
|
||||||
* @return the updated motion sensor
|
|
||||||
*/
|
|
||||||
public MotionSensor updateDetectionFromMotionSensor(MotionSensor sensor, boolean detected) {
|
|
||||||
sensor.setDetected(detected);
|
|
||||||
final MotionSensor toReturn = motionSensorService.save(sensor);
|
|
||||||
|
|
||||||
sensorSocketEndpoint.queueDeviceUpdate(
|
|
||||||
sensor, motionSensorService.findUser(sensor.getId()));
|
|
||||||
|
|
||||||
return toReturn;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping("/{id}/detect")
|
@PutMapping("/{id}/detect")
|
||||||
public MotionSensor updateDetection(
|
public MotionSensor updateDetection(
|
||||||
@PathVariable("id") Long sensorId,
|
@PathVariable("id") Long sensorId,
|
||||||
|
@ -57,11 +42,12 @@ public class MotionSensorController {
|
||||||
final Principal principal)
|
final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
|
|
||||||
return updateDetectionFromMotionSensor(
|
return motionSensorService.updateDetectionFromMotionSensor(
|
||||||
motionSensorService
|
motionSensorRepository
|
||||||
.findByIdAndUsername(sensorId, principal.getName())
|
.findByIdAndUsername(sensorId, principal.getName())
|
||||||
.orElseThrow(NotFoundException::new),
|
.orElseThrow(NotFoundException::new),
|
||||||
detected);
|
detected,
|
||||||
|
principal.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -74,6 +74,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
|
||||||
public RegularLight create(
|
public RegularLight create(
|
||||||
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal)
|
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(rl.getRoomId(), principal.getName());
|
||||||
return save(new RegularLight(), rl, principal.getName(), null);
|
return save(new RegularLight(), rl, principal.getName(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,11 @@ public class RoomController {
|
||||||
final Principal principal)
|
final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
|
|
||||||
List<Room> rooms = toList(roomRepository.findAll());
|
List<Room> rooms =
|
||||||
|
toList(
|
||||||
|
hostId != null
|
||||||
|
? roomRepository.findByUserId(hostId)
|
||||||
|
: roomRepository.findByUsername(principal.getName()));
|
||||||
return fetchOwnerOrGuest(rooms, hostId, principal);
|
return fetchOwnerOrGuest(rooms, hostId, principal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,10 +64,6 @@ public class RoomController {
|
||||||
@RequestParam(value = "hostId", required = false) Long hostId)
|
@RequestParam(value = "hostId", required = false) Long hostId)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
Room room = roomRepository.findById(id).orElseThrow(NotFoundException::new);
|
Room room = roomRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||||
/* Very ugly way of avoiding code duplication. If this method call throws no exception,
|
|
||||||
* we can return the room safely. I pass null as I do not return a list in this case.
|
|
||||||
* Refer to fetchOwnerOrGuest for further information.
|
|
||||||
*/
|
|
||||||
fetchOwnerOrGuest(null, hostId, principal);
|
fetchOwnerOrGuest(null, hostId, principal);
|
||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
|
@ -115,11 +115,16 @@ public class RoomController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void deleteById(@PathVariable("id") long id) {
|
public void deleteById(@PathVariable("id") long id, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
switchRepository.deleteAllByRoomId(id);
|
switchRepository.deleteAllByRoomId(id);
|
||||||
knobDimmerRepository.deleteAllByRoomId(id);
|
knobDimmerRepository.deleteAllByRoomId(id);
|
||||||
buttonDimmerRepository.deleteAllByRoomId(id);
|
buttonDimmerRepository.deleteAllByRoomId(id);
|
||||||
roomRepository.deleteById(id);
|
final Room r =
|
||||||
|
roomRepository
|
||||||
|
.findByIdAndUsername(id, principal.getName())
|
||||||
|
.orElseThrow(NotFoundException::new);
|
||||||
|
roomRepository.delete(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -30,7 +30,6 @@ public class SceneController {
|
||||||
@Autowired private SceneService sceneService;
|
@Autowired private SceneService sceneService;
|
||||||
@Autowired private UserRepository userService;
|
@Autowired private UserRepository userService;
|
||||||
@Autowired private StateRepository<State<?>> stateService;
|
@Autowired private StateRepository<State<?>> stateService;
|
||||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Scene> findAll(Principal principal) {
|
public List<Scene> findAll(Principal principal) {
|
||||||
|
|
|
@ -23,10 +23,11 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping("/securityCamera")
|
@RequestMapping("/securityCamera")
|
||||||
public class SecurityCameraController {
|
public class SecurityCameraController {
|
||||||
|
|
||||||
@Autowired DeviceService deviceService;
|
@Autowired private DeviceService deviceService;
|
||||||
@Autowired SecurityCameraRepository securityCameraService;
|
@Autowired private SecurityCameraRepository securityCameraService;
|
||||||
@Autowired private SceneRepository sceneRepository;
|
@Autowired private SceneRepository sceneRepository;
|
||||||
@Autowired private StateRepository<State<?>> stateRepository;
|
@Autowired private StateRepository<State<?>> stateRepository;
|
||||||
|
@Autowired private RoomRepository roomRepository;
|
||||||
|
|
||||||
private SecurityCamera save(
|
private SecurityCamera save(
|
||||||
SecurityCamera newSC, SwitchableSaveRequest sc, final Principal principal) {
|
SecurityCamera newSC, SwitchableSaveRequest sc, final Principal principal) {
|
||||||
|
@ -39,7 +40,9 @@ public class SecurityCameraController {
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public SecurityCamera create(
|
public SecurityCamera create(
|
||||||
@Valid @RequestBody SwitchableSaveRequest sc, final Principal principal) {
|
@Valid @RequestBody SwitchableSaveRequest sc, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(sc.getRoomId(), principal.getName());
|
||||||
return save(new SecurityCamera(), sc, principal);
|
return save(new SecurityCamera(), sc, principal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,7 +28,10 @@ public class SensorController {
|
||||||
@Autowired private SensorService sensorService;
|
@Autowired private SensorService sensorService;
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Sensor create(@Valid @RequestBody SensorSaveRequest s, final Principal principal) {
|
public Sensor create(@Valid @RequestBody SensorSaveRequest s, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(s.getRoomId(), principal.getName());
|
||||||
|
|
||||||
Sensor newSensor = new Sensor();
|
Sensor newSensor = new Sensor();
|
||||||
newSensor.setSensor(s.getSensor());
|
newSensor.setSensor(s.getSensor());
|
||||||
newSensor.setName(s.getName());
|
newSensor.setName(s.getName());
|
||||||
|
|
|
@ -31,8 +31,9 @@ public class SmartPlugController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public SmartPlug create(
|
public SmartPlug create(@Valid @RequestBody SwitchableSaveRequest sp, final Principal principal)
|
||||||
@Valid @RequestBody SwitchableSaveRequest sp, final Principal principal) {
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(sp.getRoomId(), principal.getName());
|
||||||
return save(new SmartPlug(), sp, principal);
|
return save(new SmartPlug(), sp, principal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -38,8 +38,9 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Switch create(
|
public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s, final Principal principal)
|
||||||
@Valid @RequestBody GenericDeviceSaveReguest s, final Principal principal) {
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(s.getRoomId(), principal.getName());
|
||||||
Switch newSwitch = new Switch();
|
Switch newSwitch = new Switch();
|
||||||
newSwitch.setName(s.getName());
|
newSwitch.setName(s.getName());
|
||||||
newSwitch.setRoomId(s.getRoomId());
|
newSwitch.setRoomId(s.getRoomId());
|
||||||
|
@ -69,7 +70,7 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceService.saveAsOwner(s, principal.getName());
|
deviceService.saveAsOwner(s, principal.getName());
|
||||||
return deviceService.saveAllAsOwner(s.getOutputs(), principal.getName());
|
return deviceService.saveAllAsOwner(s.getOutputs(), principal.getName(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -37,8 +37,9 @@ public class ThermostatController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Thermostat create(
|
public Thermostat create(@Valid @RequestBody ThermostatSaveRequest t, final Principal principal)
|
||||||
@Valid @RequestBody ThermostatSaveRequest t, final Principal principal) {
|
throws NotFoundException {
|
||||||
|
deviceService.throwIfRoomNotOwned(t.getRoomId(), principal.getName());
|
||||||
return save(new Thermostat(), t, principal);
|
return save(new Thermostat(), t, principal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
@ -15,4 +16,10 @@ public interface RoomRepository extends CrudRepository<Room, Long> {
|
||||||
*/
|
*/
|
||||||
@Query("SELECT r FROM Room r JOIN r.user u WHERE r.id = ?1 AND u.username = ?2")
|
@Query("SELECT r FROM Room r JOIN r.user u WHERE r.id = ?1 AND u.username = ?2")
|
||||||
Optional<Room> findByIdAndUsername(Long id, String username);
|
Optional<Room> findByIdAndUsername(Long id, String username);
|
||||||
|
|
||||||
|
@Query("SELECT r FROM Room r JOIN r.user u WHERE u.username = ?1")
|
||||||
|
List<Room> findByUsername(String username);
|
||||||
|
|
||||||
|
@Query("SELECT r FROM Room r JOIN r.user u WHERE u.id = ?1")
|
||||||
|
List<Room> findByUserId(Long hostId);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,17 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.data.repository.CrudRepository;
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
|
||||||
public interface UserRepository extends CrudRepository<User, Long> {
|
public interface UserRepository extends CrudRepository<User, Long> {
|
||||||
User findByUsername(String username);
|
User findByUsername(String username);
|
||||||
|
|
||||||
|
@Query("SELECT u FROM #{#entityName} u JOIN FETCH u.guests WHERE u.username = ?1")
|
||||||
|
User findByUsernameFetchGuests(String username);
|
||||||
|
|
||||||
|
@Query("SELECT u FROM #{#entityName} u JOIN FETCH u.guests WHERE u.id = ?1")
|
||||||
|
User findByIdFetchGuests(Long id);
|
||||||
|
|
||||||
User findByEmailIgnoreCase(String email);
|
User findByEmailIgnoreCase(String email);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.scheduled;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.scheduled;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.controller.MotionSensorController;
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.MotionSensorService;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SensorService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.SensorService;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.service.ThermostatService;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
||||||
|
@ -30,7 +30,7 @@ public class UpdateTasks {
|
||||||
|
|
||||||
@Autowired private ThermostatService thermostatService;
|
@Autowired private ThermostatService thermostatService;
|
||||||
|
|
||||||
@Autowired private MotionSensorController motionSensorController;
|
@Autowired private MotionSensorService motionSensorService;
|
||||||
|
|
||||||
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
||||||
|
|
||||||
|
@ -55,14 +55,18 @@ public class UpdateTasks {
|
||||||
StreamSupport.stream(motionSensorRepository.findAll().spliterator(), true)
|
StreamSupport.stream(motionSensorRepository.findAll().spliterator(), true)
|
||||||
.forEach(
|
.forEach(
|
||||||
sensor -> {
|
sensor -> {
|
||||||
motionSensorController.updateDetectionFromMotionSensor(sensor, true);
|
final User owner = motionSensorRepository.findUser(sensor.getId());
|
||||||
|
motionSensorService.updateDetectionFromMotionSensor(
|
||||||
|
sensor, true, owner.getUsername());
|
||||||
CompletableFuture.delayedExecutor(
|
CompletableFuture.delayedExecutor(
|
||||||
(long) (Math.random() * 2000), TimeUnit.MILLISECONDS)
|
(long) (Math.random() * 2000), TimeUnit.MILLISECONDS)
|
||||||
.execute(
|
.execute(
|
||||||
() ->
|
() ->
|
||||||
motionSensorController
|
motionSensorService
|
||||||
.updateDetectionFromMotionSensor(
|
.updateDetectionFromMotionSensor(
|
||||||
sensor, false));
|
sensor,
|
||||||
|
false,
|
||||||
|
owner.getUsername()));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,10 @@ public class DeviceService {
|
||||||
@Autowired private SensorSocketEndpoint endpoint;
|
@Autowired private SensorSocketEndpoint endpoint;
|
||||||
@Autowired private ThermostatService thermostatService;
|
@Autowired private ThermostatService thermostatService;
|
||||||
|
|
||||||
|
public void throwIfRoomNotOwned(Long roomId, String username) throws NotFoundException {
|
||||||
|
roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
public void triggerTriggers(Device device) {
|
public void triggerTriggers(Device device) {
|
||||||
|
|
||||||
final long deviceId = device.getId();
|
final long deviceId = device.getId();
|
||||||
|
@ -100,10 +104,9 @@ public class DeviceService {
|
||||||
|
|
||||||
public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
|
public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
device = deviceRepository.save(device);
|
|
||||||
|
|
||||||
final User currentUser = userRepository.findByUsername(guestUsername);
|
final User currentUser = userRepository.findByUsername(guestUsername);
|
||||||
final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new);
|
final User host = userRepository.findByIdFetchGuests(hostId);
|
||||||
|
if (host == null) throw new NotFoundException();
|
||||||
final Set<User> guests = Set.copyOf(host.getGuests());
|
final Set<User> guests = Set.copyOf(host.getGuests());
|
||||||
|
|
||||||
// We're telling the host that a guest has modified a device. Therefore, fromGuest becomes
|
// We're telling the host that a guest has modified a device. Therefore, fromGuest becomes
|
||||||
|
@ -128,10 +131,8 @@ public class DeviceService {
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Device> T saveAsOwner(T device, String username) {
|
private void propagateUpdateAsOwner(Device device, String username) {
|
||||||
device = deviceRepository.save(device);
|
final User user = userRepository.findByUsernameFetchGuests(username);
|
||||||
|
|
||||||
final User user = userRepository.findByUsername(username);
|
|
||||||
final Set<User> guests = user.getGuests();
|
final Set<User> guests = user.getGuests();
|
||||||
// make sure we're broadcasting from host
|
// make sure we're broadcasting from host
|
||||||
device.setFromHost(true);
|
device.setFromHost(true);
|
||||||
|
@ -140,10 +141,35 @@ public class DeviceService {
|
||||||
// broadcast to endpoint the object device, with receiving user set to guest
|
// broadcast to endpoint the object device, with receiving user set to guest
|
||||||
endpoint.queueDeviceUpdate(device, guest);
|
endpoint.queueDeviceUpdate(device, guest);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Device> List<T> saveAllAsOwner(
|
||||||
|
Iterable<T> devices, String username, boolean fromScene) {
|
||||||
|
devices = deviceRepository.saveAll(devices);
|
||||||
|
devices.forEach((d) -> propagateUpdateAsOwner(d, username));
|
||||||
|
|
||||||
|
if (!fromScene) {
|
||||||
|
devices.forEach(this::triggerTriggers);
|
||||||
|
}
|
||||||
|
|
||||||
|
return toList(devices);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Device> T saveAsOwner(T device, String username, boolean fromScene) {
|
||||||
|
device = deviceRepository.save(device);
|
||||||
|
propagateUpdateAsOwner(device, username);
|
||||||
|
|
||||||
|
if (!fromScene) {
|
||||||
|
triggerTriggers(device);
|
||||||
|
}
|
||||||
|
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public <T extends Device> T saveAsOwner(T device, String username) {
|
||||||
|
return saveAsOwner(device, username, false);
|
||||||
|
}
|
||||||
|
|
||||||
public void delete(Long id, String username) throws NotFoundException {
|
public void delete(Long id, String username) throws NotFoundException {
|
||||||
Device device =
|
Device device =
|
||||||
deviceRepository
|
deviceRepository
|
||||||
|
@ -151,7 +177,7 @@ public class DeviceService {
|
||||||
.orElseThrow(NotFoundException::new);
|
.orElseThrow(NotFoundException::new);
|
||||||
deviceRepository.delete(device);
|
deviceRepository.delete(device);
|
||||||
|
|
||||||
final User user = userRepository.findByUsername(username);
|
final User user = userRepository.findByUsernameFetchGuests(username);
|
||||||
final Set<User> guests = user.getGuests();
|
final Set<User> guests = user.getGuests();
|
||||||
|
|
||||||
device.setFromHost(true);
|
device.setFromHost(true);
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class MotionSensorService {
|
||||||
|
|
||||||
|
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
||||||
|
@Autowired private DeviceService deviceService;
|
||||||
|
@Autowired private MotionSensorRepository motionSensorRepository;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updates detection status of given motion sensor and propagates update throgh socket
|
||||||
|
*
|
||||||
|
* @param sensor the motion sensor to update
|
||||||
|
* @param detected the new detection status
|
||||||
|
* @return the updated motion sensor
|
||||||
|
*/
|
||||||
|
public MotionSensor updateDetectionFromMotionSensor(
|
||||||
|
MotionSensor sensor, boolean detected, String username) {
|
||||||
|
sensor.setDetected(detected);
|
||||||
|
final MotionSensor toReturn = deviceService.saveAsOwner(sensor, username);
|
||||||
|
|
||||||
|
sensorSocketEndpoint.queueDeviceUpdate(
|
||||||
|
sensor, motionSensorRepository.findUser(sensor.getId()));
|
||||||
|
|
||||||
|
return toReturn;
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,6 +12,8 @@ public class SensorService {
|
||||||
|
|
||||||
@Autowired private SensorRepository sensorRepository;
|
@Autowired private SensorRepository sensorRepository;
|
||||||
|
|
||||||
|
@Autowired private DeviceService deviceService;
|
||||||
|
|
||||||
@Autowired private ThermostatService thermostatService;
|
@Autowired private ThermostatService thermostatService;
|
||||||
|
|
||||||
@Autowired private SensorSocketEndpoint endpoint;
|
@Autowired private SensorSocketEndpoint endpoint;
|
||||||
|
@ -38,10 +40,10 @@ public class SensorService {
|
||||||
*/
|
*/
|
||||||
public Sensor updateValueFromSensor(Sensor sensor, BigDecimal value) {
|
public Sensor updateValueFromSensor(Sensor sensor, BigDecimal value) {
|
||||||
sensor.setValue(value);
|
sensor.setValue(value);
|
||||||
final Sensor toReturn = sensorRepository.save(sensor);
|
sensor =
|
||||||
|
deviceService.saveAsOwner(
|
||||||
|
sensor, sensorRepository.findUser(sensor.getId()).getUsername());
|
||||||
endpoint.queueDeviceUpdate(sensor, sensorRepository.findUser(sensor.getId()));
|
endpoint.queueDeviceUpdate(sensor, sensorRepository.findUser(sensor.getId()));
|
||||||
|
return sensor;
|
||||||
return toReturn;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ public class ThermostatService {
|
||||||
|
|
||||||
@Autowired private SensorSocketEndpoint endpoint;
|
@Autowired private SensorSocketEndpoint endpoint;
|
||||||
|
|
||||||
|
@Autowired private DeviceService deviceService;
|
||||||
|
|
||||||
@Autowired private ThermostatRepository thermostatRepository;
|
@Autowired private ThermostatRepository thermostatRepository;
|
||||||
|
|
||||||
private void randomJitter(Thermostat thermostat) {
|
private void randomJitter(Thermostat thermostat) {
|
||||||
|
@ -28,7 +30,8 @@ public class ThermostatService {
|
||||||
|
|
||||||
private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) {
|
private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) {
|
||||||
thermostat.setInternalSensorTemperature(value);
|
thermostat.setInternalSensorTemperature(value);
|
||||||
thermostatRepository.save(thermostat);
|
deviceService.saveAsOwner(
|
||||||
|
thermostat, thermostatRepository.findUser(thermostat.getId()).getUsername());
|
||||||
}
|
}
|
||||||
|
|
||||||
public void fakeUpdateAll() {
|
public void fakeUpdateAll() {
|
||||||
|
@ -51,7 +54,7 @@ public class ThermostatService {
|
||||||
boolean shouldUpdate = this.computeState(t);
|
boolean shouldUpdate = this.computeState(t);
|
||||||
|
|
||||||
if (shouldUpdate) {
|
if (shouldUpdate) {
|
||||||
thermostatRepository.save(t);
|
deviceService.saveAsOwner(t, thermostatRepository.findUser(t.getId()).getUsername());
|
||||||
endpoint.queueDeviceUpdate(t, thermostatRepository.findUser(t.getId()));
|
endpoint.queueDeviceUpdate(t, thermostatRepository.findUser(t.getId()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue