added security camera device
This commit is contained in:
parent
d979050306
commit
66bcecdbe0
5 changed files with 159 additions and 0 deletions
BIN
media/security_camera_videos/security_camera_1.mp4
Normal file
BIN
media/security_camera_videos/security_camera_1.mp4
Normal file
Binary file not shown.
|
@ -0,0 +1,68 @@
|
||||||
|
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.SecurityCameraSaveRequest;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCamera;
|
||||||
|
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.SecurityCameraRepository;
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.List;
|
||||||
|
import javax.validation.Valid;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@RequestMapping("/securityCamera")
|
||||||
|
public class SecurityCameraController {
|
||||||
|
|
||||||
|
@Autowired SecurityCameraRepository securityCameraService;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<SecurityCamera> findAll() {
|
||||||
|
return toList(securityCameraService.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public SecurityCamera findById(@PathVariable("id") long id) throws NotFoundException {
|
||||||
|
return securityCameraService.findById(id).orElseThrow(NotFoundException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SecurityCamera save(SecurityCamera newSC, SecurityCameraSaveRequest sc) {
|
||||||
|
newSC.setName(sc.getName());
|
||||||
|
newSC.setRoomId(sc.getRoomId());
|
||||||
|
newSC.setOn(sc.isOn());
|
||||||
|
|
||||||
|
return securityCameraService.save(newSC);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public SecurityCamera create(@Valid @RequestBody SecurityCameraSaveRequest sc) {
|
||||||
|
return save(new SecurityCamera(), sc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping
|
||||||
|
public SecurityCamera update(
|
||||||
|
@Valid @RequestBody SecurityCameraSaveRequest sc, final Principal principal)
|
||||||
|
throws NotFoundException {
|
||||||
|
return save(
|
||||||
|
securityCameraService
|
||||||
|
.findByIdAndUsername(sc.getId(), principal.getName())
|
||||||
|
.orElseThrow(NotFoundException::new),
|
||||||
|
sc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void delete(@PathVariable("id") long id) {
|
||||||
|
securityCameraService.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,56 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import org.springframework.core.io.FileSystemResource;
|
||||||
|
|
||||||
|
public class SecurityCameraSaveRequest {
|
||||||
|
|
||||||
|
/** 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;
|
||||||
|
|
||||||
|
private FileSystemResource survaillanceVideo;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,32 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import org.springframework.core.io.FileSystemResource;
|
||||||
|
|
||||||
|
public class SecurityCamera extends Switchable {
|
||||||
|
|
||||||
|
public SecurityCamera() {
|
||||||
|
super("securityCamera");
|
||||||
|
this.on = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean on;
|
||||||
|
|
||||||
|
private FileSystemResource surveillanceVideo =
|
||||||
|
new FileSystemResource(
|
||||||
|
(new File("backend/media/security_camera_videos/security_camera_1.mp4")));
|
||||||
|
|
||||||
|
public FileSystemResource getSurveillanceVideo() {
|
||||||
|
return surveillanceVideo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isOn() {
|
||||||
|
return on;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setOn(boolean on) {
|
||||||
|
this.on = on;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||||
|
|
||||||
|
public interface SecurityCameraRepository extends SwitchableRepository<SecurityCamera> {}
|
Loading…
Reference in a new issue