From f52a38082ccddcb8fd4af7ca08b1f650f1dd254f Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Sat, 14 Mar 2020 16:59:56 +0100 Subject: [PATCH] General rewrite of device controllers. Now only output devices can be changes with a PUT call on their controller. Other devices use the generic controller. --- .../controller/ButtonDimmerController.java | 9 ++- .../controller/DimmableLightController.java | 20 ++++-- .../controller/KnobDimmerController.java | 9 ++- .../controller/MotionSensorController.java | 12 +--- .../controller/RegularLightController.java | 17 +++-- .../smarthut/controller/SensorController.java | 8 --- .../controller/SmartPlugController.java | 15 +++-- .../smarthut/controller/SwitchController.java | 64 ++++++++++++++++--- .../dto/DimmableLightSaveRequest.java | 11 ++++ ...est.java => GenericDeviceSaveReguest.java} | 2 +- .../smarthut/dto/KnobDimmerSaveRequest.java | 30 --------- .../smarthut/dto/MotionSensorSaveRequest.java | 51 --------------- .../smarthut/dto/SensorSaveRequest.java | 22 ------- .../smarthut/dto/SwitchOperationRequest.java | 35 ++++++++++ .../smarthut/dto/SwitchSaveRequest.java | 52 --------------- .../smarthut/models/ButtonDimmer.java | 3 - .../smarthut/models/Connector.java | 2 +- .../smarthut/models/DimmableLight.java | 16 ++++- .../models/DimmableLightRepository.java | 2 +- .../smarthut/models/InputDevice.java | 3 + .../smarthut/models/KnobDimmer.java | 3 - .../smarthut/models/RegularLight.java | 2 +- .../models/RegularLightRepository.java | 2 +- .../smarthut/models/SmartPlug.java | 2 +- .../smarthut/models/SmartPlugRepository.java | 2 +- .../sanmarinoes/smarthut/models/Switch.java | 19 ++++++ .../smarthut/models/Switchable.java | 35 ++++++++-- .../smarthut/models/SwitchableRepository.java | 7 ++ 28 files changed, 228 insertions(+), 227 deletions(-) rename src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/{ButtonDimmerSaveRequest.java => GenericDeviceSaveReguest.java} (94%) delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequest.java delete mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableRepository.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java index 92d14db..061c6b9 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/ButtonDimmerController.java @@ -3,7 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerDimRequest; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.List; @@ -24,7 +24,10 @@ public class ButtonDimmerController @Autowired protected ButtonDimmerController( ButtonDimmerRepository inputRepository, DimmableLightRepository outputRepository) { - super(inputRepository, outputRepository, ButtonDimmer.CONNECTOR); + super( + inputRepository, + outputRepository, + DimmableLight.BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR); this.buttonDimmerRepository = inputRepository; this.dimmableLightRepository = outputRepository; } @@ -40,7 +43,7 @@ public class ButtonDimmerController } @PostMapping - public ButtonDimmer create(@Valid @RequestBody final ButtonDimmerSaveRequest bd) { + public ButtonDimmer create(@Valid @RequestBody final GenericDeviceSaveReguest bd) { ButtonDimmer newBD = new ButtonDimmer(); newBD.setName(bd.getName()); newBD.setRoomId(bd.getRoomId()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java index 898b035..dd2e1e3 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/DimmableLightController.java @@ -29,14 +29,24 @@ public class DimmableLightController { return dimmableLightService.findById(id).orElseThrow(NotFoundException::new); } + private DimmableLight save(DimmableLight initial, DimmableLightSaveRequest dl) { + initial.setIntensity(dl.getIntensity()); + initial.setName(dl.getName()); + initial.setRoomId(dl.getRoomId()); + + return dimmableLightService.save(initial); + } + @PostMapping public DimmableLight create(@Valid @RequestBody DimmableLightSaveRequest dl) { - DimmableLight newDL = new DimmableLight(); - newDL.setIntensity(dl.getIntensity()); - newDL.setName(dl.getName()); - newDL.setRoomId(dl.getRoomId()); + return save(new DimmableLight(), dl); + } - return dimmableLightService.save(newDL); + @PutMapping + public DimmableLight update(@Valid @RequestBody DimmableLightSaveRequest sp) + throws NotFoundException { + return save( + dimmableLightService.findById(sp.getId()).orElseThrow(NotFoundException::new), sp); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java index 4d51733..9f59889 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/KnobDimmerController.java @@ -2,8 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerDimRequest; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.List; @@ -25,7 +25,10 @@ public class KnobDimmerController @Autowired protected KnobDimmerController( KnobDimmerRepository inputRepository, DimmableLightRepository outputRepository) { - super(inputRepository, outputRepository, KnobDimmer.CONNECTOR); + super( + inputRepository, + outputRepository, + DimmableLight.KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR); this.knobDimmerRepository = inputRepository; this.dimmableLightRepository = outputRepository; } @@ -41,7 +44,7 @@ public class KnobDimmerController } @PostMapping - public KnobDimmer create(@Valid @RequestBody KnobDimmerSaveRequest kd) { + public KnobDimmer create(@Valid @RequestBody GenericDeviceSaveReguest kd) { KnobDimmer newKD = new KnobDimmer(); newKD.setName(kd.getName()); newKD.setRoomId(kd.getRoomId()); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java index c0ba65a..4ba03b1 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/MotionSensorController.java @@ -2,7 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.MotionSensorSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository; @@ -30,22 +30,14 @@ public class MotionSensorController { } @PostMapping - public MotionSensor create(@Valid @RequestBody MotionSensorSaveRequest ms) { + public MotionSensor create(@Valid @RequestBody GenericDeviceSaveReguest ms) { MotionSensor newMS = new MotionSensor(); - newMS.setDetected(ms.isDetected()); - newMS.setId(ms.getId()); newMS.setName(ms.getName()); newMS.setRoomId(ms.getRoomId()); return motionSensorService.save(newMS); } - @PutMapping - public MotionSensor update(@Valid @RequestBody MotionSensorSaveRequest ms) { - ms.setId(0); - return this.create(ms); - } - @DeleteMapping("/{id}") public void delete(@PathVariable("id") long id) { motionSensorService.deleteById(id); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java index acffb30..15ce2e8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RegularLightController.java @@ -36,10 +36,7 @@ public class RegularLightController { return regularLightService.findById(id).orElseThrow(NotFoundException::new); } - @PostMapping - public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) { - RegularLight newRL = new RegularLight(); - newRL.setId(rl.getId()); + private RegularLight save(RegularLight newRL, RegularLightSaveRequest rl) { newRL.setName(rl.getName()); newRL.setRoomId(rl.getRoomId()); newRL.setOn(rl.isOn()); @@ -47,10 +44,16 @@ public class RegularLightController { return regularLightService.save(newRL); } + @PostMapping + public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) { + return save(new RegularLight(), rl); + } + @PutMapping - public RegularLight update(@Valid @RequestBody RegularLightSaveRequest rl) { - rl.setId(0); - return this.create(rl); + public RegularLight update(@Valid @RequestBody RegularLightSaveRequest rl) + throws NotFoundException { + return save( + regularLightService.findById(rl.getId()).orElseThrow(NotFoundException::new), rl); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java index cceb05a..3412e0c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SensorController.java @@ -33,20 +33,12 @@ public class SensorController { public Sensor create(@Valid @RequestBody SensorSaveRequest s) { Sensor newSensor = new Sensor(); newSensor.setSensor(s.getSensor()); - newSensor.setValue(s.getValue()); - newSensor.setId(s.getId()); newSensor.setName(s.getName()); newSensor.setRoomId(s.getRoomId()); return sensorRepository.save(newSensor); } - @PutMapping - public Sensor update(@Valid @RequestBody SensorSaveRequest s) { - s.setId(0); - return this.create(s); - } - @DeleteMapping("/{id}") public void deleteById(@PathVariable("id") long id) { sensorRepository.deleteById(id); diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java index e05ce72..8a57430 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SmartPlugController.java @@ -29,9 +29,7 @@ public class SmartPlugController { return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new); } - @PostMapping - public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) { - SmartPlug newSP = new SmartPlug(); + private SmartPlug save(SmartPlug newSP, SmartPlugSaveRequest sp) { newSP.setOn(sp.isOn()); newSP.setId(sp.getId()); newSP.setName(sp.getName()); @@ -40,10 +38,15 @@ public class SmartPlugController { return smartPlugRepository.save(newSP); } + @PostMapping + public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) { + return save(new SmartPlug(), sp); + } + @PutMapping - public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp) { - sp.setId(0); - return this.create(sp); + public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp) throws NotFoundException { + return save( + smartPlugRepository.findById(sp.getId()).orElseThrow(NotFoundException::new), sp); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java index 9166e15..f90108c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/SwitchController.java @@ -2,7 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList; -import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchSaveRequest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.GenericDeviceSaveReguest; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchOperationRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import java.util.*; @@ -15,9 +16,24 @@ import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration @RequestMapping("/switch") -public class SwitchController { +public class SwitchController extends InputDeviceConnectionController { - @Autowired private SwitchRepository switchRepository; + private SwitchRepository switchRepository; + private SwitchableRepository switchableRepository; + + /** + * Contstructs the controller by requiring essential object for the controller implementation + * + * @param inputRepository the input device repository + * @param outputRepository the output device repository + */ + @Autowired + protected SwitchController( + SwitchRepository inputRepository, SwitchableRepository outputRepository) { + super(inputRepository, outputRepository, Switchable.SWITCH_SWITCHABLE_CONNECTOR); + this.switchRepository = inputRepository; + this.switchableRepository = outputRepository; + } @GetMapping public List findAll() { @@ -30,20 +46,48 @@ public class SwitchController { } @PostMapping - public Switch create(@Valid @RequestBody SwitchSaveRequest s) { + public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s) { Switch newSwitch = new Switch(); - newSwitch.setId(s.getId()); newSwitch.setName(s.getName()); newSwitch.setRoomId(s.getRoomId()); - newSwitch.setOn(s.isOn()); return switchRepository.save(newSwitch); } - @PutMapping - public Switch update(@Valid @RequestBody SwitchSaveRequest s) { - s.setId(0); - return this.create(s); + @PutMapping("/operate") + public Set operate(@Valid @RequestBody final SwitchOperationRequest sr) + throws NotFoundException { + final Switch s = switchRepository.findById(sr.getId()).orElseThrow(NotFoundException::new); + + switch (sr.getType()) { + case ON: + s.setOn(true); + break; + case OFF: + s.setOn(false); + break; + case TOGGLE: + s.toggle(); + break; + } + + switchableRepository.saveAll(s.getOutputs()); + + return s.getOutputs(); + } + + @PostMapping("/{id}/lights") + public Set addSwitchable( + @PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) + throws NotFoundException { + return addOutput(inputId, switchableId); + } + + @DeleteMapping("/{id}/lights") + public Set removeSwitchable( + @PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId) + throws NotFoundException { + return removeOutput(inputId, switchableId); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java index 03e3eb0..01bec1a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/DimmableLightSaveRequest.java @@ -6,6 +6,9 @@ import javax.validation.constraints.NotNull; public class DimmableLightSaveRequest { + /** Device id (used only for update requests) */ + private Long id; + /** The light intensity value. Goes from 0 (off) to 100 (on) */ @NotNull @Min(1) @@ -44,4 +47,12 @@ public class DimmableLightSaveRequest { public void setIntensity(Integer intensity) { this.intensity = intensity; } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GenericDeviceSaveReguest.java similarity index 94% rename from src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java rename to src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GenericDeviceSaveReguest.java index bb0b0df..8ec2671 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/ButtonDimmerSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/GenericDeviceSaveReguest.java @@ -2,7 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; import javax.validation.constraints.NotNull; -public class ButtonDimmerSaveRequest { +public class GenericDeviceSaveReguest { /** * The room this device belongs in, as a foreign key id. To use when updating and inserting from * a REST call. diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java deleted file mode 100644 index b1c0075..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/KnobDimmerSaveRequest.java +++ /dev/null @@ -1,30 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; - -import javax.validation.constraints.NotNull; - -public class KnobDimmerSaveRequest { - /** - * The room this device belongs in, as a foreign key id. To use when updating and inserting from - * a REST call. - */ - @NotNull private Long roomId; - - /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ - @NotNull private String name; - - public void setRoomId(Long roomId) { - this.roomId = roomId; - } - - public void setName(String name) { - this.name = name; - } - - public Long getRoomId() { - return roomId; - } - - public String getName() { - return name; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java deleted file mode 100644 index a44c343..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/MotionSensorSaveRequest.java +++ /dev/null @@ -1,51 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; - -import javax.validation.constraints.NotNull; - -public class MotionSensorSaveRequest { - private boolean detected; - - /** Device identifier */ - private long id; - - /** - * The room this device belongs in, as a foreign key id. To use when updating and inserting from - * a REST call. - */ - @NotNull private Long roomId; - - /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ - @NotNull private String name; - - public void setRoomId(Long roomId) { - this.roomId = roomId; - } - - public void setName(String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public Long getRoomId() { - return roomId; - } - - public String getName() { - return name; - } - - public boolean isDetected() { - return detected; - } - - public void setDetected(boolean detected) { - this.detected = detected; - } - - public void setId(long id) { - this.id = id; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java index 0985db2..e9a5c68 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SensorSaveRequest.java @@ -23,17 +23,11 @@ public class SensorSaveRequest { LIGHT } - /** The value of this sensor according to its sensor type */ - private int value; - /** The type of this sensor */ @NotNull @Enumerated(value = EnumType.STRING) private Sensor.SensorType sensor; - /** Device identifier */ - private long id; - /** * The room this device belongs in, as a foreign key id. To use when updating and inserting from * a REST call. @@ -51,10 +45,6 @@ public class SensorSaveRequest { this.name = name; } - public long getId() { - return id; - } - public Long getRoomId() { return roomId; } @@ -70,16 +60,4 @@ public class SensorSaveRequest { public void setSensor(Sensor.SensorType sensor) { this.sensor = sensor; } - - public int getValue() { - return this.value; - } - - public void setValue(int newValue) { - this.value = newValue; - } - - public void setId(long id) { - this.id = id; - } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequest.java new file mode 100644 index 0000000..3fb552b --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchOperationRequest.java @@ -0,0 +1,35 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.validation.constraints.NotNull; + +/** An on/off/toggle operation on a switch */ +public class SwitchOperationRequest { + + /** The device id */ + @NotNull private Long id; + + public enum OperationType { + ON, + OFF, + TOGGLE + } + + /** The type of switch operation */ + @NotNull private SwitchOperationRequest.OperationType type; + + public OperationType getType() { + return type; + } + + public void setType(OperationType type) { + this.type = type; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java deleted file mode 100644 index 88050c0..0000000 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/SwitchSaveRequest.java +++ /dev/null @@ -1,52 +0,0 @@ -package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; - -import javax.validation.constraints.NotNull; - -public class SwitchSaveRequest { - /** The state of this switch */ - private boolean on; - - /** Device identifier */ - private long id; - - /** - * The room this device belongs in, as a foreign key id. To use when updating and inserting from - * a REST call. - */ - @NotNull private Long roomId; - - /** The name of the device as assigned by the user (e.g. 'Master bedroom light') */ - @NotNull private String name; - - public void setRoomId(Long roomId) { - this.roomId = roomId; - } - - public void setName(String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public Long getRoomId() { - return roomId; - } - - public String getName() { - return name; - } - - public boolean isOn() { - return on; - } - - public void setOn(boolean on) { - this.on = on; - } - - public void setId(long id) { - this.id = id; - } -} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java index 85d4fe0..fc91c22 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java @@ -9,9 +9,6 @@ import javax.persistence.Entity; @Entity public class ButtonDimmer extends Dimmer { - public static final Connector CONNECTOR = - Connector.basic(ButtonDimmer::getOutputs, DimmableLight::setDimmerId); - /** The delta amount to apply to a increase or decrease intensity */ private static final int DIM_INCREMENT = 10; diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connector.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connector.java index dd68b10..91b1e88 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connector.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Connector.java @@ -33,7 +33,7 @@ public interface Connector { * @return a Connector implementation for the pair of types J and K */ static Connector basic( - Function> outputsGetter, BiConsumer inputSetter) { + Function> outputsGetter, BiConsumer inputSetter) { return (i, o, connect) -> { if (connect) { outputsGetter.apply(i).add(o); 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 277a7e9..9a75471 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 @@ -11,7 +11,14 @@ import javax.validation.constraints.NotNull; /** Represent a dimmable light */ @Entity -public class DimmableLight extends OutputDevice implements Switchable { +public class DimmableLight extends Switchable { + + public static final Connector + BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR = + Connector.basic(ButtonDimmer::getOutputs, DimmableLight::setDimmerId); + + public static final Connector KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR = + Connector.basic(KnobDimmer::getOutputs, DimmableLight::setDimmerId); public DimmableLight() { super("light"); @@ -65,5 +72,12 @@ public class DimmableLight extends OutputDevice implements Switchable { public void setDimmerId(Long dimmerId) { this.dimmerId = dimmerId; + super.setSwitchId(null); }; + + @Override + public void setSwitchId(Long switchId) { + super.setSwitchId(switchId); + this.dimmerId = null; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java index 484084b..a32b3c6 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/DimmableLightRepository.java @@ -1,3 +1,3 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface DimmableLightRepository extends DeviceRepository {} +public interface DimmableLightRepository extends SwitchableRepository {} 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 ad96347..da45b67 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 @@ -2,12 +2,15 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; import java.util.Set; import javax.persistence.Entity; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; /** * A generic abstraction for an input device, i.e. something that captures input either from the * environment (sensor) or the user (switch / dimmer). */ @Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 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/KnobDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java index d4eea0e..ce3745c 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java @@ -9,9 +9,6 @@ import javax.persistence.Entity; @Entity public class KnobDimmer extends Dimmer { - public static final Connector CONNECTOR = - Connector.basic(KnobDimmer::getOutputs, DimmableLight::setDimmerId); - public KnobDimmer() { super("knob-dimmer"); } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java index 02b5f35..de9c10a 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLight.java @@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull; /** Represents a standard non-dimmable light */ @Entity -public class RegularLight extends OutputDevice implements Switchable { +public class RegularLight extends Switchable { /** Whether the light is on or not */ @Column(name = "light_on", nullable = false) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLightRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLightRepository.java index 43c4e17..cad8831 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLightRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/RegularLightRepository.java @@ -1,3 +1,3 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface RegularLightRepository extends DeviceRepository {} +public interface RegularLightRepository extends SwitchableRepository {} 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 6a77a13..fe936b3 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 @@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull; /** A smart plug that can be turned either on or off */ @Entity -public class SmartPlug extends OutputDevice implements Switchable { +public class SmartPlug extends Switchable { /** Whether the smart plug is on */ @Column(name = "smart_plug_on", nullable = false) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlugRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlugRepository.java index 0b2fd344..08d145d 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlugRepository.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SmartPlugRepository.java @@ -1,3 +1,3 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; -public interface SmartPlugRepository extends DeviceRepository {} +public interface SmartPlugRepository extends SwitchableRepository {} 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 2576d38..d819dfe 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,12 +1,18 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.HashSet; +import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; +import javax.persistence.OneToMany; /** A switch input device */ @Entity public class Switch extends InputDevice { + @OneToMany(mappedBy = "switchDevice") + private Set switchables = new HashSet<>(); + /** The state of this switch */ @Column(nullable = false, name = "switch_on") private boolean on; @@ -22,6 +28,15 @@ public class Switch extends InputDevice { */ public void setOn(boolean state) { on = state; + + for (final Switchable s : switchables) { + s.setOn(on); + } + } + + /** Toggle between on and off state */ + public void toggle() { + setOn(!isOn()); } /** @@ -32,4 +47,8 @@ public class Switch extends InputDevice { public boolean isOn() { return on; } + + public Set getOutputs() { + return switchables; + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java index e13eea1..5ba0702 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switchable.java @@ -1,24 +1,47 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude; +import javax.persistence.*; + /** A device that can be turned either on or off */ -public interface Switchable { +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +public abstract class Switchable extends OutputDevice { + + public static final Connector SWITCH_SWITCHABLE_CONNECTOR = + Connector.basic(Switch::getOutputs, Switchable::setSwitchId); + + @ManyToOne + @GsonExclude + @JoinColumn(name = "switch_id", updatable = false, insertable = false) + private Switch switchDevice; + + @Column(name = "switch_id") + private Long switchId; + + protected Switchable(String kind) { + super(kind); + } /** * Returns whether the device is on (true) or not (false) * * @return whether the device is on (true) or not (false) */ - boolean isOn(); + public abstract boolean isOn(); /** * Sets the on status of the device * * @param on the new on status: true for on, false for off */ - void setOn(boolean on); + public abstract void setOn(boolean on); - /** Toggle between on are off state */ - default void toggle() { - setOn(!isOn()); + public Long getSwitchId() { + return switchId; + } + + public void setSwitchId(Long switchId) { + this.switchId = switchId; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableRepository.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableRepository.java new file mode 100644 index 0000000..589542d --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/SwitchableRepository.java @@ -0,0 +1,7 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.models; + +/** + * SwitchableRepository acts as a superclass for the other repositories so to mirror in the database + * the class inheritance present among the various switchable devices. + */ +public interface SwitchableRepository extends DeviceRepository {}