Updated models to implement JPA and agreed ER diagram

This commit is contained in:
Claudio Maggioni 2020-02-22 11:31:10 +01:00
parent 346d305950
commit 0cc59e608c
19 changed files with 186 additions and 122 deletions

View file

@ -14,6 +14,7 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json'
}

View file

@ -3,7 +3,6 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.*;

View file

@ -1,12 +1,12 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.Expose;
//Rules
import javax.persistence.*;
/**
* Generic abstraction for a smart home device
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Device {
/**
@ -17,30 +17,28 @@ public abstract class Device {
OUTPUT
}
private static long nextId = 1;
/**
* Device identifier
*/
@Expose
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private long id;
/**
* The name for the category of this particular device (e.g 'dimmer').
* The name of the device as assigned by the user (e.g. 'Master bedroom light')
*/
@Expose
private String name;
/**
* The name for the category of this particular device (e.g 'dimmer').
*/
@Expose
@Column
private final String kind;
/**
* The way this device behaves in the automation flow.
*/
@Expose
private final FlowType flowType;
@ -49,10 +47,6 @@ public abstract class Device {
}
public Device(String kind, FlowType flowType) {
synchronized (Device.class) {
this.id = nextId;
nextId++;
}
this.kind = kind;
this.flowType = flowType;
this.name = kind + " " + this.id;

View file

@ -1,19 +1,20 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class DimmableLight extends Light implements DimmableOutput {
import javax.persistence.Entity;
@Entity
public class DimmableLight extends Light {
/**
* The light intensity value. Goes from 0 (off) to 100 (on)
*/
private int intensity = 0;
@Override
public int getDimLevel() {
public int getIntensity() {
return intensity;
}
@Override
public void dimTo(int level) {
intensity = level;
public void setIntensity(int intensity) {
this.intensity = intensity;
}
}

View file

@ -1,20 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface DimmableOutput extends SwitchableOutput {
int getDimLevel();
void dimTo(int level);
default void dimUp() {
int newLevel = getDimLevel() + 10;
dimTo(newLevel > 100 ? 100 : newLevel);
setStatus(getDimLevel() != 0);
}
default void dimDown() {
int newLevel = getDimLevel() - 10;
dimTo(newLevel < 0 ? 0 : newLevel);
setStatus(getDimLevel() != 0);
}
}

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class Dimmer extends Device {
import javax.persistence.Entity;
@Entity
public class Dimmer extends InputDevice {
public Dimmer() {
super("dimmer", FlowType.INPUT);
super("dimmer");
}
}

View file

@ -1,5 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
@Entity
public abstract class InputDevice extends Device {
public InputDevice(String kind) {
super(kind, FlowType.INPUT);

View file

@ -1,21 +1,24 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class Light extends Device implements SwitchableOutput {
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
boolean isOn;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Light extends OutputDevice {
boolean on;
public Light() {
super("light", FlowType.OUTPUT);
this.isOn = false;
super("light");
this.on = false;
}
@Override
public boolean isOn() {
return isOn;
return on;
}
@Override
public void setStatus(boolean isOn) {
this.isOn = isOn;
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class MotionSensor extends Device {
import javax.persistence.Entity;
@Entity
public class MotionSensor extends InputDevice {
public MotionSensor() {
super("motion-sensor", FlowType.INPUT);
super("motion-sensor");
}
}

View file

@ -1,5 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class OutputDevice extends Device {
public OutputDevice(String kind) {
super(kind, FlowType.OUTPUT);

View file

@ -0,0 +1,52 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.*;
@Entity
public class Room {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@ManyToOne
@JoinColumn(name="user_id")
private User user;
@Column
private String name;
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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Room{" +
"id=" + id +
", user=" + user +
", name='" + name + '\'' +
'}';
}
}

View file

@ -1,28 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.validation.constraints.NotNull;
import java.util.*;
import java.util.ArrayList;
public abstract class Rule<T extends InputDevice, U extends OutputDevice> {
private static List<Rule<? extends InputDevice, ? extends OutputDevice>> rules = new ArrayList<>();
protected T input;
protected U output;
public Rule(T input, U output) {
this.input = input;
this.output = output;
}
abstract void apply();
public static void addRule(Rule<? extends InputDevice, ? extends OutputDevice> rule) {
rules.add(rule);
}
public static void applyOnInput(@NotNull InputDevice input) {
rules.stream().filter(input::equals).forEach(Rule::apply);
}
}

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class Sensor extends Device {
import javax.persistence.Entity;
@Entity
public class Sensor extends InputDevice {
public Sensor() {
super("sensor", FlowType.INPUT);
super("sensor");
}
}

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class SmartPlug extends Device {
import javax.persistence.Entity;
@Entity
public class SmartPlug extends OutputDevice {
public SmartPlug() {
super("smart-plug", FlowType.OUTPUT);
super("smart-plug");
}
}

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class Switch extends Device {
import javax.persistence.Entity;
@Entity
public class Switch extends InputDevice {
public Switch() {
super("switch", FlowType.INPUT);
super("switch");
}
}

View file

@ -1,12 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class SwitchRule<T extends InputDevice, U extends OutputDevice & SwitchableOutput> extends Rule<T, U> {
public SwitchRule(T input, U output) {
super(input, output);
}
@Override
void apply() {
output.toggle();
}
}

View file

@ -1,10 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface SwitchableOutput {
boolean isOn();
void setStatus(boolean isOn);
default void toggle() {
setStatus(!isOn());
}
}

View file

@ -1,12 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class TimerRule<T extends InputDevice, U extends OutputDevice & SwitchableOutput> extends Rule<T, U> {
public TimerRule(T input, U output) {
super(input, output);
}
@Override
void apply() {
}
}

View file

@ -0,0 +1,72 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.*;
import java.util.Set;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column
private String name;
@Column
private String hashedPassword;
@Column
private String email;
@OneToMany(mappedBy = "user")
private Set<Room> rooms;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getHashedPassword() {
return hashedPassword;
}
public void setHashedPassword(String hashedPassword) {
this.hashedPassword = hashedPassword;
}
public Set<Room> getRooms() {
return rooms;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", hashedPassword='" + hashedPassword + '\'' +
", email='" + email + '\'' +
", rooms=" + rooms +
'}';
}
}