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.
This commit is contained in:
parent
ed2900a3bc
commit
f52a38082c
28 changed files with 228 additions and 227 deletions
|
@ -3,7 +3,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
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.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.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -24,7 +24,10 @@ public class ButtonDimmerController
|
||||||
@Autowired
|
@Autowired
|
||||||
protected ButtonDimmerController(
|
protected ButtonDimmerController(
|
||||||
ButtonDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
ButtonDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
||||||
super(inputRepository, outputRepository, ButtonDimmer.CONNECTOR);
|
super(
|
||||||
|
inputRepository,
|
||||||
|
outputRepository,
|
||||||
|
DimmableLight.BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR);
|
||||||
this.buttonDimmerRepository = inputRepository;
|
this.buttonDimmerRepository = inputRepository;
|
||||||
this.dimmableLightRepository = outputRepository;
|
this.dimmableLightRepository = outputRepository;
|
||||||
}
|
}
|
||||||
|
@ -40,7 +43,7 @@ public class ButtonDimmerController
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public ButtonDimmer create(@Valid @RequestBody final ButtonDimmerSaveRequest bd) {
|
public ButtonDimmer create(@Valid @RequestBody final GenericDeviceSaveReguest bd) {
|
||||||
ButtonDimmer newBD = new ButtonDimmer();
|
ButtonDimmer newBD = new ButtonDimmer();
|
||||||
newBD.setName(bd.getName());
|
newBD.setName(bd.getName());
|
||||||
newBD.setRoomId(bd.getRoomId());
|
newBD.setRoomId(bd.getRoomId());
|
||||||
|
|
|
@ -29,14 +29,24 @@ public class DimmableLightController {
|
||||||
return dimmableLightService.findById(id).orElseThrow(NotFoundException::new);
|
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
|
@PostMapping
|
||||||
public DimmableLight create(@Valid @RequestBody DimmableLightSaveRequest dl) {
|
public DimmableLight create(@Valid @RequestBody DimmableLightSaveRequest dl) {
|
||||||
DimmableLight newDL = new DimmableLight();
|
return save(new DimmableLight(), dl);
|
||||||
newDL.setIntensity(dl.getIntensity());
|
}
|
||||||
newDL.setName(dl.getName());
|
|
||||||
newDL.setRoomId(dl.getRoomId());
|
|
||||||
|
|
||||||
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}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -2,8 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
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.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.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -25,7 +25,10 @@ public class KnobDimmerController
|
||||||
@Autowired
|
@Autowired
|
||||||
protected KnobDimmerController(
|
protected KnobDimmerController(
|
||||||
KnobDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
KnobDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
||||||
super(inputRepository, outputRepository, KnobDimmer.CONNECTOR);
|
super(
|
||||||
|
inputRepository,
|
||||||
|
outputRepository,
|
||||||
|
DimmableLight.KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR);
|
||||||
this.knobDimmerRepository = inputRepository;
|
this.knobDimmerRepository = inputRepository;
|
||||||
this.dimmableLightRepository = outputRepository;
|
this.dimmableLightRepository = outputRepository;
|
||||||
}
|
}
|
||||||
|
@ -41,7 +44,7 @@ public class KnobDimmerController
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public KnobDimmer create(@Valid @RequestBody KnobDimmerSaveRequest kd) {
|
public KnobDimmer create(@Valid @RequestBody GenericDeviceSaveReguest kd) {
|
||||||
KnobDimmer newKD = new KnobDimmer();
|
KnobDimmer newKD = new KnobDimmer();
|
||||||
newKD.setName(kd.getName());
|
newKD.setName(kd.getName());
|
||||||
newKD.setRoomId(kd.getRoomId());
|
newKD.setRoomId(kd.getRoomId());
|
||||||
|
|
|
@ -2,7 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
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.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensor;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.MotionSensorRepository;
|
||||||
|
@ -30,22 +30,14 @@ public class MotionSensorController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public MotionSensor create(@Valid @RequestBody MotionSensorSaveRequest ms) {
|
public MotionSensor create(@Valid @RequestBody GenericDeviceSaveReguest ms) {
|
||||||
MotionSensor newMS = new MotionSensor();
|
MotionSensor newMS = new MotionSensor();
|
||||||
newMS.setDetected(ms.isDetected());
|
|
||||||
newMS.setId(ms.getId());
|
|
||||||
newMS.setName(ms.getName());
|
newMS.setName(ms.getName());
|
||||||
newMS.setRoomId(ms.getRoomId());
|
newMS.setRoomId(ms.getRoomId());
|
||||||
|
|
||||||
return motionSensorService.save(newMS);
|
return motionSensorService.save(newMS);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
|
||||||
public MotionSensor update(@Valid @RequestBody MotionSensorSaveRequest ms) {
|
|
||||||
ms.setId(0);
|
|
||||||
return this.create(ms);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void delete(@PathVariable("id") long id) {
|
public void delete(@PathVariable("id") long id) {
|
||||||
motionSensorService.deleteById(id);
|
motionSensorService.deleteById(id);
|
||||||
|
|
|
@ -36,10 +36,7 @@ public class RegularLightController {
|
||||||
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
private RegularLight save(RegularLight newRL, RegularLightSaveRequest rl) {
|
||||||
public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) {
|
|
||||||
RegularLight newRL = new RegularLight();
|
|
||||||
newRL.setId(rl.getId());
|
|
||||||
newRL.setName(rl.getName());
|
newRL.setName(rl.getName());
|
||||||
newRL.setRoomId(rl.getRoomId());
|
newRL.setRoomId(rl.getRoomId());
|
||||||
newRL.setOn(rl.isOn());
|
newRL.setOn(rl.isOn());
|
||||||
|
@ -47,10 +44,16 @@ public class RegularLightController {
|
||||||
return regularLightService.save(newRL);
|
return regularLightService.save(newRL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) {
|
||||||
|
return save(new RegularLight(), rl);
|
||||||
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public RegularLight update(@Valid @RequestBody RegularLightSaveRequest rl) {
|
public RegularLight update(@Valid @RequestBody RegularLightSaveRequest rl)
|
||||||
rl.setId(0);
|
throws NotFoundException {
|
||||||
return this.create(rl);
|
return save(
|
||||||
|
regularLightService.findById(rl.getId()).orElseThrow(NotFoundException::new), rl);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -33,20 +33,12 @@ public class SensorController {
|
||||||
public Sensor create(@Valid @RequestBody SensorSaveRequest s) {
|
public Sensor create(@Valid @RequestBody SensorSaveRequest s) {
|
||||||
Sensor newSensor = new Sensor();
|
Sensor newSensor = new Sensor();
|
||||||
newSensor.setSensor(s.getSensor());
|
newSensor.setSensor(s.getSensor());
|
||||||
newSensor.setValue(s.getValue());
|
|
||||||
newSensor.setId(s.getId());
|
|
||||||
newSensor.setName(s.getName());
|
newSensor.setName(s.getName());
|
||||||
newSensor.setRoomId(s.getRoomId());
|
newSensor.setRoomId(s.getRoomId());
|
||||||
|
|
||||||
return sensorRepository.save(newSensor);
|
return sensorRepository.save(newSensor);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
|
||||||
public Sensor update(@Valid @RequestBody SensorSaveRequest s) {
|
|
||||||
s.setId(0);
|
|
||||||
return this.create(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
public void deleteById(@PathVariable("id") long id) {
|
public void deleteById(@PathVariable("id") long id) {
|
||||||
sensorRepository.deleteById(id);
|
sensorRepository.deleteById(id);
|
||||||
|
|
|
@ -29,9 +29,7 @@ public class SmartPlugController {
|
||||||
return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new);
|
return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
private SmartPlug save(SmartPlug newSP, SmartPlugSaveRequest sp) {
|
||||||
public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) {
|
|
||||||
SmartPlug newSP = new SmartPlug();
|
|
||||||
newSP.setOn(sp.isOn());
|
newSP.setOn(sp.isOn());
|
||||||
newSP.setId(sp.getId());
|
newSP.setId(sp.getId());
|
||||||
newSP.setName(sp.getName());
|
newSP.setName(sp.getName());
|
||||||
|
@ -40,10 +38,15 @@ public class SmartPlugController {
|
||||||
return smartPlugRepository.save(newSP);
|
return smartPlugRepository.save(newSP);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) {
|
||||||
|
return save(new SmartPlug(), sp);
|
||||||
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping
|
||||||
public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp) {
|
public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp) throws NotFoundException {
|
||||||
sp.setId(0);
|
return save(
|
||||||
return this.create(sp);
|
smartPlugRepository.findById(sp.getId()).orElseThrow(NotFoundException::new), sp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -2,7 +2,8 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||||
|
|
||||||
import static ch.usi.inf.sa4.sanmarinoes.smarthut.utils.Utils.toList;
|
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.error.NotFoundException;
|
||||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
@ -15,9 +16,24 @@ import org.springframework.web.bind.annotation.*;
|
||||||
@RestController
|
@RestController
|
||||||
@EnableAutoConfiguration
|
@EnableAutoConfiguration
|
||||||
@RequestMapping("/switch")
|
@RequestMapping("/switch")
|
||||||
public class SwitchController {
|
public class SwitchController extends InputDeviceConnectionController<Switch, Switchable> {
|
||||||
|
|
||||||
@Autowired private SwitchRepository switchRepository;
|
private SwitchRepository switchRepository;
|
||||||
|
private SwitchableRepository<Switchable> 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<Switchable> outputRepository) {
|
||||||
|
super(inputRepository, outputRepository, Switchable.SWITCH_SWITCHABLE_CONNECTOR);
|
||||||
|
this.switchRepository = inputRepository;
|
||||||
|
this.switchableRepository = outputRepository;
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
public List<Switch> findAll() {
|
public List<Switch> findAll() {
|
||||||
|
@ -30,20 +46,48 @@ public class SwitchController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
public Switch create(@Valid @RequestBody SwitchSaveRequest s) {
|
public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s) {
|
||||||
Switch newSwitch = new Switch();
|
Switch newSwitch = new Switch();
|
||||||
newSwitch.setId(s.getId());
|
|
||||||
newSwitch.setName(s.getName());
|
newSwitch.setName(s.getName());
|
||||||
newSwitch.setRoomId(s.getRoomId());
|
newSwitch.setRoomId(s.getRoomId());
|
||||||
newSwitch.setOn(s.isOn());
|
|
||||||
|
|
||||||
return switchRepository.save(newSwitch);
|
return switchRepository.save(newSwitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping
|
@PutMapping("/operate")
|
||||||
public Switch update(@Valid @RequestBody SwitchSaveRequest s) {
|
public Set<Switchable> operate(@Valid @RequestBody final SwitchOperationRequest sr)
|
||||||
s.setId(0);
|
throws NotFoundException {
|
||||||
return this.create(s);
|
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<? extends OutputDevice> addSwitchable(
|
||||||
|
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId)
|
||||||
|
throws NotFoundException {
|
||||||
|
return addOutput(inputId, switchableId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}/lights")
|
||||||
|
public Set<? extends OutputDevice> removeSwitchable(
|
||||||
|
@PathVariable("id") long inputId, @RequestParam("switchableId") Long switchableId)
|
||||||
|
throws NotFoundException {
|
||||||
|
return removeOutput(inputId, switchableId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
@DeleteMapping("/{id}")
|
||||||
|
|
|
@ -6,6 +6,9 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
public class DimmableLightSaveRequest {
|
public class DimmableLightSaveRequest {
|
||||||
|
|
||||||
|
/** Device id (used only for update requests) */
|
||||||
|
private Long id;
|
||||||
|
|
||||||
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
||||||
@NotNull
|
@NotNull
|
||||||
@Min(1)
|
@Min(1)
|
||||||
|
@ -44,4 +47,12 @@ public class DimmableLightSaveRequest {
|
||||||
public void setIntensity(Integer intensity) {
|
public void setIntensity(Integer intensity) {
|
||||||
this.intensity = intensity;
|
this.intensity = intensity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
import javax.validation.constraints.NotNull;
|
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
|
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||||
* a REST call.
|
* a REST call.
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -23,17 +23,11 @@ public class SensorSaveRequest {
|
||||||
LIGHT
|
LIGHT
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The value of this sensor according to its sensor type */
|
|
||||||
private int value;
|
|
||||||
|
|
||||||
/** The type of this sensor */
|
/** The type of this sensor */
|
||||||
@NotNull
|
@NotNull
|
||||||
@Enumerated(value = EnumType.STRING)
|
@Enumerated(value = EnumType.STRING)
|
||||||
private Sensor.SensorType sensor;
|
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
|
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||||
* a REST call.
|
* a REST call.
|
||||||
|
@ -51,10 +45,6 @@ public class SensorSaveRequest {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getRoomId() {
|
public Long getRoomId() {
|
||||||
return roomId;
|
return roomId;
|
||||||
}
|
}
|
||||||
|
@ -70,16 +60,4 @@ public class SensorSaveRequest {
|
||||||
public void setSensor(Sensor.SensorType sensor) {
|
public void setSensor(Sensor.SensorType sensor) {
|
||||||
this.sensor = 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -9,9 +9,6 @@ import javax.persistence.Entity;
|
||||||
@Entity
|
@Entity
|
||||||
public class ButtonDimmer extends Dimmer {
|
public class ButtonDimmer extends Dimmer {
|
||||||
|
|
||||||
public static final Connector<ButtonDimmer, DimmableLight> CONNECTOR =
|
|
||||||
Connector.basic(ButtonDimmer::getOutputs, DimmableLight::setDimmerId);
|
|
||||||
|
|
||||||
/** The delta amount to apply to a increase or decrease intensity */
|
/** The delta amount to apply to a increase or decrease intensity */
|
||||||
private static final int DIM_INCREMENT = 10;
|
private static final int DIM_INCREMENT = 10;
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ public interface Connector<I extends InputDevice, O extends OutputDevice> {
|
||||||
* @return a Connector implementation for the pair of types J and K
|
* @return a Connector implementation for the pair of types J and K
|
||||||
*/
|
*/
|
||||||
static <J extends InputDevice, K extends OutputDevice> Connector<J, K> basic(
|
static <J extends InputDevice, K extends OutputDevice> Connector<J, K> basic(
|
||||||
Function<J, Set<K>> outputsGetter, BiConsumer<K, Long> inputSetter) {
|
Function<J, Set<? super K>> outputsGetter, BiConsumer<K, Long> inputSetter) {
|
||||||
return (i, o, connect) -> {
|
return (i, o, connect) -> {
|
||||||
if (connect) {
|
if (connect) {
|
||||||
outputsGetter.apply(i).add(o);
|
outputsGetter.apply(i).add(o);
|
||||||
|
|
|
@ -11,7 +11,14 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** Represent a dimmable light */
|
/** Represent a dimmable light */
|
||||||
@Entity
|
@Entity
|
||||||
public class DimmableLight extends OutputDevice implements Switchable {
|
public class DimmableLight extends Switchable {
|
||||||
|
|
||||||
|
public static final Connector<ButtonDimmer, DimmableLight>
|
||||||
|
BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR =
|
||||||
|
Connector.basic(ButtonDimmer::getOutputs, DimmableLight::setDimmerId);
|
||||||
|
|
||||||
|
public static final Connector<KnobDimmer, DimmableLight> KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR =
|
||||||
|
Connector.basic(KnobDimmer::getOutputs, DimmableLight::setDimmerId);
|
||||||
|
|
||||||
public DimmableLight() {
|
public DimmableLight() {
|
||||||
super("light");
|
super("light");
|
||||||
|
@ -65,5 +72,12 @@ public class DimmableLight extends OutputDevice implements Switchable {
|
||||||
|
|
||||||
public void setDimmerId(Long dimmerId) {
|
public void setDimmerId(Long dimmerId) {
|
||||||
this.dimmerId = dimmerId;
|
this.dimmerId = dimmerId;
|
||||||
|
super.setSwitchId(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSwitchId(Long switchId) {
|
||||||
|
super.setSwitchId(switchId);
|
||||||
|
this.dimmerId = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
public interface DimmableLightRepository extends DeviceRepository<DimmableLight> {}
|
public interface DimmableLightRepository extends SwitchableRepository<DimmableLight> {}
|
||||||
|
|
|
@ -2,12 +2,15 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import javax.persistence.Entity;
|
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
|
* A generic abstraction for an input device, i.e. something that captures input either from the
|
||||||
* environment (sensor) or the user (switch / dimmer).
|
* environment (sensor) or the user (switch / dimmer).
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
|
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
|
||||||
public abstract class InputDevice extends Device {
|
public abstract class InputDevice extends Device {
|
||||||
public InputDevice(String kind) {
|
public InputDevice(String kind) {
|
||||||
super(kind, FlowType.INPUT);
|
super(kind, FlowType.INPUT);
|
||||||
|
|
|
@ -9,9 +9,6 @@ import javax.persistence.Entity;
|
||||||
@Entity
|
@Entity
|
||||||
public class KnobDimmer extends Dimmer {
|
public class KnobDimmer extends Dimmer {
|
||||||
|
|
||||||
public static final Connector<KnobDimmer, DimmableLight> CONNECTOR =
|
|
||||||
Connector.basic(KnobDimmer::getOutputs, DimmableLight::setDimmerId);
|
|
||||||
|
|
||||||
public KnobDimmer() {
|
public KnobDimmer() {
|
||||||
super("knob-dimmer");
|
super("knob-dimmer");
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** Represents a standard non-dimmable light */
|
/** Represents a standard non-dimmable light */
|
||||||
@Entity
|
@Entity
|
||||||
public class RegularLight extends OutputDevice implements Switchable {
|
public class RegularLight extends Switchable {
|
||||||
|
|
||||||
/** Whether the light is on or not */
|
/** Whether the light is on or not */
|
||||||
@Column(name = "light_on", nullable = false)
|
@Column(name = "light_on", nullable = false)
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
public interface RegularLightRepository extends DeviceRepository<RegularLight> {}
|
public interface RegularLightRepository extends SwitchableRepository<RegularLight> {}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
/** A smart plug that can be turned either on or off */
|
/** A smart plug that can be turned either on or off */
|
||||||
@Entity
|
@Entity
|
||||||
public class SmartPlug extends OutputDevice implements Switchable {
|
public class SmartPlug extends Switchable {
|
||||||
|
|
||||||
/** Whether the smart plug is on */
|
/** Whether the smart plug is on */
|
||||||
@Column(name = "smart_plug_on", nullable = false)
|
@Column(name = "smart_plug_on", nullable = false)
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
public interface SmartPlugRepository extends DeviceRepository<SmartPlug> {}
|
public interface SmartPlugRepository extends SwitchableRepository<SmartPlug> {}
|
||||||
|
|
|
@ -1,12 +1,18 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
|
||||||
/** A switch input device */
|
/** A switch input device */
|
||||||
@Entity
|
@Entity
|
||||||
public class Switch extends InputDevice {
|
public class Switch extends InputDevice {
|
||||||
|
|
||||||
|
@OneToMany(mappedBy = "switchDevice")
|
||||||
|
private Set<Switchable> switchables = new HashSet<>();
|
||||||
|
|
||||||
/** The state of this switch */
|
/** The state of this switch */
|
||||||
@Column(nullable = false, name = "switch_on")
|
@Column(nullable = false, name = "switch_on")
|
||||||
private boolean on;
|
private boolean on;
|
||||||
|
@ -22,6 +28,15 @@ public class Switch extends InputDevice {
|
||||||
*/
|
*/
|
||||||
public void setOn(boolean state) {
|
public void setOn(boolean state) {
|
||||||
on = 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() {
|
public boolean isOn() {
|
||||||
return on;
|
return on;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<Switchable> getOutputs() {
|
||||||
|
return switchables;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,24 +1,47 @@
|
||||||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
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 */
|
/** 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> 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)
|
* Returns whether the device is on (true) or not (false)
|
||||||
*
|
*
|
||||||
* @return 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
|
* Sets the on status of the device
|
||||||
*
|
*
|
||||||
* @param on the new on status: true for on, false for off
|
* @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 */
|
public Long getSwitchId() {
|
||||||
default void toggle() {
|
return switchId;
|
||||||
setOn(!isOn());
|
}
|
||||||
|
|
||||||
|
public void setSwitchId(Long switchId) {
|
||||||
|
this.switchId = switchId;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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<T extends Switchable> extends DeviceRepository<T> {}
|
Loading…
Reference in a new issue