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("/") @GetMapping("/")
List<Device> testDevices() { 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 * Generic abstraction for a smart home device
*/ */
@Entity @Entity
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Device { public abstract class Device {
/** /**
@ -25,27 +25,55 @@ public abstract class Device {
@Column(name = "id", updatable = false, nullable = false) @Column(name = "id", updatable = false, nullable = false)
private long id; 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') * The name of the device as assigned by the user (e.g. 'Master bedroom light')
*/ */
@Column
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').
* Not stored in the database (thanks to 'transient') but set thanks to constructors
*/ */
@Column private transient 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.
* 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() { public long getId() {
return id; 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) { public Device(String kind, FlowType flowType) {
this.kind = kind; this.kind = kind;
this.flowType = flowType; this.flowType = flowType;

View file

@ -1,13 +1,22 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
/**
* Represent a dimmable light
*/
@Entity @Entity
public class DimmableLight extends Light { public class DimmableLight extends Light {
public DimmableLight() {
super("light");
}
/** /**
* The light intensity value. Goes from 0 (off) to 100 (on) * The light intensity value. Goes from 0 (off) to 100 (on)
*/ */
@Column
private int intensity = 0; private int intensity = 0;
public int getIntensity() { 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; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
/**
* Represents a generic dimmer input device
*/
@Entity @Entity
public class Dimmer extends InputDevice { @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public Dimmer() { public abstract class Dimmer extends InputDevice {
super("dimmer"); public Dimmer(String kind) {
super(kind);
} }
} }

View file

@ -2,6 +2,10 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity; 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 @Entity
public abstract class InputDevice extends Device { public abstract class InputDevice extends Device {
public InputDevice(String kind) { 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.Inheritance;
import javax.persistence.InheritanceType; import javax.persistence.InheritanceType;
/**
* Represents a generic light
*/
@Entity @Entity
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Light extends OutputDevice { public abstract class Light extends OutputDevice {
/**
* Whether the light is on or not
*/
@Column(name="light_on") @Column(name="light_on")
boolean on; boolean on;
public Light() { protected Light(String kind) {
super("light"); super(kind);
this.on = false; this.on = false;
} }

View file

@ -2,6 +2,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Entity; import javax.persistence.Entity;
/**
* Represents a motion sensor device
*/
@Entity @Entity
public class MotionSensor extends InputDevice { public class MotionSensor extends InputDevice {
public MotionSensor() { 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.Inheritance;
import javax.persistence.InheritanceType; import javax.persistence.InheritanceType;
/**
* Represents a generic output device, i.e. something that causes some behaviour (light, smartPlugs, ...).
*/
@Entity @Entity
@Inheritance(strategy = InheritanceType.JOINED) @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
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,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; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.*; import javax.persistence.*;
import java.util.Set;
/**
* Represents a room in the house owned by the user
*/
@Entity @Entity
public class Room { public class Room {
@ -10,13 +14,25 @@ public class Room {
@Column(name = "id", updatable = false, nullable = false) @Column(name = "id", updatable = false, nullable = false)
private Long id; private Long id;
/**
* User that owns the house this room is in
*/
@ManyToOne @ManyToOne
@JoinColumn(name="user_id") @JoinColumn(name="user_id")
private User user; private User user;
/**
* The user given name of this room (e.g. 'Master bedroom')
*/
@Column @Column
private String name; private String name;
/**
* Collectiion of devices present in this room
*/
@OneToMany(mappedBy = "room")
private Set<Device> devices;
public Long getId() { public Long getId() {
return id; return id;
} }
@ -41,12 +57,17 @@ public class Room {
this.name = name; this.name = name;
} }
public Set<Device> getDevices() {
return devices;
}
@Override @Override
public String toString() { public String toString() {
return "Room{" + return "Room{" +
"id=" + id + "id=" + id +
", user=" + user + ", user=" + user +
", name='" + name + '\'' + ", 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; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity; 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 @Entity
public class Sensor extends InputDevice { 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() { public Sensor() {
super("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; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
/**
* A smart plug that can be turned either on or off
*/
@Entity @Entity
public class SmartPlug extends OutputDevice { 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() { public SmartPlug() {
super("smart-plug"); 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; import javax.persistence.Entity;
/**
* A switch input device
* TODO: define switch behaviour (push button vs on/off state)
*/
@Entity @Entity
public class Switch extends InputDevice { public class Switch extends InputDevice {
public Switch() { 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 javax.persistence.*;
import java.util.Set; import java.util.Set;
/**
* A user of the Smarthut application
*/
@Entity(name = "smarthutuser") @Entity(name = "smarthutuser")
public class User { public class User {
@ -11,15 +14,29 @@ public class User {
@Column(name = "id", updatable = false, nullable = false) @Column(name = "id", updatable = false, nullable = false)
private Long id; private Long id;
/**
* The full name of the user
*/
@Column @Column
private String name; private String name;
/**
* A properly salted way to store the password
* TODO: define the implementation of salt
*/
@Column @Column
private String hashedPassword; private String hashedPassword;
/**
* The user's email
* TODO: validate email in setters
*/
@Column @Column
private String email; private String email;
/**
* All rooms in the user's house
*/
@OneToMany(mappedBy = "user") @OneToMany(mappedBy = "user")
private Set<Room> rooms; 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);
}
}
}