diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index cf2cb56..e2bc359 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -26,7 +26,7 @@ public class ButtonDimmerController ButtonDimmerRepository inputRepository, DimmableRepository outputRepository, DeviceService deviceService) { - super(inputRepository, outputRepository, DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR); + super(inputRepository, outputRepository); this.deviceService = deviceService; this.buttonDimmerRepository = inputRepository; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java index 575d196..4b92125 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/InputDeviceConnectionController.java @@ -23,7 +23,7 @@ import org.springframework.web.bind.annotation.RequestBody; * @param the output device attached to I */ public abstract class InputDeviceConnectionController< - I extends InputDevice, O extends OutputDevice> { + I extends InputDevice & Connectable, O extends OutputDevice> { private class Connection { private final I input; @@ -53,26 +53,20 @@ public abstract class InputDeviceConnectionController< @Autowired private DeviceService deviceService; - private DeviceRepository inputRepository; + private final DeviceRepository inputRepository; - private DeviceRepository outputReposiory; - - private Connector connector; + private final DeviceRepository outputReposiory; /** * Contstructs the controller by requiring essential object for the controller implementation * * @param inputRepository the input device repository * @param outputRepository the output device repository - * @param connector a appropriate Connector instance for the I and O tyoes. */ protected InputDeviceConnectionController( - DeviceRepository inputRepository, - DeviceRepository outputRepository, - Connector connector) { + DeviceRepository inputRepository, DeviceRepository outputRepository) { this.inputRepository = inputRepository; this.outputReposiory = outputRepository; - this.connector = connector; } private Connection checkConnectionIDs(Long inputId, List outputs, String username) @@ -104,7 +98,7 @@ public abstract class InputDeviceConnectionController< final Connection pair = checkConnectionIDs(inputId, outputs, username); for (final O o : pair.getOutputs()) { - connector.connect(pair.getInput(), o, true); + pair.getInput().connect(o, true); } deviceService.saveAllAsOwner(pair.getOutputs(), username); @@ -124,7 +118,7 @@ public abstract class InputDeviceConnectionController< final Connection pair = checkConnectionIDs(inputId, outputs, username); for (final O o : pair.getOutputs()) { - connector.connect(pair.getInput(), o, false); + pair.getInput().connect(o, false); } deviceService.saveAllAsOwner(pair.getOutputs(), username); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index f0900e4..f80e1fd 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -25,7 +25,7 @@ public class KnobDimmerController extends InputDeviceConnectionController outputRepository, DeviceService deviceService) { - super(inputRepository, outputRepository, Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR); + super(inputRepository, outputRepository); this.knobDimmerRepository = inputRepository; this.deviceService = deviceService; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index 96f5a52..299c7e5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -17,8 +17,8 @@ import org.springframework.web.bind.annotation.*; @RequestMapping("/switch") public class SwitchController extends InputDeviceConnectionController { - private SwitchRepository switchRepository; - private DeviceService deviceService; + private final SwitchRepository switchRepository; + private final DeviceService deviceService; /** * Contstructs the controller by requiring essential object for the controller implementation @@ -31,7 +31,7 @@ public class SwitchController extends InputDeviceConnectionController outputRepository, DeviceService deviceService) { - super(inputRepository, outputRepository, Switchable.SWITCH_SWITCHABLE_CONNECTOR); + super(inputRepository, outputRepository); this.deviceService = deviceService; this.switchRepository = inputRepository; } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connectable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connectable.java new file mode 100644 index 0000000..8423930 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connectable.java @@ -0,0 +1,5 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +public interface Connectable { + void connect(O output, boolean connect); +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connector.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connector.java deleted file mode 100644 index 325f0dd..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connector.java +++ /dev/null @@ -1,46 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.models; - -import java.util.Set; -import java.util.function.Function; - -/** - * A rule on how to connect an input device type to an output device type - * - * @param the input device type - * @param the output device type - */ -@FunctionalInterface -public interface Connector { - - /** - * Connects or disconnects input to output - * - * @param input the input device - * @param output the output device - * @param connect true if connection, false if disconnection - */ - void connect(I input, O output, boolean connect); - - /** - * Produces a basic implementation of a connector, assuming there is a ManyToMany relationship - * between J and K - * - * @param outputsGetter the getter method of the set of outputs on the input class - * @param inputsGetter the getter method of the set of outputs on the input class - * @param the input device type - * @param the output device type - * @return a Connector implementation for the pair of types J and K - */ - static Connector basic( - Function> outputsGetter, Function> inputsGetter) { - return (i, o, connect) -> { - if (connect) { - outputsGetter.apply(i).add(o); - inputsGetter.apply(o).add(i); - } else { - outputsGetter.apply(i).remove(o); - inputsGetter.apply(o).remove(i); - } - }; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java index 6ae3150..bc093e0 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java @@ -12,12 +12,6 @@ import javax.validation.constraints.NotNull; @Inheritance(strategy = InheritanceType.SINGLE_TABLE) public class Dimmable extends Switchable implements RangeTriggerable { - public static final Connector KNOB_DIMMER_DIMMABLE_CONNECTOR = - Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers); - - public static final Connector BUTTON_DIMMER_DIMMABLE_CONNECTOR = - Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers); - protected Dimmable(String kind) { super(kind); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java index e920d2f..cd3ee64 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -9,7 +9,7 @@ import javax.persistence.*; /** Represents a generic dimmer input device */ @Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) -public abstract class Dimmer extends InputDevice { +public abstract class Dimmer extends InputDevice implements Connectable { public Dimmer(String kind) { super(kind); } @@ -37,4 +37,14 @@ public abstract class Dimmer extends InputDevice { public void addDimmable(Dimmable dimmable) { dimmables.add(dimmable); } + + public void connect(Dimmable output, boolean connect) { + if (connect) { + output.getDimmers().add(this); + getOutputs().add(output); + } else { + output.getDimmers().remove(this); + getOutputs().remove(output); + } + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index 7825eae..82ab671 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -8,7 +8,7 @@ import javax.persistence.*; /** A switch input device */ @Entity -public class Switch extends InputDevice implements BooleanTriggerable { +public class Switch extends InputDevice implements BooleanTriggerable, Connectable { @ManyToMany( cascade = { @@ -64,6 +64,16 @@ public class Switch extends InputDevice implements BooleanTriggerable { return switchables; } + public void connect(Switchable output, boolean connect) { + if (connect) { + output.getSwitches().add(this); + getOutputs().add(output); + } else { + output.getSwitches().remove(this); + getOutputs().remove(output); + } + } + @Override public boolean readTriggerState() { return on; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index 9db6361..a1a5366 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -11,9 +11,6 @@ import javax.persistence.*; @Inheritance(strategy = InheritanceType.JOINED) public abstract class Switchable extends OutputDevice { - public static final Connector SWITCH_SWITCHABLE_CONNECTOR = - Connector.basic(Switch::getOutputs, Switchable::getSwitches); - @ManyToMany( mappedBy = "switchables", cascade = {