Upd. models and added repository for each entity

This commit is contained in:
Claudio Maggioni (maggicl) 2020-02-23 00:11:09 +01:00
parent 32f1412564
commit a353007651
27 changed files with 270 additions and 43 deletions

View file

@ -14,6 +14,7 @@ public class WelcomeController {
@GetMapping("/")
List<Device> testDevices() {
return Arrays.asList(new Dimmer(), new Light(), new MotionSensor(), new Sensor(), new SmartPlug(), new Switch());
return Arrays.asList(new KnobDimmer(), new RegularLight(), new MotionSensor(),
new Sensor(), new SmartPlug(), new Switch());
}
}

View file

@ -0,0 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**
* Represents a dimmer that can only instruct an increase or decrease of intensity
* (i.e. like a dimmer with a '+' and a '-' button)
*/
@Entity
public class ButtonDimmer extends Dimmer {
public ButtonDimmer() {
super("button-dimmer");
}
}

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface ButtonDimmerRepository extends CrudRepository<ButtonDimmer, Long> {
}

View file

@ -6,7 +6,7 @@ import javax.persistence.*;
* Generic abstraction for a smart home device
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Device {
/**
@ -25,27 +25,55 @@ public abstract class Device {
@Column(name = "id", updatable = false, nullable = false)
private long id;
/**
* The room this device belongs in
*/
@ManyToOne
@JoinColumn(name="room_id")
private Room room;
/**
* The name of the device as assigned by the user (e.g. 'Master bedroom light')
*/
@Column
private String name;
/**
* The name for the category of this particular device (e.g 'dimmer').
* Not stored in the database (thanks to 'transient') but set thanks to constructors
*/
@Column
private final String kind;
private transient final String kind;
/**
* The way this device behaves in the automation flow.
* Not stored in the database (thanks to 'transient') but set thanks to constructors
*/
private final FlowType flowType;
private transient final FlowType flowType;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Device(String kind, FlowType flowType) {
this.kind = kind;
this.flowType = flowType;

View file

@ -1,13 +1,22 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
/**
* Represent a dimmable light
*/
@Entity
public class DimmableLight extends Light {
public DimmableLight() {
super("light");
}
/**
* The light intensity value. Goes from 0 (off) to 100 (on)
*/
@Column
private int intensity = 0;
public int getIntensity() {

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface DimmableLightRepository extends CrudRepository<DimmableLight, Long> {
}

View file

@ -1,10 +1,16 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
/**
* Represents a generic dimmer input device
*/
@Entity
public class Dimmer extends InputDevice {
public Dimmer() {
super("dimmer");
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Dimmer extends InputDevice {
public Dimmer(String kind) {
super(kind);
}
}

View file

@ -2,6 +2,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**
* A generic abstraction for an input device, i.e. something that captures input either from the environment (sensor)
* or the user (switch / dimmer).
*/
@Entity
public abstract class InputDevice extends Device {
public InputDevice(String kind) {

View file

@ -0,0 +1,13 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**
* Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity value, like a knob)
*/
@Entity
public class KnobDimmer extends Dimmer {
public KnobDimmer() {
super("knob-dimmer");
}
}

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface KnobDimmerRepository extends CrudRepository<KnobDimmer, Long> {
}

View file

@ -5,14 +5,21 @@ import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
/**
* Represents a generic light
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class Light extends OutputDevice {
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Light extends OutputDevice {
/**
* Whether the light is on or not
*/
@Column(name="light_on")
boolean on;
public Light() {
super("light");
protected Light(String kind) {
super(kind);
this.on = false;
}

View file

@ -2,6 +2,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**
* Represents a motion sensor device
*/
@Entity
public class MotionSensor extends InputDevice {
public MotionSensor() {

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface MotionSensorRepository extends CrudRepository<MotionSensor, Long> {
}

View file

@ -4,8 +4,11 @@ import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
/**
* Represents a generic output device, i.e. something that causes some behaviour (light, smartPlugs, ...).
*/
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class OutputDevice extends Device {
public OutputDevice(String kind) {
super(kind, FlowType.OUTPUT);

View file

@ -0,0 +1,13 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**
* Represents a standard non-dimmable light
*/
@Entity
public class RegularLight extends Light {
public RegularLight() {
super("regular-light");
}
}

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface RegularLightRepository extends CrudRepository<RegularLight, Long> {
}

View file

@ -1,7 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.*;
import java.util.Set;
/**
* Represents a room in the house owned by the user
*/
@Entity
public class Room {
@ -10,13 +14,25 @@ public class Room {
@Column(name = "id", updatable = false, nullable = false)
private Long id;
/**
* User that owns the house this room is in
*/
@ManyToOne
@JoinColumn(name="user_id")
private User user;
/**
* The user given name of this room (e.g. 'Master bedroom')
*/
@Column
private String name;
/**
* Collectiion of devices present in this room
*/
@OneToMany(mappedBy = "room")
private Set<Device> devices;
public Long getId() {
return id;
}
@ -41,12 +57,17 @@ public class Room {
this.name = name;
}
public Set<Device> getDevices() {
return devices;
}
@Override
public String toString() {
return "Room{" +
"id=" + id +
", user=" + user +
", name='" + name + '\'' +
", devices=" + devices +
'}';
}
}

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface RoomRepository extends CrudRepository<Room, User> {
}

View file

@ -1,9 +1,43 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
/**
* A sensor input device that measures a quantity in a continuous scale (e.g. temperature)
*/
@Entity
public class Sensor extends InputDevice {
/**
* Type of sensor, i.e. of the thing the sensor measures.
*/
enum SensorType {
/**
* A sensor that measures temperature in degrees celsius
*/
TEMPERATURE,
/**
* A sensor that measures relative humidity in percentage points
*/
HUMIDITY,
/**
* A sensor that measures light in degrees
*/
LIGHT
}
/**
* The type of this sensor
*/
@Column
@Enumerated(value=EnumType.STRING)
private SensorType sensor;
public Sensor() {
super("sensor");
}

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface SensorRepository extends CrudRepository<Sensor, Long> {
}

View file

@ -1,9 +1,28 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
/**
* A smart plug that can be turned either on or off
*/
@Entity
public class SmartPlug extends OutputDevice {
/**
* Whether the smart plug is on
*/
@Column(name="smart_plug_on")
private boolean on;
public boolean isOn() {
return on;
}
public void setOn(boolean on) {
this.on = on;
}
public SmartPlug() {
super("smart-plug");
}

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface SmartPlugRepository extends CrudRepository<SmartPlug, Long> {
}

View file

@ -2,6 +2,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity;
/**
* A switch input device
* TODO: define switch behaviour (push button vs on/off state)
*/
@Entity
public class Switch extends InputDevice {
public Switch() {

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface SwitchRepository extends CrudRepository<Switch, Long> {
}

View file

@ -3,6 +3,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.*;
import java.util.Set;
/**
* A user of the Smarthut application
*/
@Entity(name = "smarthutuser")
public class User {
@ -11,15 +14,29 @@ public class User {
@Column(name = "id", updatable = false, nullable = false)
private Long id;
/**
* The full name of the user
*/
@Column
private String name;
/**
* A properly salted way to store the password
* TODO: define the implementation of salt
*/
@Column
private String hashedPassword;
/**
* The user's email
* TODO: validate email in setters
*/
@Column
private String email;
/**
* All rooms in the user's house
*/
@OneToMany(mappedBy = "user")
private Set<Room> rooms;

View file

@ -0,0 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {
}

View file

@ -1,29 +0,0 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.ArrayList;
public class house {
//a list of the rooms present in the house
private ArrayList<Room> rooms;
//Constructor
public house(){
this.rooms=new ArrayList<Room>();
}
//this method add a Room to the house
public void addRoom(Room room){
if(!this.rooms.contains(room)) {
this.rooms.add(room);
}
}
//this method removes the given room if it is present in the house
public void remove(Room room){
int a=this.rooms.indexOf(room);
if(a!=-1) {
this.rooms.remove(a);
}
}
}