From a428d57fe11556ef1e975bf7d60110137f9cb421 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Wed, 22 Apr 2020 15:30:23 +0200 Subject: [PATCH] WIP: controllers check for owner or guest, device service needs review --- .../controller/DimmableLightController.java | 26 ++++++++---- .../controller/RegularLightController.java | 21 +++++++--- .../smarthut/service/DeviceService.java | 42 +++++++++++++++++-- 3 files changed, 73 insertions(+), 16 deletions(-) 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 8c3265a..eaace7a 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 @@ -35,12 +35,18 @@ public class DimmableLightController { return dimmableLightRepository.findById(id).orElseThrow(NotFoundException::new); } - private DimmableLight save(DimmableLight initial, DimmableSaveRequest dl, String username) { + private DimmableLight save( + DimmableLight initial, DimmableSaveRequest dl, String username, Long hostId) + throws NotFoundException { initial.setIntensity(dl.getIntensity()); initial.setName(dl.getName()); initial.setRoomId(dl.getRoomId()); - return deviceService.saveAsOwner(initial, username); + if (hostId == null) { + return deviceService.saveAsOwner(initial, username); + } else { + return deviceService.saveAsGuest(initial, username, hostId); + } } /* @@ -49,8 +55,9 @@ public class DimmableLightController { */ @PostMapping public DimmableLight create( - @Valid @RequestBody DimmableSaveRequest dl, final Principal principal) { - return save(new DimmableLight(), dl, principal.getName()); + @Valid @RequestBody DimmableSaveRequest dl, final Principal principal) + throws NotFoundException { + return save(new DimmableLight(), dl, principal.getName(), null); } private DimmableLight fetchIfOwnerOrGuest(final Principal principal, Long id, Long hostId) @@ -79,15 +86,18 @@ public class DimmableLightController { } /* - Here you must behave differently if hostId is given or not: - - if not given, assume the owner of the device wants to update the device. In this case, save with DeviceService.saveAsOwner(); - - if given, assume a guest wants to update the intensity of this light. In this case, save with DeviceService.saveAsGuest(); + Logic for saving either as owner or guest is handled in method save of this controller */ @PutMapping public DimmableLight update( @Valid @RequestBody DimmableSaveRequest sp, final Principal principal, Long hostId) throws NotFoundException { - return save(fetchIfOwnerOrGuest(principal, sp.getId(), hostId), sp, principal.getName()); + + return save( + fetchIfOwnerOrGuest(principal, sp.getId(), hostId), + sp, + principal.getName(), + hostId); } @DeleteMapping("/{id}") 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 06fccfb..b73bff3 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 @@ -63,25 +63,36 @@ public class RegularLightController { return regularLightService.findById(id).orElseThrow(NotFoundException::new); } - private RegularLight save(RegularLight initial, SwitchableSaveRequest rl, String username) { + private RegularLight save( + RegularLight initial, SwitchableSaveRequest rl, String username, Long hostId) + throws NotFoundException { initial.setName(rl.getName()); initial.setRoomId(rl.getRoomId()); initial.setOn(rl.isOn()); - return deviceService.saveAsOwner(initial, username); + if (hostId == null) { + return deviceService.saveAsOwner(initial, username); + } else { + return deviceService.saveAsGuest(initial, username, hostId); + } } @PostMapping public RegularLight create( - @Valid @RequestBody SwitchableSaveRequest rl, final Principal principal) { - return save(new RegularLight(), rl, principal.getName()); + @Valid @RequestBody SwitchableSaveRequest rl, final Principal principal) + throws NotFoundException { + return save(new RegularLight(), rl, principal.getName(), null); } @PutMapping public RegularLight update( @Valid @RequestBody SwitchableSaveRequest rl, final Principal principal, Long hostId) throws NotFoundException { - return save(fetchIfOwnerOrGuest(principal, rl.getId(), hostId), rl, principal.getName()); + return save( + fetchIfOwnerOrGuest(principal, rl.getId(), hostId), + rl, + principal.getName(), + hostId); } @DeleteMapping("/{id}") 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 a4502ec..da2614b 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 @@ -1,10 +1,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.service; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +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.User; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint; +import java.beans.Transient; import java.util.Set; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -21,15 +24,48 @@ public class DeviceService { TODO: remember to put a @Transient @GsonIgnore (but not @SocketGsonIgnore) property on device to signal a device update TODO: coming from DeviceService.saveAsGuest() */ + public T saveAsGuest( + @Transient @GsonExclude T device, String guestUsername, Long hostId) + throws NotFoundException { + final User currentUser = userRepository.findByUsername(guestUsername); + final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new); + final Set guests = host.getGuests(); - // TODO: create saveAsGuest(device, guestUsername, hostId) + // filter out currentUser from guests as we do not want to broadcast an update to the + // updating user itself + if (guests.contains(currentUser)) { + guests.remove(currentUser); + } + + // broadcasting from not a host + device.setFromHost(false); + // broadcast device update for host + endpoint.queueDeviceUpdate(device, host); + userRepository.save(host); + endpoint.flushDeviceUpdates(); + for (final User guest : guests) { + // enqueue all device updates for all other guests + endpoint.queueDeviceUpdate(device, guest); + userRepository.save(guest); + } + // broadcast device updates for all other guests + endpoint.flushDeviceUpdates(); + return deviceRepository.save(device); + } public T saveAsOwner(T device, String username) { final User user = userRepository.findByUsername(username); final Set guests = user.getGuests(); + // make sure we're broadcasting from host + device.setFromHost(true); for (final User guest : guests) { - // set set from host true - // broadcast to endpoint the object device, with recieving user set to guest + // broadcast to endpoint the object device, with receiving user set to guest + endpoint.queueDeviceUpdate(device, guest); + userRepository.save(guest); } + // after queueing the device update for each user, flush them all in a single message + // can be moved inside the foreach loop to send a single message for each update enqueued + endpoint.flushDeviceUpdates(); + return deviceRepository.save(device); } }