Merge branch 'device-unification' into 'dev'

Device unification

See merge request sa4-2020/the-sanmarinoes/backend!17
This commit is contained in:
Claudio Maggioni 2020-03-02 22:05:05 +01:00
commit eede89b7be
32 changed files with 123 additions and 176 deletions

View file

@ -50,6 +50,16 @@ public class SpringFoxConfig {
return List.of(new ApiKey("Bearer", "Authorization", "header")); return List.of(new ApiKey("Bearer", "Authorization", "header"));
} }
/**
* Return a Java functional API predicate for regex matches
*
* @param regex the regex to match on
* @return a Java functional API predicate
*/
private Predicate<String> regexPredicate(final String regex) {
return regex(regex)::apply;
}
/** /**
* Configures the paths the documentation must be generated for. Add a path here only when the * Configures the paths the documentation must be generated for. Add a path here only when the
* spec has been totally defined. * spec has been totally defined.
@ -57,7 +67,9 @@ public class SpringFoxConfig {
* @return A predicate that tests whether a path must be included or not * @return A predicate that tests whether a path must be included or not
*/ */
private Predicate<String> paths() { private Predicate<String> paths() {
return ((Predicate<String>) regex("/auth.*")::apply).or(regex("/register.*")::apply); return regexPredicate("/auth.*")
.or(regexPredicate("/room.*"))
.or(regexPredicate("/register.*"));
} }
/** /**

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.ButtonDimmerSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.ButtonDimmerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -16,8 +19,8 @@ public class ButtonDimmerController {
@Autowired private ButtonDimmerRepository buttonDimmerService; @Autowired private ButtonDimmerRepository buttonDimmerService;
@GetMapping @GetMapping
public Iterable<ButtonDimmer> findAll() { public List<ButtonDimmer> findAll() {
return buttonDimmerService.findAll(); return toList(buttonDimmerService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -31,7 +34,6 @@ public class ButtonDimmerController {
newBD.setLights(bd.getLights()); newBD.setLights(bd.getLights());
newBD.setId(bd.getId()); newBD.setId(bd.getId());
newBD.setName(bd.getName()); newBD.setName(bd.getName());
newBD.setRoom(bd.getRoom());
newBD.setRoomId(bd.getRoomId()); newBD.setRoomId(bd.getRoomId());
return buttonDimmerService.save(newBD); return buttonDimmerService.save(newBD);

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.DimmableLightSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableLightSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -17,8 +20,8 @@ public class DimmableLightController {
@Autowired private DimmableLightRepository dimmableLightService; @Autowired private DimmableLightRepository dimmableLightService;
@GetMapping @GetMapping
public Iterable<DimmableLight> findAll() { public List<DimmableLight> findAll() {
return dimmableLightService.findAll(); return toList(dimmableLightService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -32,7 +35,6 @@ public class DimmableLightController {
newDL.setIntensity(dl.getIntensity()); newDL.setIntensity(dl.getIntensity());
newDL.setId(dl.getId()); newDL.setId(dl.getId());
newDL.setName(dl.getName()); newDL.setName(dl.getName());
newDL.setRoom(dl.getRoom());
newDL.setRoomId(dl.getRoomId()); newDL.setRoomId(dl.getRoomId());
return dimmableLightService.save(newDL); return dimmableLightService.save(newDL);

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.KnobDimmerSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmer;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.KnobDimmerRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -17,8 +20,8 @@ public class KnobDimmerController {
@Autowired private KnobDimmerRepository knobDimmerService; @Autowired private KnobDimmerRepository knobDimmerService;
@GetMapping @GetMapping
public Iterable<KnobDimmer> findAll() { public List<KnobDimmer> findAll() {
return knobDimmerService.findAll(); return toList(knobDimmerService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -32,7 +35,6 @@ public class KnobDimmerController {
newKD.setLights(kd.getLights()); newKD.setLights(kd.getLights());
newKD.setId(kd.getId()); newKD.setId(kd.getId());
newKD.setName(kd.getName()); newKD.setName(kd.getName());
newKD.setRoom(kd.getRoom());
newKD.setRoomId(kd.getRoomId()); newKD.setRoomId(kd.getRoomId());
return knobDimmerService.save(newKD); return knobDimmerService.save(newKD);

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.MotionSensorSaveRequest;
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;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -17,8 +20,8 @@ public class MotionSensorController {
@Autowired private MotionSensorRepository motionSensorService; @Autowired private MotionSensorRepository motionSensorService;
@GetMapping @GetMapping
public Iterable<MotionSensor> findAll() { public List<MotionSensor> findAll() {
return motionSensorService.findAll(); return toList(motionSensorService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -32,7 +35,6 @@ public class MotionSensorController {
newMS.setDetected(ms.isDetected()); newMS.setDetected(ms.isDetected());
newMS.setId(ms.getId()); newMS.setId(ms.getId());
newMS.setName(ms.getName()); newMS.setName(ms.getName());
newMS.setRoom(ms.getRoom());
newMS.setRoomId(ms.getRoomId()); newMS.setRoomId(ms.getRoomId());
return motionSensorService.save(newMS); return motionSensorService.save(newMS);

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.RegularLightSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RegularLightSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -24,8 +27,8 @@ public class RegularLightController {
@Autowired private RegularLightRepository regularLightService; @Autowired private RegularLightRepository regularLightService;
@GetMapping @GetMapping
public Iterable<RegularLight> findAll() { public List<RegularLight> findAll() {
return regularLightService.findAll(); return toList(regularLightService.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -38,7 +41,6 @@ public class RegularLightController {
RegularLight newRL = new RegularLight(); RegularLight newRL = new RegularLight();
newRL.setId(rl.getId()); newRL.setId(rl.getId());
newRL.setName(rl.getName()); newRL.setName(rl.getName());
newRL.setRoom(rl.getRoom());
newRL.setRoomId(rl.getRoomId()); newRL.setRoomId(rl.getRoomId());
newRL.setOn(rl.isOn()); newRL.setOn(rl.isOn());

View file

@ -1,5 +1,7 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.RoomSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal; import java.security.Principal;
@ -19,13 +21,15 @@ public class RoomController {
@Autowired private UserRepository userRepository; @Autowired private UserRepository userRepository;
@Autowired private DeviceRepository<Device> deviceRepository;
@GetMapping @GetMapping
public Iterable<Room> findAll() { public List<Room> findAll() {
return roomRepository.findAll(); return toList(roomRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public Optional<Room> findById(@PathVariable("id") long id) { public @ResponseBody Optional<Room> findById(@PathVariable("id") long id) {
return roomRepository.findById(id); return roomRepository.findById(id);
} }
@ -38,7 +42,6 @@ public class RoomController {
final String icon = r.getIcon(); final String icon = r.getIcon();
newRoom.setUserId(userId); newRoom.setUserId(userId);
newRoom.setUser(null);
if (img != null) { if (img != null) {
newRoom.setImage(img.getBytes()); newRoom.setImage(img.getBytes());
} else if (setWhenNull) { } else if (setWhenNull) {
@ -54,12 +57,14 @@ public class RoomController {
} }
@PostMapping @PostMapping
public Room create(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { public @ResponseBody Room create(
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
return this.save(r, principal, true); return this.save(r, principal, true);
} }
@PutMapping @PutMapping
public Room update(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { public @ResponseBody Room update(
@Valid @RequestBody RoomSaveRequest r, final Principal principal) {
return this.save(r, principal, false); return this.save(r, principal, false);
} }
@ -67,4 +72,13 @@ public class RoomController {
public void deleteById(@PathVariable("id") long id) { public void deleteById(@PathVariable("id") long id) {
roomRepository.deleteById(id); roomRepository.deleteById(id);
} }
/**
* Returns a List<Device> of all Devices that are present in a given room (identified by its
* id).
*/
@GetMapping(path = "/{roomId}/devices")
public @ResponseBody List<? extends Device> getDevices(@PathVariable("roomId") long roomid) {
return deviceRepository.findByRoomId(roomid);
}
} }

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.SensorSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SensorSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*; import java.util.*;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
@ -16,8 +19,8 @@ public class SensorController {
@Autowired private SensorRepository sensorRepository; @Autowired private SensorRepository sensorRepository;
@GetMapping @GetMapping
public Iterable<Sensor> findAll() { public List<Sensor> findAll() {
return sensorRepository.findAll(); return toList(sensorRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -32,7 +35,6 @@ public class SensorController {
newSensor.setValue(s.getValue()); newSensor.setValue(s.getValue());
newSensor.setId(s.getId()); newSensor.setId(s.getId());
newSensor.setName(s.getName()); newSensor.setName(s.getName());
newSensor.setRoom(s.getRoom());
newSensor.setRoomId(s.getRoomId()); newSensor.setRoomId(s.getRoomId());
return sensorRepository.save(newSensor); return sensorRepository.save(newSensor);

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.SmartPlugSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SmartPlugSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*; import java.util.*;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
@ -16,8 +19,8 @@ public class SmartPlugController {
@Autowired private SmartPlugRepository smartPlugRepository; @Autowired private SmartPlugRepository smartPlugRepository;
@GetMapping @GetMapping
public Iterable<SmartPlug> findAll() { public List<SmartPlug> findAll() {
return smartPlugRepository.findAll(); return toList(smartPlugRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -31,7 +34,6 @@ public class SmartPlugController {
newSP.setOn(sp.isOn()); newSP.setOn(sp.isOn());
newSP.setId(sp.getId()); newSP.setId(sp.getId());
newSP.setName(sp.getName()); newSP.setName(sp.getName());
newSP.setRoom(sp.getRoom());
newSP.setRoomId(sp.getRoomId()); newSP.setRoomId(sp.getRoomId());
return smartPlugRepository.save(newSP); return smartPlugRepository.save(newSP);

View file

@ -1,8 +1,11 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; 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.SwitchSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.util.*; import java.util.*;
import java.util.List;
import javax.validation.Valid; import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.*; import org.springframework.boot.autoconfigure.*;
@ -16,8 +19,8 @@ public class SwitchController {
@Autowired private SwitchRepository switchRepository; @Autowired private SwitchRepository switchRepository;
@GetMapping @GetMapping
public Iterable<Switch> findAll() { public List<Switch> findAll() {
return switchRepository.findAll(); return toList(switchRepository.findAll());
} }
@GetMapping("/{id}") @GetMapping("/{id}")
@ -30,7 +33,6 @@ public class SwitchController {
Switch newSwitch = new Switch(); Switch newSwitch = new Switch();
newSwitch.setId(s.getId()); newSwitch.setId(s.getId());
newSwitch.setName(s.getName()); newSwitch.setName(s.getName());
newSwitch.setRoom(s.getRoom());
newSwitch.setRoomId(s.getRoomId()); newSwitch.setRoomId(s.getRoomId());
newSwitch.setOn(s.isOn()); newSwitch.setOn(s.isOn());

View file

@ -1,7 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import javax.persistence.Lob; import javax.persistence.Lob;
@ -13,9 +12,6 @@ public class KnobDimmerSaveRequest {
/** Device identifier */ /** Device identifier */
private long id; private long id;
/** The room this device belongs in */
private Room room;
/** /**
* 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.
@ -29,10 +25,6 @@ public class KnobDimmerSaveRequest {
this.id = id; this.id = id;
} }
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) { public void setRoomId(Long roomId) {
this.roomId = roomId; this.roomId = roomId;
} }
@ -45,10 +37,6 @@ public class KnobDimmerSaveRequest {
return id; return id;
} }
public Room getRoom() {
return room;
}
public Long getRoomId() { public Long getRoomId() {
return roomId; return roomId;
} }

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
public class MotionSensorSaveRequest { public class MotionSensorSaveRequest {
@ -9,9 +8,6 @@ public class MotionSensorSaveRequest {
/** Device identifier */ /** Device identifier */
private long id; private long id;
/** The room this device belongs in */
private Room room;
/** /**
* 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.
@ -25,10 +21,6 @@ public class MotionSensorSaveRequest {
this.id = id; this.id = id;
} }
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) { public void setRoomId(Long roomId) {
this.roomId = roomId; this.roomId = roomId;
} }
@ -41,10 +33,6 @@ public class MotionSensorSaveRequest {
return id; return id;
} }
public Room getRoom() {
return room;
}
public Long getRoomId() { public Long getRoomId() {
return roomId; return roomId;
} }

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
public class RegularLightSaveRequest { public class RegularLightSaveRequest {
@ -10,9 +9,6 @@ public class RegularLightSaveRequest {
/** Device identifier */ /** Device identifier */
private long id; private long id;
/** The room this device belongs in */
private Room room;
/** /**
* 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.
@ -26,10 +22,6 @@ public class RegularLightSaveRequest {
this.id = id; this.id = id;
} }
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) { public void setRoomId(Long roomId) {
this.roomId = roomId; this.roomId = roomId;
} }
@ -42,10 +34,6 @@ public class RegularLightSaveRequest {
return id; return id;
} }
public Room getRoom() {
return room;
}
public Long getRoomId() { public Long getRoomId() {
return roomId; return roomId;
} }

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Sensor;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import javax.persistence.EnumType; import javax.persistence.EnumType;
@ -35,9 +34,6 @@ public class SensorSaveRequest {
/** Device identifier */ /** Device identifier */
private long id; private long id;
/** The room this device belongs in */
private Room room;
/** /**
* 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 +47,6 @@ public class SensorSaveRequest {
this.id = id; this.id = id;
} }
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) { public void setRoomId(Long roomId) {
this.roomId = roomId; this.roomId = roomId;
} }
@ -67,10 +59,6 @@ public class SensorSaveRequest {
return id; return id;
} }
public Room getRoom() {
return room;
}
public Long getRoomId() { public Long getRoomId() {
return roomId; return roomId;
} }

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
public class SmartPlugSaveRequest { public class SmartPlugSaveRequest {
@ -10,9 +9,6 @@ public class SmartPlugSaveRequest {
/** Device identifier */ /** Device identifier */
private long id; private long id;
/** The room this device belongs in */
private Room room;
/** /**
* 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.
@ -26,10 +22,6 @@ public class SmartPlugSaveRequest {
this.id = id; this.id = id;
} }
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) { public void setRoomId(Long roomId) {
this.roomId = roomId; this.roomId = roomId;
} }
@ -42,10 +34,6 @@ public class SmartPlugSaveRequest {
return id; return id;
} }
public Room getRoom() {
return room;
}
public Long getRoomId() { public Long getRoomId() {
return roomId; return roomId;
} }

View file

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
public class SwitchSaveRequest { public class SwitchSaveRequest {
@ -10,9 +9,6 @@ public class SwitchSaveRequest {
/** Device identifier */ /** Device identifier */
private long id; private long id;
/** The room this device belongs in */
private Room room;
/** /**
* 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.
@ -26,10 +22,6 @@ public class SwitchSaveRequest {
this.id = id; this.id = id;
} }
public void setRoom(Room room) {
this.room = room;
}
public void setRoomId(Long roomId) { public void setRoomId(Long roomId) {
this.roomId = roomId; this.roomId = roomId;
} }
@ -42,10 +34,6 @@ public class SwitchSaveRequest {
return id; return id;
} }
public Room getRoom() {
return room;
}
public Long getRoomId() { public Long getRoomId() {
return roomId; return roomId;
} }

View file

@ -1,6 +1,9 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany;
/** /**
* Represents a dimmer that can only instruct an increase or decrease of intensity (i.e. like a * Represents a dimmer that can only instruct an increase or decrease of intensity (i.e. like a
@ -12,6 +15,9 @@ public class ButtonDimmer extends Dimmer {
super("button-dimmer"); super("button-dimmer");
} }
@OneToMany(mappedBy = "dimmer")
private Set<DimmableLight> lights;
/** Increases the current intensity level of the dimmable light by 1 */ /** Increases the current intensity level of the dimmable light by 1 */
public void increaseIntensity() { public void increaseIntensity() {
for (DimmableLight dl : lights) { for (DimmableLight dl : lights) {

View file

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

View file

@ -26,12 +26,6 @@ public abstract class Device {
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
private long id; private long id;
/** The room this device belongs in */
@ManyToOne
@JoinColumn(name = "room_id", nullable = false, updatable = false, insertable = false)
@ApiModelProperty(hidden = true)
private Room room;
/** /**
* 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.
@ -69,14 +63,6 @@ public abstract class Device {
this.id = id; this.id = id;
} }
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public String getName() { public String getName() {
return name; return name;
} }

View file

@ -0,0 +1,13 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
/**
* DeviceRepository acts as a superclass for the other repositories so to mirror in the database the
* class inheritance present among the various devices.
*/
public interface DeviceRepository<T extends Device> extends CrudRepository<T, Long> {
List<T> findByRoomId(@Param("roomId") long roomId);
}

View file

@ -2,6 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.validation.constraints.Max; import javax.validation.constraints.Max;
import javax.validation.constraints.Min; import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -14,6 +15,8 @@ public class DimmableLight extends Light {
super("light"); super("light");
} }
@ManyToOne private Dimmer dimmer;
/** The light intensity value. Goes from 0 (off) to 100 (on) */ /** The light intensity value. Goes from 0 (off) to 100 (on) */
@NotNull @NotNull
@Column(nullable = false) @Column(nullable = false)

View file

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

View file

@ -1,6 +1,8 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.Set;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.OneToMany;
/** /**
* Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity * Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity
@ -12,6 +14,9 @@ public class KnobDimmer extends Dimmer {
super("knob-dimmer"); super("knob-dimmer");
} }
@OneToMany(mappedBy = "dimmer")
private Set<DimmableLight> lights;
/** /**
* Increases or decreases the current intensity level by 5, moving between absolute multiples of * Increases or decreases the current intensity level by 5, moving between absolute multiples of
* 5 between 0 and 100, of all dimmable lights mapped to this knob * 5 between 0 and 100, of all dimmable lights mapped to this knob

View file

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

View file

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

View file

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

View file

@ -1,7 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@ -42,17 +41,6 @@ public class Room {
@Column(nullable = false) @Column(nullable = false)
private String name; private String name;
/** Collection of devices present in this room */
@ApiModelProperty(hidden = true)
@OneToMany(mappedBy = "room")
private Set<Device> devices;
/** User that owns the house this room is in */
@ManyToOne
@JoinColumn(name = "user_id", nullable = false, updatable = false, insertable = false)
@ApiModelProperty(hidden = true)
private User user;
public Long getId() { public Long getId() {
return id; return id;
} }
@ -69,14 +57,6 @@ public class Room {
this.userId = userId; this.userId = userId;
} }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getName() { public String getName() {
return name; return name;
} }
@ -85,10 +65,6 @@ public class Room {
this.name = name; this.name = name;
} }
public Set<Device> getDevices() {
return devices;
}
public byte[] getIcon() { public byte[] getIcon() {
return icon; return icon;
} }
@ -107,6 +83,6 @@ public class Room {
@Override @Override
public String toString() { public String toString() {
return "Room{" + "id=" + id + ", user=" + user + ", name='" + name + "\'}"; return "Room{" + "id=" + id + ", name='" + name + "\'}";
} }
} }

View file

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

View file

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

View file

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

View file

@ -1,7 +1,6 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models; package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.util.Set;
import javax.persistence.*; import javax.persistence.*;
import javax.validation.constraints.Email; import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
@ -52,11 +51,6 @@ public class User {
@Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address") @Pattern(regexp = ".+@.+\\..+", message = "Please provide a valid email address")
private String email; private String email;
/** All rooms in the user's house */
@OneToMany(mappedBy = "user")
@ApiModelProperty(hidden = true)
private Set<Room> rooms;
@Column(nullable = false) @Column(nullable = false)
@ApiModelProperty(hidden = true) @ApiModelProperty(hidden = true)
private Boolean isEnabled = false; private Boolean isEnabled = false;
@ -101,10 +95,6 @@ public class User {
this.password = password; this.password = password;
} }
public Set<Room> getRooms() {
return rooms;
}
public Boolean getEnabled() { public Boolean getEnabled() {
return isEnabled; return isEnabled;
} }

View file

@ -0,0 +1,14 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.utils;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/** A class with a bunch of useful static methods */
public class Utils {
private Utils() {}
public static <T> List<T> toList(Iterable<T> iterable) {
return StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList());
}
}