Merge branch '71-when-creating-a-scene-users-can-t-copy-the-configuration-from-another-scene' into 'dev'
Resolve "When creating a scene, users can't copy the configuration from another scene." Closes #71 See merge request sa4-2020/the-sanmarinoes/backend!111
This commit is contained in:
commit
1ddc8f5ba6
5 changed files with 52 additions and 4 deletions
|
@ -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)
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue