Corrections on Scene controllers and method cloneState on all OutputDevices
This commit is contained in:
parent
2bfff689b3
commit
57a6e22e2a
11 changed files with 129 additions and 111 deletions
|
@ -75,6 +75,9 @@ public class SpringFoxConfig {
|
||||||
.or(PathSelectors.regex("/securityCamera.*")::apply)
|
.or(PathSelectors.regex("/securityCamera.*")::apply)
|
||||||
.or(PathSelectors.regex("/sensor.*")::apply)
|
.or(PathSelectors.regex("/sensor.*")::apply)
|
||||||
.or(PathSelectors.regex("/smartPlug.*")::apply)
|
.or(PathSelectors.regex("/smartPlug.*")::apply)
|
||||||
|
.or(PathSelectors.regex("/scene.*")::apply)
|
||||||
|
.or(PathSelectors.regex("/switchableState.*")::apply)
|
||||||
|
.or(PathSelectors.regex("/dimmableState.*")::apply)
|
||||||
.or(PathSelectors.regex("/switch.*")::apply)
|
.or(PathSelectors.regex("/switch.*")::apply)
|
||||||
.or(PathSelectors.regex("/motionSensor.*")::apply)
|
.or(PathSelectors.regex("/motionSensor.*")::apply)
|
||||||
.or(PathSelectors.regex("/curtains.*")::apply)
|
.or(PathSelectors.regex("/curtains.*")::apply)
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableStateSaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableState;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableStateRepository;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@RequestMapping("/dimmableState")
|
||||||
|
public class DimmableStateController {
|
||||||
|
|
||||||
|
@Autowired private DimmableStateRepository dimmableStateRepository;
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
public DimmableState<?> update(@Valid @RequestBody DimmableStateSaveRequest ss)
|
||||||
|
throws NotFoundException {
|
||||||
|
final DimmableState<?> initial =
|
||||||
|
dimmableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
|
||||||
|
initial.setIntensity(ss.getIntensity());
|
||||||
|
dimmableStateRepository.save(initial);
|
||||||
|
return initial;
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void delete(@PathVariable("id") long id) {
|
||||||
|
dimmableStateRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,8 +5,8 @@ import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SceneSaveRequest;
|
||||||
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 java.security.Principal;
|
import java.security.Principal;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -29,15 +29,19 @@ public class SceneController {
|
||||||
@Autowired SceneRepository sceneService;
|
@Autowired SceneRepository sceneService;
|
||||||
@Autowired UserRepository userService;
|
@Autowired UserRepository userService;
|
||||||
@Autowired StateRepository<State<?>> stateService;
|
@Autowired StateRepository<State<?>> stateService;
|
||||||
|
@Autowired DeviceRepository<Device> deviceRepository;
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Scene> findAll() {
|
public List<Scene> findAll(Principal principal) {
|
||||||
return toList(sceneService.findAll());
|
return toList(sceneService.findByUsername(principal.getName()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public @ResponseBody Scene findById(@PathVariable("id") long id) throws NotFoundException {
|
public @ResponseBody Scene findById(@PathVariable("id") long id, Principal principal)
|
||||||
return sceneService.findById(id).orElseThrow(NotFoundException::new);
|
throws NotFoundException {
|
||||||
|
return sceneService
|
||||||
|
.findByIdAndUsername(id, principal.getName())
|
||||||
|
.orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
|
@ -55,6 +59,25 @@ public class SceneController {
|
||||||
return sceneService.save(newScene);
|
return sceneService.save(newScene);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping("/{id}/apply")
|
||||||
|
public @ResponseBody List<Device> apply(@PathVariable("id") long id, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
final Scene newScene =
|
||||||
|
sceneService
|
||||||
|
.findByIdAndUsername(id, principal.getName())
|
||||||
|
.orElseThrow(NotFoundException::new);
|
||||||
|
|
||||||
|
final List<Device> updated = new ArrayList<>();
|
||||||
|
|
||||||
|
for (final State<?> s : newScene.getStates()) {
|
||||||
|
s.apply();
|
||||||
|
updated.add(s.getDevice());
|
||||||
|
}
|
||||||
|
deviceRepository.saveAll(updated);
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
public @ResponseBody Scene update(
|
public @ResponseBody Scene update(
|
||||||
@PathVariable("id") long id, @RequestBody SceneSaveRequest s, final Principal principal)
|
@PathVariable("id") long id, @RequestBody SceneSaveRequest s, final Principal principal)
|
||||||
|
@ -81,7 +104,7 @@ public class SceneController {
|
||||||
* Returns a List<State> of all Devices that are associated to a given scene (identified by its
|
* Returns a List<State> of all Devices that are associated to a given scene (identified by its
|
||||||
* id).
|
* id).
|
||||||
*/
|
*/
|
||||||
@GetMapping(path = "/{sceneId}/devices")
|
@GetMapping(path = "/{sceneId}/states")
|
||||||
public List<State<?>> getDevices(@PathVariable("sceneId") long sceneId) {
|
public List<State<?>> getDevices(@PathVariable("sceneId") long sceneId) {
|
||||||
Iterable<State<?>> states = stateService.findBySceneId(sceneId);
|
Iterable<State<?>> states = stateService.findBySceneId(sceneId);
|
||||||
return toList(states);
|
return toList(states);
|
||||||
|
|
|
@ -1,19 +1,14 @@
|
||||||
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.dto.SwitchableStateSaveRequest;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableStateSaveRequest;
|
||||||
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.SwitchableState;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableStateRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableStateRepository;
|
||||||
import java.util.List;
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
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.PutMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
@ -24,41 +19,20 @@ import org.springframework.web.bind.annotation.RestController;
|
||||||
@RequestMapping("/switchableState")
|
@RequestMapping("/switchableState")
|
||||||
public class SwitchableStateController {
|
public class SwitchableStateController {
|
||||||
|
|
||||||
@Autowired private SwitchableStateRepository switchableStateService;
|
@Autowired private SwitchableStateRepository switchableStateRepository;
|
||||||
|
|
||||||
@GetMapping
|
|
||||||
public List<SwitchableState<?>> findAll() {
|
|
||||||
return toList(switchableStateService.findAll());
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
|
||||||
public SwitchableState<?> findById(@PathVariable("id") long id) throws NotFoundException {
|
|
||||||
return switchableStateService.findById(id).orElseThrow(NotFoundException::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
private SwitchableState<?> save(SwitchableState<?> initial, SwitchableStateSaveRequest ss) {
|
|
||||||
initial.setDeviceId(ss.getDeviceId());
|
|
||||||
initial.setSceneId(ss.getSceneId());
|
|
||||||
initial.setOn(ss.isOn());
|
|
||||||
|
|
||||||
return switchableStateService.save(initial);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping
|
|
||||||
public SwitchableState<?> create(@Valid @RequestBody SwitchableStateSaveRequest dl) {
|
|
||||||
return save(new SwitchableState<>(), dl);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss)
|
public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
return save(
|
final SwitchableState<?> initial =
|
||||||
switchableStateService.findById(ss.getId()).orElseThrow(NotFoundException::new),
|
switchableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
|
||||||
ss);
|
initial.setOn(ss.isOn());
|
||||||
|
switchableStateRepository.save(initial);
|
||||||
|
return initial;
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void delete(@PathVariable("id") long id) {
|
public void delete(@PathVariable("id") long id) {
|
||||||
switchableStateService.deleteById(id);
|
switchableStateRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
import javax.validation.constraints.Max;
|
||||||
|
import javax.validation.constraints.Min;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class DimmableStateSaveRequest {
|
||||||
|
|
||||||
|
/** Device id (used only for update requests) */
|
||||||
|
@NotNull private Long id;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Min(0)
|
||||||
|
@Max(100)
|
||||||
|
private Integer intensity = 0;
|
||||||
|
|
||||||
|
public Integer getIntensity() {
|
||||||
|
return intensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIntensity(Integer intensity) {
|
||||||
|
this.intensity = intensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,30 +5,10 @@ import javax.validation.constraints.NotNull;
|
||||||
public class SwitchableStateSaveRequest {
|
public class SwitchableStateSaveRequest {
|
||||||
|
|
||||||
/** Device id (used only for update requests) */
|
/** Device id (used only for update requests) */
|
||||||
private Long id;
|
@NotNull private Long id;
|
||||||
|
|
||||||
@NotNull private Long deviceId;
|
|
||||||
|
|
||||||
@NotNull private Long sceneId;
|
|
||||||
|
|
||||||
@NotNull private boolean on;
|
@NotNull private boolean on;
|
||||||
|
|
||||||
public Long getDeviceId() {
|
|
||||||
return deviceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDeviceId(Long deviceId) {
|
|
||||||
this.deviceId = deviceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getSceneId() {
|
|
||||||
return sceneId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSceneId(Long sceneId) {
|
|
||||||
this.sceneId = sceneId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isOn() {
|
public boolean isOn() {
|
||||||
return on;
|
return on;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
|
|
||||||
import javax.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
public class ThermostatStateSaveRequest {
|
|
||||||
|
|
||||||
/** Device id (used only for update requests) */
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
@NotNull private Long deviceId;
|
|
||||||
|
|
||||||
@NotNull private Long sceneId;
|
|
||||||
|
|
||||||
private Thermostat.Mode mode;
|
|
||||||
|
|
||||||
public Thermostat.Mode getMode() {
|
|
||||||
return mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMode(Thermostat.Mode mode) {
|
|
||||||
this.mode = mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getDeviceId() {
|
|
||||||
return deviceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDeviceId(Long deviceId) {
|
|
||||||
this.deviceId = deviceId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getSceneId() {
|
|
||||||
return sceneId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setSceneId(Long sceneId) {
|
|
||||||
this.sceneId = sceneId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,23 +1,20 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
||||||
|
import java.util.Set;
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import javax.validation.constraints.Max;
|
import javax.validation.constraints.Max;
|
||||||
import javax.validation.constraints.Min;
|
import javax.validation.constraints.Min;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||||
public class Dimmable extends Switchable {
|
public class Dimmable extends Switchable {
|
||||||
|
|
||||||
public static final Connector<KnobDimmer, Dimmable>
|
public static final Connector<KnobDimmer, Dimmable> KNOB_DIMMER_DIMMABLE_CONNECTOR =
|
||||||
KNOB_DIMMER_DIMMABLE_CONNECTOR =
|
|
||||||
Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers);
|
Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers);
|
||||||
|
|
||||||
public static final Connector<ButtonDimmer, Dimmable>
|
public static final Connector<ButtonDimmer, Dimmable> BUTTON_DIMMER_DIMMABLE_CONNECTOR =
|
||||||
BUTTON_DIMMER_DIMMABLE_CONNECTOR =
|
|
||||||
Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers);
|
Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers);
|
||||||
|
|
||||||
protected Dimmable(String kind) {
|
protected Dimmable(String kind) {
|
||||||
|
@ -79,4 +76,13 @@ public class Dimmable extends Switchable {
|
||||||
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
|
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
|
||||||
setIntensity(state.getIntensity());
|
setIntensity(state.getIntensity());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public DimmableState<Dimmable> cloneState() {
|
||||||
|
final DimmableState<Dimmable> newState = new DimmableState<>();
|
||||||
|
newState.setDeviceId(getId());
|
||||||
|
newState.setDevice(this);
|
||||||
|
newState.setIntensity(getIntensity());
|
||||||
|
return newState;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,4 +12,6 @@ public abstract class OutputDevice extends Device {
|
||||||
public OutputDevice(String kind) {
|
public OutputDevice(String kind) {
|
||||||
super(kind, FlowType.OUTPUT);
|
super(kind, FlowType.OUTPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public abstract State<? extends OutputDevice> cloneState();
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,7 @@ public interface SceneRepository extends CrudRepository<Scene, 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<Scene> findByIdAndUsername(Long id, String username);
|
Optional<Scene> findByIdAndUsername(Long id, String username);
|
||||||
|
|
||||||
|
@Query("SELECT r FROM Room r JOIN r.user u WHERE u.username = ?1")
|
||||||
|
List<Scene> findByUsername(String username);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
||||||
import javax.persistence.*;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import javax.persistence.*;
|
||||||
|
|
||||||
/** A device that can be turned either on or off */
|
/** A device that can be turned either on or off */
|
||||||
@Entity
|
@Entity
|
||||||
|
@ -42,4 +42,13 @@ public abstract class Switchable extends OutputDevice {
|
||||||
public void readStateAndSet(SwitchableState<? extends Switchable> state) {
|
public void readStateAndSet(SwitchableState<? extends Switchable> state) {
|
||||||
setOn(state.isOn());
|
setOn(state.isOn());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public State<? extends OutputDevice> cloneState() {
|
||||||
|
final SwitchableState<Switchable> newState = new SwitchableState<>();
|
||||||
|
newState.setDeviceId(getId());
|
||||||
|
newState.setDevice(this);
|
||||||
|
newState.setOn(isOn());
|
||||||
|
return newState;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue