Simplified Models for States

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-17 20:20:12 +02:00
parent 7bb4cd34e1
commit 2bfff689b3
19 changed files with 43 additions and 99 deletions

View File

@ -18,13 +18,13 @@ import org.springframework.web.bind.annotation.*;
@EnableAutoConfiguration
@RequestMapping("/buttonDimmer")
public class ButtonDimmerController
extends InputDeviceConnectionController<ButtonDimmer, Dimmable<?>> {
extends InputDeviceConnectionController<ButtonDimmer, Dimmable> {
private ButtonDimmerRepository buttonDimmerRepository;
private DimmableRepository<Dimmable<?>> dimmableRepository;
private DimmableRepository<Dimmable> dimmableRepository;
@Autowired
protected ButtonDimmerController(
ButtonDimmerRepository inputRepository, DimmableRepository<Dimmable<?>> outputRepository) {
ButtonDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
super(
inputRepository,
outputRepository,
@ -53,7 +53,7 @@ public class ButtonDimmerController
}
@PutMapping("/dim")
public Set<Dimmable<?>> dim(
public Set<Dimmable> dim(
@Valid @RequestBody final ButtonDimmerDimRequest bd, final Principal principal)
throws NotFoundException {
final ButtonDimmer buttonDimmer =

View File

@ -18,14 +18,14 @@ import org.springframework.web.bind.annotation.*;
@EnableAutoConfiguration
@RequestMapping("/knobDimmer")
public class KnobDimmerController
extends InputDeviceConnectionController<KnobDimmer, Dimmable<?>> {
extends InputDeviceConnectionController<KnobDimmer, Dimmable> {
@Autowired private KnobDimmerRepository knobDimmerRepository;
@Autowired private DimmableRepository<Dimmable<?>> dimmableRepository;
@Autowired private DimmableRepository<Dimmable> dimmableRepository;
@Autowired
protected KnobDimmerController(
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable<?>> outputRepository) {
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
super(
inputRepository,
outputRepository,
@ -54,7 +54,7 @@ public class KnobDimmerController
}
@PutMapping("/dimTo")
public Set<Dimmable<?>> dimTo(
public Set<Dimmable> dimTo(
@Valid @RequestBody final KnobDimmerDimRequest bd, final Principal principal)
throws NotFoundException {
final KnobDimmer dimmer =

View File

@ -1,13 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
/**
* An IDimmable Device whose state can be set by a DimmableState of the corresponding type
* @param <T> the type of the class that extends this interface (just for type bounds, does not mean actually anything)
*/
public interface AlterableFromDimmableState<T extends OutputDevice & AlterableFromDimmableState<T>>
extends IDimmable, AlterableFromState<State<T>> {
default void readStateAndSet(State<T> state) {
final DimmableState<T> hack = (DimmableState<T>) state;
setDimAmount(hack.getDimAmount());
}
}

View File

@ -1,6 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
/** An OutputDevice whose state can be set by a State of corresponding device type */
public interface AlterableFromState<T extends State<? extends OutputDevice>> {
void readStateAndSet(T state);
}

View File

@ -1,13 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
/**
* An ISwitchable Device whose state can be set by a SwitchableState of the corresponding type
* @param <T> the type of the class that extends this interface (just for type bounds, does not mean actually anything)
*/
public interface AlterableFromSwitchableState<T extends OutputDevice & AlterableFromSwitchableState<T>>
extends ISwtichable, AlterableFromState<State<T>> {
default void readStateAndSet(State<T> state) {
final SwitchableState<T> hack = (SwitchableState<T>) state;
setOn(hack.isOn());
}
}

View File

@ -7,7 +7,7 @@ import javax.persistence.Entity;
* 0 is completely closed 100 is completely open
*/
@Entity
public class Curtains extends Dimmable<Curtains> {
public class Curtains extends Dimmable {
public Curtains() {
super("curtains");
}

View File

@ -10,13 +10,13 @@ import java.util.Set;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Dimmable<T extends Dimmable<T>> extends Switchable implements IDimmable, AlterableFromDimmableState<T> {
public class Dimmable extends Switchable {
public static final Connector<KnobDimmer, Dimmable<?>>
public static final Connector<KnobDimmer, Dimmable>
KNOB_DIMMER_DIMMABLE_CONNECTOR =
Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers);
public static final Connector<ButtonDimmer, Dimmable<?>>
public static final Connector<ButtonDimmer, Dimmable>
BUTTON_DIMMER_DIMMABLE_CONNECTOR =
Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers);
@ -76,13 +76,7 @@ public class Dimmable<T extends Dimmable<T>> extends Switchable implements IDimm
return this.dimmers;
}
@Override
public Integer getDimAmount() {
return getIntensity();
}
@Override
public void setDimAmount(Integer intensity) {
setIntensity(intensity);
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
setIntensity(state.getIntensity());
}
}

View File

@ -4,7 +4,7 @@ import javax.persistence.*;
/** Represent a dimmable light */
@Entity
public class DimmableLight extends Dimmable<DimmableLight> {
public class DimmableLight extends Dimmable {
public DimmableLight() {
super("dimmableLight");
}

View File

@ -8,7 +8,7 @@ import javax.validation.constraints.NotNull;
/** Represent a state for an IDimmable device */
@Entity
public class DimmableState<T extends OutputDevice & AlterableFromState<State<T>>> extends State<T> {
public class DimmableState<T extends Dimmable> extends State<T> {
/** The light intensity value. Goes from 0 (off) to 100 (on) */
@NotNull
@ -17,11 +17,16 @@ public class DimmableState<T extends OutputDevice & AlterableFromState<State<T>>
@Max(100)
private Integer dimAmount = 0;
public Integer getDimAmount() {
public Integer getIntensity() {
return dimAmount;
}
public void setDimAmount(Integer dimAmount) {
public void setIntensity(Integer dimAmount) {
this.dimAmount = dimAmount;
}
@Override
public void apply() {
getDevice().readStateAndSet(this);
}
}

View File

@ -21,7 +21,7 @@ public abstract class Dimmer extends InputDevice {
joinColumns = @JoinColumn(name = "dimmer_id"),
inverseJoinColumns = @JoinColumn(name = "dimmable_id")
)
private Set<Dimmable<?>> dimmables = new HashSet<>();
private Set<Dimmable> 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<Dimmable<?>> getOutputs() {
public Set<Dimmable> getOutputs() {
return this.dimmables;
}
/** Add a light to be controller by this dimmer */
public void addDimmable(Dimmable<?> dimmable) {
public void addDimmable(Dimmable dimmable) {
dimmables.add(dimmable);
}
}

View File

@ -1,10 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
/**
* Although not totally enforced in code, it represents an OutputDevice that can be set a state (here "dimAmount")
* that ranges from 0 to 100
*/
public interface IDimmable {
Integer getDimAmount();
void setDimAmount(Integer intensity);
}

View File

@ -1,20 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
/** Interface equivalent of Switchable. Altough not totally enforced in code, it represents an OutputDevice that can be
* turned on or off
*/
public interface ISwtichable {
/**
* Returns whether the device is on (true) or not (false)
*
* @return whether the device is on (true) or not (false)
*/
boolean isOn();
/**
* Sets the on status of the device
*
* @param on the new on status: true for on, false for off
*/
void setOn(boolean on);
}

View File

@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull;
/** Represents a standard non-dimmable light */
@Entity
public class RegularLight extends Switchable implements AlterableFromSwitchableState<RegularLight> {
public class RegularLight extends Switchable {
/** Whether the light is on or not */
@Column(name = "light_on", nullable = false)

View File

@ -5,7 +5,7 @@ import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
@Entity
public class SecurityCamera extends Switchable implements AlterableFromSwitchableState<SecurityCamera> {
public class SecurityCamera extends Switchable {
public SecurityCamera() {
super("securityCamera");

View File

@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull;
/** A smart plug that can be turned either on or off */
@Entity
public class SmartPlug extends Switchable implements AlterableFromSwitchableState<SmartPlug> {
public class SmartPlug extends Switchable {
/** The average consumption of an active plug when on in Watt */
public static final Double AVERAGE_CONSUMPTION_KW = 200.0;

View File

@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull;
*/
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class State<D extends OutputDevice & AlterableFromState<State<D>>> {
public abstract class State<D extends OutputDevice> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@ -42,9 +42,7 @@ public abstract class State<D extends OutputDevice & AlterableFromState<State<D>
private Long sceneId;
/** Sets the state of the connected device to the state represented by this object. */
public void apply() {
device.readStateAndSet(this);
}
public abstract void apply();
public long getId() {
return id;

View File

@ -8,7 +8,7 @@ import java.util.Set;
/** A device that can be turned either on or off */
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Switchable extends OutputDevice implements ISwtichable {
public abstract class Switchable extends OutputDevice {
public static final Connector<Switch, Switchable> SWITCH_SWITCHABLE_CONNECTOR =
Connector.basic(Switch::getOutputs, Switchable::getSwitches);
@ -38,4 +38,8 @@ public abstract class Switchable extends OutputDevice implements ISwtichable {
public Set<Switch> getSwitches() {
return inputs;
}
public void readStateAndSet(SwitchableState<? extends Switchable> state) {
setOn(state.isOn());
}
}

View File

@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull;
/** Represents a state for a Switchable device */
@Entity
public class SwitchableState<T extends OutputDevice & AlterableFromState<State<T>>> extends State<T> {
public class SwitchableState<T extends Switchable> extends State<T> {
@Column(name = "switchable_on", nullable = false)
@NotNull
@ -19,4 +19,9 @@ public class SwitchableState<T extends OutputDevice & AlterableFromState<State<T
public void setOn(boolean on) {
this.on = on;
}
@Override
public void apply() {
getDevice().readStateAndSet(this);
}
}

View File

@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull;
/** A thermostat capable of controlling cooling and heating. */
@Entity
public class Thermostat extends Switchable implements AlterableFromSwitchableState<Thermostat> {
public class Thermostat extends Switchable {
@Override
public boolean isOn() {