Merge remote-tracking branch 'origin/dev' into dev

# Conflicts:
#	src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Thermostat.java
This commit is contained in:
Jacob Salvi 2020-04-27 17:58:20 +02:00
commit 57fdb8eacd
15 changed files with 87 additions and 44 deletions

View file

@ -6,10 +6,9 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Automation;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.AutomationRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SceneRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.UserRepository;
import java.security.Principal; import java.security.Principal;
import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -31,24 +30,15 @@ public class AutomationController {
@Autowired private AutomationRepository automationRepository; @Autowired private AutomationRepository automationRepository;
@Autowired private SceneRepository sceneRepository; @Autowired private SceneRepository sceneRepository;
@Autowired private ScenePriorityRepository scenePriorityRepository; @Autowired private ScenePriorityRepository scenePriorityRepository;
@Autowired private UserRepository userService;
@GetMapping @GetMapping
public List<Automation> getAll( public List<Automation> getAll(
@RequestParam(value = "hostId", required = false) Long hostId, final Principal user) @RequestParam(value = "hostId", required = false) Long hostId,
final Principal principal)
throws NotFoundException { throws NotFoundException {
return sceneRepository final Long userId = userService.findByUsername(principal.getName()).getId();
.findByUsername(user.getName()) return automationRepository.findAllByUserId(userId);
.stream()
.map(s -> scenePriorityRepository.findAllBySceneId(s.getId()))
.flatMap(Collection::stream)
.distinct()
.map(
t ->
automationRepository
.findById(t.getAutomationId())
.orElseThrow(IllegalStateException::new))
.distinct()
.collect(Collectors.toList());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -56,25 +46,31 @@ public class AutomationController {
return automationRepository.findById(id).orElseThrow(NotFoundException::new); return automationRepository.findById(id).orElseThrow(NotFoundException::new);
} }
private Automation save(Automation newRL, AutomationSaveRequest s) { private Automation save(Automation newRL, AutomationSaveRequest s, Principal principal) {
final Long userId = userService.findByUsername(principal.getName()).getId();
newRL.setName(s.getName()); newRL.setName(s.getName());
newRL.setUserId(userId);
return automationRepository.save(newRL); return automationRepository.save(newRL);
} }
@PostMapping @PostMapping
public Automation create(@Valid @RequestBody AutomationSaveRequest automationSaveRequest) { public Automation create(
return save(new Automation(), automationSaveRequest); @Valid @RequestBody AutomationSaveRequest automationSaveRequest, Principal principal) {
return save(new Automation(), automationSaveRequest, principal);
} }
@PutMapping @PutMapping
public Automation update(@Valid @RequestBody AutomationSaveRequest automation) public Automation update(
@Valid @RequestBody AutomationSaveRequest automation, Principal principal)
throws NotFoundException { throws NotFoundException {
return save( return save(
automationRepository automationRepository
.findById(automation.getId()) .findById(automation.getId())
.orElseThrow(NotFoundException::new), .orElseThrow(NotFoundException::new),
automation); automation,
principal);
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View file

@ -29,7 +29,7 @@ public class RangeTriggerController {
return rangeTriggerRepository.findAllByAutomationId(automationId); return rangeTriggerRepository.findAllByAutomationId(automationId);
} }
private RangeTrigger<?> save(RangeTrigger newRL, RangeTriggerSaveRequest s) { private RangeTrigger<?> save(RangeTrigger<?> newRL, RangeTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId()); newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId()); newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator()); newRL.setOperator(s.getOperator());
@ -41,7 +41,7 @@ public class RangeTriggerController {
@PostMapping @PostMapping
public RangeTrigger<?> create( public RangeTrigger<?> create(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) { @Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new RangeTrigger(), booleanTriggerSaveRequest); return save(new RangeTrigger<>(), booleanTriggerSaveRequest);
} }
@PutMapping @PutMapping

View file

@ -4,6 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ScenePrioritySaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriority;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -23,9 +24,10 @@ public class ScenePriorityController {
@Autowired ScenePriorityRepository scenePriorityRepository; @Autowired ScenePriorityRepository scenePriorityRepository;
@GetMapping("/{id}") @GetMapping("/{automationId}")
public ScenePriority get(@PathVariable long id) throws NotFoundException { public List<ScenePriority> getByAutomationId(@PathVariable long automationId)
return scenePriorityRepository.findById(id).orElseThrow(NotFoundException::new); throws NotFoundException {
return scenePriorityRepository.findAllByAutomationId(automationId);
} }
private ScenePriority save(ScenePriority newRL, ScenePrioritySaveRequest s) { private ScenePriority save(ScenePriority newRL, ScenePrioritySaveRequest s) {

View file

@ -17,6 +17,16 @@ public class Automation {
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
private long id; private long id;
@ManyToOne
@JoinColumn(name = "user_id", updatable = false, insertable = false)
@GsonExclude
private User user;
@NotNull
@Column(name = "user_id", nullable = false)
@GsonExclude
private Long userId;
@OneToMany(mappedBy = "automation", orphanRemoval = true) @OneToMany(mappedBy = "automation", orphanRemoval = true)
@GsonExclude @GsonExclude
private Set<Trigger<?>> triggers = new HashSet<>(); private Set<Trigger<?>> triggers = new HashSet<>();
@ -50,4 +60,20 @@ public class Automation {
public void setName(String name) { public void setName(String name) {
this.name = name; this.name = name;
} }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
} }

View file

@ -1,5 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
public interface AutomationRepository extends CrudRepository<Automation, Long> {} public interface AutomationRepository extends CrudRepository<Automation, Long> {
List<Automation> findAllByUserId(@Param("userId") long userId);
}

View file

@ -2,11 +2,8 @@ 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 javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity @Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class BooleanTrigger<D extends Device & BooleanTriggerable> extends Trigger<D> { public class BooleanTrigger<D extends Device & BooleanTriggerable> extends Trigger<D> {
@Column(name = "switchable_on") @Column(name = "switchable_on")

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
@ -23,6 +24,7 @@ public class Dimmable extends Switchable implements RangeTriggerable {
@ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH)
@GsonExclude @GsonExclude
@SocketGsonExclude
private Set<Dimmer> dimmers; private Set<Dimmer> dimmers;
/** The light intensity value. Goes from 0 (off) to 100 (on) */ /** The light intensity value. Goes from 0 (off) to 100 (on) */

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
@ -15,6 +16,7 @@ public abstract class Dimmer extends InputDevice {
@ManyToMany(cascade = CascadeType.DETACH) @ManyToMany(cascade = CascadeType.DETACH)
@GsonExclude @GsonExclude
@SocketGsonExclude
@JoinTable( @JoinTable(
name = "dimmer_dimmable", name = "dimmer_dimmable",
joinColumns = @JoinColumn(name = "dimmer_id"), joinColumns = @JoinColumn(name = "dimmer_id"),

View file

@ -3,12 +3,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@Entity @Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D> { public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D> {
@Override @Override

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.JoinColumn; import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
@ -13,6 +16,12 @@ import javax.validation.constraints.NotNull;
@Entity @Entity
public class ScenePriority { public class ScenePriority {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true)
private long id;
@ManyToOne @ManyToOne
@JoinColumn(name = "automation_id", updatable = false, insertable = false) @JoinColumn(name = "automation_id", updatable = false, insertable = false)
@GsonExclude @GsonExclude
@ -32,8 +41,7 @@ public class ScenePriority {
@GsonExclude @GsonExclude
private Scene scene; private Scene scene;
@Id @Column(name = "scene_id", nullable = false, updatable = false)
@Column(name = "scene_id", nullable = false, updatable = false, unique = true)
@NotNull @NotNull
private Long sceneId; private Long sceneId;

View file

@ -9,4 +9,6 @@ public interface ScenePriorityRepository extends CrudRepository<ScenePriority, L
List<ScenePriority> findAllBySceneId(@Param("sceneId") long sceneId); List<ScenePriority> findAllBySceneId(@Param("sceneId") long sceneId);
void deleteBySceneId(@Param("sceneId") long sceneId); void deleteBySceneId(@Param("sceneId") long sceneId);
List<ScenePriority> findAllByAutomationId(@Param("automationId") long automationId);
} }

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
@ -11,6 +12,7 @@ public class Switch extends InputDevice implements BooleanTriggerable {
@ManyToMany(cascade = CascadeType.DETACH) @ManyToMany(cascade = CascadeType.DETACH)
@GsonExclude @GsonExclude
@SocketGsonExclude
@JoinTable( @JoinTable(
name = "switch_switchable", name = "switch_switchable",
joinColumns = @JoinColumn(name = "switch_id"), joinColumns = @JoinColumn(name = "switch_id"),

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 ch.usi.inf.sa4.sanmarinoes.smarthut.config.SocketGsonExclude;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
@ -15,6 +16,7 @@ public abstract class Switchable extends OutputDevice {
@ManyToMany(mappedBy = "switchables", cascade = CascadeType.DETACH) @ManyToMany(mappedBy = "switchables", cascade = CascadeType.DETACH)
@GsonExclude @GsonExclude
@SocketGsonExclude
private Set<Switch> inputs = new HashSet<>(); private Set<Switch> inputs = new HashSet<>();
protected Switchable(String kind) { protected Switchable(String kind) {

View file

@ -1,9 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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.SocketGsonExclude;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -34,10 +39,6 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
BigDecimal measured = this.getMeasuredTemperature(); BigDecimal measured = this.getMeasuredTemperature();
BigDecimal target = this.getTargetTemperature(); BigDecimal target = this.getTargetTemperature();
System.out.println("measured is");
// System.out.println(measured.getClass().getName());
System.out.println("target is");
System.out.println(target.getClass().getName());
BigDecimal delta = target.subtract(measured); BigDecimal delta = target.subtract(measured);
if (delta.abs().doubleValue() < 0.25) { if (delta.abs().doubleValue() < 0.25) {
@ -85,9 +86,10 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
@Column private boolean useExternalSensors = false; @Column private boolean useExternalSensors = false;
// @OneToMany(mappedBy = "scene", orphanRemoval = true) @OneToMany(mappedBy = "scene", orphanRemoval = true)
// @GsonExclude @GsonExclude
// private Set<BooleanTrigger<? extends Device>> triggers = new HashSet<>() @SocketGsonExclude
private Set<BooleanTrigger<? extends Device>> triggers = new HashSet<>();
/** Creates a thermostat with a temperature sensor and its initial OFF state */ /** Creates a thermostat with a temperature sensor and its initial OFF state */
public Thermostat() { public Thermostat() {

View file

@ -15,7 +15,7 @@ import javax.persistence.PreRemove;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@Entity @Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Trigger<D extends Device> { public abstract class Trigger<D extends Device> {
public abstract boolean triggered(); public abstract boolean triggered();
@ -26,7 +26,7 @@ public abstract class Trigger<D extends Device> {
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
private long id; private long id;
@ManyToOne(targetEntity = OutputDevice.class) @ManyToOne(targetEntity = Device.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false) @JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude @GsonExclude
private D device; private D device;