From acec5a0af8eab50dce576207465660f5c54c07af Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 12 May 2020 18:32:23 +0200 Subject: [PATCH] Degenerified states --- .../controller/CurtainsController.java | 6 +- .../controller/DimmableLightController.java | 8 +- .../controller/DimmableStateController.java | 4 +- .../controller/RegularLightController.java | 8 +- .../smarthut/controller/SceneController.java | 8 +- .../controller/SecurityCameraController.java | 8 +- .../controller/SmartPlugController.java | 6 +- .../controller/SwitchableStateController.java | 4 +- .../controller/ThermostatController.java | 6 +- .../sanmarinoes/smarthut/models/Device.java | 2 +- .../sanmarinoes/smarthut/models/Dimmable.java | 8 +- .../smarthut/models/DimmableState.java | 12 ++- .../models/DimmableStateRepository.java | 2 +- .../smarthut/models/OutputDevice.java | 8 +- .../sanmarinoes/smarthut/models/Scene.java | 60 +------------- .../sanmarinoes/smarthut/models/State.java | 79 +++++++------------ .../smarthut/models/StateRepository.java | 2 +- .../smarthut/models/Switchable.java | 8 +- .../smarthut/models/SwitchableState.java | 24 +++--- .../models/SwitchableStateRepository.java | 2 +- .../smarthut/service/SceneService.java | 12 +-- 21 files changed, 99 insertions(+), 178 deletions(-) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java index 9fbf704..5e01f9a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java @@ -18,7 +18,7 @@ public class CurtainsController { @Autowired private DeviceService deviceService; @Autowired private CurtainsRepository curtainsService; @Autowired private SceneRepository sceneRepository; - @Autowired private StateRepository> stateRepository; + @Autowired private StateRepository 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 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 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) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 990c105..e251d7c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -18,7 +18,7 @@ public class DimmableLightController extends GuestEnabledController> stateRepository; + private final StateRepository stateRepository; private final DeviceService deviceService; @Autowired @@ -26,7 +26,7 @@ public class DimmableLightController extends GuestEnabledController> stateRepository, + StateRepository stateRepository, DeviceService deviceService) { super(userRepository, dimmableLightRepository); this.dimmableLightRepository = dimmableLightRepository; @@ -87,7 +87,7 @@ public class DimmableLightController extends GuestEnabledController sceneBinding( + public State sceneBinding( @PathVariable("id") long deviceId, @RequestParam long sceneId, final Principal principal) @@ -97,7 +97,7 @@ public class DimmableLightController extends GuestEnabledController 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) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateController.java index d866d78..83ffb53 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableStateController.java @@ -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); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index c3bdc8b..6da88d8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -29,7 +29,7 @@ public class RegularLightController extends GuestEnabledController private RegularLightRepository regularLightRepository; private SceneRepository sceneRepository; - private StateRepository> stateRepository; + private StateRepository stateRepository; private DeviceService deviceService; @Autowired @@ -37,7 +37,7 @@ public class RegularLightController extends GuestEnabledController UserRepository userRepository, RegularLightRepository regularLightRepository, SceneRepository sceneRepository, - StateRepository> stateRepository, + StateRepository stateRepository, DeviceService deviceService) { super(userRepository, regularLightRepository); this.regularLightRepository = regularLightRepository; @@ -98,7 +98,7 @@ public class RegularLightController extends GuestEnabledController } @PostMapping("/{id}/state") - public State sceneBinding( + public State sceneBinding( @PathVariable("id") long deviceId, @RequestParam long sceneId, final Principal principal) @@ -107,7 +107,7 @@ public class RegularLightController extends GuestEnabledController regularLightRepository .findByIdAndUsername(deviceId, principal.getName()) .orElseThrow(NotFoundException::new); - State 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) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java index f2d0795..4db036f 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SceneController.java @@ -22,7 +22,7 @@ public class SceneController { @Autowired private SceneRepository sceneRepository; @Autowired private UserRepository userRepository; @Autowired private SceneService sceneService; - @Autowired private StateRepository> stateRepository; + @Autowired private StateRepository stateRepository; @GetMapping public List findAll( @@ -78,7 +78,7 @@ public class SceneController { } @PostMapping("/{id}/copyFrom/{copyId}") - public @ResponseBody List> copy( + public @ResponseBody List 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> getDevices(@PathVariable("sceneId") long sceneId) { - Iterable> states = stateRepository.findBySceneId(sceneId); + public List getStates(@PathVariable("sceneId") long sceneId) { + Iterable states = stateRepository.findBySceneId(sceneId); return toList(states); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java index d006879..781b46a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java @@ -26,7 +26,7 @@ public class SecurityCameraController { private final DeviceService deviceService; private final SecurityCameraRepository securityCameraService; private final SceneRepository sceneRepository; - private final StateRepository> stateRepository; + private final StateRepository stateRepository; private final CameraConfigurationService cameraConfigurationService; @Autowired @@ -34,7 +34,7 @@ public class SecurityCameraController { DeviceService deviceService, SecurityCameraRepository securityCameraService, SceneRepository sceneRepository, - StateRepository> stateRepository, + StateRepository stateRepository, CameraConfigurationService cameraConfigurationService) { this.deviceService = deviceService; this.securityCameraService = securityCameraService; @@ -80,7 +80,7 @@ public class SecurityCameraController { } @PostMapping("/{id}/state") - public State 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 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) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index 88a78f6..43d659b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -19,7 +19,7 @@ public class SmartPlugController { @Autowired private DeviceService deviceService; @Autowired private SmartPlugRepository smartPlugRepository; @Autowired private SceneRepository sceneRepository; - @Autowired private StateRepository> stateRepository; + @Autowired private StateRepository 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 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 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) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateController.java index ed6f30c..2fc9f18 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchableStateController.java @@ -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); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java index 0696822..72b4934 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ThermostatController.java @@ -21,7 +21,7 @@ public class ThermostatController { @Autowired private ThermostatRepository thermostatRepository; @Autowired private ThermostatPopulationService thermostatService; @Autowired private SceneRepository sceneRepository; - @Autowired private StateRepository> stateRepository; + @Autowired private StateRepository 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 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 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) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index bc16615..0f99d0d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -67,7 +67,7 @@ public abstract class Device { @OneToMany(mappedBy = "device", orphanRemoval = true) @GsonExclude @SocketGsonExclude - private Set> states; + private Set states; @Transient @GsonExclude private Long fromHostId = null; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java index 37f40e1..74b4479 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java @@ -65,15 +65,13 @@ public class Dimmable extends Switchable implements RangeTriggerable { intensity = on ? oldIntensity : 0; } - public void readStateAndSet(DimmableState state) { + public void readStateAndSet(DimmableState state) { setIntensity(state.getIntensity()); } - @Override - public State cloneState() { - final DimmableState newState = new DimmableState<>(); + public State cloneState() { + final DimmableState newState = new DimmableState(); newState.setDeviceId(getId()); - newState.setDevice(this); newState.setIntensity(getIntensity()); return newState; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java index e004c1f..c9cceec 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableState.java @@ -8,7 +8,11 @@ import lombok.Setter; /** Represent a state for an IDimmable device */ @Entity -public class DimmableState extends State { +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 extends State { @Override public void apply() { - getDevice().readStateAndSet(this); + ((Dimmable) getDevice()).readStateAndSet(this); } @Override - protected State copy() { - final DimmableState d = new DimmableState<>(); + protected DimmableState copy() { + final DimmableState d = new DimmableState(); d.setIntensity(intensity); return d; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateRepository.java index 00edb96..8cced24 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableStateRepository.java @@ -1,3 +1,3 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface DimmableStateRepository extends StateRepository> {} +public interface DimmableStateRepository extends StateRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java index ba1813e..4147fba 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java @@ -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 cloneState(); + public abstract State cloneState(); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java index 63cccb4..b9d80de 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Scene.java @@ -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> states = new HashSet<>(); + private Set 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> getStates() { - return states; - } - - public void setStates(Set> states) { - this.states = states; - } - - public Long getUserId() { - return userId; - } - - public void setUserId(Long userId) { - this.userId = userId; - } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java index 8637500..ae737ab 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/State.java @@ -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 { +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 copy(); + protected abstract State copy(); - public State copyToSceneId(Long sceneId) { - final State 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; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java index d2d1278..b3195b7 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/StateRepository.java @@ -5,7 +5,7 @@ import javax.transaction.Transactional; import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.query.Param; -public interface StateRepository> extends CrudRepository { +public interface StateRepository extends CrudRepository { @Transactional void deleteAllBySceneId(long roomId); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index a1a5366..f2d2c44 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -45,15 +45,13 @@ public abstract class Switchable extends OutputDevice { return inputs; } - public void readStateAndSet(SwitchableState state) { + public void readStateAndSet(SwitchableState state) { setOn(state.isOn()); } - @Override - public State cloneState() { - final SwitchableState newState = new SwitchableState<>(); + public State cloneState() { + final SwitchableState newState = new SwitchableState(); newState.setDeviceId(getId()); - newState.setDevice(this); newState.setOn(isOn()); return newState; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java index be21b6e..38307af 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableState.java @@ -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 extends State { +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 copy() { - final SwitchableState d = new SwitchableState<>(); + protected SwitchableState copy() { + final SwitchableState d = new SwitchableState(); d.setOn(on); return d; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableStateRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableStateRepository.java index 933ac6c..5c9850b 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableStateRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableStateRepository.java @@ -1,3 +1,3 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface SwitchableStateRepository extends StateRepository> {} +public interface SwitchableStateRepository extends StateRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java index 824ea7e..711a94d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/service/SceneService.java @@ -11,7 +11,7 @@ public class SceneService { private final DevicePopulationService devicePopulationService; private final DevicePropagationService devicePropagationService; - private final StateRepository> stateRepository; + private final StateRepository stateRepository; private final SceneRepository sceneRepository; public Scene findByValidatedId(Long id) { @@ -22,7 +22,7 @@ public class SceneService { public SceneService( DevicePopulationService devicePopulationService, DevicePropagationService devicePropagationService, - StateRepository> stateRepository, + StateRepository stateRepository, SceneRepository sceneRepository) { this.devicePopulationService = devicePopulationService; this.devicePropagationService = devicePropagationService; @@ -33,7 +33,7 @@ public class SceneService { private List copyStatesToDevices(Scene fromScene) { final List 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> copyStates(Scene to, Scene from) { - final ArrayList> states = new ArrayList<>(); - for (final State s : from.getStates()) { + public List copyStates(Scene to, Scene from) { + final ArrayList states = new ArrayList<>(); + for (final State s : from.getStates()) { states.add(stateRepository.save(s.copyToSceneId(to.getId()))); } return states;