Merge branch 'sonar-fix' into 'dev'

Degenerified states

See merge request sa4-2020/the-sanmarinoes/backend!139
This commit is contained in:
Claudio Maggioni 2020-05-12 18:37:08 +02:00
commit ba9001a9c8
21 changed files with 99 additions and 178 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,3 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 * Creates a State object initialized to point at this device and with values copied from this
* this device's state * 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.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import lombok.Data;
/** /**
* Represent a collection of state changes to devices even in different rooms but belonging to the * Represent a collection of state changes to devices even in different rooms but belonging to the
* same user * same user
*/ */
@Data
@Entity @Entity
public class Scene { public class Scene {
@ -30,7 +32,7 @@ public class Scene {
@OneToMany(mappedBy = "scene", orphanRemoval = true) @OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude @GsonExclude
private Set<State<?>> states = new HashSet<>(); private Set<State> states = new HashSet<>();
/** The user given name of this room (e.g. 'Master bedroom') */ /** The user given name of this room (e.g. 'Master bedroom') */
@Column(nullable = false) @Column(nullable = false)
@ -41,60 +43,4 @@ public class Scene {
/** Determines whether a guest can access this scene */ /** Determines whether a guest can access this scene */
@Column private boolean guestAccessEnabled; @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 ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import javax.persistence.*; import javax.persistence.*;
import lombok.Getter;
import lombok.Setter;
/** /**
* Represents instructions on how to change the state of a particular device. Many states (plus * Represents instructions on how to change the state of a particular device. Many states (plus
@ -11,93 +13,66 @@ import javax.persistence.*;
@Entity @Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"device_id", "scene_id"})}) @Table(uniqueConstraints = {@UniqueConstraint(columnNames = {"device_id", "scene_id"})})
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @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 @Id
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true) @Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
@Getter
private long id; 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 * The device this state belongs in, as a foreign key id. To use when updating and inserting
* from a REST call. * from a REST call.
*/ */
@Column(name = "device_id", nullable = false) @Column(name = "device_id", nullable = false)
@Getter
@Setter
private Long deviceId; private Long deviceId;
@ManyToOne @ManyToOne
@JoinColumn(name = "scene_id", updatable = false, insertable = false) @JoinColumn(name = "scene_id", updatable = false, insertable = false)
@GsonExclude @GsonExclude
@Getter
@Setter
private Scene scene; private Scene scene;
@Column(name = "scene_id", nullable = false) @Column(name = "scene_id", nullable = false)
@Getter
@Setter
private Long sceneId; 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. */ /** 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 */ /** 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) { public State copyToSceneId(Long sceneId) {
final State<D> s = copy(); final State s = copy();
s.setDeviceId(this.deviceId); s.setDeviceId(this.deviceId);
s.device = this.device;
s.setSceneId(sceneId); s.setSceneId(sceneId);
s.setScene(this.scene);
return s; 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 @PreRemove
public void removeDeviceAndScene() { public void removeDeviceAndScene() {
this.setScene(null);
this.setSceneId(null); this.setSceneId(null);
this.setScene(null);
this.setDevice(null);
this.setDeviceId(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.CrudRepository;
import org.springframework.data.repository.query.Param; 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 @Transactional
void deleteAllBySceneId(long roomId); void deleteAllBySceneId(long roomId);

View file

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

View file

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

View file

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