Input output connection routes now accept array of outputs

This commit is contained in:
Claudio Maggioni 2020-04-11 17:24:23 +02:00
parent d979050306
commit e46ac02da5
4 changed files with 39 additions and 25 deletions

View file

@ -77,14 +77,14 @@ public class ButtonDimmerController
@PostMapping("/{id}/lights") @PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addLight( public Set<? extends OutputDevice> addLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return addOutput(inputId, lightId); return addOutput(inputId, lightId);
} }
@DeleteMapping("/{id}/lights") @DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeLight( public Set<? extends OutputDevice> removeLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return removeOutput(inputId, lightId); return removeOutput(inputId, lightId);
} }

View file

@ -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.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set; import java.util.Set;
/** /**
@ -14,11 +16,11 @@ import java.util.Set;
public abstract class InputDeviceConnectionController< public abstract class InputDeviceConnectionController<
I extends InputDevice, O extends OutputDevice> { I extends InputDevice, O extends OutputDevice> {
private class IOPair { private class Connection {
private final I input; 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.input = input;
this.output = output; this.output = output;
} }
@ -46,31 +48,39 @@ public abstract class InputDeviceConnectionController<
this.connector = connector; 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 = final I input =
inputRepository inputRepository
.findById(inputId) .findById(inputId)
.orElseThrow(() -> new NotFoundException("input device")); .orElseThrow(() -> new NotFoundException("input device"));
final O output = final List<O> outputDevices = new ArrayList<>();
for (final Long outputId : outputs) {
outputDevices.add(
outputReposiory outputReposiory
.findById(outputId) .findById(outputId)
.orElseThrow(() -> new NotFoundException("output device")); .orElseThrow(() -> new NotFoundException("output device")));
return new IOPair(input, output); }
return new Connection(input, outputDevices);
} }
/** /**
* Implements the output device connection creation (add) route * Implements the output device connection creation (add) route
* *
* @param inputId input device id * @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 * @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid * @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 { throws NotFoundException {
final IOPair pair = checkConnectionIDs(inputId, outputId); final Connection pair = checkConnectionIDs(inputId, outputId);
connector.connect(pair.input, pair.output, true);
outputReposiory.save(pair.output); for (final O o : pair.output) {
connector.connect(pair.input, o, true);
}
outputReposiory.saveAll(pair.output);
return pair.input.getOutputs(); return pair.input.getOutputs();
} }
@ -78,15 +88,19 @@ public abstract class InputDeviceConnectionController<
* Implements the output device connection destruction (remove) route * Implements the output device connection destruction (remove) route
* *
* @param inputId input device id * @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 * @return the list of output devices attached to the input device of id inputId
* @throws NotFoundException if inputId or outputId are not valid * @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 { throws NotFoundException {
final IOPair pair = checkConnectionIDs(inputId, outputId); final Connection pair = checkConnectionIDs(inputId, outputId);
connector.connect(pair.input, pair.output, false);
outputReposiory.save(pair.output); for (final O o : pair.output) {
connector.connect(pair.input, o, false);
}
outputReposiory.saveAll(pair.output);
return pair.input.getOutputs(); return pair.input.getOutputs();
} }
} }

View file

@ -70,14 +70,14 @@ public class KnobDimmerController
@PostMapping("/{id}/lights") @PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addLight( public Set<? extends OutputDevice> addLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return addOutput(inputId, lightId); return addOutput(inputId, lightId);
} }
@DeleteMapping("/{id}/lights") @DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeLight( public Set<? extends OutputDevice> removeLight(
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId) @PathVariable("id") long inputId, @RequestBody List<Long> lightId)
throws NotFoundException { throws NotFoundException {
return removeOutput(inputId, lightId); return removeOutput(inputId, lightId);
} }

View file

@ -83,14 +83,14 @@ public class SwitchController extends InputDeviceConnectionController<Switch, Sw
@PostMapping("/{id}/lights") @PostMapping("/{id}/lights")
public Set<? extends OutputDevice> addSwitchable( public Set<? extends OutputDevice> addSwitchable(
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) @PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
throws NotFoundException { throws NotFoundException {
return addOutput(inputId, switchableId); return addOutput(inputId, switchableId);
} }
@DeleteMapping("/{id}/lights") @DeleteMapping("/{id}/lights")
public Set<? extends OutputDevice> removeSwitchable( public Set<? extends OutputDevice> removeSwitchable(
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) @PathVariable("id") long inputId, @RequestBody List<Long> switchableId)
throws NotFoundException { throws NotFoundException {
return removeOutput(inputId, switchableId); return removeOutput(inputId, switchableId);
} }