Scene copy implemented

This commit is contained in:
Claudio Maggioni (maggicl) 2020-05-04 16:10:17 +02:00
parent 297fe5f9b3
commit 16d1af2b36
5 changed files with 52 additions and 4 deletions

View file

@ -72,6 +72,24 @@ public class SceneController {
return sceneService.apply(newScene, principal.getName(), false); return sceneService.apply(newScene, principal.getName(), false);
} }
@PostMapping("/{id}/copyFrom/{copyId}")
public @ResponseBody List<State<?>> copy(
@PathVariable("id") long id,
@PathVariable("copyId") long copyId,
final Principal principal)
throws NotFoundException {
final Scene scene =
sceneRepository
.findByIdAndUsername(id, principal.getName())
.orElseThrow(NotFoundException::new);
final Scene copyFrom =
sceneRepository
.findByIdAndUsername(copyId, principal.getName())
.orElseThrow(NotFoundException::new);
return sceneService.copyStates(scene, copyFrom);
}
@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)

View file

@ -25,4 +25,11 @@ public class DimmableState<T extends Dimmable> extends State<T> {
public void apply() { public void apply() {
getDevice().readStateAndSet(this); getDevice().readStateAndSet(this);
} }
@Override
protected State<T> copy() {
final DimmableState<T> d = new DimmableState<>();
d.setIntensity(intensity);
return d;
}
} }

View file

@ -45,6 +45,16 @@ public abstract class State<D extends OutputDevice> {
/** Sets the state of the connected device to the state represented by this object. */ /** Sets the state of the connected device to the state represented by this object. */
public abstract void apply(); public abstract void apply();
/** Creates a perfect copy of this state, except for the id field and the sceneId/scene */
protected abstract State<D> copy();
public State<D> copyToSceneId(Long sceneId) {
final State<D> s = copy();
s.setDeviceId(this.deviceId);
s.setSceneId(sceneId);
return s;
}
public long getId() { public long getId() {
return id; return id;
} }

View file

@ -22,4 +22,11 @@ public class SwitchableState<T extends Switchable> extends State<T> {
public void apply() { public void apply() {
getDevice().readStateAndSet(this); getDevice().readStateAndSet(this);
} }
@Override
protected State<T> copy() {
final SwitchableState<T> d = new SwitchableState<>();
d.setOn(on);
return d;
}
} }

View file

@ -1,9 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.service; package ch.usi.inf.sa4.sanmarinoes.smarthut.service;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Scene;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.State;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -14,6 +11,7 @@ public class SceneService {
@Autowired private DeviceRepository<Device> deviceRepository; @Autowired private DeviceRepository<Device> deviceRepository;
@Autowired private DeviceService deviceService; @Autowired private DeviceService deviceService;
@Autowired private StateRepository<State<?>> stateRepository;
public List<Device> apply(Scene newScene, String username, boolean fromTrigger) { public List<Device> apply(Scene newScene, String username, boolean fromTrigger) {
final List<Device> updated = new ArrayList<>(); final List<Device> updated = new ArrayList<>();
@ -26,4 +24,12 @@ public class SceneService {
deviceService.populateComputedFields(updated); deviceService.populateComputedFields(updated);
return updated; return updated;
} }
public List<State<?>> copyStates(Scene to, Scene from) {
final ArrayList<State<?>> states = new ArrayList<>();
for (final State<?> s : from.getStates()) {
states.add(stateRepository.save(s.copyToSceneId(to.getId())));
}
return states;
}
} }