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 ButtonDimmerRepository buttonDimmerRepository;
|
||||
private DimmableRepository<Dimmable> dimmableRepository;
|
||||
|
||||
@Autowired
|
||||
protected ButtonDimmerController(
|
||||
|
@ -30,12 +29,14 @@ public class ButtonDimmerController
|
|||
super(inputRepository, outputRepository, DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR);
|
||||
this.deviceService = deviceService;
|
||||
this.buttonDimmerRepository = inputRepository;
|
||||
this.dimmableRepository = outputRepository;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
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();
|
||||
newBD.setName(bd.getName());
|
||||
newBD.setRoomId(bd.getRoomId());
|
||||
|
@ -61,7 +62,7 @@ public class ButtonDimmerController
|
|||
break;
|
||||
}
|
||||
|
||||
dimmableRepository.saveAll(buttonDimmer.getOutputs());
|
||||
deviceService.saveAllAsOwner(buttonDimmer.getOutputs(), principal.getName(), false);
|
||||
|
||||
return buttonDimmer.getOutputs();
|
||||
}
|
||||
|
|
|
@ -30,7 +30,9 @@ public class CurtainsController {
|
|||
|
||||
@PostMapping
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -57,6 +57,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
|
|||
public DimmableLight create(
|
||||
@Valid @RequestBody DimmableSaveRequest dl, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(dl.getRoomId(), principal.getName());
|
||||
return save(new DimmableLight(), dl, principal.getName(), null);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,19 +1,20 @@
|
|||
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.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.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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
|
||||
* output add and output remove route
|
||||
|
@ -26,14 +27,16 @@ public abstract class InputDeviceConnectionController<
|
|||
|
||||
private class Connection {
|
||||
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.output = output;
|
||||
this.outputs = outputs;
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired private DeviceService deviceService;
|
||||
|
||||
private DeviceRepository<I> inputRepository;
|
||||
|
||||
private DeviceRepository<O> outputReposiory;
|
||||
|
@ -56,17 +59,17 @@ public abstract class InputDeviceConnectionController<
|
|||
this.connector = connector;
|
||||
}
|
||||
|
||||
private Connection checkConnectionIDs(Long inputId, List<Long> outputs)
|
||||
private Connection checkConnectionIDs(Long inputId, List<Long> outputs, String username)
|
||||
throws NotFoundException {
|
||||
final I input =
|
||||
inputRepository
|
||||
.findById(inputId)
|
||||
.findByIdAndUsername(inputId, username)
|
||||
.orElseThrow(() -> new NotFoundException("input device"));
|
||||
final List<O> outputDevices = new ArrayList<>();
|
||||
for (final Long outputId : outputs) {
|
||||
outputDevices.add(
|
||||
outputReposiory
|
||||
.findById(outputId)
|
||||
.findByIdAndUsername(outputId, username)
|
||||
.orElseThrow(() -> new NotFoundException("output device")));
|
||||
}
|
||||
return new Connection(input, outputDevices);
|
||||
|
@ -76,19 +79,19 @@ public abstract class InputDeviceConnectionController<
|
|||
* Implements the output device connection creation (add) route
|
||||
*
|
||||
* @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
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> addOutput(Long inputId, List<Long> outputId)
|
||||
throws NotFoundException {
|
||||
final Connection pair = checkConnectionIDs(inputId, outputId);
|
||||
protected Set<? extends OutputDevice> addOutput(
|
||||
Long inputId, List<Long> outputs, String username) throws NotFoundException {
|
||||
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);
|
||||
}
|
||||
|
||||
outputReposiory.saveAll(pair.output);
|
||||
deviceService.saveAllAsOwner(pair.outputs, username, false);
|
||||
return pair.input.getOutputs();
|
||||
}
|
||||
|
||||
|
@ -96,33 +99,37 @@ public abstract class InputDeviceConnectionController<
|
|||
* Implements the output device connection destruction (remove) route
|
||||
*
|
||||
* @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
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> removeOutput(Long inputId, List<Long> outputId)
|
||||
throws NotFoundException {
|
||||
final Connection pair = checkConnectionIDs(inputId, outputId);
|
||||
protected Set<? extends OutputDevice> removeOutput(
|
||||
Long inputId, List<Long> outputs, String username) throws NotFoundException {
|
||||
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);
|
||||
}
|
||||
|
||||
outputReposiory.saveAll(pair.output);
|
||||
deviceService.saveAllAsOwner(pair.outputs, username, false);
|
||||
return pair.input.getOutputs();
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/lights")
|
||||
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 {
|
||||
return toList(addOutput(inputId, lightId));
|
||||
return toList(addOutput(inputId, lightId, principal.getName()));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/lights")
|
||||
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 {
|
||||
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 KnobDimmerRepository knobDimmerRepository;
|
||||
@Autowired private DimmableRepository<Dimmable> dimmableRepository;
|
||||
|
||||
@Autowired
|
||||
protected KnobDimmerController(
|
||||
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
|
||||
super(inputRepository, outputRepository, Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR);
|
||||
this.knobDimmerRepository = inputRepository;
|
||||
this.dimmableRepository = outputRepository;
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
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();
|
||||
newKD.setName(kd.getName());
|
||||
newKD.setRoomId(kd.getRoomId());
|
||||
|
@ -49,7 +49,7 @@ public class KnobDimmerController extends InputDeviceConnectionController<KnobDi
|
|||
.orElseThrow(NotFoundException::new);
|
||||
|
||||
dimmer.setLightIntensity(bd.getIntensity());
|
||||
dimmableRepository.saveAll(dimmer.getOutputs());
|
||||
deviceService.saveAllAsOwner(dimmer.getOutputs(), principal.getName(), false);
|
||||
|
||||
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.MotionSensorRepository;
|
||||
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 java.security.Principal;
|
||||
import javax.validation.Valid;
|
||||
|
@ -18,14 +19,15 @@ import org.springframework.web.bind.annotation.*;
|
|||
public class MotionSensorController {
|
||||
|
||||
@Autowired private DeviceService deviceService;
|
||||
|
||||
@Autowired private MotionSensorRepository motionSensorService;
|
||||
|
||||
@Autowired private MotionSensorService motionSensorService;
|
||||
@Autowired private MotionSensorRepository motionSensorRepository;
|
||||
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
||||
|
||||
@PostMapping
|
||||
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();
|
||||
newMS.setName(ms.getName());
|
||||
newMS.setRoomId(ms.getRoomId());
|
||||
|
@ -33,23 +35,6 @@ public class MotionSensorController {
|
|||
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")
|
||||
public MotionSensor updateDetection(
|
||||
@PathVariable("id") Long sensorId,
|
||||
|
@ -57,11 +42,12 @@ public class MotionSensorController {
|
|||
final Principal principal)
|
||||
throws NotFoundException {
|
||||
|
||||
return updateDetectionFromMotionSensor(
|
||||
motionSensorService
|
||||
return motionSensorService.updateDetectionFromMotionSensor(
|
||||
motionSensorRepository
|
||||
.findByIdAndUsername(sensorId, principal.getName())
|
||||
.orElseThrow(NotFoundException::new),
|
||||
detected);
|
||||
detected,
|
||||
principal.getName());
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
|
@ -74,6 +74,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
|
|||
public RegularLight create(
|
||||
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(rl.getRoomId(), principal.getName());
|
||||
return save(new RegularLight(), rl, principal.getName(), null);
|
||||
}
|
||||
|
||||
|
|
|
@ -49,7 +49,11 @@ public class RoomController {
|
|||
final Principal principal)
|
||||
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);
|
||||
}
|
||||
|
||||
|
@ -60,10 +64,6 @@ public class RoomController {
|
|||
@RequestParam(value = "hostId", required = false) Long hostId)
|
||||
throws NotFoundException {
|
||||
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);
|
||||
return room;
|
||||
}
|
||||
|
@ -115,11 +115,16 @@ public class RoomController {
|
|||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteById(@PathVariable("id") long id) {
|
||||
public void deleteById(@PathVariable("id") long id, final Principal principal)
|
||||
throws NotFoundException {
|
||||
switchRepository.deleteAllByRoomId(id);
|
||||
knobDimmerRepository.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 UserRepository userService;
|
||||
@Autowired private StateRepository<State<?>> stateService;
|
||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
|
||||
@GetMapping
|
||||
public List<Scene> findAll(Principal principal) {
|
||||
|
|
|
@ -23,10 +23,11 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
@RequestMapping("/securityCamera")
|
||||
public class SecurityCameraController {
|
||||
|
||||
@Autowired DeviceService deviceService;
|
||||
@Autowired SecurityCameraRepository securityCameraService;
|
||||
@Autowired private DeviceService deviceService;
|
||||
@Autowired private SecurityCameraRepository securityCameraService;
|
||||
@Autowired private SceneRepository sceneRepository;
|
||||
@Autowired private StateRepository<State<?>> stateRepository;
|
||||
@Autowired private RoomRepository roomRepository;
|
||||
|
||||
private SecurityCamera save(
|
||||
SecurityCamera newSC, SwitchableSaveRequest sc, final Principal principal) {
|
||||
|
@ -39,7 +40,9 @@ public class SecurityCameraController {
|
|||
|
||||
@PostMapping
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -28,7 +28,10 @@ public class SensorController {
|
|||
@Autowired private SensorService sensorService;
|
||||
|
||||
@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();
|
||||
newSensor.setSensor(s.getSensor());
|
||||
newSensor.setName(s.getName());
|
||||
|
|
|
@ -31,8 +31,9 @@ public class SmartPlugController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public SmartPlug create(
|
||||
@Valid @RequestBody SwitchableSaveRequest sp, final Principal principal) {
|
||||
public SmartPlug create(@Valid @RequestBody SwitchableSaveRequest sp, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(sp.getRoomId(), principal.getName());
|
||||
return save(new SmartPlug(), sp, principal);
|
||||
}
|
||||
|
||||
|
|
|
@ -38,8 +38,9 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public Switch create(
|
||||
@Valid @RequestBody GenericDeviceSaveReguest s, final Principal principal) {
|
||||
public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(s.getRoomId(), principal.getName());
|
||||
Switch newSwitch = new Switch();
|
||||
newSwitch.setName(s.getName());
|
||||
newSwitch.setRoomId(s.getRoomId());
|
||||
|
@ -69,7 +70,7 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
|
|||
}
|
||||
|
||||
deviceService.saveAsOwner(s, principal.getName());
|
||||
return deviceService.saveAllAsOwner(s.getOutputs(), principal.getName());
|
||||
return deviceService.saveAllAsOwner(s.getOutputs(), principal.getName(), false);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
|
@ -37,8 +37,9 @@ public class ThermostatController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public Thermostat create(
|
||||
@Valid @RequestBody ThermostatSaveRequest t, final Principal principal) {
|
||||
public Thermostat create(@Valid @RequestBody ThermostatSaveRequest t, final Principal principal)
|
||||
throws NotFoundException {
|
||||
deviceService.throwIfRoomNotOwned(t.getRoomId(), principal.getName());
|
||||
return save(new Thermostat(), t, principal);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
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")
|
||||
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;
|
||||
|
||||
import java.util.*;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface UserRepository extends CrudRepository<User, Long> {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
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.service.MotionSensorService;
|
||||
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.socket.SensorSocketEndpoint;
|
||||
|
@ -30,7 +30,7 @@ public class UpdateTasks {
|
|||
|
||||
@Autowired private ThermostatService thermostatService;
|
||||
|
||||
@Autowired private MotionSensorController motionSensorController;
|
||||
@Autowired private MotionSensorService motionSensorService;
|
||||
|
||||
@Autowired private SensorSocketEndpoint sensorSocketEndpoint;
|
||||
|
||||
|
@ -55,14 +55,18 @@ public class UpdateTasks {
|
|||
StreamSupport.stream(motionSensorRepository.findAll().spliterator(), true)
|
||||
.forEach(
|
||||
sensor -> {
|
||||
motionSensorController.updateDetectionFromMotionSensor(sensor, true);
|
||||
final User owner = motionSensorRepository.findUser(sensor.getId());
|
||||
motionSensorService.updateDetectionFromMotionSensor(
|
||||
sensor, true, owner.getUsername());
|
||||
CompletableFuture.delayedExecutor(
|
||||
(long) (Math.random() * 2000), TimeUnit.MILLISECONDS)
|
||||
.execute(
|
||||
() ->
|
||||
motionSensorController
|
||||
motionSensorService
|
||||
.updateDetectionFromMotionSensor(
|
||||
sensor, false));
|
||||
sensor,
|
||||
false,
|
||||
owner.getUsername()));
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -25,6 +25,10 @@ public class DeviceService {
|
|||
@Autowired private SensorSocketEndpoint endpoint;
|
||||
@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) {
|
||||
|
||||
final long deviceId = device.getId();
|
||||
|
@ -100,10 +104,9 @@ public class DeviceService {
|
|||
|
||||
public <T extends Device> T saveAsGuest(T device, String guestUsername, Long hostId)
|
||||
throws NotFoundException {
|
||||
device = deviceRepository.save(device);
|
||||
|
||||
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());
|
||||
|
||||
// We're telling the host that a guest has modified a device. Therefore, fromGuest becomes
|
||||
|
@ -128,10 +131,8 @@ public class DeviceService {
|
|||
return device;
|
||||
}
|
||||
|
||||
public <T extends Device> T saveAsOwner(T device, String username) {
|
||||
device = deviceRepository.save(device);
|
||||
|
||||
final User user = userRepository.findByUsername(username);
|
||||
private void propagateUpdateAsOwner(Device device, String username) {
|
||||
final User user = userRepository.findByUsernameFetchGuests(username);
|
||||
final Set<User> guests = user.getGuests();
|
||||
// make sure we're broadcasting from host
|
||||
device.setFromHost(true);
|
||||
|
@ -140,10 +141,35 @@ public class DeviceService {
|
|||
// broadcast to endpoint the object device, with receiving user set to 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;
|
||||
}
|
||||
|
||||
public <T extends Device> T saveAsOwner(T device, String username) {
|
||||
return saveAsOwner(device, username, false);
|
||||
}
|
||||
|
||||
public void delete(Long id, String username) throws NotFoundException {
|
||||
Device device =
|
||||
deviceRepository
|
||||
|
@ -151,7 +177,7 @@ public class DeviceService {
|
|||
.orElseThrow(NotFoundException::new);
|
||||
deviceRepository.delete(device);
|
||||
|
||||
final User user = userRepository.findByUsername(username);
|
||||
final User user = userRepository.findByUsernameFetchGuests(username);
|
||||
final Set<User> guests = user.getGuests();
|
||||
|
||||
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 DeviceService deviceService;
|
||||
|
||||
@Autowired private ThermostatService thermostatService;
|
||||
|
||||
@Autowired private SensorSocketEndpoint endpoint;
|
||||
|
@ -38,10 +40,10 @@ public class SensorService {
|
|||
*/
|
||||
public Sensor updateValueFromSensor(Sensor sensor, BigDecimal 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()));
|
||||
|
||||
return toReturn;
|
||||
return sensor;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,8 @@ public class ThermostatService {
|
|||
|
||||
@Autowired private SensorSocketEndpoint endpoint;
|
||||
|
||||
@Autowired private DeviceService deviceService;
|
||||
|
||||
@Autowired private ThermostatRepository thermostatRepository;
|
||||
|
||||
private void randomJitter(Thermostat thermostat) {
|
||||
|
@ -28,7 +30,8 @@ public class ThermostatService {
|
|||
|
||||
private void updateValueForThermostat(Thermostat thermostat, BigDecimal value) {
|
||||
thermostat.setInternalSensorTemperature(value);
|
||||
thermostatRepository.save(thermostat);
|
||||
deviceService.saveAsOwner(
|
||||
thermostat, thermostatRepository.findUser(thermostat.getId()).getUsername());
|
||||
}
|
||||
|
||||
public void fakeUpdateAll() {
|
||||
|
@ -51,7 +54,7 @@ public class ThermostatService {
|
|||
boolean shouldUpdate = this.computeState(t);
|
||||
|
||||
if (shouldUpdate) {
|
||||
thermostatRepository.save(t);
|
||||
deviceService.saveAsOwner(t, thermostatRepository.findUser(t.getId()).getUsername());
|
||||
endpoint.queueDeviceUpdate(t, thermostatRepository.findUser(t.getId()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue