Added conditions in PUT automation/fast

This commit is contained in:
omenem 2020-05-08 17:44:30 +02:00
parent 09c08580ca
commit b72f527b0d
4 changed files with 81 additions and 27 deletions

View file

@ -28,6 +28,7 @@ public class AutomationController {
@Autowired private AutomationRepository automationRepository; @Autowired private AutomationRepository automationRepository;
@Autowired private ScenePriorityRepository sceneRepository; @Autowired private ScenePriorityRepository sceneRepository;
@Autowired private TriggerRepository<Trigger<?>> triggerRepository; @Autowired private TriggerRepository<Trigger<?>> triggerRepository;
@Autowired private ConditionRepository<Condition<?>> conditionRepository;
@Autowired private UserRepository userService; @Autowired private UserRepository userService;
@GetMapping @GetMapping
@ -87,6 +88,7 @@ public class AutomationController {
triggerRepository.deleteAllByAutomationId(a.getId()); triggerRepository.deleteAllByAutomationId(a.getId());
sceneRepository.deleteAllByAutomationId(a.getId()); sceneRepository.deleteAllByAutomationId(a.getId());
conditionRepository.deleteAllByAutomationId(a.getId());
Iterable<Trigger<?>> tt = Iterable<Trigger<?>> tt =
triggerRepository.saveAll( triggerRepository.saveAll(
@ -103,10 +105,20 @@ public class AutomationController {
.map(t -> t.setAutomationId(a.getId())) .map(t -> t.setAutomationId(a.getId()))
.collect(Collectors.toList())); .collect(Collectors.toList()));
Iterable<Condition<?>> cc =
conditionRepository.saveAll(
req.getConditions()
.stream()
.map(AutomationFastUpdateRequest.ConditionDTO::toModel)
.map(t -> t.setAutomationId(a.getId()))
.collect(Collectors.toList()));
a.getScenes().clear(); a.getScenes().clear();
a.getTriggers().clear(); a.getTriggers().clear();
a.getConditions().clear();
ss.forEach(t -> a.getScenes().add(t)); ss.forEach(t -> a.getScenes().add(t));
tt.forEach(t -> a.getTriggers().add(t)); tt.forEach(t -> a.getTriggers().add(t));
cc.forEach(t -> a.getConditions().add(t));
return a; return a;
} }

View file

@ -1,8 +1,13 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanTrigger;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Condition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeTrigger;
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.Thermostat;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Trigger;
import java.util.List; import java.util.List;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
@ -57,8 +62,58 @@ public class AutomationFastUpdateRequest {
} }
} }
public abstract class ConditionDTO {
@NotNull public long deviceId;
public abstract Condition<?> toModel();
}
public class BooleanConditionDTO extends ConditionDTO {
@NotNull public boolean on;
@Override
public Condition<?> toModel() {
BooleanCondition<?> t = new BooleanCondition<>();
t.setDeviceId(this.deviceId);
t.setOn(this.on);
return t;
}
}
public class RangeConditionDTO extends ConditionDTO {
@NotNull RangeCondition.Operator operator;
@NotNull double range;
@Override
public Condition<?> toModel() {
RangeCondition<?> t = new RangeCondition<>();
t.setDeviceId(this.deviceId);
t.setOperator(this.operator);
t.setRange(this.range);
return t;
}
}
public class ThermostatConditionDTO extends ConditionDTO {
@NotNull ThermostatCondition.Operator operator;
@NotNull private Thermostat.Mode mode;
@Override
public Condition<?> toModel() {
ThermostatCondition t = new ThermostatCondition();
t.setDeviceId(this.deviceId);
t.setOperator(this.operator);
t.setMode(this.mode);
return t;
}
}
@NotNull private List<ScenePriorityDTO> scenes; @NotNull private List<ScenePriorityDTO> scenes;
@NotNull private List<TriggerDTO> triggers; @NotNull private List<TriggerDTO> triggers;
@NotNull private List<ConditionDTO> conditions;
@NotNull private long id; @NotNull private long id;
@NotNull @NotEmpty private String name; @NotNull @NotEmpty private String name;
@ -94,4 +149,12 @@ public class AutomationFastUpdateRequest {
public void setId(long id) { public void setId(long id) {
this.id = id; this.id = id;
} }
public List<ConditionDTO> getConditions() {
return conditions;
}
public void setConditions(List<ConditionDTO> conditions) {
this.conditions = conditions;
}
} }

View file

@ -31,16 +31,6 @@ public abstract class Condition<D extends Device> {
@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;
@ManyToOne(targetEntity = Device.class) @ManyToOne(targetEntity = Device.class)
@JoinColumn(name = "device_id", updatable = false, insertable = false) @JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude @GsonExclude
@ -73,22 +63,6 @@ public abstract class Condition<D extends Device> {
this.id = id; this.id = id;
} }
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;
}
public D getDevice() { public D getDevice() {
return device; return device;
} }
@ -117,7 +91,8 @@ public abstract class Condition<D extends Device> {
return automationId; return automationId;
} }
public void setAutomationId(Long automationId) { public Condition<D> setAutomationId(Long automationId) {
this.automationId = automationId; this.automationId = automationId;
return this;
} }
} }

View file

@ -1,6 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List; import java.util.List;
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;
@ -9,4 +10,7 @@ public interface ConditionRepository<T extends Condition<?>> extends CrudReposit
List<T> findAllByDeviceId(@Param("deviceId") long deviceId); List<T> findAllByDeviceId(@Param("deviceId") long deviceId);
List<T> findAllByAutomationId(@Param("automationId") long automationId); List<T> findAllByAutomationId(@Param("automationId") long automationId);
@Transactional
void deleteAllByAutomationId(@Param("automationId") long automationId);
} }