From 7cbacd7406f80f4570f1d7c2487a16e6cc6f56b5 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Thu, 16 Apr 2020 17:43:25 +0200 Subject: [PATCH 1/3] Implemented Many-to-Many behaviour for Switches and Dimmers --- .../controller/ButtonDimmerController.java | 14 -------- .../InputDeviceConnectionController.java | 22 +++++++++++++ .../controller/KnobDimmerController.java | 14 -------- .../smarthut/controller/SwitchController.java | 15 +-------- .../smarthut/models/Connector.java | 10 +++--- .../sanmarinoes/smarthut/models/Device.java | 2 +- .../smarthut/models/DimmableLight.java | 33 +++++++++---------- .../sanmarinoes/smarthut/models/Dimmer.java | 29 ++++++++-------- .../smarthut/models/InputDevice.java | 2 +- .../smarthut/models/OutputDevice.java | 2 +- .../smarthut/models/SmartPlug.java | 10 ++++++ .../sanmarinoes/smarthut/models/Switch.java | 22 ++++++------- .../smarthut/models/Switchable.java | 22 +++++-------- .../sa4/sanmarinoes/smarthut/utils/Utils.java | 2 +- 14 files changed, 88 insertions(+), 111 deletions(-) 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 ed517f8..316a938 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 @@ -75,20 +75,6 @@ public class ButtonDimmerController return buttonDimmer.getOutputs(); } - @PostMapping("/{id}/lights") - public Set addLight( - @PathVariable("id") long inputId, @RequestBody List lightId) - throws NotFoundException { - return addOutput(inputId, lightId); - } - - @DeleteMapping("/{id}/lights") - public Set removeLight( - @PathVariable("id") long inputId, @RequestBody List lightId) - throws NotFoundException { - return removeOutput(inputId, lightId); - } - @DeleteMapping("/{id}") public void delete(@PathVariable("id") long id) { buttonDimmerRepository.deleteById(id); 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 ca2997a..dd61224 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 @@ -2,10 +2,18 @@ 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 com.google.gson.Gson; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + import java.util.ArrayList; import java.util.List; import java.util.Set; +import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; + /** * An abstract controller for an input device that has output connected to it. Aids to create the * output add and output remove route @@ -103,4 +111,18 @@ public abstract class InputDeviceConnectionController< outputReposiory.saveAll(pair.output); return pair.input.getOutputs(); } + + @PostMapping("/{id}/lights") + public List addLight( + @PathVariable("id") long inputId, @RequestBody List lightId) + throws NotFoundException { + return toList(addOutput(inputId, lightId)); + } + + @DeleteMapping("/{id}/lights") + public List removeLight( + @PathVariable("id") long inputId, @RequestBody List lightId) + throws NotFoundException { + return toList(removeOutput(inputId, lightId)); + } } 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 a259d98..fbad655 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 @@ -68,20 +68,6 @@ public class KnobDimmerController return dimmer.getOutputs(); } - @PostMapping("/{id}/lights") - public Set addLight( - @PathVariable("id") long inputId, @RequestBody List lightId) - throws NotFoundException { - return addOutput(inputId, lightId); - } - - @DeleteMapping("/{id}/lights") - public Set removeLight( - @PathVariable("id") long inputId, @RequestBody List lightId) - throws NotFoundException { - return removeOutput(inputId, lightId); - } - @DeleteMapping("/{id}") public void delete(@PathVariable("id") long id) { knobDimmerRepository.deleteById(id); 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 5794a01..3a87b18 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 @@ -9,6 +9,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.security.Principal; import java.util.*; import java.util.List; +import java.util.stream.Collectors; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; @@ -81,20 +82,6 @@ public class SwitchController extends InputDeviceConnectionController addSwitchable( - @PathVariable("id") long inputId, @RequestBody List switchableId) - throws NotFoundException { - return addOutput(inputId, switchableId); - } - - @DeleteMapping("/{id}/lights") - public Set removeSwitchable( - @PathVariable("id") long inputId, @RequestBody List switchableId) - throws NotFoundException { - return removeOutput(inputId, switchableId); - } - @DeleteMapping("/{id}") public void deleteById(@PathVariable("id") long id) { switchRepository.deleteById(id); 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 index 91b1e88..701a010 100644 --- 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 @@ -23,25 +23,25 @@ public interface Connector { void connect(I input, O output, boolean connect); /** - * Produces a basic implementation of a connector, assuming there is a OneToMany relationship + * 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 inputSetter the setter method for the input id on the output 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, BiConsumer inputSetter) { + 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); } - - inputSetter.accept(o, connect ? i.getId() : null); }; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index 295fe37..4302774 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -8,7 +8,7 @@ import javax.validation.constraints.NotNull; /** Generic abstraction for a smart home device */ @Entity -@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Inheritance(strategy = InheritanceType.JOINED) public abstract class Device { /** Ways a device can behave in the automation flow. For now only input/output */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java index 6b537c6..ce5c593 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java @@ -1,13 +1,13 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; +import com.google.common.base.Objects; + +import javax.persistence.*; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; +import java.util.Set; /** Represent a dimmable light */ @Entity @@ -15,22 +15,18 @@ public class DimmableLight extends Switchable { public static final Connector BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR = - Connector.basic(ButtonDimmer::getOutputs, DimmableLight::setDimmerId); + Connector.basic(ButtonDimmer::getOutputs, DimmableLight::getDimmers); public static final Connector KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR = - Connector.basic(KnobDimmer::getOutputs, DimmableLight::setDimmerId); + Connector.basic(KnobDimmer::getOutputs, DimmableLight::getDimmers); public DimmableLight() { super("dimmableLight"); } - @ManyToOne + @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) @GsonExclude - @JoinColumn(name = "dimmer_id", updatable = false, insertable = false) - private Dimmer dimmer; - - @Column(name = "dimmer_id") - private Long dimmerId; + private Set dimmers; /** The light intensity value. Goes from 0 (off) to 100 (on) */ @NotNull @@ -76,14 +72,15 @@ public class DimmableLight extends Switchable { intensity = on ? oldIntensity : 0; } - public void setDimmerId(Long dimmerId) { - this.dimmerId = dimmerId; - super.setSwitchId(null); + public Set getDimmers() { + return this.dimmers; } @Override - public void setSwitchId(Long switchId) { - super.setSwitchId(switchId); - this.dimmerId = null; + public String toString() { + return Objects.toStringHelper(this) + .add("intensity", intensity) + .add("oldIntensity", oldIntensity) + .toString(); } } 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 d06bece..e0b4161 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 @@ -1,12 +1,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; + import java.util.HashSet; import java.util.Set; -import javax.persistence.Entity; -import javax.persistence.Inheritance; -import javax.persistence.InheritanceType; -import javax.persistence.OneToMany; -import javax.persistence.PreRemove; +import javax.persistence.*; /** Represents a generic dimmer input device */ @Entity @@ -16,8 +14,14 @@ public abstract class Dimmer extends InputDevice { super(kind); } - @OneToMany(mappedBy = "dimmer") - private Set lights = new HashSet<>(); + @ManyToMany(cascade = CascadeType.DETACH) + @GsonExclude + @JoinTable( + name = "dimmer_dimmable", + joinColumns = @JoinColumn(name = "dimmer_id"), + inverseJoinColumns = @JoinColumn(name = "dimmable_id") + ) + private Set dimmables = new HashSet<>(); /** * Get the lights connected to this dimmer @@ -26,18 +30,11 @@ public abstract class Dimmer extends InputDevice { */ @Override public Set getOutputs() { - return this.lights; + return this.dimmables; } /** Add a light to be controller by this dimmer */ public void addDimmableLight(DimmableLight dimmableLight) { - lights.add(dimmableLight); - } - - @PreRemove - private void removeLightsFromDimmer() { - for (DimmableLight dl : getOutputs()) { - dl.setDimmerId(null); - } + dimmables.add(dimmableLight); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java index da45b67..2acf0c2 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java @@ -10,7 +10,7 @@ import javax.persistence.InheritanceType; * environment (sensor) or the user (switch / dimmer). */ @Entity -@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Inheritance(strategy = InheritanceType.JOINED) public abstract class InputDevice extends Device { public InputDevice(String kind) { super(kind, FlowType.INPUT); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java index c5b401f..56e68c5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java @@ -7,7 +7,7 @@ import javax.persistence.*; * ...). */ @Entity -@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Inheritance(strategy = InheritanceType.JOINED) public abstract class OutputDevice extends Device { public OutputDevice(String kind) { super(kind, FlowType.OUTPUT); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java index 9b07e69..8424bde 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java @@ -1,5 +1,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import com.google.common.base.Objects; + import java.math.BigDecimal; import javax.persistence.Column; import javax.persistence.Entity; @@ -44,4 +46,12 @@ public class SmartPlug extends Switchable { public SmartPlug() { super("smartPlug"); } + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("totalConsumption", totalConsumption) + .add("on", on) + .toString(); + } } 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 8a8057c..e78bf9f 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 @@ -1,17 +1,22 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; + import java.util.HashSet; import java.util.Set; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.OneToMany; -import javax.persistence.PreRemove; +import javax.persistence.*; /** A switch input device */ @Entity public class Switch extends InputDevice { - @OneToMany(mappedBy = "switchDevice") + @ManyToMany(cascade = CascadeType.DETACH) + @GsonExclude + @JoinTable( + name = "switch_switchable", + joinColumns = @JoinColumn(name = "switch_id"), + inverseJoinColumns = @JoinColumn(name = "switchable_id") + ) private Set switchables = new HashSet<>(); /** The state of this switch */ @@ -52,11 +57,4 @@ public class Switch extends InputDevice { public Set getOutputs() { return switchables; } - - @PreRemove - public void removeSwitchable() { - for (Switchable s : getOutputs()) { - s.setSwitchId(null); - } - } } 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 5ba0702..6d7260f 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 @@ -2,22 +2,20 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; import javax.persistence.*; +import java.util.HashSet; +import java.util.Set; /** A device that can be turned either on or off */ @Entity -@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Inheritance(strategy = InheritanceType.JOINED) public abstract class Switchable extends OutputDevice { public static final Connector SWITCH_SWITCHABLE_CONNECTOR = - Connector.basic(Switch::getOutputs, Switchable::setSwitchId); + Connector.basic(Switch::getOutputs, Switchable::getSwitches); - @ManyToOne + @ManyToMany(mappedBy = "switchables", cascade = CascadeType.DETACH) @GsonExclude - @JoinColumn(name = "switch_id", updatable = false, insertable = false) - private Switch switchDevice; - - @Column(name = "switch_id") - private Long switchId; + private Set inputs = new HashSet<>(); protected Switchable(String kind) { super(kind); @@ -37,11 +35,7 @@ public abstract class Switchable extends OutputDevice { */ public abstract void setOn(boolean on); - public Long getSwitchId() { - return switchId; - } - - public void setSwitchId(Long switchId) { - this.switchId = switchId; + public Set getSwitches() { + return inputs; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java index 99d363b..319a8e3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/utils/Utils.java @@ -14,7 +14,7 @@ public final class Utils { void apply(T input) throws Throwable; } - public static List toList(Iterable iterable) { + public static List toList(Iterable iterable) { return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList()); } From cde88f2ca94387247e5e5ec45adab3ecce019577 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Thu, 16 Apr 2020 18:28:33 +0200 Subject: [PATCH 2/3] Added new hierarchy for dimmable devices and removed duplications in controllers and dtos --- .../controller/ButtonDimmerController.java | 14 ++-- .../controller/CurtainsController.java | 10 +-- .../controller/DimmableLightController.java | 8 +- .../controller/KnobDimmerController.java | 14 ++-- .../controller/RegularLightController.java | 8 +- .../controller/SecurityCameraController.java | 8 +- .../controller/SmartPlugController.java | 9 +-- .../smarthut/dto/CurtainsSaveRequest.java | 57 -------------- ...eRequest.java => DimmableSaveRequest.java} | 8 +- .../dto/SecurityCameraSaveRequest.java | 53 ------------- .../smarthut/dto/SmartPlugSaveRequest.java | 52 ------------- ...equest.java => SwitchableSaveRequest.java} | 4 +- .../smarthut/models/ButtonDimmer.java | 4 +- .../sanmarinoes/smarthut/models/Curtains.java | 37 ++------- .../smarthut/models/CurtainsRepository.java | 2 +- .../sanmarinoes/smarthut/models/Dimmable.java | 78 +++++++++++++++++++ .../smarthut/models/DimmableLight.java | 77 +----------------- .../models/DimmableLightRepository.java | 2 +- .../smarthut/models/DimmableRepository.java | 4 + .../sanmarinoes/smarthut/models/Dimmer.java | 6 +- .../smarthut/models/KnobDimmer.java | 2 +- .../smarthut/ButtonDimmerTests.java | 2 +- .../sanmarinoes/smarthut/CurtainsTests.java | 14 ++-- .../sanmarinoes/smarthut/KnobDimmerTests.java | 2 +- 24 files changed, 146 insertions(+), 329 deletions(-) delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java rename src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/{DimmableLightSaveRequest.java => DimmableSaveRequest.java} (90%) delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SecurityCameraSaveRequest.java delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java rename src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/{RegularLightSaveRequest.java => SwitchableSaveRequest.java} (94%) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableRepository.java 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 316a938..1870abc 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 @@ -18,19 +18,19 @@ import org.springframework.web.bind.annotation.*; @EnableAutoConfiguration @RequestMapping("/buttonDimmer") public class ButtonDimmerController - extends InputDeviceConnectionController { + extends InputDeviceConnectionController { private ButtonDimmerRepository buttonDimmerRepository; - private DimmableLightRepository dimmableLightRepository; + private DimmableRepository dimmableRepository; @Autowired protected ButtonDimmerController( - ButtonDimmerRepository inputRepository, DimmableLightRepository outputRepository) { + ButtonDimmerRepository inputRepository, DimmableRepository outputRepository) { super( inputRepository, outputRepository, - DimmableLight.BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR); + DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR); this.buttonDimmerRepository = inputRepository; - this.dimmableLightRepository = outputRepository; + this.dimmableRepository = outputRepository; } @GetMapping @@ -53,7 +53,7 @@ public class ButtonDimmerController } @PutMapping("/dim") - public Set dim( + public Set dim( @Valid @RequestBody final ButtonDimmerDimRequest bd, final Principal principal) throws NotFoundException { final ButtonDimmer buttonDimmer = @@ -70,7 +70,7 @@ public class ButtonDimmerController break; } - dimmableLightRepository.saveAll(buttonDimmer.getOutputs()); + dimmableRepository.saveAll(buttonDimmer.getOutputs()); return buttonDimmer.getOutputs(); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java index 5095966..54541cf 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/CurtainsController.java @@ -2,7 +2,7 @@ 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.CurtainsSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Curtains; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.CurtainsRepository; @@ -28,21 +28,21 @@ public class CurtainsController { return curtainsService.findById(id).orElseThrow(NotFoundException::new); } - private Curtains save(Curtains newRL, CurtainsSaveRequest s) { + private Curtains save(Curtains newRL, DimmableSaveRequest s) { newRL.setName(s.getName()); newRL.setRoomId(s.getRoomId()); - newRL.setOpenedAmount(s.getOpening()); + newRL.setIntensity(s.getIntensity()); return curtainsService.save(newRL); } @PostMapping - public Curtains create(@Valid @RequestBody CurtainsSaveRequest curtain) { + public Curtains create(@Valid @RequestBody DimmableSaveRequest curtain) { return save(new Curtains(), curtain); } @PutMapping - public Curtains update(@Valid @RequestBody CurtainsSaveRequest curtain) + public Curtains update(@Valid @RequestBody DimmableSaveRequest curtain) throws NotFoundException { return save( curtainsService.findById(curtain.getId()).orElseThrow(NotFoundException::new), diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 944b6c2..ee33933 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -2,7 +2,7 @@ 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.DimmableLightSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository; @@ -30,7 +30,7 @@ public class DimmableLightController { return dimmableLightService.findById(id).orElseThrow(NotFoundException::new); } - private DimmableLight save(DimmableLight initial, DimmableLightSaveRequest dl) { + private DimmableLight save(DimmableLight initial, DimmableSaveRequest dl) { initial.setIntensity(dl.getIntensity()); initial.setName(dl.getName()); initial.setRoomId(dl.getRoomId()); @@ -39,13 +39,13 @@ public class DimmableLightController { } @PostMapping - public DimmableLight create(@Valid @RequestBody DimmableLightSaveRequest dl) { + public DimmableLight create(@Valid @RequestBody DimmableSaveRequest dl) { return save(new DimmableLight(), dl); } @PutMapping public DimmableLight update( - @Valid @RequestBody DimmableLightSaveRequest sp, final Principal principal) + @Valid @RequestBody DimmableSaveRequest sp, final Principal principal) throws NotFoundException { return save( dimmableLightService 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 fbad655..adf75ea 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 @@ -18,20 +18,20 @@ import org.springframework.web.bind.annotation.*; @EnableAutoConfiguration @RequestMapping("/knobDimmer") public class KnobDimmerController - extends InputDeviceConnectionController { + extends InputDeviceConnectionController { @Autowired private KnobDimmerRepository knobDimmerRepository; - @Autowired private DimmableLightRepository dimmableLightRepository; + @Autowired private DimmableRepository dimmableRepository; @Autowired protected KnobDimmerController( - KnobDimmerRepository inputRepository, DimmableLightRepository outputRepository) { + KnobDimmerRepository inputRepository, DimmableRepository outputRepository) { super( inputRepository, outputRepository, - DimmableLight.KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR); + Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR); this.knobDimmerRepository = inputRepository; - this.dimmableLightRepository = outputRepository; + this.dimmableRepository = outputRepository; } @GetMapping @@ -54,7 +54,7 @@ public class KnobDimmerController } @PutMapping("/dimTo") - public Set dimTo( + public Set dimTo( @Valid @RequestBody final KnobDimmerDimRequest bd, final Principal principal) throws NotFoundException { final KnobDimmer dimmer = @@ -63,7 +63,7 @@ public class KnobDimmerController .orElseThrow(NotFoundException::new); dimmer.setLightIntensity(bd.getIntensity()); - dimmableLightRepository.saveAll(dimmer.getOutputs()); + dimmableRepository.saveAll(dimmer.getOutputs()); return dimmer.getOutputs(); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index e033555..cdb3e9d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -2,7 +2,7 @@ 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.RegularLightSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository; @@ -37,7 +37,7 @@ public class RegularLightController { return regularLightService.findById(id).orElseThrow(NotFoundException::new); } - private RegularLight save(RegularLight newRL, RegularLightSaveRequest rl) { + private RegularLight save(RegularLight newRL, SwitchableSaveRequest rl) { newRL.setName(rl.getName()); newRL.setRoomId(rl.getRoomId()); newRL.setOn(rl.isOn()); @@ -46,13 +46,13 @@ public class RegularLightController { } @PostMapping - public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) { + public RegularLight create(@Valid @RequestBody SwitchableSaveRequest rl) { return save(new RegularLight(), rl); } @PutMapping public RegularLight update( - @Valid @RequestBody RegularLightSaveRequest rl, final Principal principal) + @Valid @RequestBody SwitchableSaveRequest rl, final Principal principal) throws NotFoundException { return save( regularLightService diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java index 9baf353..dcaafc4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SecurityCameraController.java @@ -2,7 +2,7 @@ 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.SecurityCameraSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCamera; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCameraRepository; @@ -37,7 +37,7 @@ public class SecurityCameraController { return securityCameraService.findById(id).orElseThrow(NotFoundException::new); } - private SecurityCamera save(SecurityCamera newSC, SecurityCameraSaveRequest sc) { + private SecurityCamera save(SecurityCamera newSC, SwitchableSaveRequest sc) { newSC.setName(sc.getName()); newSC.setRoomId(sc.getRoomId()); newSC.setOn(sc.isOn()); @@ -46,13 +46,13 @@ public class SecurityCameraController { } @PostMapping - public SecurityCamera create(@Valid @RequestBody SecurityCameraSaveRequest sc) { + public SecurityCamera create(@Valid @RequestBody SwitchableSaveRequest sc) { return save(new SecurityCamera(), sc); } @PutMapping public SecurityCamera update( - @Valid @RequestBody SecurityCameraSaveRequest sc, final Principal principal) + @Valid @RequestBody SwitchableSaveRequest sc, final Principal principal) throws NotFoundException { return save( securityCameraService diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index 5ef4eed..45c33ff 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -2,11 +2,10 @@ 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.SmartPlugSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.security.Principal; -import java.util.*; import java.util.List; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; @@ -30,7 +29,7 @@ public class SmartPlugController { return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new); } - private SmartPlug save(SmartPlug newSP, SmartPlugSaveRequest sp) { + private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp) { newSP.setOn(sp.isOn()); newSP.setId(sp.getId()); newSP.setName(sp.getName()); @@ -40,12 +39,12 @@ public class SmartPlugController { } @PostMapping - public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) { + public SmartPlug create(@Valid @RequestBody SwitchableSaveRequest sp) { return save(new SmartPlug(), sp); } @PutMapping - public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp, final Principal principal) + public SmartPlug update(@Valid @RequestBody SwitchableSaveRequest sp, final Principal principal) throws NotFoundException { return save( smartPlugRepository diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java deleted file mode 100644 index 1dc9122..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/CurtainsSaveRequest.java +++ /dev/null @@ -1,57 +0,0 @@ -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 CurtainsSaveRequest { - /** The state of this switch */ - @NotNull - @Min(0) - @Max(100) - private int openedAmount; - - /** Device identifier */ - @NotNull private long id; - - /** - * The room this device belongs in, as a foreign key id. To use when updating and inserting from - * a REST call. - */ - @NotNull private Long roomId; - - /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ - @NotNull private String name; - - public void setRoomId(Long roomId) { - this.roomId = roomId; - } - - public void setName(String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public Long getRoomId() { - return roomId; - } - - public String getName() { - return name; - } - - public int getOpening() { - return this.openedAmount; - } - - public void setOpening(int newOpening) { - this.openedAmount = newOpening; - } - - public void setId(long id) { - this.id = id; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableSaveRequest.java similarity index 90% rename from src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java rename to src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableSaveRequest.java index 74e911b..acefa72 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableSaveRequest.java @@ -4,10 +4,10 @@ import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; -public class DimmableLightSaveRequest { +public class DimmableSaveRequest { /** Device id (used only for update requests) */ - private Long id; + private long id; /** The light intensity value. Goes from 0 (off) to 100 (on) */ @NotNull @@ -48,11 +48,11 @@ public class DimmableLightSaveRequest { this.intensity = intensity; } - public Long getId() { + public long getId() { return id; } - public void setId(Long id) { + public void setId(long id) { this.id = id; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SecurityCameraSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SecurityCameraSaveRequest.java deleted file mode 100644 index 8778eba..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SecurityCameraSaveRequest.java +++ /dev/null @@ -1,53 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; - -import javax.validation.constraints.NotNull; - -public class SecurityCameraSaveRequest { - - /** The state of this Security Camera */ - private boolean on; - - /** 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. - */ - @NotNull private Long roomId; - - /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ - @NotNull private String name; - - public void setRoomId(Long roomId) { - this.roomId = roomId; - } - - public void setName(String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public Long getRoomId() { - return roomId; - } - - public String getName() { - return name; - } - - public boolean isOn() { - return on; - } - - public void setOn(boolean on) { - this.on = on; - } - - public void setId(long id) { - this.id = id; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java deleted file mode 100644 index 6b2f9b5..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SmartPlugSaveRequest.java +++ /dev/null @@ -1,52 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; - -import javax.validation.constraints.NotNull; - -public class SmartPlugSaveRequest { - /** Whether the smart plug is on */ - @NotNull private boolean on; - - /** 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. - */ - @NotNull private Long roomId; - - /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ - @NotNull private String name; - - public void setRoomId(Long roomId) { - this.roomId = roomId; - } - - public void setName(String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public Long getRoomId() { - return roomId; - } - - public String getName() { - return name; - } - - public boolean isOn() { - return on; - } - - public void setOn(boolean on) { - this.on = on; - } - - public void setId(long id) { - this.id = id; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchableSaveRequest.java similarity index 94% rename from src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java rename to src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchableSaveRequest.java index be9cd13..16caaed 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RegularLightSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchableSaveRequest.java @@ -2,12 +2,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; import javax.validation.constraints.NotNull; -public class RegularLightSaveRequest { +public class SwitchableSaveRequest { /** The state of this switch */ private boolean on; /** Device identifier */ - private Long id; + private long id; /** * The room this device belongs in, as a foreign key id. To use when updating and inserting from diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java index da9af1c..e41265a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java @@ -18,14 +18,14 @@ public class ButtonDimmer extends Dimmer { /** Increases the current intensity level of the dimmable light by DIM_INCREMENT */ public void increaseIntensity() { - for (DimmableLight dl : getOutputs()) { + for (Dimmable dl : getOutputs()) { dl.setIntensity(dl.getIntensity() + DIM_INCREMENT); } } /** Decreases the current intensity level of the dimmable light by DIM_INCREMENT */ public void decreaseIntensity() { - for (DimmableLight dl : getOutputs()) { + for (Dimmable dl : getOutputs()) { dl.setIntensity(dl.getIntensity() - DIM_INCREMENT); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java index cfc3c2f..d9e8d98 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Curtains.java @@ -1,41 +1,14 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -import javax.persistence.Column; import javax.persistence.Entity; -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; -import javax.validation.constraints.NotNull; +/** + * Represents a curtain. The intensity represents how much the curtains are opened, + * 0 is completely closed 100 is completely open + */ @Entity -public class Curtains extends OutputDevice { - - /** - * it represents how much the curtains are opened, 0 is completely closed 100 is completely open - */ - @NotNull - @Column(nullable = false) - @Min(0) - @Max(100) - private int openedAmount; - +public class Curtains extends Dimmable { public Curtains() { super("curtains"); - this.openedAmount = 0; - } - - /** return the current state of the curtain */ - public int getOpenedAmount() { - return this.openedAmount; - } - - /** sets the state to a desired one */ - public void setOpenedAmount(int newOpening) { - if (newOpening < 0) { - this.openedAmount = 0; - } else if (newOpening > 100) { - this.openedAmount = 100; - } else { - this.openedAmount = newOpening; - } } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java index 83f4d16..39821f1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/CurtainsRepository.java @@ -1,3 +1,3 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface CurtainsRepository extends DeviceRepository {} +public interface CurtainsRepository extends DimmableRepository {} 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 new file mode 100644 index 0000000..d8c083a --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmable.java @@ -0,0 +1,78 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; + +import javax.persistence.*; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import java.util.Set; + +@Entity +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +public class Dimmable extends Switchable { + + 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); + } + + @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) + @GsonExclude + private Set dimmers; + + /** The light intensity value. Goes from 0 (off) to 100 (on) */ + @NotNull + @Column(nullable = false) + @Min(0) + @Max(100) + private Integer intensity = 0; + + @NotNull + @Column(nullable = false) + private Integer oldIntensity = 100; + + public Integer getIntensity() { + return intensity; + } + + /** + * Sets the intensity to a certain level. Out of bound values are corrected to the respective + * extremums. An intensity level of 0 turns the light off, but keeps the old intensity level + * stored. + * + * @param intensity the intensity level (may be out of bounds) + */ + public void setIntensity(Integer intensity) { + if (intensity <= 0) { + this.intensity = 0; + } else if (intensity > 100) { + this.intensity = 100; + this.oldIntensity = 100; + } else { + this.intensity = intensity; + this.oldIntensity = intensity; + } + } + + @Override + public boolean isOn() { + return intensity != 0; + } + + @Override + public void setOn(boolean on) { + intensity = on ? oldIntensity : 0; + } + + public Set getDimmers() { + return this.dimmers; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java index ce5c593..89806a7 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java @@ -1,86 +1,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; -import com.google.common.base.Objects; - import javax.persistence.*; -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; -import javax.validation.constraints.NotNull; -import java.util.Set; /** Represent a dimmable light */ @Entity -public class DimmableLight extends Switchable { - - public static final Connector - BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR = - Connector.basic(ButtonDimmer::getOutputs, DimmableLight::getDimmers); - - public static final Connector KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR = - Connector.basic(KnobDimmer::getOutputs, DimmableLight::getDimmers); - +public class DimmableLight extends Dimmable { public DimmableLight() { super("dimmableLight"); } - - @ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH) - @GsonExclude - private Set dimmers; - - /** The light intensity value. Goes from 0 (off) to 100 (on) */ - @NotNull - @Column(nullable = false) - @Min(0) - @Max(100) - private Integer intensity = 0; - - @NotNull - @Column(nullable = false) - private Integer oldIntensity = 100; - - public Integer getIntensity() { - return intensity; - } - - /** - * Sets the intensity to a certain level. Out of bound values are corrected to the respective - * extremums. An intensity level of 0 turns the light off, but keeps the old intensity level - * stored. - * - * @param intensity the intensity level (may be out of bounds) - */ - public void setIntensity(Integer intensity) { - if (intensity <= 0) { - this.intensity = 0; - } else if (intensity > 100) { - this.intensity = 100; - this.oldIntensity = 100; - } else { - this.intensity = intensity; - this.oldIntensity = intensity; - } - } - - @Override - public boolean isOn() { - return intensity != 0; - } - - @Override - public void setOn(boolean on) { - intensity = on ? oldIntensity : 0; - } - - public Set getDimmers() { - return this.dimmers; - } - - @Override - public String toString() { - return Objects.toStringHelper(this) - .add("intensity", intensity) - .add("oldIntensity", oldIntensity) - .toString(); - } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java index a32b3c6..5a45949 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java @@ -1,3 +1,3 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface DimmableLightRepository extends SwitchableRepository {} +public interface DimmableLightRepository extends DimmableRepository {} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableRepository.java new file mode 100644 index 0000000..be791c9 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableRepository.java @@ -0,0 +1,4 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +public interface DimmableRepository extends SwitchableRepository { +} 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 e0b4161..29d0c6b 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 @@ -21,7 +21,7 @@ public abstract class Dimmer extends InputDevice { joinColumns = @JoinColumn(name = "dimmer_id"), inverseJoinColumns = @JoinColumn(name = "dimmable_id") ) - private Set dimmables = new HashSet<>(); + private Set dimmables = new HashSet<>(); /** * Get the lights connected to this dimmer @@ -29,12 +29,12 @@ public abstract class Dimmer extends InputDevice { * @return duh */ @Override - public Set getOutputs() { + public Set getOutputs() { return this.dimmables; } /** Add a light to be controller by this dimmer */ - public void addDimmableLight(DimmableLight dimmableLight) { + public void addDimmable(Dimmable dimmableLight) { dimmables.add(dimmableLight); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java index dc39404..87b527d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java @@ -24,7 +24,7 @@ public class KnobDimmer extends Dimmer { */ public void setLightIntensity(int intensity) { this.intensity = intensity; - for (DimmableLight dl : getOutputs()) { + for (Dimmable dl : getOutputs()) { dl.setIntensity(intensity); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java index 22b3e0c..0568ab6 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java @@ -30,7 +30,7 @@ public class ButtonDimmerTests { dl = new DimmableLight(); dl.setIntensity(10); ; - buttonDimmer.addDimmableLight(dl); + buttonDimmer.addDimmable(dl); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java index e49f736..06bd3e2 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/CurtainsTests.java @@ -19,27 +19,27 @@ public class CurtainsTests { @Test @DisplayName("State when just created") public void initialState() { - assertEquals(0, this.curtains.getOpenedAmount()); + assertEquals(0, this.curtains.getIntensity()); } @Test @DisplayName("Check wether setting the opening works") public void normalSet() { - this.curtains.setOpenedAmount(42); - assertEquals(42, this.curtains.getOpenedAmount()); + this.curtains.setIntensity(42); + assertEquals(42, this.curtains.getIntensity()); } @Test @DisplayName("Set setting a negative number") public void setNeg() { - this.curtains.setOpenedAmount(-1); - assertEquals(0, this.curtains.getOpenedAmount()); + this.curtains.setIntensity(-1); + assertEquals(0, this.curtains.getIntensity()); } @Test @DisplayName("Setting state to a number greater than 100") public void setLarge() { - this.curtains.setOpenedAmount(32768); - assertEquals(100, this.curtains.getOpenedAmount()); + this.curtains.setIntensity(32768); + assertEquals(100, this.curtains.getIntensity()); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java index 155242d..c3a92ac 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java @@ -30,7 +30,7 @@ public class KnobDimmerTests { dl = new DimmableLight(); dl.setIntensity(10); ; - knobDimmer.addDimmableLight(dl); + knobDimmer.addDimmable(dl); } } From ff5dadd1287d8a6527a4d50663e23de1930a3646 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Thu, 16 Apr 2020 18:34:17 +0200 Subject: [PATCH 3/3] fixed tests --- .../usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java | 5 +++-- .../ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java index 0568ab6..45ad962 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/ButtonDimmerTests.java @@ -3,6 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; import static org.junit.jupiter.api.Assertions.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -38,7 +39,7 @@ public class ButtonDimmerTests { @DisplayName(" increase the intensity ") public void increase() { buttonDimmer.increaseIntensity(); - for (DimmableLight dl : buttonDimmer.getOutputs()) { + for (Dimmable dl : buttonDimmer.getOutputs()) { assertTrue(dl.getIntensity() > 10); } } @@ -47,7 +48,7 @@ public class ButtonDimmerTests { @DisplayName(" decrease the intensity ") public void decrease() { buttonDimmer.decreaseIntensity(); - for (DimmableLight dl : buttonDimmer.getOutputs()) { + for (Dimmable dl : buttonDimmer.getOutputs()) { assertTrue(dl.getIntensity() < 10); } } diff --git a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java index c3a92ac..8eb7f74 100644 --- a/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java +++ b/src/test/java/ch/usi/inf/sa4/sanmarinoes/smarthut/KnobDimmerTests.java @@ -2,6 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut; import static org.junit.jupiter.api.Assertions.*; +import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Dimmable; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; import org.junit.jupiter.api.BeforeEach; @@ -38,7 +39,7 @@ public class KnobDimmerTests { @DisplayName(" set the intensity ") public void increase() { knobDimmer.setLightIntensity(30); - for (DimmableLight dl : knobDimmer.getOutputs()) { + for (Dimmable dl : knobDimmer.getOutputs()) { assertEquals(30, dl.getIntensity()); } }