Simplified Models for States
This commit is contained in:
parent
7bb4cd34e1
commit
2bfff689b3
19 changed files with 43 additions and 99 deletions
|
@ -18,13 +18,13 @@ import org.springframework.web.bind.annotation.*;
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@RequestMapping("/buttonDimmer")
|
@RequestMapping("/buttonDimmer")
|
||||||
public class ButtonDimmerController
|
public class ButtonDimmerController
|
||||||
extends InputDeviceConnectionController<ButtonDimmer, Dimmable<?>> {
|
extends InputDeviceConnectionController<ButtonDimmer, Dimmable> {
|
||||||
private ButtonDimmerRepository buttonDimmerRepository;
|
private ButtonDimmerRepository buttonDimmerRepository;
|
||||||
private DimmableRepository<Dimmable<?>> dimmableRepository;
|
private DimmableRepository<Dimmable> dimmableRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected ButtonDimmerController(
|
protected ButtonDimmerController(
|
||||||
ButtonDimmerRepository inputRepository, DimmableRepository<Dimmable<?>> outputRepository) {
|
ButtonDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
|
||||||
super(
|
super(
|
||||||
inputRepository,
|
inputRepository,
|
||||||
outputRepository,
|
outputRepository,
|
||||||
|
@ -53,7 +53,7 @@ public class ButtonDimmerController
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/dim")
|
@PutMapping("/dim")
|
||||||
public Set<Dimmable<?>> dim(
|
public Set<Dimmable> dim(
|
||||||
@Valid @RequestBody final ButtonDimmerDimRequest bd, final Principal principal)
|
@Valid @RequestBody final ButtonDimmerDimRequest bd, final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
final ButtonDimmer buttonDimmer =
|
final ButtonDimmer buttonDimmer =
|
||||||
|
|
|
@ -18,14 +18,14 @@ import org.springframework.web.bind.annotation.*;
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@RequestMapping("/knobDimmer")
|
@RequestMapping("/knobDimmer")
|
||||||
public class KnobDimmerController
|
public class KnobDimmerController
|
||||||
extends InputDeviceConnectionController<KnobDimmer, Dimmable<?>> {
|
extends InputDeviceConnectionController<KnobDimmer, Dimmable> {
|
||||||
|
|
||||||
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
||||||
@Autowired private DimmableRepository<Dimmable<?>> dimmableRepository;
|
@Autowired private DimmableRepository<Dimmable> dimmableRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
protected KnobDimmerController(
|
protected KnobDimmerController(
|
||||||
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable<?>> outputRepository) {
|
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
|
||||||
super(
|
super(
|
||||||
inputRepository,
|
inputRepository,
|
||||||
outputRepository,
|
outputRepository,
|
||||||
|
@ -54,7 +54,7 @@ public class KnobDimmerController
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/dimTo")
|
@PutMapping("/dimTo")
|
||||||
public Set<Dimmable<?>> dimTo(
|
public Set<Dimmable> dimTo(
|
||||||
@Valid @RequestBody final KnobDimmerDimRequest bd, final Principal principal)
|
@Valid @RequestBody final KnobDimmerDimRequest bd, final Principal principal)
|
||||||
throws NotFoundException {
|
throws NotFoundException {
|
||||||
final KnobDimmer dimmer =
|
final KnobDimmer dimmer =
|
||||||
|
|
|
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,7 +7,7 @@ import javax.persistence.Entity;
|
||||||
* 0 is completely closed 100 is completely open
|
* 0 is completely closed 100 is completely open
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
public class Curtains extends Dimmable<Curtains> {
|
public class Curtains extends Dimmable {
|
||||||
public Curtains() {
|
public Curtains() {
|
||||||
super("curtains");
|
super("curtains");
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,13 +10,13 @@ import java.util.Set;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
@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 =
|
KNOB_DIMMER_DIMMABLE_CONNECTOR =
|
||||||
Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers);
|
Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers);
|
||||||
|
|
||||||
public static final Connector<ButtonDimmer, Dimmable<?>>
|
public static final Connector<ButtonDimmer, Dimmable>
|
||||||
BUTTON_DIMMER_DIMMABLE_CONNECTOR =
|
BUTTON_DIMMER_DIMMABLE_CONNECTOR =
|
||||||
Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers);
|
Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers);
|
||||||
|
|
||||||
|
@ -76,13 +76,7 @@ public class Dimmable<T extends Dimmable<T>> extends Switchable implements IDimm
|
||||||
return this.dimmers;
|
return this.dimmers;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
public void readStateAndSet(DimmableState<? extends Dimmable> state) {
|
||||||
public Integer getDimAmount() {
|
setIntensity(state.getIntensity());
|
||||||
return getIntensity();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void setDimAmount(Integer intensity) {
|
|
||||||
setIntensity(intensity);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import javax.persistence.*;
|
||||||
|
|
||||||
/** Represent a dimmable light */
|
/** Represent a dimmable light */
|
||||||
@Entity
|
@Entity
|
||||||
public class DimmableLight extends Dimmable<DimmableLight> {
|
public class DimmableLight extends Dimmable {
|
||||||
public DimmableLight() {
|
public DimmableLight() {
|
||||||
super("dimmableLight");
|
super("dimmableLight");
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** Represent a state for an IDimmable device */
|
/** Represent a state for an IDimmable device */
|
||||||
@Entity
|
@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) */
|
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -17,11 +17,16 @@ public class DimmableState<T extends OutputDevice & AlterableFromState<State<T>>
|
||||||
@Max(100)
|
@Max(100)
|
||||||
private Integer dimAmount = 0;
|
private Integer dimAmount = 0;
|
||||||
|
|
||||||
public Integer getDimAmount() {
|
public Integer getIntensity() {
|
||||||
return dimAmount;
|
return dimAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDimAmount(Integer dimAmount) {
|
public void setIntensity(Integer dimAmount) {
|
||||||
this.dimAmount = dimAmount;
|
this.dimAmount = dimAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply() {
|
||||||
|
getDevice().readStateAndSet(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ public abstract class Dimmer extends InputDevice {
|
||||||
joinColumns = @JoinColumn(name = "dimmer_id"),
|
joinColumns = @JoinColumn(name = "dimmer_id"),
|
||||||
inverseJoinColumns = @JoinColumn(name = "dimmable_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
|
* Get the lights connected to this dimmer
|
||||||
|
@ -29,12 +29,12 @@ public abstract class Dimmer extends InputDevice {
|
||||||
* @return duh
|
* @return duh
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Set<Dimmable<?>> getOutputs() {
|
public Set<Dimmable> getOutputs() {
|
||||||
return this.dimmables;
|
return this.dimmables;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Add a light to be controller by this dimmer */
|
/** Add a light to be controller by this dimmer */
|
||||||
public void addDimmable(Dimmable<?> dimmable) {
|
public void addDimmable(Dimmable dimmable) {
|
||||||
dimmables.add(dimmable);
|
dimmables.add(dimmable);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -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);
|
|
||||||
}
|
|
|
@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** Represents a standard non-dimmable light */
|
/** Represents a standard non-dimmable light */
|
||||||
@Entity
|
@Entity
|
||||||
public class RegularLight extends Switchable implements AlterableFromSwitchableState<RegularLight> {
|
public class RegularLight extends Switchable {
|
||||||
|
|
||||||
/** Whether the light is on or not */
|
/** Whether the light is on or not */
|
||||||
@Column(name = "light_on", nullable = false)
|
@Column(name = "light_on", nullable = false)
|
||||||
|
|
|
@ -5,7 +5,7 @@ import javax.persistence.Entity;
|
||||||
import javax.validation.constraints.NotNull;
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class SecurityCamera extends Switchable implements AlterableFromSwitchableState<SecurityCamera> {
|
public class SecurityCamera extends Switchable {
|
||||||
|
|
||||||
public SecurityCamera() {
|
public SecurityCamera() {
|
||||||
super("securityCamera");
|
super("securityCamera");
|
||||||
|
|
|
@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** A smart plug that can be turned either on or off */
|
/** A smart plug that can be turned either on or off */
|
||||||
@Entity
|
@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 */
|
/** The average consumption of an active plug when on in Watt */
|
||||||
public static final Double AVERAGE_CONSUMPTION_KW = 200.0;
|
public static final Double AVERAGE_CONSUMPTION_KW = 200.0;
|
||||||
|
|
|
@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull;
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||||
public abstract class State<D extends OutputDevice & AlterableFromState<State<D>>> {
|
public abstract class State<D extends OutputDevice> {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
@ -42,9 +42,7 @@ public abstract class State<D extends OutputDevice & AlterableFromState<State<D>
|
||||||
private Long sceneId;
|
private Long sceneId;
|
||||||
|
|
||||||
/** Sets the state of the connected device to the state represented by this object. */
|
/** Sets the state of the connected device to the state represented by this object. */
|
||||||
public void apply() {
|
public abstract void apply();
|
||||||
device.readStateAndSet(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
|
|
|
@ -8,7 +8,7 @@ import java.util.Set;
|
||||||
/** A device that can be turned either on or off */
|
/** A device that can be turned either on or off */
|
||||||
@Entity
|
@Entity
|
||||||
@Inheritance(strategy = InheritanceType.JOINED)
|
@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 =
|
public static final Connector<Switch, Switchable> SWITCH_SWITCHABLE_CONNECTOR =
|
||||||
Connector.basic(Switch::getOutputs, Switchable::getSwitches);
|
Connector.basic(Switch::getOutputs, Switchable::getSwitches);
|
||||||
|
@ -38,4 +38,8 @@ public abstract class Switchable extends OutputDevice implements ISwtichable {
|
||||||
public Set<Switch> getSwitches() {
|
public Set<Switch> getSwitches() {
|
||||||
return inputs;
|
return inputs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void readStateAndSet(SwitchableState<? extends Switchable> state) {
|
||||||
|
setOn(state.isOn());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** Represents a state for a Switchable device */
|
/** Represents a state for a Switchable device */
|
||||||
@Entity
|
@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)
|
@Column(name = "switchable_on", nullable = false)
|
||||||
@NotNull
|
@NotNull
|
||||||
|
@ -19,4 +19,9 @@ public class SwitchableState<T extends OutputDevice & AlterableFromState<State<T
|
||||||
public void setOn(boolean on) {
|
public void setOn(boolean on) {
|
||||||
this.on = on;
|
this.on = on;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void apply() {
|
||||||
|
getDevice().readStateAndSet(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** A thermostat capable of controlling cooling and heating. */
|
/** A thermostat capable of controlling cooling and heating. */
|
||||||
@Entity
|
@Entity
|
||||||
public class Thermostat extends Switchable implements AlterableFromSwitchableState<Thermostat> {
|
public class Thermostat extends Switchable {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isOn() {
|
public boolean isOn() {
|
||||||
|
|
Loading…
Reference in a new issue