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

This commit is contained in:
Jacob Salvi 2020-04-26 13:32:35 +02:00
commit e3d7908537
18 changed files with 483 additions and 9 deletions

View file

@ -0,0 +1,84 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.AutomationSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
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 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;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/automation")
public class AutomationController {
@Autowired private AutomationRepository automationRepository;
@Autowired private SceneRepository sceneRepository;
@Autowired private ScenePriorityRepository scenePriorityRepository;
@GetMapping
public List<Automation> getAll(
@RequestParam(value = "hostId", required = false) Long hostId, final Principal user)
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());
}
@GetMapping("/{id}")
public Automation get(@PathVariable long id) throws NotFoundException {
return automationRepository.findById(id).orElseThrow(NotFoundException::new);
}
private Automation save(Automation newRL, AutomationSaveRequest s) {
newRL.setName(s.getName());
return automationRepository.save(newRL);
}
@PostMapping
public Automation create(@Valid @RequestBody AutomationSaveRequest automationSaveRequest) {
return save(new Automation(), automationSaveRequest);
}
@PutMapping
public Automation update(@Valid @RequestBody AutomationSaveRequest automation)
throws NotFoundException {
return save(
automationRepository
.findById(automation.getId())
.orElseThrow(NotFoundException::new),
automation);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable long id) {
automationRepository.deleteById(id);
}
}

View file

@ -0,0 +1,61 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTriggerRepository;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/booleanTrigger")
public class BooleanTriggerController {
@Autowired BooleanTriggerRepository booleanTriggerRepository;
@GetMapping("/{automationId}")
public List<BooleanTrigger<?>> getAll(@PathVariable long automationId) {
return booleanTriggerRepository.findAllByAutomationId(automationId);
}
private BooleanTrigger<?> save(BooleanTrigger<?> newRL, BooleanTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId());
newRL.setOn(s.isOn());
return booleanTriggerRepository.save(newRL);
}
@PostMapping
public BooleanTrigger<?> create(
@Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new BooleanTrigger<>(), booleanTriggerSaveRequest);
}
@PutMapping
public BooleanTrigger<?> update(
@Valid @RequestBody BooleanTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException {
return save(
booleanTriggerRepository
.findById(booleanTriggerSaveRequest.getId())
.orElseThrow(NotFoundException::new),
booleanTriggerSaveRequest);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable long id) {
booleanTriggerRepository.deleteById(id);
}
}

View file

@ -0,0 +1,62 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeTriggerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTriggerRepository;
import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/rangeTrigger")
public class RangeTriggerController {
@Autowired RangeTriggerRepository rangeTriggerRepository;
@GetMapping("/{automationId}")
public List<RangeTrigger<?>> getAll(@PathVariable long automationId) {
return rangeTriggerRepository.findAllByAutomationId(automationId);
}
private RangeTrigger<?> save(RangeTrigger newRL, RangeTriggerSaveRequest s) {
newRL.setDeviceId(s.getDeviceId());
newRL.setAutomationId(s.getAutomationId());
newRL.setOperator(s.getOperator());
newRL.setRange(s.getRange());
return rangeTriggerRepository.save(newRL);
}
@PostMapping
public RangeTrigger<?> create(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest) {
return save(new RangeTrigger(), booleanTriggerSaveRequest);
}
@PutMapping
public RangeTrigger<?> update(
@Valid @RequestBody RangeTriggerSaveRequest booleanTriggerSaveRequest)
throws NotFoundException {
return save(
rangeTriggerRepository
.findById(booleanTriggerSaveRequest.getId())
.orElseThrow(NotFoundException::new),
booleanTriggerSaveRequest);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable long id) {
rangeTriggerRepository.deleteById(id);
}
}

View file

@ -0,0 +1,60 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
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 javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
@RequestMapping("/scenePriority")
public class ScenePriorityController {
@Autowired ScenePriorityRepository scenePriorityRepository;
@GetMapping("/{id}")
public ScenePriority get(@PathVariable long id) throws NotFoundException {
return scenePriorityRepository.findById(id).orElseThrow(NotFoundException::new);
}
private ScenePriority save(ScenePriority newRL, ScenePrioritySaveRequest s) {
newRL.setPriority(s.getPriority());
newRL.setAutomationId(s.getAutomationId());
newRL.setSceneId(s.getSceneId());
return scenePriorityRepository.save(newRL);
}
@PostMapping
public ScenePriority create(
@Valid @RequestBody ScenePrioritySaveRequest scenePrioritySaveRequest) {
return save(new ScenePriority(), scenePrioritySaveRequest);
}
@PutMapping
public ScenePriority update(
@Valid @RequestBody ScenePrioritySaveRequest scenePrioritySaveRequest)
throws NotFoundException {
return save(
scenePriorityRepository
.findById(scenePrioritySaveRequest.getSceneId())
.orElseThrow(NotFoundException::new),
scenePrioritySaveRequest);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable long id) {
scenePriorityRepository.deleteBySceneId(id);
}
}

