Code review

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-21 16:58:12 +02:00
parent 618c152466
commit 4357606187
13 changed files with 60 additions and 42 deletions

View file

@ -23,7 +23,7 @@ public class Automation {
@GsonExclude
private Set<Trigger<?>> triggers = new HashSet<>();
@OneToMany(mappedBy = "scenes", cascade = CascadeType.DETACH)
@OneToMany(mappedBy = "scenes", cascade = CascadeType.REMOVE)
@GsonExclude
private Set<ScenePriority> scenes = new HashSet<>();

View file

@ -7,7 +7,7 @@ import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class BooleanTrigger<D extends Device> extends Trigger<D> {
public class BooleanTrigger<D extends Device & BooleanTriggerable> extends Trigger<D> {
@Column(name = "switchable_on")
private boolean on;
@ -23,4 +23,9 @@ public class BooleanTrigger<D extends Device> extends Trigger<D> {
public boolean check(boolean on) {
return this.on == on;
}
@Override
public <T extends Device> boolean check() {
return getDevice().readTriggerState() == isOn();
}
}

View file

@ -0,0 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface BooleanTriggerable {
boolean readTriggerState();
}

View file

@ -1,7 +1,6 @@
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.*;
@ -19,8 +18,7 @@ public abstract class Dimmer extends InputDevice {
@JoinTable(
name = "dimmer_dimmable",
joinColumns = @JoinColumn(name = "dimmer_id"),
inverseJoinColumns = @JoinColumn(name = "dimmable_id")
)
inverseJoinColumns = @JoinColumn(name = "dimmable_id"))
private Set<Dimmable> dimmables = new HashSet<>();
/**

View file

@ -8,10 +8,9 @@ import javax.persistence.Entity;
* value, like a knob)
*/
@Entity
public class KnobDimmer extends Dimmer {
public class KnobDimmer extends Dimmer implements RangeTriggerable {
@Column
Integer intensity = 0;
@Column private int intensity = 0;
public KnobDimmer() {
super("knobDimmer");
@ -28,4 +27,9 @@ public class KnobDimmer extends Dimmer {
dl.setIntensity(intensity);
}
}
@Override
public double readTriggerState() {
return intensity;
}
}

View file

@ -5,7 +5,7 @@ import javax.persistence.Entity;
/** Represents a motion sensor device */
@Entity
public class MotionSensor extends InputDevice {
public class MotionSensor extends InputDevice implements BooleanTriggerable {
@Column(nullable = false)
private boolean detected;
@ -21,4 +21,9 @@ public class MotionSensor extends InputDevice {
public MotionSensor() {
super("motionSensor");
}
@Override
public boolean readTriggerState() {
return detected;
}
}

View file

@ -10,6 +10,11 @@ import javax.validation.constraints.NotNull;
@Inheritance(strategy = InheritanceType.JOINED)
public class RangeTrigger<D extends Device> extends Trigger<D> {
@Override
public <T extends Device> boolean check(T d) {
return false;
}
enum Operator {
EQUAL,
LESS,

View file

@ -0,0 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface RangeTriggerable {
double readTriggerState();
}

View file

@ -1,11 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@ -16,12 +13,6 @@ import javax.validation.constraints.NotNull;
@Entity
public class ScenePriority {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true)
private long id;
@ManyToOne
@JoinColumn(name = "automation_id", updatable = false, insertable = false)
@GsonExclude
@ -41,18 +32,11 @@ public class ScenePriority {
@GsonExclude
private Scene scene;
@Column(name = "scene_id", nullable = false)
@Id
@Column(name = "scene_id", nullable = false, updatable = false, unique = true)
@NotNull
private Long sceneId;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Integer getPriority() {
return priority;
}

View file

@ -11,7 +11,7 @@ import javax.validation.constraints.NotNull;
/** A sensor input device that measures a quantity in a continuous scale (e.g. temperature) */
@Entity
public class Sensor extends InputDevice {
public class Sensor extends InputDevice implements RangeTriggerable {
public static final Map<SensorType, BigDecimal> TYPICAL_VALUES =
Map.of(
@ -19,6 +19,11 @@ public class Sensor extends InputDevice {
SensorType.HUMIDITY, new BigDecimal(40.0),
SensorType.LIGHT, new BigDecimal(1000));
@Override
public double readTriggerState() {
return value.doubleValue();
}
/** Type of sensor, i.e. of the thing the sensor measures. */
public enum SensorType {
/** A sensor that measures temperature in degrees celsius */

View file

@ -1,22 +1,20 @@
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.*;
/** A switch input device */
@Entity
public class Switch extends InputDevice {
public class Switch extends InputDevice implements BooleanTriggerable {
@ManyToMany(cascade = CascadeType.DETACH)
@GsonExclude
@JoinTable(
name = "switch_switchable",
joinColumns = @JoinColumn(name = "switch_id"),
inverseJoinColumns = @JoinColumn(name = "switchable_id")
)
inverseJoinColumns = @JoinColumn(name = "switchable_id"))
private Set<Switchable> switchables = new HashSet<>();
/** The state of this switch */
@ -57,4 +55,9 @@ public class Switch extends InputDevice {
public Set<Switchable> getOutputs() {
return switchables;
}
@Override
public boolean readTriggerState() {
return on;
}
}

View file

@ -4,9 +4,7 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import com.google.gson.annotations.SerializedName;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@ -15,7 +13,7 @@ import javax.validation.constraints.NotNull;
/** A thermostat capable of controlling cooling and heating. */
@Entity
public class Thermostat extends Switchable {
public class Thermostat extends Switchable implements BooleanTriggerable {
@Override
public boolean isOn() {
@ -56,6 +54,11 @@ public class Thermostat extends Switchable {
return true;
}
@Override
public boolean readTriggerState() {
return mode != Mode.OFF;
}
public enum Mode {
@SerializedName("OFF")
OFF,
@ -92,11 +95,6 @@ public class Thermostat extends Switchable {
this.mode = Mode.OFF;
}
@Override
public List<Trigger<? extends Device>> triggeredTriggers() {
return triggers.stream().filter(t -> t.check(this.isOn())).collect(Collectors.toList());
}
public void setMode(Mode state) {
this.mode = state;
}

View file

@ -28,9 +28,10 @@ public class DeviceService {
public <T extends Device> T save(T device) {
final Long deviceId = device.getId();
final long deviceId = device.getId();
List<Trigger<? extends Device>> triggers = triggerRepository.findAllByDeviceId(deviceId);
List<? extends Trigger<? extends Device>> triggers =
triggerRepository.findAllByDeviceId(deviceId);
// map activated -> automations
// remove duplicates