updated button and knob dimmers

This commit is contained in:
Tommaso Rodolfo Masera 2020-02-27 17:24:08 +01:00
parent 3e57e047a5
commit c6a769c1b8
2 changed files with 93 additions and 0 deletions

View file

@ -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<DimmableLight> lights = new HashSet<DimmableLight>();
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();
}
}

View file

@ -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<DimmableLight> lights = new HashSet<DimmableLight>();
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();
}
}