Merge branch '51-users-can-add-smart-curtains-blinds' into 'dev'

Resolve "Users can add smart curtains/blinds"

Closes #51

See merge request sa4-2020/the-sanmarinoes/backend!70
This commit is contained in:
Claudio Maggioni 2020-04-15 16:43:06 +02:00
commit afea11931f
8 changed files with 204 additions and 3 deletions

View File

@ -75,6 +75,7 @@ public class SpringFoxConfig {
.or(PathSelectors.regex("/smartPlug.*")::apply) .or(PathSelectors.regex("/smartPlug.*")::apply)
.or(PathSelectors.regex("/switch.*")::apply) .or(PathSelectors.regex("/switch.*")::apply)
.or(PathSelectors.regex("/motionSensor.*")::apply) .or(PathSelectors.regex("/motionSensor.*")::apply)
.or(PathSelectors.regex("/curtains.*")::apply)
.or(PathSelectors.regex("/auth/profile.*")::apply); .or(PathSelectors.regex("/auth/profile.*")::apply);
} }

View File

@ -0,0 +1,56 @@
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.CurtainsSaveRequest;
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Curtains;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.CurtainsRepository;
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.*;
@RestController
@EnableAutoConfiguration
@RequestMapping("/curtains")
public class CurtainsController {
@Autowired private CurtainsRepository curtainsService;
@GetMapping
public List<Curtains> findAll() {
return toList(curtainsService.findAll());
}
@GetMapping("/{id}")
public Curtains findById(@PathVariable("id") long id) throws NotFoundException {
return curtainsService.findById(id).orElseThrow(NotFoundException::new);
}
private Curtains save(Curtains newRL, CurtainsSaveRequest s) {
newRL.setName(s.getName());
newRL.setRoomId(s.getRoomId());
newRL.setOpenedAmount(s.getOpening());
return curtainsService.save(newRL);
}
@PostMapping
public Curtains create(@Valid @RequestBody CurtainsSaveRequest curtain) {
return save(new Curtains(), curtain);
}
@PutMapping
public Curtains update(@Valid @RequestBody CurtainsSaveRequest curtain)
throws NotFoundException {
return save(
curtainsService.findById(curtain.getId()).orElseThrow(NotFoundException::new),
curtain);
}
@DeleteMapping("/{id}")
public void delete(@PathVariable("id") long id) {
curtainsService.deleteById(id);
}
}

View File

@ -0,0 +1,57 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
public class CurtainsSaveRequest {
/** The state of this switch */
@NotNull
@Min(0)
@Max(100)
private int openedAmount;
/** Device identifier */
@NotNull 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 int getOpening() {
return this.openedAmount;
}
public void setOpening(int newOpening) {
this.openedAmount = newOpening;
}
public void setId(long id) {
this.id = id;
}
}

View File

@ -0,0 +1,41 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
@Entity
public class Curtains extends OutputDevice {
/**
* it represents how much the curtains are opened, 0 is completely closed 100 is completely open
*/
@NotNull
@Column(nullable = false)
@Min(0)
@Max(100)
private int openedAmount;
public Curtains() {
super("curtains");
this.openedAmount = 0;
}
/** return the current state of the curtain */
public int getOpenedAmount() {
return this.openedAmount;
}
/** sets the state to a desired one */
public void setOpenedAmount(int newOpening) {
if (newOpening < 0) {
this.openedAmount = 0;
} else if (newOpening > 100) {
this.openedAmount = 100;
} else {
this.openedAmount = newOpening;
}
}
}

View File

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

View File

@ -2,12 +2,11 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import javax.transaction.Transactional;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository; import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import javax.transaction.Transactional;
/** /**
* DeviceRepository acts as a superclass for the other repositories so to mirror in the database the * DeviceRepository acts as a superclass for the other repositories so to mirror in the database the
* class inheritance present among the various devices. * class inheritance present among the various devices.

View File

@ -1,6 +1,5 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut.socket; package ch.usi.inf.sa4.sanmarinoes.smarthut.socket;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonConfig;
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils; import ch.usi.inf.sa4.sanmarinoes.smarthut.config.JWTTokenUtils;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;

View File

@ -0,0 +1,45 @@
package ch.usi.inf.sa4.sanmarinoes.smarthut;
import static org.junit.jupiter.api.Assertions.assertEquals;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Curtains;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
@DisplayName("Curtains tests")
public class CurtainsTests {
private Curtains curtains;
@BeforeEach
public void createCurtains() {
this.curtains = new Curtains();
}
@Test
@DisplayName("State when just created")
public void initialState() {
assertEquals(0, this.curtains.getOpenedAmount());
}
@Test
@DisplayName("Check wether setting the opening works")
public void normalSet() {
this.curtains.setOpenedAmount(42);
assertEquals(42, this.curtains.getOpenedAmount());
}
@Test
@DisplayName("Set setting a negative number")
public void setNeg() {
this.curtains.setOpenedAmount(-1);
assertEquals(0, this.curtains.getOpenedAmount());
}
@Test
@DisplayName("Setting state to a number greater than 100")
public void setLarge() {
this.curtains.setOpenedAmount(32768);
assertEquals(100, this.curtains.getOpenedAmount());
}
}