View file

@ -0,0 +1,23 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
public class AutomationSaveRequest {
private long id;
@NotNull @NotEmpty private String name;
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View file

@ -0,0 +1,42 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotNull;
public class BooleanTriggerSaveRequest {
private long id;
@NotNull private Long deviceId;
@NotNull private Long automationId;
private boolean on;
public long getId() {
return id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -0,0 +1,57 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
import javax.validation.constraints.NotNull;
public class RangeTriggerSaveRequest {
private long id;
@NotNull private Long deviceId;
@NotNull private Long automationId;
@NotNull private RangeTrigger.Operator operator;
@NotNull private double range;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public RangeTrigger.Operator getOperator() {
return operator;
}
public void setOperator(RangeTrigger.Operator operator) {
this.operator = operator;
}
public double getRange() {
return range;
}
public void setRange(Double range) {
this.range = range;
}
}

View file

@ -0,0 +1,38 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class ScenePrioritySaveRequest {
@NotNull private Long automationId;
@Min(0)
private Integer priority;
@NotNull private Long sceneId;
public Long getAutomationId() {
return automationId;
}
public void setAutomationId(Long automationId) {
this.automationId = automationId;
}
public Integer getPriority() {
return priority;
}
public void setPriority(Integer priority) {
this.priority = priority;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
}

View file

@ -5,6 +5,8 @@ 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 javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
@Entity @Entity
public class Automation { public class Automation {
@ -23,6 +25,8 @@ public class Automation {
@GsonExclude @GsonExclude
private Set<ScenePriority> scenes = new HashSet<>(); private Set<ScenePriority> scenes = new HashSet<>();
@NotNull @NotEmpty private String name;
public long getId() { public long getId() {
return id; return id;
} }
@ -38,4 +42,12 @@ public class Automation {
public Set<ScenePriority> getScenes() { public Set<ScenePriority> getScenes() {
return scenes; return scenes;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
} }

View file

@ -1,4 +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.query.Param;
public interface BooleanTriggerRepository public interface BooleanTriggerRepository
extends TriggerRepository<BooleanTrigger<? extends Device>> {} extends TriggerRepository<BooleanTrigger<? extends Device>> {
List<BooleanTrigger<?>> findAllByAutomationId(@Param("automationId") long automationId);
}

View file

@ -38,7 +38,7 @@ public abstract class Device {
@OneToMany(mappedBy = "device", orphanRemoval = true) @OneToMany(mappedBy = "device", orphanRemoval = true)
@GsonExclude @GsonExclude
// @SocketGsonExclude @SocketGsonExclude
private Set<Trigger<? extends Device>> triggers = new HashSet<>(); private Set<Trigger<? extends Device>> triggers = new HashSet<>();
/** /**

View file

@ -14,6 +14,10 @@ import org.springframework.data.repository.query.Param;
public interface DeviceRepository<T extends Device> extends CrudRepository<T, Long> { public interface DeviceRepository<T extends Device> extends CrudRepository<T, Long> {
List<T> findByRoomId(@Param("roomId") long roomId); List<T> findByRoomId(@Param("roomId") long roomId);
@Query(
"SELECT COUNT(d) FROM Device d JOIN d.room r JOIN r.user u WHERE d.name = ?1 AND u.username = ?2")
Integer findDuplicates(String name, String username);
/** /**
* Finds devices by their id and a username * Finds devices by their id and a username
* *

View file

@ -29,7 +29,7 @@ public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D
throw new IllegalStateException(); throw new IllegalStateException();
} }
enum Operator { public enum Operator {
@SerializedName("EQUAL") @SerializedName("EQUAL")
EQUAL, EQUAL,
@SerializedName("LESS") @SerializedName("LESS")
@ -62,7 +62,7 @@ public class RangeTrigger<D extends Device & RangeTriggerable> extends Trigger<D
return range; return range;
} }
public void setRange(Float range) { public void setRange(Double range) {
this.range = range; this.range = range;
} }
} }

View file

@ -1,3 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface RangeTriggerRepository extends TriggerRepository<RangeTrigger<? extends Device>> {} import java.util.List;
import org.springframework.data.repository.query.Param;
public interface RangeTriggerRepository extends TriggerRepository<RangeTrigger<? extends Device>> {
List<RangeTrigger<?>> findAllByAutomationId(@Param("automationId") long automationId);
}

View file

@ -1,5 +1,12 @@
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 ScenePriorityRepository extends CrudRepository<ScenePriority, Long> {} public interface ScenePriorityRepository extends CrudRepository<ScenePriority, Long> {
List<ScenePriority> findAllBySceneId(@Param("sceneId") long sceneId);
void deleteBySceneId(@Param("sceneId") long sceneId);
}

View file

@ -29,7 +29,15 @@ public class DeviceService {
roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new); roomRepository.findByIdAndUsername(roomId, username).orElseThrow(NotFoundException::new);
} }
public void triggerTriggers(Device device) { private void renameIfDuplicate(Device toCreate, String username) {
while (deviceRepository.findDuplicates(toCreate.getName(), username)
- (toCreate.getId() <= 0 ? 0 : 1)
> 0) {
toCreate.setName(toCreate.getName() + " (new)");
}
}
private void triggerTriggers(Device device) {
final long deviceId = device.getId(); final long deviceId = device.getId();
@ -107,6 +115,8 @@ public class DeviceService {
final User currentUser = userRepository.findByUsername(guestUsername); final User currentUser = userRepository.findByUsername(guestUsername);
final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new); final User host = userRepository.findById(hostId).orElseThrow(NotFoundException::new);
if (!host.getGuests().contains(currentUser)) throw new NotFoundException(); if (!host.getGuests().contains(currentUser)) throw new NotFoundException();
renameIfDuplicate(device, host.getUsername());
device = deviceRepository.save(device);
final Set<User> guests = Set.copyOf(host.getGuests()); final Set<User> guests = Set.copyOf(host.getGuests());
// We're telling the host that a guest has modified a device. Therefore, fromGuest becomes // We're telling the host that a guest has modified a device. Therefore, fromGuest becomes
@ -145,6 +155,7 @@ public class DeviceService {
public <T extends Device> List<T> saveAllAsOwner( public <T extends Device> List<T> saveAllAsOwner(
Iterable<T> devices, String username, boolean fromScene) { Iterable<T> devices, String username, boolean fromScene) {
devices.forEach(d -> renameIfDuplicate(d, username));
devices = deviceRepository.saveAll(devices); devices = deviceRepository.saveAll(devices);
devices.forEach((d) -> propagateUpdateAsOwner(d, username)); devices.forEach((d) -> propagateUpdateAsOwner(d, username));
@ -156,6 +167,7 @@ public class DeviceService {
} }
public <T extends Device> T saveAsOwner(T device, String username, boolean fromScene) { public <T extends Device> T saveAsOwner(T device, String username, boolean fromScene) {
renameIfDuplicate(device, username);
device = deviceRepository.save(device); device = deviceRepository.save(device);
propagateUpdateAsOwner(device, username); propagateUpdateAsOwner(device, username);

View file

@ -27,7 +27,7 @@ spring.mail.properties.mail.smtp.writetimeout=5000
email.registrationSubject=Complete your SmartHut.sm registration email.registrationSubject=Complete your SmartHut.sm registration
email.registration=To confirm your registration, please click here: email.registration=To confirm your registration, please click here:
email.registrationPath=http://localhost:8080/register/confirm-account?token= email.registrationPath=http://localhost:8080/register/confirm-account?token=
email.registrationRedirect=http://localhost:3000 email.registrationRedirect=http://localhost:3000/login
email.resetpasswordSubject=SmartHut.sm password reset email.resetpasswordSubject=SmartHut.sm password reset
email.resetpassword=To reset your password, please click here: email.resetpassword=To reset your password, please click here:

View file

@ -32,7 +32,7 @@ spring.mail.properties.mail.smtp.writetimeout=5000
email.registrationSubject=Complete your SmartHut.sm registration email.registrationSubject=Complete your SmartHut.sm registration
email.registration=To confirm your registration, please click here: email.registration=To confirm your registration, please click here:
email.registrationPath=${BACKEND_URL}/register/confirm-account?token= email.registrationPath=${BACKEND_URL}/register/confirm-account?token=
email.registrationRedirect=${FRONTEND_URL} email.registrationRedirect=${FRONTEND_URL}/login
# Password reset email properties # Password reset email properties