Merge branch '52-security-cameras' into 'dev'

Resolve "Users can add security camera in a room"

Closes #52

See merge request sa4-2020/the-sanmarinoes/backend!76
This commit is contained in:
Claudio Maggioni 2020-04-15 17:06:30 +02:00
commit 9088a765b8
8 changed files with 163 additions and 1 deletions

View file

@ -71,6 +71,7 @@ public class SpringFoxConfig {
.or(PathSelectors.regex("/dimmableLight.*")::apply) .or(PathSelectors.regex("/dimmableLight.*")::apply)
.or(PathSelectors.regex("/knobDimmer.*")::apply) .or(PathSelectors.regex("/knobDimmer.*")::apply)
.or(PathSelectors.regex("/regularLight.*")::apply) .or(PathSelectors.regex("/regularLight.*")::apply)
.or(PathSelectors.regex("/securityCamera.*")::apply)
.or(PathSelectors.regex("/sensor.*")::apply) .or(PathSelectors.regex("/sensor.*")::apply)
.or(PathSelectors.regex("/smartPlug.*")::apply) .or(PathSelectors.regex("/smartPlug.*")::apply)
.or(PathSelectors.regex("/switch.*")::apply) .or(PathSelectors.regex("/switch.*")::apply)

View file

@ -51,6 +51,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
// dont authenticate this particular request // dont authenticate this particular request
.authorizeRequests() .authorizeRequests()
.antMatchers( .antMatchers(
"/security_camera_videos/**",
"/sensor-socket", "/sensor-socket",
"/auth/login", "/auth/login",
"/swagger-ui.html", "/swagger-ui.html",

View file

@ -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);
}
}

View file

@ -7,7 +7,7 @@ public class RegularLightSaveRequest {
private boolean on; private boolean on;
/** Device identifier */ /** Device identifier */
private long id; 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

View file

@ -0,0 +1,53 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.NotNull;
public class SecurityCameraSaveRequest {
/** The state of this Security Camera */
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;
}
}

View file

@ -0,0 +1,36 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
@Entity
public class SecurityCamera extends Switchable {
public SecurityCamera() {
super("securityCamera");
this.on = false;
}
@Column(name = "camera_on", nullable = false)
@NotNull
private boolean on;
@Column(name = "video", nullable = false)
@NotNull
private String path = "/security_camera_videos/security_camera_1.mp4";
public String getPath() {
return path;
}
@Override
public boolean isOn() {
return on;
}
@Override
public void setOn(boolean on) {
this.on = on;
}
}

View file

@ -0,0 +1,3 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
public interface SecurityCameraRepository extends SwitchableRepository<SecurityCamera> {}