diff --git a/build.gradle b/build.gradle index a293ca9..48ad88a 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java index a875104..4069d44 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/WelcomeController.java @@ -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.*; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index 12575d0..3f373d6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -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; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java index 988d4b5..c79f785 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLight.java @@ -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; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableOutput.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableOutput.java deleted file mode 100644 index ccdc80a..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableOutput.java +++ /dev/null @@ -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); - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java index caf2330..691a7b7 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Dimmer.java @@ -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"); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java index 7af532e..cd36ea4 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/InputDevice.java @@ -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); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Light.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Light.java index ad6ad55..989a7fa 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Light.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Light.java @@ -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; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java index c76cb76..4526e97 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/MotionSensor.java @@ -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"); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java index 634c35e..8c94814 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/OutputDevice.java @@ -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); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java new file mode 100644 index 0000000..6e4afa3 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Room.java @@ -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 + '\'' + + '}'; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Rule.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Rule.java deleted file mode 100644 index a4862dd..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Rule.java +++ /dev/null @@ -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 { - - private static List> 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 rule) { - rules.add(rule); - } - - public static void applyOnInput(@NotNull InputDevice input) { - rules.stream().filter(input::equals).forEach(Rule::apply); - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java index e4dafe8..e92c6db 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Sensor.java @@ -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"); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java index 0b3a2e7..6d7db70 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlug.java @@ -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"); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index cc56c3f..142665c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -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"); } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchRule.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchRule.java deleted file mode 100644 index 0430176..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchRule.java +++ /dev/null @@ -1,12 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.models; - -public class SwitchRule extends Rule { - public SwitchRule(T input, U output) { - super(input, output); - } - - @Override - void apply() { - output.toggle(); - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableOutput.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableOutput.java deleted file mode 100644 index aaa5b23..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableOutput.java +++ /dev/null @@ -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()); - } -} \ No newline at end of file diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/TimerRule.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/TimerRule.java deleted file mode 100644 index ad78358..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/TimerRule.java +++ /dev/null @@ -1,12 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.models; - -public class TimerRule extends Rule { - public TimerRule(T input, U output) { - super(input, output); - } - - @Override - void apply() { - - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java new file mode 100644 index 0000000..294dcf3 --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -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 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 getRooms() { + return rooms; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", name='" + name + '\'' + + ", hashedPassword='" + hashedPassword + '\'' + + ", email='" + email + '\'' + + ", rooms=" + rooms + + '}'; + } +}