Corrections on Scene controllers and method cloneState on all OutputDevices

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-17 20:54:09 +02:00
parent 2bfff689b3
commit 57a6e22e2a
11 changed files with 129 additions and 111 deletions

View File

@ -75,6 +75,9 @@ public class SpringFoxConfig {
.or(PathSelectors.regex("/securityCamera.*")::apply)
.or(PathSelectors.regex("/sensor.*")::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("/motionSensor.*")::apply)
.or(PathSelectors.regex("/curtains.*")::apply)

View File

@ -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);
}
}

View File

@ -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.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
@ -29,15 +29,19 @@ public class SceneController {
@Autowired SceneRepository sceneService;
@Autowired UserRepository userService;
@Autowired StateRepository<State<?>> stateService;
@Autowired DeviceRepository<Device> deviceRepository;
@GetMapping
public List<Scene> findAll() {
return toList(sceneService.findAll());
public List<Scene> findAll(Principal principal) {
return toList(sceneService.findByUsername(principal.getName()));
}
@GetMapping("/{id}")
public @ResponseBody Scene findById(@PathVariable("id") long id) throws NotFoundException {
return sceneService.findById(id).orElseThrow(NotFoundException::new);
public @ResponseBody Scene findById(@PathVariable("id") long id, Principal principal)
throws NotFoundException {
return sceneService
.findByIdAndUsername(id, principal.getName())
.orElseThrow(NotFoundException::new);
}
@PostMapping
@ -55,6 +59,25 @@ public class SceneController {
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}")
public @ResponseBody Scene update(
@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
* id).
*/
@GetMapping(path = "/{sceneId}/devices")
@GetMapping(path = "/{sceneId}/states")
public List<State<?>> getDevices(@PathVariable("sceneId") long sceneId) {
Iterable<State<?>> states = stateService.findBySceneId(sceneId);
return toList(states);

View File

@ -1,19 +1,14 @@
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.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableState;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SwitchableStateRepository;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -24,41 +19,20 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/switchableState")
public class SwitchableStateController {
@Autowired private SwitchableStateRepository switchableStateService;
@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);
}
@Autowired private SwitchableStateRepository switchableStateRepository;
@PutMapping
public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss)
throws NotFoundException {
return save(
switchableStateService.findById(ss.getId()).orElseThrow(NotFoundException::new),
ss);
final SwitchableState<?> initial =
switchableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
initial.setOn(ss.isOn());
switchableStateRepository.save(initial);
return initial;
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
switchableStateService.deleteById(id);
switchableStateRepository.deleteById(id);
}
}

View File

@ -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;
}
}

View File

@ -5,30 +5,10 @@ import javax.validation.constraints.NotNull;
public class SwitchableStateSaveRequest {
/** Device id (used only for update requests) */
private Long id;
@NotNull private Long deviceId;
@NotNull private Long sceneId;
@NotNull private Long id;
@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() {
return on;
}

View File

@ -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;
}
}

View File

@ -1,23 +1,20 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Set;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Dimmable extends Switchable {
public static final Connector<KnobDimmer, Dimmable>
KNOB_DIMMER_DIMMABLE_CONNECTOR =
public static final Connector<KnobDimmer, Dimmable> KNOB_DIMMER_DIMMABLE_CONNECTOR =
Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers);
public static final Connector<ButtonDimmer, Dimmable>
BUTTON_DIMMER_DIMMABLE_CONNECTOR =
public static final Connector<ButtonDimmer, Dimmable> BUTTON_DIMMER_DIMMABLE_CONNECTOR =
Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers);
protected Dimmable(String kind) {
@ -79,4 +76,13 @@ public class Dimmable extends Switchable {
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
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;
}
}

View File

@ -12,4 +12,6 @@ public abstract class OutputDevice extends Device {
public OutputDevice(String kind) {
super(kind, FlowType.OUTPUT);
}
public abstract State<? extends OutputDevice> cloneState();
}

View File

@ -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,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")
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);
}

View File

@ -1,9 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
/** A device that can be turned either on or off */
@Entity
@ -42,4 +42,13 @@ public abstract class Switchable extends OutputDevice {
public void readStateAndSet(SwitchableState<? extends Switchable> state) {
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;
}
}