Added controllers repos and dtos
This commit is contained in:
parent
1f5f948e23
commit
1bde4344be
13 changed files with 386 additions and 22 deletions
|
@ -0,0 +1,61 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.BooleanConditionSaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanCondition;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.BooleanConditionRepository;
|
||||||
|
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("/booleanCondition")
|
||||||
|
public class BooleanConditionController {
|
||||||
|
|
||||||
|
@Autowired BooleanConditionRepository booleanConditionRepository;
|
||||||
|
|
||||||
|
@GetMapping("/{automationId}")
|
||||||
|
public List<BooleanCondition<?>> getAll(@PathVariable long automationId) {
|
||||||
|
return booleanConditionRepository.findAllByAutomationId(automationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private BooleanCondition<?> save(BooleanCondition<?> newRL, BooleanConditionSaveRequest s) {
|
||||||
|
newRL.setDeviceId(s.getDeviceId());
|
||||||
|
newRL.setAutomationId(s.getAutomationId());
|
||||||
|
newRL.setOn(s.isOn());
|
||||||
|
|
||||||
|
return booleanConditionRepository.save(newRL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public BooleanCondition<?> create(
|
||||||
|
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest) {
|
||||||
|
return save(new BooleanCondition<>(), booleanTriggerSaveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
public BooleanCondition<?> update(
|
||||||
|
@Valid @RequestBody BooleanConditionSaveRequest booleanTriggerSaveRequest)
|
||||||
|
throws NotFoundException {
|
||||||
|
return save(
|
||||||
|
booleanConditionRepository
|
||||||
|
.findById(booleanTriggerSaveRequest.getId())
|
||||||
|
.orElseThrow(NotFoundException::new),
|
||||||
|
booleanTriggerSaveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void delete(@PathVariable long id) {
|
||||||
|
booleanConditionRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RangeConditionSaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeConditionRepository;
|
||||||
|
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("/rangeCondition")
|
||||||
|
public class RangeConditionController {
|
||||||
|
|
||||||
|
@Autowired RangeConditionRepository rangeConditionRepository;
|
||||||
|
|
||||||
|
@GetMapping("/{automationId}")
|
||||||
|
public List<RangeCondition<?>> getAll(@PathVariable long automationId) {
|
||||||
|
return rangeConditionRepository.findAllByAutomationId(automationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private RangeCondition<?> save(RangeCondition<?> newRL, RangeConditionSaveRequest s) {
|
||||||
|
newRL.setDeviceId(s.getDeviceId());
|
||||||
|
newRL.setAutomationId(s.getAutomationId());
|
||||||
|
newRL.setOperator(s.getOperator());
|
||||||
|
newRL.setRange(s.getRange());
|
||||||
|
|
||||||
|
return rangeConditionRepository.save(newRL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public RangeCondition<?> create(
|
||||||
|
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest) {
|
||||||
|
return save(new RangeCondition<>(), booleanTriggerSaveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
public RangeCondition<?> update(
|
||||||
|
@Valid @RequestBody RangeConditionSaveRequest booleanTriggerSaveRequest)
|
||||||
|
throws NotFoundException {
|
||||||
|
return save(
|
||||||
|
rangeConditionRepository
|
||||||
|
.findById(booleanTriggerSaveRequest.getId())
|
||||||
|
.orElseThrow(NotFoundException::new),
|
||||||
|
booleanTriggerSaveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void delete(@PathVariable long id) {
|
||||||
|
rangeConditionRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ThermostatConditionSaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatConditionRepository;
|
||||||
|
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("/thermostatCondition")
|
||||||
|
public class ThermostatConditionController {
|
||||||
|
|
||||||
|
@Autowired ThermostatConditionRepository thermostatConditionRepository;
|
||||||
|
|
||||||
|
@GetMapping("/{automationId}")
|
||||||
|
public List<ThermostatCondition> getAll(@PathVariable long automationId) {
|
||||||
|
return thermostatConditionRepository.findAllByAutomationId(automationId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ThermostatCondition save(ThermostatCondition newRL, ThermostatConditionSaveRequest s) {
|
||||||
|
newRL.setDeviceId(s.getDeviceId());
|
||||||
|
newRL.setAutomationId(s.getAutomationId());
|
||||||
|
newRL.setOperator(s.getOperator());
|
||||||
|
newRL.setMode(s.getMode());
|
||||||
|
|
||||||
|
return thermostatConditionRepository.save(newRL);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ThermostatCondition create(
|
||||||
|
@Valid @RequestBody ThermostatConditionSaveRequest booleanTriggerSaveRequest) {
|
||||||
|
return save(new ThermostatCondition(), booleanTriggerSaveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
public ThermostatCondition update(
|
||||||
|
@Valid @RequestBody ThermostatConditionSaveRequest booleanTriggerSaveRequest)
|
||||||
|
throws NotFoundException {
|
||||||
|
return save(
|
||||||
|
thermostatConditionRepository
|
||||||
|
.findById(booleanTriggerSaveRequest.getId())
|
||||||
|
.orElseThrow(NotFoundException::new),
|
||||||
|
booleanTriggerSaveRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void delete(@PathVariable long id) {
|
||||||
|
thermostatConditionRepository.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class BooleanConditionSaveRequest {
|
||||||
|
|
||||||
|
@NotNull private long id;
|
||||||
|
|
||||||
|
@NotNull private Long deviceId;
|
||||||
|
|
||||||
|
@NotNull private Long automationId;
|
||||||
|
|
||||||
|
@NotNull 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,54 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RangeCondition.Operator;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class RangeConditionSaveRequest {
|
||||||
|
|
||||||
|
@NotNull private long id;
|
||||||
|
|
||||||
|
@NotNull private Long deviceId;
|
||||||
|
|
||||||
|
@NotNull private Long automationId;
|
||||||
|
|
||||||
|
@NotNull private RangeCondition.Operator operator;
|
||||||
|
|
||||||
|
@NotNull private double range;
|
||||||
|
|
||||||
|
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 Operator getOperator() {
|
||||||
|
return operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperator(Operator operator) {
|
||||||
|
this.operator = operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getRange() {
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRange(double range) {
|
||||||
|
this.range = range;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Thermostat.Mode;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ThermostatCondition.Operator;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
|
public class ThermostatConditionSaveRequest {
|
||||||
|
|
||||||
|
@NotNull private long id;
|
||||||
|
|
||||||
|
@NotNull private Long deviceId;
|
||||||
|
|
||||||
|
@NotNull private Long automationId;
|
||||||
|
|
||||||
|
@NotNull private ThermostatCondition.Operator operator;
|
||||||
|
|
||||||
|
@NotNull private Thermostat.Mode mode;
|
||||||
|
|
||||||
|
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 Operator getOperator() {
|
||||||
|
return operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOperator(Operator operator) {
|
||||||
|
this.operator = operator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Mode getMode() {
|
||||||
|
return mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMode(Mode mode) {
|
||||||
|
this.mode = mode;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
public interface BooleanConditionRepository
|
||||||
|
extends ConditionRepository<BooleanCondition<? extends Device>> {
|
||||||
|
|
||||||
|
List<BooleanCondition<?>> findAllByAutomationId(@Param("automationId") long automationId);
|
||||||
|
}
|
|
@ -41,10 +41,6 @@ public abstract class Condition<D extends Device> {
|
||||||
@GsonExclude
|
@GsonExclude
|
||||||
private Long userId;
|
private Long userId;
|
||||||
|
|
||||||
@NotNull
|
|
||||||
@Column(nullable = false)
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
@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
|
||||||
|
@ -93,14 +89,6 @@ public abstract class Condition<D extends Device> {
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public D getDevice() {
|
public D getDevice() {
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +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 ConditionRepository<T extends Condition<?>> extends CrudRepository<T, Long> {
|
||||||
|
|
||||||
|
List<T> findAllByDeviceId(@Param("deviceId") long deviceId);
|
||||||
|
}
|
|
@ -9,8 +9,8 @@ import javax.validation.constraints.NotNull;
|
||||||
@Entity
|
@Entity
|
||||||
public class RangeCondition<D extends Device & RangeTriggerable> extends Condition<D> {
|
public class RangeCondition<D extends Device & RangeTriggerable> extends Condition<D> {
|
||||||
|
|
||||||
protected RangeCondition(String kind) {
|
public RangeCondition() {
|
||||||
super(kind);
|
super("booleanCondition");
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Operator {
|
public enum Operator {
|
||||||
|
@ -28,17 +28,17 @@ public class RangeCondition<D extends Device & RangeTriggerable> extends Conditi
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private RangeTrigger.Operator operator;
|
private RangeCondition.Operator operator;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private double range;
|
private double range;
|
||||||
|
|
||||||
public RangeTrigger.Operator getOperator() {
|
public RangeCondition.Operator getOperator() {
|
||||||
return operator;
|
return operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOperator(RangeTrigger.Operator operator) {
|
public void setOperator(RangeCondition.Operator operator) {
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
public interface RangeConditionRepository
|
||||||
|
extends ConditionRepository<RangeCondition<? extends Device>> {
|
||||||
|
|
||||||
|
List<RangeCondition<?>> findAllByAutomationId(@Param("automationId") long automationId);
|
||||||
|
}
|
|
@ -7,8 +7,8 @@ import javax.persistence.Entity;
|
||||||
@Entity
|
@Entity
|
||||||
public class ThermostatCondition extends Condition<Thermostat> {
|
public class ThermostatCondition extends Condition<Thermostat> {
|
||||||
|
|
||||||
protected ThermostatCondition(String kind) {
|
public ThermostatCondition() {
|
||||||
super(kind);
|
super("thermostatCondition");
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Operator {
|
public enum Operator {
|
||||||
|
@ -32,12 +32,12 @@ public class ThermostatCondition extends Condition<Thermostat> {
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Thermostat.Mode getState() {
|
public Thermostat.Mode getMode() {
|
||||||
return mode;
|
return mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setState(Thermostat.Mode state) {
|
public void setMode(Thermostat.Mode mode) {
|
||||||
this.mode = state;
|
this.mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
|
||||||
|
public interface ThermostatConditionRepository extends ConditionRepository<ThermostatCondition> {
|
||||||
|
|
||||||
|
List<ThermostatCondition> findAllByAutomationId(@Param("automationId") long automationId);
|
||||||
|
}
|
Loading…
Reference in a new issue