Knob dimmer now with new rest conversation scheme

This commit is contained in:
Claudio Maggioni 2020-03-13 18:48:19 +01:00
parent 3a97d40858
commit 6fef7663dd
7 changed files with 88 additions and 80 deletions

View file

@ -13,7 +13,7 @@ import java.util.Set;
*/
public abstract class InputDeviceConnectionController<
I extends InputDevice & OutputConnectable<O>,
O extends OutputDevice & InputConnectable<I>> {
O extends OutputDevice & InputConnectable<? super I>> {
private class IOPair {
private final I input;

View file

@ -2,11 +2,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerDimRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.List;
import java.util.Set;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -15,39 +16,67 @@ import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
@RequestMapping("/knobDimmer")
public class KnobDimmerController {
public class KnobDimmerController
extends InputDeviceConnectionController<KnobDimmer, DimmableLight> {
@Autowired private KnobDimmerRepository knobDimmerService;
@Autowired private KnobDimmerRepository knobDimmerRepository;
@Autowired private DimmableLightRepository dimmableLightRepository;
@Autowired
protected KnobDimmerController(
KnobDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
super(inputRepository, outputRepository);
this.knobDimmerRepository = inputRepository;
this.dimmableLightRepository = outputRepository;
}
@GetMapping
public List<KnobDimmer> findAll() {
return toList(knobDimmerService.findAll());
return toList(knobDimmerRepository.findAll());
}
@GetMapping("/{id}")
public KnobDimmer findById(@PathVariable("id") long id) throws NotFoundException {
return knobDimmerService.findById(id).orElseThrow(NotFoundException::new);
return knobDimmerRepository.findById(id).orElseThrow(NotFoundException::new);
}
@PostMapping
public KnobDimmer create(@Valid @RequestBody KnobDimmerSaveRequest kd) {
KnobDimmer newKD = new KnobDimmer();
newKD.setLights(kd.getLights());
newKD.setId(kd.getId());
newKD.setName(kd.getName());
newKD.setRoomId(kd.getRoomId());
return knobDimmerService.save(newKD);
return knobDimmerRepository.save(newKD);
}
@PutMapping
public KnobDimmer update(@Valid @RequestBody KnobDimmerSaveRequest kd) {
kd.setId(0);
return this.create(kd);
@PutMapping("/dimTo")
public Set<DimmableLight> dimTo(@Valid @RequestBody final KnobDimmerDimRequest bd)
throws NotFoundException {
final KnobDimmer dimmer =
knobDimmerRepository.findById(bd.getId()).orElseThrow(NotFoundException::new);
dimmer.setLightIntensity(bd.getIntensity());
dimmableLightRepository.saveAll(dimmer.getLights());
return dimmer.getOutputs();
}
@PostMapping("/{id}/lights")
public Set<DimmableLight> addLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
throws NotFoundException {
return addOutput(inputId, lightId);
}
@DeleteMapping("/{id}/lights")
public Set<DimmableLight> removeLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
throws NotFoundException {
return removeOutput(inputId, lightId);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
knobDimmerService.deleteById(id);
knobDimmerRepository.deleteById(id);
}
}

View file

@ -0,0 +1,33 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class KnobDimmerDimRequest {
/** The device id */
@NotNull private Long id;
/** The absolute intensity value */
@NotNull
@Min(0)
@Max(100)
private Integer intensity;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getIntensity() {
return intensity;
}
public void setIntensity(Integer intensity) {
this.intensity = intensity;
}
}

View file

@ -1,17 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Lob;
import javax.validation.constraints.NotNull;
public class KnobDimmerSaveRequest {
@Lob private Set<DimmableLight> lights = new HashSet<DimmableLight>();
/** Device identifier */
private long id;
/**
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
* a REST call.
@ -29,10 +20,6 @@ public class KnobDimmerSaveRequest {
this.name = name;
}
public long getId() {
return id;
}
public Long getRoomId() {
return roomId;
}
@ -40,16 +27,4 @@ public class KnobDimmerSaveRequest {
public String getName() {
return name;
}
public void setLights(Set<DimmableLight> lights) {
this.lights = lights;
}
public Set<DimmableLight> getLights() {
return lights;
}
public void setId(long id) {
this.id = id;
}
}

View file

@ -24,7 +24,6 @@ public class ButtonDimmer extends Dimmer implements OutputConnectable<DimmableLi
public void increaseIntensity() {
for (DimmableLight dl : lights) {
dl.setIntensity(dl.getIntensity() + DIM_INCREMENT);
System.out.println("malusa: " + dl.getIntensity());
}
}
@ -32,7 +31,6 @@ public class ButtonDimmer extends Dimmer implements OutputConnectable<DimmableLi
public void decreaseIntensity() {
for (DimmableLight dl : lights) {
dl.setIntensity(dl.getIntensity() - DIM_INCREMENT);
System.out.println("malusa: " + dl.getIntensity());
}
}

View file

@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull;
/** Represent a dimmable light */
@Entity
public class DimmableLight extends Light implements InputConnectable<ButtonDimmer> {
public class DimmableLight extends Light implements InputConnectable<Dimmer> {
public DimmableLight() {
super("light");

View file

@ -9,7 +9,7 @@ import javax.persistence.OneToMany;
* value, like a knob)
*/
@Entity
public class KnobDimmer extends Dimmer {
public class KnobDimmer extends Dimmer implements OutputConnectable<DimmableLight> {
public KnobDimmer() {
super("knob-dimmer");
}
@ -18,49 +18,17 @@ public class KnobDimmer extends Dimmer {
private Set<DimmableLight> lights;
/**
* Increases or decreases the current intensity level by 5, moving between absolute multiples of
* 5 between 0 and 100, of all dimmable lights mapped to this knob
* Sets absolutely the intensity level of all lights connected
*
* @param inc The direction the knob is turned with
* @param intensity the intensity (must be from 0 to 100)
*/
public void modifyIntensity(boolean inc) {
public void setLightIntensity(int intensity) {
for (DimmableLight dl : lights) {
int remainder = dl.getIntensity() / 5;
if (inc) {
dl.setIntensity(dl.getIntensity() - remainder);
dl.setIntensity((dl.getIntensity() + 5) % 105);
} else {
dl.setIntensity(dl.getIntensity() + (5 - remainder));
dl.setIntensity((dl.getIntensity() - 5) % 105);
}
dl.setIntensity(intensity);
}
}
/**
* Adds a DimmableLight to this set of DimmableLights
*
* @param dl The DimmableLight to be added
*/
public void addLight(DimmableLight dl) {
lights.add(dl);
}
/**
* Removes the given DimmableLight
*
* @param dl The DimmableLight to be removed
*/
public void removeLight(DimmableLight dl) {
lights.remove(dl);
}
/** Clears this set */
public void clearSet() {
lights.clear();
}
public void setLights(Set<DimmableLight> lights) {
this.lights = lights;
}
@ -68,4 +36,9 @@ public class KnobDimmer extends Dimmer {
public Set<DimmableLight> getLights() {
return lights;
}
@Override
public Set<DimmableLight> getOutputs() {
return lights;
}
}