From c6a769c1b8f61fb754b7e399e665943a160024b7 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Thu, 27 Feb 2020 17:24:08 +0100 Subject: [PATCH] updated button and knob dimmers --- .../smarthut/models/ButtonDimmer.java | 43 ++++++++++++++++ .../smarthut/models/KnobDimmer.java | 50 +++++++++++++++++++ 2 files changed, 93 insertions(+) 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 e666c3e..e44e990 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 @@ -1,6 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.HashSet; +import java.util.Set; import javax.persistence.Entity; +import javax.persistence.OneToMany; /** * Represents a dimmer that can only instruct an increase or decrease of intensity (i.e. like a @@ -8,7 +11,47 @@ import javax.persistence.Entity; */ @Entity public class ButtonDimmer extends Dimmer { + @OneToMany(mappedBy = "") + private Set lights = new HashSet(); + public ButtonDimmer() { super("button-dimmer"); } + + /** Increases the current intensity level of the dimmable light by 1 */ + public void increaseIntensity() { + for (DimmableLight dl : lights) { + dl.setIntensity(dl.getIntensity() + 1); + } + } + + /** Decreases the current intensity level of the dimmable light by 1 */ + public void decreaseIntensity() { + for (DimmableLight dl : lights) { + dl.setIntensity(dl.getIntensity() - 1); + } + } + + /** + * 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(); + } } 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 80d59ab..e34ef76 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 @@ -1,6 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.HashSet; +import java.util.Set; import javax.persistence.Entity; +import javax.persistence.OneToMany; /** * Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity @@ -8,7 +11,54 @@ import javax.persistence.Entity; */ @Entity public class KnobDimmer extends Dimmer { + @OneToMany(mappedBy = "") + private Set lights = new HashSet(); + public KnobDimmer() { super("knob-dimmer"); } + + /** + * Increases or decreases the current intensity level by 5, moving between absolute multiples of + * 5 between 0 and 100, of all dimmable lights mapped to this knob + * + * @param inc The direction the knob is turned with + */ + public void modifyIntensity(boolean inc) { + + for (DimmableLight dl : lights) { + int remainder = dl.getIntensity() / 5; + + 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(); + } }