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

View file

@ -29,7 +29,7 @@ public class RangeTriggerController {
return rangeTriggerRepository.findAllByAutomationId(automationId);
}
private RangeTrigger<?> save(RangeTrigger newRL, RangeTriggerSaveRequest s) {
private RangeTrigger<?> save(RangeTrigger<?> newRL, RangeTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator());
@ -41,7 +41,7 @@ public class RangeTriggerController {
@PostMapping
public RangeTrigger<?> create(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new RangeTrigger(), booleanTriggerSaveRequest);
return save(new RangeTrigger<>(), booleanTriggerSaveRequest);
}
@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.models.ScenePriority;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ScenePriorityRepository;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -23,9 +24,10 @@ public class ScenePriorityController {
@Autowired ScenePriorityRepository scenePriorityRepository;
@GetMapping("/{id}")
public ScenePriority get(@PathVariable long id) throws NotFoundException {
return scenePriorityRepository.findById(id).orElseThrow(NotFoundException::new);
@GetMapping("/{automationId}")
public List<ScenePriority> getByAutomationId(@PathVariable long automationId)
throws NotFoundException {
return scenePriorityRepository.findAllByAutomationId(automationId);
}
private ScenePriority save(ScenePriority newRL, ScenePrioritySaveRequest s) {

View file

@ -17,6 +17,16 @@ public class Automation {
@ApiModelProperty(hidden = true)
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)
@GsonExclude
private Set<Trigger<?>> triggers = new HashSet<>();
@ -50,4 +60,20 @@ public class Automation {
public void setName(String 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;
import java.util.List;
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.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class BooleanTrigger<D extends Device & BooleanTriggerable> extends Trigger<D> {
@Column(name = "switchable_on")

View file

@ -1,6 +1,7 @@
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 java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.Max;
@ -23,6 +24,7 @@ public class Dimmable extends Switchable implements RangeTriggerable {
@ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH)
@GsonExclude
@SocketGsonExclude
private Set<Dimmer> dimmers;
/** 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;
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.Set;
import javax.persistence.*;
@ -15,6 +16,7 @@ public abstract class Dimmer extends InputDevice {
@ManyToMany(cascade = CascadeType.DETACH)
@GsonExclude
@SocketGsonExclude
@JoinTable(
name = "dimmer_dimmable",
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 javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.validation.constraints.NotNull;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D> {
@Override

View file

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

View file

@ -9,4 +9,6 @@ public interface ScenePriorityRepository extends CrudRepository<ScenePriority, L
List<ScenePriority> findAllBySceneId(@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;
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.Set;
import javax.persistence.*;
@ -11,6 +12,7 @@ public class Switch extends InputDevice implements BooleanTriggerable {
@ManyToMany(cascade = CascadeType.DETACH)
@GsonExclude
@SocketGsonExclude
@JoinTable(
name = "switch_switchable",
joinColumns = @JoinColumn(name = "switch_id"),

View file

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

View file

@ -1,9 +1,14 @@
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 java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
@ -34,10 +39,6 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
BigDecimal measured = this.getMeasuredTemperature();
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);
if (delta.abs().doubleValue() < 0.25) {
@ -85,9 +86,10 @@ public class Thermostat extends Switchable implements BooleanTriggerable {
@Column private boolean useExternalSensors = false;
// @OneToMany(mappedBy = "scene", orphanRemoval = true)
// @GsonExclude
// private Set<BooleanTrigger<? extends Device>> triggers = new HashSet<>()
@OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude
@SocketGsonExclude
private Set<BooleanTrigger<? extends Device>> triggers = new HashSet<>();
/** Creates a thermostat with a temperature sensor and its initial OFF state */
public Thermostat() {

View file

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