controller for triggers

This commit is contained in:
omenem 2020-04-25 15:59:06 +02:00
parent db35205ed8
commit d61c7bbc0b
7 changed files with 238 additions and 4 deletions

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,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

@ -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

@ -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);
}