added sceneBinding method in cotrollers of output devices

This commit is contained in:
omenem 2020-04-18 10:54:53 +02:00
parent f565a3be6e
commit e79ab46cb8
6 changed files with 137 additions and 0 deletions

View file

@ -6,6 +6,11 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Curtains;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.CurtainsRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
import java.security.Principal;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
@ -17,6 +22,8 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/curtains")
public class CurtainsController {
@Autowired private CurtainsRepository curtainsService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository stateRepository;
@GetMapping
public List<Curtains> findAll() {
@ -53,4 +60,21 @@ public class CurtainsController {
public void delete(@PathVariable("id") long id) {
curtainsService.deleteById(id);
}
@PostMapping("/{id}/state")
public void sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
throws NotFoundException {
Curtains c =
curtainsService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
DimmableState<Dimmable> s = c.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId);
stateRepository.save(s);
}
}

View file

@ -4,8 +4,12 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
import java.security.Principal;
import java.util.List;
import javax.validation.Valid;
@ -19,6 +23,8 @@ import org.springframework.web.bind.annotation.*;
public class DimmableLightController {
@Autowired private DimmableLightRepository dimmableLightService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository stateRepository;
@GetMapping
public List<DimmableLight> findAll() {
@ -58,4 +64,23 @@ public class DimmableLightController {
public void delete(@PathVariable("id") long id) {
dimmableLightService.deleteById(id);
}
// the full url should be: "/dimmableLight/{id}/state?sceneId={sceneId}
// however it is not necessary to specify the query in the mapping
@PostMapping("/{id}/state")
public void sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
throws NotFoundException {
DimmableLight d =
dimmableLightService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<Dimmable> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId);
stateRepository.save(s);
}
}

View file

@ -4,8 +4,12 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.OutputDevice;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
import java.security.Principal;
import java.util.List;
import javax.validation.Valid;
@ -18,6 +22,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@ -26,6 +31,8 @@ import org.springframework.web.bind.annotation.RestController;
public class RegularLightController {
@Autowired private RegularLightRepository regularLightService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository stateRepository;
@GetMapping
public List<RegularLight> findAll() {
@ -65,4 +72,22 @@ public class RegularLightController {
public void delete(@PathVariable("id") long id) {
regularLightService.deleteById(id);
}
// the full url should be: "/dimmableLight/{id}/state?sceneId={sceneId}
// however it is not necessary to specify the query in the mapping
@PostMapping("/{id}/state")
public void sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
throws NotFoundException {
RegularLight d =
regularLightService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends OutputDevice> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId);
stateRepository.save(s);
}
}

View file

@ -4,8 +4,12 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.OutputDevice;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCamera;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCameraRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.StateRepository;
import java.security.Principal;
import java.util.List;
import javax.validation.Valid;
@ -18,6 +22,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@ -26,6 +31,8 @@ import org.springframework.web.bind.annotation.RestController;
public class SecurityCameraController {
@Autowired SecurityCameraRepository securityCameraService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository stateRepository;
@GetMapping
public List<SecurityCamera> findAll() {
@ -65,4 +72,21 @@ public class SecurityCameraController {
public void delete(@PathVariable("id") long id) {
securityCameraService.deleteById(id);
}
@PostMapping("/{id}/state")
public void sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
throws NotFoundException {
SecurityCamera d =
securityCameraService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends OutputDevice> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId);
stateRepository.save(s);
}
}

View file

@ -18,6 +18,8 @@ import org.springframework.web.bind.annotation.*;
public class SmartPlugController {
@Autowired private SmartPlugRepository smartPlugRepository;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository stateRepository;
@GetMapping
public List<SmartPlug> findAll() {
@ -69,4 +71,21 @@ public class SmartPlugController {
public void deleteById(@PathVariable("id") long id) {
smartPlugRepository.deleteById(id);
}
@PostMapping("/{id}/state")
public void sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
throws NotFoundException {
SmartPlug d =
smartPlugRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends OutputDevice> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId);
stateRepository.save(s);
}
}

View file

@ -20,6 +20,9 @@ public class ThermostatController {
@Autowired private ThermostatService thermostatService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository stateRepository;
@GetMapping
public List<Thermostat> findAll(Principal user) {
return thermostatService.findAll(user.getName());
@ -65,4 +68,21 @@ public class ThermostatController {
public void deleteById(@PathVariable("id") long id) {
thermostatRepository.deleteById(id);
}
@PostMapping("/{id}/state")
public void sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
throws NotFoundException {
Thermostat d =
thermostatRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends OutputDevice> s = d.cloneState();
sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sceneId);
stateRepository.save(s);
}
}