Created models for scenes and states

This commit is contained in:
Claudio Maggioni 2020-04-15 16:21:39 +02:00
parent 1fe31c0d46
commit 6be999ffcc
5 changed files with 205 additions and 0 deletions

View file

@ -3,6 +3,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -57,6 +59,10 @@ public abstract class Device {
*/ */
@Transient private final FlowType flowType; @Transient private final FlowType flowType;
@OneToMany(mappedBy = "device", orphanRemoval = true)
@GsonExclude
private Set<State> states = new HashSet<>();
public long getId() { public long getId() {
return id; return id;
} }

View file

@ -0,0 +1,27 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
/** Represent a dimmable light state */
@Entity
public class DimmableLightState extends State {
/** The light intensity value. Goes from 0 (off) to 100 (on) */
@NotNull
@Column(nullable = false)
@Min(0)
@Max(100)
private Integer intensity = 0;
public Integer getIntensity() {
return intensity;
}
public void setIntensity(Integer intensity) {
this.intensity = intensity;
}
}

View file

@ -0,0 +1,67 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
import io.swagger.annotations.ApiModelProperty;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
/**
* Represent a collection of state changes to devices even in different rooms but belonging to the
* same user
*/
@Entity
public class Scene {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true)
private long id;
@ManyToOne
@JoinColumn(name = "user_id", updatable = false, insertable = false)
@GsonExclude
private User user;
@OneToMany(mappedBy = "scene", orphanRemoval = true)
@GsonExclude
private Set<State> states = new HashSet<>();
@NotNull
@Column(name = "user_id", nullable = false)
private Long userId;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Set<State> getStates() {
return states;
}
public void setStates(Set<State> states) {
this.states = states;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
}

View file

@ -0,0 +1,83 @@
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.*;
import javax.validation.constraints.NotNull;
/**
* Represents instructions on how to change the state of a particular device. Many states (plus
* other properties) form a Scene
*/
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class State {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false, unique = true)
@ApiModelProperty(hidden = true)
private long id;
@ManyToOne
@JoinColumn(name = "device_id", updatable = false, insertable = false)
@GsonExclude
private Device device;
/**
* The device this state belongs in, as a foreign key id. To use when updating and inserting
* from a REST call.
*/
@Column(name = "device_id", nullable = false)
@NotNull
private Long deviceId;
@ManyToOne
@JoinColumn(name = "scene_id", updatable = false, insertable = false)
@GsonExclude
private Scene scene;
@Column(name = "scene_id", nullable = false)
@NotNull
private Long sceneId;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Device getDevice() {
return device;
}
public void setDevice(Device device) {
this.device = device;
}
public Long getDeviceId() {
return deviceId;
}
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}
public Scene getScene() {
return scene;
}
public void setScene(Scene scene) {
this.scene = scene;
}
public Long getSceneId() {
return sceneId;
}
public void setSceneId(Long sceneId) {
this.sceneId = sceneId;
}
}

View file

@ -0,0 +1,22 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
/** A state for RegularLight and SmartPlug */
@Entity
public class SwitchableState extends State {
@Column(name = "switchable_on", nullable = false)
@NotNull
private boolean on;
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
}