WIP: controllers check for owner or guest, device service needs review
This commit is contained in:
parent
c4f295d7d9
commit
a428d57fe1
3 changed files with 73 additions and 16 deletions
|
@ -35,12 +35,18 @@ public class DimmableLightController {
|
||||||
return dimmableLightRepository.findById(id).orElseThrow(NotFoundException::new);
|
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.setIntensity(dl.getIntensity());
|
||||||
initial.setName(dl.getName());
|
initial.setName(dl.getName());
|
||||||
initial.setRoomId(dl.getRoomId());
|
initial.setRoomId(dl.getRoomId());
|
||||||
|
|
||||||
|
if (hostId == null) {
|
||||||
return deviceService.saveAsOwner(initial, username);
|
return deviceService.saveAsOwner(initial, username);
|
||||||
|
} else {
|
||||||
|
return deviceService.saveAsGuest(initial, username, hostId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -49,8 +55,9 @@ public class DimmableLightController {
|
||||||
*/
|
*/
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public DimmableLight create(
|
public DimmableLight create(
|
||||||
@Valid @RequestBody DimmableSaveRequest dl, final Principal principal) {
|
@Valid @RequestBody DimmableSaveRequest dl, final Principal principal)
|
||||||
return save(new DimmableLight(), dl, principal.getName());
|
throws NotFoundException {
|
||||||
|
return save(new DimmableLight(), dl, principal.getName(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DimmableLight fetchIfOwnerOrGuest(final Principal principal, Long id, Long hostId)
|
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:
|
Logic for saving either as owner or guest is handled in method save of this controller
|
||||||
- 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();
|
|
||||||
*/
|
*/
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public DimmableLight update(
|
public DimmableLight update(
|
||||||
@Valid @RequestBody DimmableSaveRequest sp, final Principal principal, Long hostId)
|
@Valid @RequestBody DimmableSaveRequest sp, final Principal principal, Long hostId)
|
||||||
throws NotFoundException {
|
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}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -63,25 +63,36 @@ public class RegularLightController {
|
||||||
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
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.setName(rl.getName());
|
||||||
initial.setRoomId(rl.getRoomId());
|
initial.setRoomId(rl.getRoomId());
|
||||||
initial.setOn(rl.isOn());
|
initial.setOn(rl.isOn());
|
||||||
|
|
||||||
|
if (hostId == null) {
|
||||||
return deviceService.saveAsOwner(initial, username);
|
return deviceService.saveAsOwner(initial, username);
|
||||||
|
} else {
|
||||||
|
return deviceService.saveAsGuest(initial, username, hostId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public RegularLight create(
|
public RegularLight create(
|
||||||
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal) {
|
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal)
|
||||||
return save(new RegularLight(), rl, principal.getName());
|
throws NotFoundException {
|
||||||
|
return save(new RegularLight(), rl, principal.getName(), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public RegularLight update(
|
public RegularLight update(
|
||||||
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal, Long hostId)
|
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal, Long hostId)
|
||||||
throws NotFoundException {
|
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}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
|
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.Device;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
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.User;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.socket.SensorSocketEndpoint;
|
||||||
|
import java.beans.Transient;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
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: remember to put a @Transient @GsonIgnore (but not @SocketGsonIgnore) property on device to signal a device update
|
||||||
TODO: coming from DeviceService.saveAsGuest()
|
TODO: coming from DeviceService.saveAsGuest()
|
||||||
*/
|
*/
|
||||||
|
public <T extends Device> 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<User> 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 extends Device> T saveAsOwner(T device, String username) {
|
public <T extends Device> T saveAsOwner(T device, String username) {
|
||||||
final User user = userRepository.findByUsername(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
|
||||||
|
device.setFromHost(true);
|
||||||
for (final User guest : guests) {
|
for (final User guest : guests) {
|
||||||
// set set from host true
|
// broadcast to endpoint the object device, with receiving user set to guest
|
||||||
// broadcast to endpoint the object device, with recieving 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue