Degenerified states

This commit is contained in:
Claudio Maggioni 2020-05-12 18:32:23 +02:00
parent 6223434979
commit acec5a0af8
21 changed files with 99 additions and 178 deletions

View file

@ -18,7 +18,7 @@ public class CurtainsController {
@Autowired private DeviceService deviceService;
@Autowired private CurtainsRepository curtainsService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
private Curtains save(Curtains newRL, DimmableSaveRequest s, final Principal principal) {
newRL.setName(s.getName());
@ -53,7 +53,7 @@ public class CurtainsController {
}
@PostMapping("/{id}/state")
public State<? extends Dimmable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -63,7 +63,7 @@ public class CurtainsController {
curtainsService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = c.cloneState();
State s = c.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -18,7 +18,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
private final DimmableLightRepository dimmableLightRepository;
private final SceneRepository sceneRepository;
private final StateRepository<State<?>> stateRepository;
private final StateRepository<State> stateRepository;
private final DeviceService deviceService;
@Autowired
@ -26,7 +26,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
UserRepository userRepository,
DimmableLightRepository dimmableLightRepository,
SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
DeviceService deviceService) {
super(userRepository, dimmableLightRepository);
this.dimmableLightRepository = dimmableLightRepository;
@ -87,7 +87,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
* necessary to specify the query in the mapping
*/
@PostMapping("/{id}/state")
public State<? extends Dimmable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -97,7 +97,7 @@ public class DimmableLightController extends GuestEnabledController<DimmableLigh
dimmableLightRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Dimmable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -17,9 +17,9 @@ public class DimmableStateController {
@Autowired private DimmableStateRepository dimmableStateRepository;
@PutMapping
public DimmableState<?> update(@Valid @RequestBody DimmableStateSaveRequest ss)
public DimmableState update(@Valid @RequestBody DimmableStateSaveRequest ss)
throws NotFoundException {
final DimmableState<?> initial =
final DimmableState initial =
dimmableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
initial.setIntensity(ss.getIntensity());
dimmableStateRepository.save(initial);

View file

@ -29,7 +29,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
private RegularLightRepository regularLightRepository;
private SceneRepository sceneRepository;
private StateRepository<State<?>> stateRepository;
private StateRepository<State> stateRepository;
private DeviceService deviceService;
@Autowired
@ -37,7 +37,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
UserRepository userRepository,
RegularLightRepository regularLightRepository,
SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
DeviceService deviceService) {
super(userRepository, regularLightRepository);
this.regularLightRepository = regularLightRepository;
@ -98,7 +98,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -107,7 +107,7 @@ public class RegularLightController extends GuestEnabledController<RegularLight>
regularLightRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -22,7 +22,7 @@ public class SceneController {
@Autowired private SceneRepository sceneRepository;
@Autowired private UserRepository userRepository;
@Autowired private SceneService sceneService;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
@GetMapping
public List<Scene> findAll(
@ -78,7 +78,7 @@ public class SceneController {
}
@PostMapping("/{id}/copyFrom/{copyId}")
public @ResponseBody List<State<?>> copy(
public @ResponseBody List<State> copy(
@PathVariable("id") long id,
@PathVariable("copyId") long copyId,
final Principal principal)
@ -126,8 +126,8 @@ public class SceneController {
* id).
*/
@GetMapping(path = "/{sceneId}/states")
public List<State<?>> getDevices(@PathVariable("sceneId") long sceneId) {
Iterable<State<?>> states = stateRepository.findBySceneId(sceneId);
public List<State> getStates(@PathVariable("sceneId") long sceneId) {
Iterable<State> states = stateRepository.findBySceneId(sceneId);
return toList(states);
}
}

View file

@ -26,7 +26,7 @@ public class SecurityCameraController {
private final DeviceService deviceService;
private final SecurityCameraRepository securityCameraService;
private final SceneRepository sceneRepository;
private final StateRepository<State<?>> stateRepository;
private final StateRepository<State> stateRepository;
private final CameraConfigurationService cameraConfigurationService;
@Autowired
@ -34,7 +34,7 @@ public class SecurityCameraController {
DeviceService deviceService,
SecurityCameraRepository securityCameraService,
SceneRepository sceneRepository,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
CameraConfigurationService cameraConfigurationService) {
this.deviceService = deviceService;
this.securityCameraService = securityCameraService;
@ -80,7 +80,7 @@ public class SecurityCameraController {
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -90,7 +90,7 @@ public class SecurityCameraController {
securityCameraService
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -19,7 +19,7 @@ public class SmartPlugController {
@Autowired private DeviceService deviceService;
@Autowired private SmartPlugRepository smartPlugRepository;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp, final Principal principal) {
newSP.setOn(sp.isOn());
@ -67,7 +67,7 @@ public class SmartPlugController {
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -77,7 +77,7 @@ public class SmartPlugController {
smartPlugRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -22,9 +22,9 @@ public class SwitchableStateController {
@Autowired private SwitchableStateRepository switchableStateRepository;
@PutMapping
public SwitchableState<?> update(@Valid @RequestBody SwitchableStateSaveRequest ss)
public SwitchableState update(@Valid @RequestBody SwitchableStateSaveRequest ss)
throws NotFoundException {
final SwitchableState<?> initial =
final SwitchableState initial =
switchableStateRepository.findById(ss.getId()).orElseThrow(NotFoundException::new);
initial.setOn(ss.isOn());
switchableStateRepository.save(initial);

View file

@ -21,7 +21,7 @@ public class ThermostatController {
@Autowired private ThermostatRepository thermostatRepository;
@Autowired private ThermostatPopulationService thermostatService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository;
@Autowired private StateRepository<State> stateRepository;
private Thermostat save(Thermostat newT, ThermostatSaveRequest t, final Principal principal) {
newT.setTargetTemperature(t.getTargetTemperature());
@ -63,7 +63,7 @@ public class ThermostatController {
}
@PostMapping("/{id}/state")
public State<? extends Switchable> sceneBinding(
public State sceneBinding(
@PathVariable("id") long deviceId,
@RequestParam long sceneId,
final Principal principal)
@ -73,7 +73,7 @@ public class ThermostatController {
thermostatRepository
.findByIdAndUsername(deviceId, principal.getName())
.orElseThrow(NotFoundException::new);
State<? extends Switchable> s = d.cloneState();
State s = d.cloneState();
final Scene sc = sceneRepository.findById(sceneId).orElseThrow(NotFoundException::new);
s.setSceneId(sc.getId());
if (stateRepository.countByDeviceIdAndSceneId(deviceId, sceneId) > 0)

View file

@ -67,7 +67,7 @@ public abstract class Device {
@OneToMany(mappedBy = "device", orphanRemoval = true)
@GsonExclude
@SocketGsonExclude
private Set<State<?>> states;
private Set<State> states;
@Transient @GsonExclude private Long fromHostId = null;

View file

@ -65,15 +65,13 @@ public class Dimmable extends Switchable implements RangeTriggerable {
intensity = on ? oldIntensity : 0;
}
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
public void readStateAndSet(DimmableState state) {
setIntensity(state.getIntensity());
}
@Override
public State<? extends Dimmable> cloneState() {
final DimmableState<Dimmable> newState = new DimmableState<>();
public State cloneState() {
final DimmableState newState = new DimmableState();
newState.setDeviceId(getId());
newState.setDevice(this);
newState.setIntensity(getIntensity());
return newState;
}

View file

@ -8,7 +8,11 @@ import lombok.Setter;
/** Represent a state for an IDimmable device */
@Entity
public class DimmableState<T extends Dimmable> extends State<T> {
public class DimmableState extends State {
public void setDevice(Dimmable device) {
setInnerDevice(device);
}
/** The light intensity value. Goes from 0 (off) to 100 (on) */
@Min(0)
@ -19,12 +23,12 @@ public class DimmableState<T extends Dimmable> extends State<T> {
@Override
public void apply() {
getDevice().readStateAndSet(this);
((Dimmable) getDevice()).readStateAndSet(this);
}
@Override
protected State<T> copy() {
final DimmableState<T> d = new DimmableState<>();
protected DimmableState copy() {
final DimmableState d = new DimmableState();
d.setIntensity(intensity);
return d;
}

View file

@ -1,3 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface DimmableStateRepository extends StateRepository<DimmableState<?>> {}
public interface DimmableStateRepository extends StateRepository<DimmableState> {}

View file

@ -14,10 +14,10 @@ public abstract class OutputDevice extends Device {
}
/**
* Creates a State<?> object initialized to point at this device and with values copied from
* this device's state
* Creates a State object initialized to point at this device and with values copied from this
* device's state
*
* @return a new State<?> object
* @return a new State object
*/
public abstract State<? extends OutputDevice> cloneState();
public abstract State cloneState();
}

View file

@ -5,11 +5,13 @@ import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import lombok.Data;
/**
* Represent a collection of state changes to devices even in different rooms but belonging to the
* same user
*/
@Data
@Entity
public class Scene {
@ -30,7 +32,7 @@ public class Scene {
@OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude
private Set<State<?>> states = new HashSet<>();
private Set<State> states = new HashSet<>();
/** The user given name of this room (e.g. 'Master bedroom') */
@Column(nullable = false)
@ -41,60 +43,4 @@ public class Scene {
/** Determines whether a guest can access this scene */
@Column private boolean guestAccessEnabled;
public Icon getIcon() {
return icon;
}
public void setIcon(Icon icon) {
this.icon = icon;
}
public boolean isGuestAccessEnabled() {
return guestAccessEnabled;
}
public void setGuestAccessEnabled(boolean guestAccessEnabled) {
this.guestAccessEnabled = guestAccessEnabled;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Set<State<?>> getStates() {
return states;
}
public void setStates(Set<State<?>> states) {
this.states = states;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
}

View file

@ -3,6 +3,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;
/**
* Represents instructions on how to change the state of a particular device. Many states (plus
@ -11,93 +13,66 @@ import javax.persistence.*;
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"device_id", "scene_id"})})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class State<D extends OutputDevice> {
public abstract class State {
@ManyToOne(targetEntity = OutputDevice.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude
@Getter
private OutputDevice device;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true)
@Getter
private long id;
@ManyToOne(targetEntity = OutputDevice.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude
private D device;
/**
* The device this state belongs in, as a foreign key id. To use when updating and inserting
* from a REST call.
*/
@Column(name = "device_id", nullable = false)
@Getter
@Setter
private Long deviceId;
@ManyToOne
@JoinColumn(name = "scene_id", updatable = false, insertable = false)
@GsonExclude
@Getter
@Setter
private Scene scene;
@Column(name = "scene_id", nullable = false)
@Getter
@Setter
private Long sceneId;
protected void setInnerDevice(OutputDevice device) {
this.device = device;
}
/** Sets the state of the connected device to the state represented by this object. */
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();
protected abstract State copy();
public State<D> copyToSceneId(Long sceneId) {
final State<D> s = copy();
public State copyToSceneId(Long sceneId) {
final State s = copy();
s.setDeviceId(this.deviceId);
s.device = this.device;
s.setSceneId(sceneId);
s.setScene(this.scene);
return s;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public D getDevice() {
return device;
}
public void setDevice(D device) {
this.device = device;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
@PreRemove
public void removeDeviceAndScene() {
this.setScene(null);
this.setSceneId(null);
this.setDevice(null);
this.setScene(null);
this.setDeviceId(null);
this.device = null;
}
}

View file

@ -5,7 +5,7 @@ import javax.transaction.Transactional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface StateRepository<T extends State<?>> extends CrudRepository<T, Long> {
public interface StateRepository<T extends State> extends CrudRepository<T, Long> {
@Transactional
void deleteAllBySceneId(long roomId);

View file

@ -45,15 +45,13 @@ public abstract class Switchable extends OutputDevice {
return inputs;
}
public void readStateAndSet(SwitchableState<? extends Switchable> state) {
public void readStateAndSet(SwitchableState state) {
setOn(state.isOn());
}
@Override
public State<? extends Switchable> cloneState() {
final SwitchableState<Switchable> newState = new SwitchableState<>();
public State cloneState() {
final SwitchableState newState = new SwitchableState();
newState.setDeviceId(getId());
newState.setDevice(this);
newState.setOn(isOn());
return newState;
}

View file

@ -2,30 +2,30 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import lombok.Getter;
import lombok.Setter;
/** Represents a state for a Switchable device */
@Entity
public class SwitchableState<T extends Switchable> extends State<T> {
public class SwitchableState extends State {
public void setDevice(Switchable device) {
setInnerDevice(device);
}
@Column(name = "switchable_on")
@Getter
@Setter
private boolean on;
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
@Override
public void apply() {
getDevice().readStateAndSet(this);
((Switchable) getDevice()).readStateAndSet(this);
}
@Override
protected State<T> copy() {
final SwitchableState<T> d = new SwitchableState<>();
protected SwitchableState copy() {
final SwitchableState d = new SwitchableState();
d.setOn(on);
return d;
}

View file

@ -1,3 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface SwitchableStateRepository extends StateRepository<SwitchableState<?>> {}
public interface SwitchableStateRepository extends StateRepository<SwitchableState> {}

View file

@ -11,7 +11,7 @@ public class SceneService {
private final DevicePopulationService devicePopulationService;
private final DevicePropagationService devicePropagationService;
private final StateRepository<State<?>> stateRepository;
private final StateRepository<State> stateRepository;
private final SceneRepository sceneRepository;
public Scene findByValidatedId(Long id) {
@ -22,7 +22,7 @@ public class SceneService {
public SceneService(
DevicePopulationService devicePopulationService,
DevicePropagationService devicePropagationService,
StateRepository<State<?>> stateRepository,
StateRepository<State> stateRepository,
SceneRepository sceneRepository) {
this.devicePopulationService = devicePopulationService;
this.devicePropagationService = devicePropagationService;
@ -33,7 +33,7 @@ public class SceneService {
private List<Device> copyStatesToDevices(Scene fromScene) {
final List<Device> updated = new ArrayList<>(fromScene.getStates().size());
for (final State<?> s : fromScene.getStates()) {
for (final State s : fromScene.getStates()) {
s.apply();
updated.add(s.getDevice());
}
@ -54,9 +54,9 @@ public class SceneService {
return updated;
}
public List<State<?>> copyStates(Scene to, Scene from) {
final ArrayList<State<?>> states = new ArrayList<>();
for (final State<?> s : from.getStates()) {
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;