Updated to the skeleton data model

Added the concept of rules and a few interfaces
This commit is contained in:
Claudio Maggioni 2020-02-21 14:23:07 +01:00
parent 6fc17052c6
commit 0489d0221c
11 changed files with 148 additions and 19 deletions

View File

@ -1,8 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
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.*;
@ -16,4 +17,10 @@ public class WelcomeController {
List<Device> testDevices() {
return Arrays.asList(new Dimmer(), new Light(), new MotionSensor(), new Sensor(), new SmartPlug(), new Switch());
}
@GetMapping("/giovanni")
public String index(@RequestParam(name = "name", required = false, defaultValue = "World") String name, Model model) {
model.addAttribute("name", name);
return "index";
}
}

View File

@ -2,6 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import com.google.gson.annotations.Expose;
//Rules
/**
* Generic abstraction for a smart home device
*/
@ -23,6 +25,12 @@ public abstract class Device {
@Expose
private long id;
/**
* The name for the category of this particular device (e.g 'dimmer').
*/
@Expose
private String name;
/**
* The name for the category of this particular device (e.g 'dimmer').
*/
@ -42,10 +50,11 @@ public abstract class Device {
public Device(String kind, FlowType flowType) {
synchronized (Device.class) {
id = nextId;
this.id = nextId;
nextId++;
}
this.kind = kind;
this.flowType = flowType;
this.name = kind + " " + this.id;
}
}

View File

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

View File

@ -0,0 +1,20 @@
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

@ -0,0 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public abstract class InputDevice extends Device {
public InputDevice(String kind) {
super(kind, FlowType.INPUT);
}
}

View File

@ -1,24 +1,21 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public class Light extends Device {
public class Light extends Device implements SwitchableOutput {
/**
* If the light bulb is dimmable. If set to true, the only valid intensity values are 0 (off) or 100 (on)
*/
private final boolean dimmable;
/**
* The light intensity value. Goes from 0 (off) to 100 (on)
*/
private int intensity = 0;
public Light(int intensity, boolean dimmable) {
super("light", FlowType.OUTPUT);
this.intensity = intensity;
this.dimmable = dimmable;
}
boolean isOn;
public Light() {
this(0, false);
super("light", FlowType.OUTPUT);
this.isOn = false;
}
@Override
public boolean isOn() {
return isOn;
}
@Override
public void setStatus(boolean isOn) {
this.isOn = isOn;
}
}

View File

@ -0,0 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public abstract class OutputDevice extends Device {
public OutputDevice(String kind) {
super(kind, FlowType.OUTPUT);
}
}

View File

@ -0,0 +1,28 @@
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

@ -0,0 +1,12 @@
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

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

View File

@ -0,0 +1,12 @@
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() {
}
}