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 { dependencies {
implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation('org.springframework.boot:spring-boot-starter-web') { implementation('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-json' 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 ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.*; import java.util.*;

View file

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

View file

@ -1,19 +1,20 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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) * The light intensity value. Goes from 0 (off) to 100 (on)
*/ */
private int intensity = 0; private int intensity = 0;
@Override public int getIntensity() {
public int getDimLevel() {
return intensity; return intensity;
} }
@Override public void setIntensity(int intensity) {
public void dimTo(int level) { this.intensity = intensity;
intensity = level;
} }
} }

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; 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() { public Dimmer() {
super("dimmer", FlowType.INPUT); super("dimmer");
} }
} }

View file

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

View file

@ -1,21 +1,24 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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() { public Light() {
super("light", FlowType.OUTPUT); super("light");
this.isOn = false; this.on = false;
} }
@Override
public boolean isOn() { public boolean isOn() {
return isOn; return on;
} }
@Override public void setOn(boolean on) {
public void setStatus(boolean isOn) { this.on = on;
this.isOn = isOn;
} }
} }

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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() { public MotionSensor() {
super("motion-sensor", FlowType.INPUT); super("motion-sensor");
} }
} }

View file

@ -1,5 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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 abstract class OutputDevice extends Device {
public OutputDevice(String kind) { public OutputDevice(String kind) {
super(kind, FlowType.OUTPUT); 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; 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() { public Sensor() {
super("sensor", FlowType.INPUT); super("sensor");
} }
} }

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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() { public SmartPlug() {
super("smart-plug", FlowType.OUTPUT); super("smart-plug");
} }
} }

View file

@ -1,7 +1,10 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; 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() { 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 +
'}';
}
}