Knob dimmer now with new rest conversation scheme
This commit is contained in:
parent
3a97d40858
commit
6fef7663dd
7 changed files with 88 additions and 80 deletions
|
@ -13,7 +13,7 @@ import java.util.Set;
|
||||||
*/
|
*/
|
||||||
public abstract class InputDeviceConnectionController<
|
public abstract class InputDeviceConnectionController<
|
||||||
I extends InputDevice & OutputConnectable<O>,
|
I extends InputDevice & OutputConnectable<O>,
|
||||||
O extends OutputDevice & InputConnectable<I>> {
|
O extends OutputDevice & InputConnectable<? super I>> {
|
||||||
|
|
||||||
private class IOPair {
|
private class IOPair {
|
||||||
private final I input;
|
private final I input;
|
||||||
|
|
|
@ -2,11 +2,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
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.dto.KnobDimmerSaveRequest;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
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.*;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
@ -15,39 +16,67 @@ import org.springframework.web.bind.annotation.*;
|
||||||
@RestController
|
@RestController
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@RequestMapping("/knobDimmer")
|
@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
|
@GetMapping
|
||||||
public List<KnobDimmer> findAll() {
|
public List<KnobDimmer> findAll() {
|
||||||
return toList(knobDimmerService.findAll());
|
return toList(knobDimmerRepository.findAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@GetMapping("/{id}")
|
||||||
public KnobDimmer findById(@PathVariable("id") long id) throws NotFoundException {
|
public KnobDimmer findById(@PathVariable("id") long id) throws NotFoundException {
|
||||||
return knobDimmerService.findById(id).orElseThrow(NotFoundException::new);
|
return knobDimmerRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public KnobDimmer create(@Valid @RequestBody KnobDimmerSaveRequest kd) {
|
public KnobDimmer create(@Valid @RequestBody KnobDimmerSaveRequest kd) {
|
||||||
KnobDimmer newKD = new KnobDimmer();
|
KnobDimmer newKD = new KnobDimmer();
|
||||||
newKD.setLights(kd.getLights());
|
|
||||||
newKD.setId(kd.getId());
|
|
||||||
newKD.setName(kd.getName());
|
newKD.setName(kd.getName());
|
||||||
newKD.setRoomId(kd.getRoomId());
|
newKD.setRoomId(kd.getRoomId());
|
||||||
|
|
||||||
return knobDimmerService.save(newKD);
|
return knobDimmerRepository.save(newKD);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping("/dimTo")
|
||||||
public KnobDimmer update(@Valid @RequestBody KnobDimmerSaveRequest kd) {
|
public Set<DimmableLight> dimTo(@Valid @RequestBody final KnobDimmerDimRequest bd)
|
||||||
kd.setId(0);
|
throws NotFoundException {
|
||||||
return this.create(kd);
|
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}")
|
@DeleteMapping("/{id}")
|
||||||
public void delete(@PathVariable("id") long id) {
|
public void delete(@PathVariable("id") long id) {
|
||||||
knobDimmerService.deleteById(id);
|
knobDimmerRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,17 +1,8 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
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;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class KnobDimmerSaveRequest {
|
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
|
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||||
* a REST call.
|
* a REST call.
|
||||||
|
@ -29,10 +20,6 @@ public class KnobDimmerSaveRequest {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getRoomId() {
|
public Long getRoomId() {
|
||||||
return roomId;
|
return roomId;
|
||||||
}
|
}
|
||||||
|
@ -40,16 +27,4 @@ public class KnobDimmerSaveRequest {
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ public class ButtonDimmer extends Dimmer implements OutputConnectable<DimmableLi
|
||||||
public void increaseIntensity() {
|
public void increaseIntensity() {
|
||||||
for (DimmableLight dl : lights) {
|
for (DimmableLight dl : lights) {
|
||||||
dl.setIntensity(dl.getIntensity() + DIM_INCREMENT);
|
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() {
|
public void decreaseIntensity() {
|
||||||
for (DimmableLight dl : lights) {
|
for (DimmableLight dl : lights) {
|
||||||
dl.setIntensity(dl.getIntensity() - DIM_INCREMENT);
|
dl.setIntensity(dl.getIntensity() - DIM_INCREMENT);
|
||||||
System.out.println("malusa: " + dl.getIntensity());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** Represent a dimmable light */
|
/** Represent a dimmable light */
|
||||||
@Entity
|
@Entity
|
||||||
public class DimmableLight extends Light implements InputConnectable<ButtonDimmer> {
|
public class DimmableLight extends Light implements InputConnectable<Dimmer> {
|
||||||
|
|
||||||
public DimmableLight() {
|
public DimmableLight() {
|
||||||
super("light");
|
super("light");
|
||||||
|
|
|
@ -9,7 +9,7 @@ import javax.persistence.OneToMany;
|
||||||
* value, like a knob)
|
* value, like a knob)
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
public class KnobDimmer extends Dimmer {
|
public class KnobDimmer extends Dimmer implements OutputConnectable<DimmableLight> {
|
||||||
public KnobDimmer() {
|
public KnobDimmer() {
|
||||||
super("knob-dimmer");
|
super("knob-dimmer");
|
||||||
}
|
}
|
||||||
|
@ -18,48 +18,16 @@ public class KnobDimmer extends Dimmer {
|
||||||
private Set<DimmableLight> lights;
|
private Set<DimmableLight> lights;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increases or decreases the current intensity level by 5, moving between absolute multiples of
|
* Sets absolutely the intensity level of all lights connected
|
||||||
* 5 between 0 and 100, of all dimmable lights mapped to this knob
|
|
||||||
*
|
*
|
||||||
* @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) {
|
for (DimmableLight dl : lights) {
|
||||||
int remainder = dl.getIntensity() / 5;
|
dl.setIntensity(intensity);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
public void setLights(Set<DimmableLight> lights) {
|
||||||
this.lights = lights;
|
this.lights = lights;
|
||||||
|
@ -68,4 +36,9 @@ public class KnobDimmer extends Dimmer {
|
||||||
public Set<DimmableLight> getLights() {
|
public Set<DimmableLight> getLights() {
|
||||||
return lights;
|
return lights;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<DimmableLight> getOutputs() {
|
||||||
|
return lights;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue