Input output connection routes now accept array of outputs
This commit is contained in:
parent
d979050306
commit
e46ac02da5
4 changed files with 39 additions and 25 deletions
|
@ -77,14 +77,14 @@ public class ButtonDimmerController
|
|||
|
||||
@PostMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> addLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
|
||||
throws NotFoundException {
|
||||
return addOutput(inputId, lightId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> removeLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
|
||||
throws NotFoundException {
|
||||
return removeOutput(inputId, lightId);
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
|||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -14,11 +16,11 @@ import java.util.Set;
|
|||
public abstract class InputDeviceConnectionController<
|
||||
I extends InputDevice, O extends OutputDevice> {
|
||||
|
||||
private class IOPair {
|
||||
private class Connection {
|
||||
private final I input;
|
||||
private final O output;
|
||||
private final List<O> output;
|
||||
|
||||
private IOPair(I input, O output) {
|
||||
private Connection(I input, List<O> output) {
|
||||
this.input = input;
|
||||
this.output = output;
|
||||
}
|
||||
|
@ -46,31 +48,39 @@ public abstract class InputDeviceConnectionController<
|
|||
this.connector = connector;
|
||||
}
|
||||
|
||||
private IOPair checkConnectionIDs(Long inputId, Long outputId) throws NotFoundException {
|
||||
private Connection checkConnectionIDs(Long inputId, List<Long> outputs)
|
||||
throws NotFoundException {
|
||||
final I input =
|
||||
inputRepository
|
||||
.findById(inputId)
|
||||
.orElseThrow(() -> new NotFoundException("input device"));
|
||||
final O output =
|
||||
final List<O> outputDevices = new ArrayList<>();
|
||||
for (final Long outputId : outputs) {
|
||||
outputDevices.add(
|
||||
outputReposiory
|
||||
.findById(outputId)
|
||||
.orElseThrow(() -> new NotFoundException("output device"));
|
||||
return new IOPair(input, output);
|
||||
.orElseThrow(() -> new NotFoundException("output device")));
|
||||
}
|
||||
return new Connection(input, outputDevices);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the output device connection creation (add) route
|
||||
*
|
||||
* @param inputId input device id
|
||||
* @param outputId output device id
|
||||
* @param outputId output device id list
|
||||
* @return the list of output devices attached to the input device of id inputId
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> addOutput(Long inputId, Long outputId)
|
||||
protected Set<? extends OutputDevice> addOutput(Long inputId, List<Long> outputId)
|
||||
throws NotFoundException {
|
||||
final IOPair pair = checkConnectionIDs(inputId, outputId);
|
||||
connector.connect(pair.input, pair.output, true);
|
||||
outputReposiory.save(pair.output);
|
||||
final Connection pair = checkConnectionIDs(inputId, outputId);
|
||||
|
||||
for (final O o : pair.output) {
|
||||
connector.connect(pair.input, o, true);
|
||||
}
|
||||
|
||||
outputReposiory.saveAll(pair.output);
|
||||
return pair.input.getOutputs();
|
||||
}
|
||||
|
||||
|
@ -78,15 +88,19 @@ public abstract class InputDeviceConnectionController<
|
|||
* Implements the output device connection destruction (remove) route
|
||||
*
|
||||
* @param inputId input device id
|
||||
* @param outputId output device id
|
||||
* @param outputId output device id list
|
||||
* @return the list of output devices attached to the input device of id inputId
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> removeOutput(Long inputId, Long outputId)
|
||||
protected Set<? extends OutputDevice> removeOutput(Long inputId, List<Long> outputId)
|
||||
throws NotFoundException {
|
||||
final IOPair pair = checkConnectionIDs(inputId, outputId);
|
||||
connector.connect(pair.input, pair.output, false);
|
||||
outputReposiory.save(pair.output);
|
||||
final Connection pair = checkConnectionIDs(inputId, outputId);
|
||||
|
||||
for (final O o : pair.output) {
|
||||
connector.connect(pair.input, o, false);
|
||||
}
|
||||
|
||||
outputReposiory.saveAll(pair.output);
|
||||
return pair.input.getOutputs();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,14 +70,14 @@ public class KnobDimmerController
|
|||
|
||||
@PostMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> addLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
|
||||
throws NotFoundException {
|
||||
return addOutput(inputId, lightId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> removeLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
@PathVariable("id") long inputId, @RequestBody List<Long> lightId)
|
||||
throws NotFoundException {
|
||||
return removeOutput(inputId, lightId);
|
||||
}
|
||||
|
|
|
@ -83,14 +83,14 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
|
|||
|
||||
@PostMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> addSwitchable(
|
||||
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId)
|
||||
@PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
|
||||
throws NotFoundException {
|
||||
return addOutput(inputId, switchableId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> removeSwitchable(
|
||||
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId)
|
||||
@PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
|
||||
throws NotFoundException {
|
||||
return removeOutput(inputId, switchableId);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue