Merge branch '26-redefine-device-update-calls-in-rest-api-as-single-route-and-have-specialized-routes-for' into 'dev'
Resolve "Redefine device update calls in REST api as single route and have specialized routes for triggers on input devices" Closes #26 See merge request sa4-2020/the-sanmarinoes/backend!34
This commit is contained in:
commit
23e3293e9c
46 changed files with 783 additions and 451 deletions
|
@ -23,6 +23,7 @@ public class GsonConfig {
|
|||
private Gson gson() {
|
||||
final GsonBuilder builder = new GsonBuilder();
|
||||
builder.registerTypeAdapter(Json.class, new SpringfoxJsonToGsonAdapter());
|
||||
builder.addSerializationExclusionStrategy(new AnnotationExclusionStrategy());
|
||||
return builder.create();
|
||||
}
|
||||
}
|
||||
|
@ -34,3 +35,16 @@ class SpringfoxJsonToGsonAdapter implements JsonSerializer<Json> {
|
|||
return JsonParser.parseString(json.value());
|
||||
}
|
||||
}
|
||||
|
||||
/** GSON exclusion strategy to exclude attributes with @GsonExclude */
|
||||
class AnnotationExclusionStrategy implements ExclusionStrategy {
|
||||
@Override
|
||||
public boolean shouldSkipField(FieldAttributes f) {
|
||||
return f.getAnnotation(GsonExclude.class) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldSkipClass(Class<?> clazz) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
public @interface GsonExclude {}
|
|
@ -1,6 +1,5 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.config;
|
||||
|
||||
import static springfox.documentation.builders.PathSelectors.regex;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
@ -10,10 +9,9 @@ import org.springframework.context.annotation.Configuration;
|
|||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.service.ApiKey;
|
||||
import springfox.documentation.service.SecurityScheme;
|
||||
import springfox.documentation.service.*;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spi.service.contexts.SecurityContext;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
|
@ -39,7 +37,8 @@ public class SpringFoxConfig {
|
|||
.paths(paths()::test)
|
||||
.build()
|
||||
.apiInfo(apiInfo())
|
||||
.securitySchemes(securitySchemes());
|
||||
.securitySchemes(securitySchemes())
|
||||
.securityContexts(List.of(securityContext()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -51,14 +50,32 @@ public class SpringFoxConfig {
|
|||
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;
|
||||
private SecurityContext securityContext() {
|
||||
return SecurityContext.builder()
|
||||
.securityReferences(defaultAuth())
|
||||
.forPaths(authenticatedPaths()::test)
|
||||
.build();
|
||||
}
|
||||
|
||||
private List<SecurityReference> defaultAuth() {
|
||||
final AuthorizationScope authorizationScope =
|
||||
new AuthorizationScope("global", "accessEverything");
|
||||
return List.of(
|
||||
new SecurityReference("Bearer", new AuthorizationScope[] {authorizationScope}));
|
||||
}
|
||||
|
||||
private Predicate<String> authenticatedPaths() {
|
||||
return ((Predicate<String>) PathSelectors.regex("/auth/update")::apply)
|
||||
.or(PathSelectors.regex("/room.*")::apply)
|
||||
.or(PathSelectors.regex("/device.*")::apply)
|
||||
.or(PathSelectors.regex("/buttonDimmer.*")::apply)
|
||||
.or(PathSelectors.regex("/dimmableLight.*")::apply)
|
||||
.or(PathSelectors.regex("/knobDimmer.*")::apply)
|
||||
.or(PathSelectors.regex("/regularLight.*")::apply)
|
||||
.or(PathSelectors.regex("/sensor.*")::apply)
|
||||
.or(PathSelectors.regex("/smartPlug.*")::apply)
|
||||
.or(PathSelectors.regex("/switch.*")::apply)
|
||||
.or(PathSelectors.regex("/motionSensor.*")::apply);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,11 +2,12 @@ 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.ButtonDimmerDimRequest;
|
||||
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.models.ButtonDimmer;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.ButtonDimmerRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
|
@ -15,37 +16,77 @@ import org.springframework.web.bind.annotation.*;
|
|||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
@RequestMapping("/buttonDimmer")
|
||||
public class ButtonDimmerController {
|
||||
@Autowired private ButtonDimmerRepository buttonDimmerService;
|
||||
public class ButtonDimmerController
|
||||
extends InputDeviceConnectionController<ButtonDimmer, DimmableLight> {
|
||||
private ButtonDimmerRepository buttonDimmerRepository;
|
||||
private DimmableLightRepository dimmableLightRepository;
|
||||
|
||||
@Autowired
|
||||
protected ButtonDimmerController(
|
||||
ButtonDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
||||
super(
|
||||
inputRepository,
|
||||
outputRepository,
|
||||
DimmableLight.BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR);
|
||||
this.buttonDimmerRepository = inputRepository;
|
||||
this.dimmableLightRepository = outputRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<ButtonDimmer> findAll() {
|
||||
return toList(buttonDimmerService.findAll());
|
||||
return toList(buttonDimmerRepository.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ButtonDimmer findById(@PathVariable("id") long id) throws NotFoundException {
|
||||
return buttonDimmerService.findById(id).orElseThrow(NotFoundException::new);
|
||||
return buttonDimmerRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ButtonDimmer create(@Valid @RequestBody final ButtonDimmerSaveRequest bd) {
|
||||
public ButtonDimmer create(@Valid @RequestBody final GenericDeviceSaveReguest bd) {
|
||||
ButtonDimmer newBD = new ButtonDimmer();
|
||||
newBD.setLights(bd.getLights());
|
||||
newBD.setId(bd.getId());
|
||||
newBD.setName(bd.getName());
|
||||
newBD.setRoomId(bd.getRoomId());
|
||||
|
||||
return buttonDimmerService.save(newBD);
|
||||
return buttonDimmerRepository.save(newBD);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public ButtonDimmer update(@Valid @RequestBody ButtonDimmerSaveRequest bd) {
|
||||
return this.create(bd);
|
||||
@PutMapping("/dim")
|
||||
public Set<DimmableLight> dim(@Valid @RequestBody final ButtonDimmerDimRequest bd)
|
||||
throws NotFoundException {
|
||||
final ButtonDimmer buttonDimmer =
|
||||
buttonDimmerRepository.findById(bd.getId()).orElseThrow(NotFoundException::new);
|
||||
|
||||
switch (bd.getDimType()) {
|
||||
case UP:
|
||||
buttonDimmer.increaseIntensity();
|
||||
break;
|
||||
case DOWN:
|
||||
buttonDimmer.decreaseIntensity();
|
||||
break;
|
||||
}
|
||||
|
||||
dimmableLightRepository.saveAll(buttonDimmer.getOutputs());
|
||||
|
||||
return buttonDimmer.getOutputs();
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> addLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
throws NotFoundException {
|
||||
return addOutput(inputId, lightId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> removeLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
throws NotFoundException {
|
||||
return removeOutput(inputId, lightId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable("id") long id) {
|
||||
buttonDimmerService.deleteById(id);
|
||||
buttonDimmerRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DeviceSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.BadDataException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Device;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DeviceRepository;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RoomRepository;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
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("/device")
|
||||
public class DeviceController {
|
||||
|
||||
@Autowired private DeviceRepository<Device> deviceRepository;
|
||||
@Autowired private RoomRepository roomRepository;
|
||||
|
||||
@PutMapping
|
||||
public Device update(@Valid @RequestBody DeviceSaveRequest deviceSaveRequest)
|
||||
throws NotFoundException, BadDataException {
|
||||
final Device d =
|
||||
deviceRepository
|
||||
.findById(deviceSaveRequest.getId())
|
||||
.orElseThrow(NotFoundException::new);
|
||||
|
||||
// check if roomId is valid
|
||||
roomRepository
|
||||
.findById(deviceSaveRequest.getRoomId())
|
||||
.orElseThrow(() -> new BadDataException("roomId is not a valid room id"));
|
||||
|
||||
d.setRoomId(deviceSaveRequest.getRoomId());
|
||||
d.setName(deviceSaveRequest.getName());
|
||||
|
||||
deviceRepository.save(d);
|
||||
return d;
|
||||
}
|
||||
}
|
|
@ -29,20 +29,24 @@ public class DimmableLightController {
|
|||
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
|
||||
public DimmableLight create(@Valid @RequestBody DimmableLightSaveRequest dl) {
|
||||
DimmableLight newDL = new DimmableLight();
|
||||
newDL.setIntensity(dl.getIntensity());
|
||||
newDL.setId(dl.getId());
|
||||
newDL.setName(dl.getName());
|
||||
newDL.setRoomId(dl.getRoomId());
|
||||
|
||||
return dimmableLightService.save(newDL);
|
||||
return save(new DimmableLight(), dl);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public DimmableLight update(@Valid @RequestBody DimmableLightSaveRequest dl) {
|
||||
return this.create(dl);
|
||||
public DimmableLight update(@Valid @RequestBody DimmableLightSaveRequest sp)
|
||||
throws NotFoundException {
|
||||
return save(
|
||||
dimmableLightService.findById(sp.getId()).orElseThrow(NotFoundException::new), sp);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
|
@ -0,0 +1,92 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* An abstract controller for an input device that has output connected to it. Aids to create the
|
||||
* output add and output remove route
|
||||
*
|
||||
* @param <I> the type of device this controller is for
|
||||
* @param <O> the output device attached to I
|
||||
*/
|
||||
public abstract class InputDeviceConnectionController<
|
||||
I extends InputDevice, O extends OutputDevice> {
|
||||
|
||||
private class IOPair {
|
||||
private final I input;
|
||||
private final O output;
|
||||
|
||||
private IOPair(I input, O output) {
|
||||
this.input = input;
|
||||
this.output = output;
|
||||
}
|
||||
}
|
||||
|
||||
private DeviceRepository<I> inputRepository;
|
||||
|
||||
private DeviceRepository<O> outputReposiory;
|
||||
|
||||
private Connector<I, O> connector;
|
||||
|
||||
/**
|
||||
* Contstructs the controller by requiring essential object for the controller implementation
|
||||
*
|
||||
* @param inputRepository the input device repository
|
||||
* @param outputRepository the output device repository
|
||||
* @param connector a appropriate Connector instance for the I and O tyoes.
|
||||
*/
|
||||
protected InputDeviceConnectionController(
|
||||
DeviceRepository<I> inputRepository,
|
||||
DeviceRepository<O> outputRepository,
|
||||
Connector<I, O> connector) {
|
||||
this.inputRepository = inputRepository;
|
||||
this.outputReposiory = outputRepository;
|
||||
this.connector = connector;
|
||||
}
|
||||
|
||||
private IOPair checkConnectionIDs(Long inputId, Long outputId) throws NotFoundException {
|
||||
final I input =
|
||||
inputRepository
|
||||
.findById(inputId)
|
||||
.orElseThrow(() -> new NotFoundException("input device"));
|
||||
final O output =
|
||||
outputReposiory
|
||||
.findById(outputId)
|
||||
.orElseThrow(() -> new NotFoundException("output device"));
|
||||
return new IOPair(input, output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the output device connection creation (add) route
|
||||
*
|
||||
* @param inputId input device id
|
||||
* @param outputId output device id
|
||||
* @return the list of output devices attached to the input device of id inputId
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> addOutput(Long inputId, Long outputId)
|
||||
throws NotFoundException {
|
||||
final IOPair pair = checkConnectionIDs(inputId, outputId);
|
||||
connector.connect(pair.input, pair.output, true);
|
||||
outputReposiory.save(pair.output);
|
||||
return pair.input.getOutputs();
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements the output device connection destruction (remove) route
|
||||
*
|
||||
* @param inputId input device id
|
||||
* @param outputId output device id
|
||||
* @return the list of output devices attached to the input device of id inputId
|
||||
* @throws NotFoundException if inputId or outputId are not valid
|
||||
*/
|
||||
protected Set<? extends OutputDevice> removeOutput(Long inputId, Long outputId)
|
||||
throws NotFoundException {
|
||||
final IOPair pair = checkConnectionIDs(inputId, outputId);
|
||||
connector.connect(pair.input, pair.output, false);
|
||||
outputReposiory.save(pair.output);
|
||||
return pair.input.getOutputs();
|
||||
}
|
||||
}
|
|
@ -2,11 +2,12 @@ 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.GenericDeviceSaveReguest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.KnobDimmerDimRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
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.*;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
|
@ -15,38 +16,70 @@ import org.springframework.web.bind.annotation.*;
|
|||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
@RequestMapping("/knobDimmer")
|
||||
public class KnobDimmerController {
|
||||
public class KnobDimmerController
|
||||
extends InputDeviceConnectionController<KnobDimmer, DimmableLight> {
|
||||
|
||||
@Autowired private KnobDimmerRepository knobDimmerService;
|
||||
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
||||
@Autowired private DimmableLightRepository dimmableLightRepository;
|
||||
|
||||
@Autowired
|
||||
protected KnobDimmerController(
|
||||
KnobDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
||||
super(
|
||||
inputRepository,
|
||||
outputRepository,
|
||||
DimmableLight.KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR);
|
||||
this.knobDimmerRepository = inputRepository;
|
||||
this.dimmableLightRepository = outputRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<KnobDimmer> findAll() {
|
||||
return toList(knobDimmerService.findAll());
|
||||
return toList(knobDimmerRepository.findAll());
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public KnobDimmer findById(@PathVariable("id") long id) throws NotFoundException {
|
||||
return knobDimmerService.findById(id).orElseThrow(NotFoundException::new);
|
||||
return knobDimmerRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public KnobDimmer create(@Valid @RequestBody KnobDimmerSaveRequest kd) {
|
||||
public KnobDimmer create(@Valid @RequestBody GenericDeviceSaveReguest kd) {
|
||||
KnobDimmer newKD = new KnobDimmer();
|
||||
newKD.setLights(kd.getLights());
|
||||
newKD.setId(kd.getId());
|
||||
newKD.setName(kd.getName());
|
||||
newKD.setRoomId(kd.getRoomId());
|
||||
|
||||
return knobDimmerService.save(newKD);
|
||||
return knobDimmerRepository.save(newKD);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public KnobDimmer update(@Valid @RequestBody KnobDimmerSaveRequest kd) {
|
||||
return this.create(kd);
|
||||
@PutMapping("/dimTo")
|
||||
public Set<DimmableLight> dimTo(@Valid @RequestBody final KnobDimmerDimRequest bd)
|
||||
throws NotFoundException {
|
||||
final KnobDimmer dimmer =
|
||||
knobDimmerRepository.findById(bd.getId()).orElseThrow(NotFoundException::new);
|
||||
|
||||
dimmer.setLightIntensity(bd.getIntensity());
|
||||
dimmableLightRepository.saveAll(dimmer.getOutputs());
|
||||
|
||||
return dimmer.getOutputs();
|
||||
}
|
||||
|
||||
@PostMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> addLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
throws NotFoundException {
|
||||
return addOutput(inputId, lightId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/lights")
|
||||
public Set<? extends OutputDevice> removeLight(
|
||||
@PathVariable("id") long inputId, @RequestParam("lightId") Long lightId)
|
||||
throws NotFoundException {
|
||||
return removeOutput(inputId, lightId);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable("id") long id) {
|
||||
knobDimmerService.deleteById(id);
|
||||
knobDimmerRepository.deleteById(id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ 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.GenericDeviceSaveReguest;
|
||||
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.MotionSensorRepository;
|
||||
|
@ -30,21 +30,14 @@ public class MotionSensorController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public MotionSensor create(@Valid @RequestBody MotionSensorSaveRequest ms) {
|
||||
public MotionSensor create(@Valid @RequestBody GenericDeviceSaveReguest ms) {
|
||||
MotionSensor newMS = new MotionSensor();
|
||||
newMS.setDetected(ms.isDetected());
|
||||
newMS.setId(ms.getId());
|
||||
newMS.setName(ms.getName());
|
||||
newMS.setRoomId(ms.getRoomId());
|
||||
|
||||
return motionSensorService.save(newMS);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public MotionSensor update(@Valid @RequestBody MotionSensorSaveRequest ms) {
|
||||
return this.create(ms);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable("id") long id) {
|
||||
motionSensorService.deleteById(id);
|
||||
|
|
|
@ -36,10 +36,7 @@ public class RegularLightController {
|
|||
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) {
|
||||
RegularLight newRL = new RegularLight();
|
||||
newRL.setId(rl.getId());
|
||||
private RegularLight save(RegularLight newRL, RegularLightSaveRequest rl) {
|
||||
newRL.setName(rl.getName());
|
||||
newRL.setRoomId(rl.getRoomId());
|
||||
newRL.setOn(rl.isOn());
|
||||
|
@ -47,9 +44,16 @@ public class RegularLightController {
|
|||
return regularLightService.save(newRL);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) {
|
||||
return save(new RegularLight(), rl);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public RegularLight update(@Valid @RequestBody RegularLightSaveRequest rl) {
|
||||
return this.create(rl);
|
||||
public RegularLight update(@Valid @RequestBody RegularLightSaveRequest rl)
|
||||
throws NotFoundException {
|
||||
return save(
|
||||
regularLightService.findById(rl.getId()).orElseThrow(NotFoundException::new), rl);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
|
|
|
@ -44,12 +44,12 @@ public class RoomController {
|
|||
newRoom.setUserId(userId);
|
||||
newRoom.setName(r.getName());
|
||||
if (img != null) {
|
||||
newRoom.setImage(img.getBytes());
|
||||
newRoom.setImage(img);
|
||||
} else if (setWhenNull) {
|
||||
newRoom.setImage(null);
|
||||
}
|
||||
if (icon != null) {
|
||||
newRoom.setIcon(icon.getBytes());
|
||||
newRoom.setIcon(icon);
|
||||
} else if (setWhenNull) {
|
||||
newRoom.setIcon(null);
|
||||
}
|
||||
|
|
|
@ -33,19 +33,12 @@ public class SensorController {
|
|||
public Sensor create(@Valid @RequestBody SensorSaveRequest s) {
|
||||
Sensor newSensor = new Sensor();
|
||||
newSensor.setSensor(s.getSensor());
|
||||
newSensor.setValue(s.getValue());
|
||||
newSensor.setId(s.getId());
|
||||
newSensor.setName(s.getName());
|
||||
newSensor.setRoomId(s.getRoomId());
|
||||
|
||||
return sensorRepository.save(newSensor);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Sensor update(@Valid @RequestBody SensorSaveRequest s) {
|
||||
return this.create(s);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void deleteById(@PathVariable("id") long id) {
|
||||
sensorRepository.deleteById(id);
|
||||
|
|
|
@ -29,9 +29,7 @@ public class SmartPlugController {
|
|||
return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) {
|
||||
SmartPlug newSP = new SmartPlug();
|
||||
private SmartPlug save(SmartPlug newSP, SmartPlugSaveRequest sp) {
|
||||
newSP.setOn(sp.isOn());
|
||||
newSP.setId(sp.getId());
|
||||
newSP.setName(sp.getName());
|
||||
|
@ -40,9 +38,15 @@ public class SmartPlugController {
|
|||
return smartPlugRepository.save(newSP);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) {
|
||||
return save(new SmartPlug(), sp);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp) {
|
||||
return this.create(sp);
|
||||
public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp) throws NotFoundException {
|
||||
return save(
|
||||
smartPlugRepository.findById(sp.getId()).orElseThrow(NotFoundException::new), sp);
|
||||
}
|
||||
|
||||
@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 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.models.*;
|
||||
import java.util.*;
|
||||
|
@ -15,9 +16,24 @@ import org.springframework.web.bind.annotation.*;
|
|||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
@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
|
||||
public List<Switch> findAll() {
|
||||
|
@ -30,19 +46,48 @@ public class SwitchController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public Switch create(@Valid @RequestBody SwitchSaveRequest s) {
|
||||
public Switch create(@Valid @RequestBody GenericDeviceSaveReguest s) {
|
||||
Switch newSwitch = new Switch();
|
||||
newSwitch.setId(s.getId());
|
||||
newSwitch.setName(s.getName());
|
||||
newSwitch.setRoomId(s.getRoomId());
|
||||
newSwitch.setOn(s.isOn());
|
||||
|
||||
return switchRepository.save(newSwitch);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Switch update(@Valid @RequestBody SwitchSaveRequest s) {
|
||||
return this.create(s);
|
||||
@PutMapping("/operate")
|
||||
public Set<Switchable> operate(@Valid @RequestBody final SwitchOperationRequest sr)
|
||||
throws NotFoundException {
|
||||
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}")
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.controller;
|
||||
|
||||
import org.springframework.boot.autoconfigure.*;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@EnableAutoConfiguration
|
||||
public class WelcomeController {
|
||||
|
||||
@GetMapping
|
||||
ResponseEntity<Void> testConnection() {
|
||||
return ResponseEntity.ok(null);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/** A 'dim' event from a button dimmer. */
|
||||
public class ButtonDimmerDimRequest {
|
||||
|
||||
/** The device id */
|
||||
@NotNull private Long id;
|
||||
|
||||
public enum DimType {
|
||||
UP,
|
||||
DOWN;
|
||||
}
|
||||
|
||||
/** Whether the dim is up or down */
|
||||
@NotNull private DimType dimType;
|
||||
|
||||
public DimType getDimType() {
|
||||
return dimType;
|
||||
}
|
||||
|
||||
public void setDimType(DimType dimType) {
|
||||
this.dimType = dimType;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
|
@ -1,63 +0,0 @@
|
|||
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.Room;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class ButtonDimmerSaveRequest {
|
||||
@Lob private Set<DimmableLight> lights = new HashSet<DimmableLight>();
|
||||
|
||||
/** Device identifier */
|
||||
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
|
||||
* 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 Set<DimmableLight> getLights() {
|
||||
return this.lights;
|
||||
}
|
||||
|
||||
public void setLights(Set<DimmableLight> newLights) {
|
||||
this.lights = newLights;
|
||||
}
|
||||
|
||||
public void setRoom(Room room) {
|
||||
this.room = room;
|
||||
}
|
||||
|
||||
public void setRoomId(Long roomId) {
|
||||
this.roomId = roomId;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Room getRoom() {
|
||||
return room;
|
||||
}
|
||||
|
||||
public Long getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
}
|
|
@ -1,11 +1,9 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class SwitchSaveRequest {
|
||||
/** The state of this switch */
|
||||
private boolean on;
|
||||
|
||||
public class DeviceSaveRequest {
|
||||
/** Device identifier */
|
||||
private long id;
|
||||
|
||||
|
@ -16,33 +14,29 @@ public class SwitchSaveRequest {
|
|||
@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;
|
||||
}
|
||||
@NotNull @NotEmpty private String name;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
||||
public void setRoomId(Long roomId) {
|
||||
this.roomId = roomId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean isOn() {
|
||||
return on;
|
||||
}
|
||||
|
||||
public void setOn(boolean on) {
|
||||
this.on = on;
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -1,24 +1,20 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.Room;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class DimmableLightSaveRequest {
|
||||
|
||||
/** Device id (used only for update requests) */
|
||||
private Long id;
|
||||
|
||||
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
||||
@NotNull
|
||||
@Min(1)
|
||||
@Max(100)
|
||||
private Integer intensity = 0;
|
||||
|
||||
/** Device identifier */
|
||||
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
|
||||
* a REST call.
|
||||
|
@ -28,10 +24,6 @@ public class DimmableLightSaveRequest {
|
|||
/** The name of the device as assigned by the user (e.g. 'Master bedroom light') */
|
||||
@NotNull private String name;
|
||||
|
||||
public void setRoom(Room room) {
|
||||
this.room = room;
|
||||
}
|
||||
|
||||
public void setRoomId(Long roomId) {
|
||||
this.roomId = roomId;
|
||||
}
|
||||
|
@ -40,14 +32,6 @@ public class DimmableLightSaveRequest {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Room getRoom() {
|
||||
return room;
|
||||
}
|
||||
|
||||
public Long getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
@ -60,10 +44,15 @@ public class DimmableLightSaveRequest {
|
|||
return intensity;
|
||||
}
|
||||
|
||||
public void setIntensity(Integer intensity) throws IllegalArgumentException {
|
||||
if (intensity < 0 || intensity > 100) {
|
||||
throw new IllegalArgumentException("The intensity level can't go below 0 or above 100");
|
||||
}
|
||||
public void setIntensity(Integer intensity) {
|
||||
this.intensity = intensity;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,12 +2,7 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
|||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class MotionSensorSaveRequest {
|
||||
private boolean detected;
|
||||
|
||||
/** Device identifier */
|
||||
private long id;
|
||||
|
||||
public class GenericDeviceSaveReguest {
|
||||
/**
|
||||
* The room this device belongs in, as a foreign key id. To use when updating and inserting from
|
||||
* a REST call.
|
||||
|
@ -25,10 +20,6 @@ public class MotionSensorSaveRequest {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Long getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
@ -36,12 +27,4 @@ public class MotionSensorSaveRequest {
|
|||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public boolean isDetected() {
|
||||
return detected;
|
||||
}
|
||||
|
||||
public void setDetected(boolean detected) {
|
||||
this.detected = detected;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
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 KnobDimmerDimRequest {
|
||||
|
||||
/** The device id */
|
||||
@NotNull private Long id;
|
||||
|
||||
/** The absolute intensity value */
|
||||
@NotNull
|
||||
@Min(0)
|
||||
@Max(100)
|
||||
private Integer intensity;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getIntensity() {
|
||||
return intensity;
|
||||
}
|
||||
|
||||
public void setIntensity(Integer intensity) {
|
||||
this.intensity = intensity;
|
||||
}
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Lob;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class KnobDimmerSaveRequest {
|
||||
@Lob private Set<DimmableLight> lights = new HashSet<DimmableLight>();
|
||||
|
||||
/** 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 void setLights(Set<DimmableLight> lights) {
|
||||
this.lights = lights;
|
||||
}
|
||||
|
||||
public Set<DimmableLight> getLights() {
|
||||
return lights;
|
||||
}
|
||||
}
|
|
@ -45,4 +45,8 @@ public class RegularLightSaveRequest {
|
|||
public void setOn(boolean on) {
|
||||
this.on = on;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,17 +23,11 @@ public class SensorSaveRequest {
|
|||
LIGHT
|
||||
}
|
||||
|
||||
/** The value of this sensor according to its sensor type */
|
||||
private int value;
|
||||
|
||||
/** The type of this sensor */
|
||||
@NotNull
|
||||
@Enumerated(value = EnumType.STRING)
|
||||
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
|
||||
* a REST call.
|
||||
|
@ -51,10 +45,6 @@ public class SensorSaveRequest {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public Long getRoomId() {
|
||||
return roomId;
|
||||
}
|
||||
|
@ -70,12 +60,4 @@ public class SensorSaveRequest {
|
|||
public void setSensor(Sensor.SensorType sensor) {
|
||||
this.sensor = sensor;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
public void setValue(int newValue) {
|
||||
this.value = newValue;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,4 +45,8 @@ public class SmartPlugSaveRequest {
|
|||
public void setOn(boolean on) {
|
||||
this.on = on;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.error;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
|
||||
public class BadDataException extends Exception {
|
||||
public BadDataException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -6,6 +6,10 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
|||
@ResponseStatus(code = HttpStatus.NOT_FOUND)
|
||||
public class NotFoundException extends Exception {
|
||||
public NotFoundException() {
|
||||
super("Not Found");
|
||||
super("Not found");
|
||||
}
|
||||
|
||||
public NotFoundException(String what) {
|
||||
super(what + " not found");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
|
||||
import java.util.Set;
|
||||
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
|
||||
|
@ -11,60 +8,25 @@ import javax.persistence.OneToMany;
|
|||
*/
|
||||
@Entity
|
||||
public class ButtonDimmer extends Dimmer {
|
||||
|
||||
/** The delta amount to apply to a increase or decrease intensity */
|
||||
private static final int DIM_INCREMENT = 10;
|
||||
|
||||
public ButtonDimmer() {
|
||||
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 DIM_INCREMENT */
|
||||
public void increaseIntensity() {
|
||||
for (DimmableLight dl : lights) {
|
||||
dl.setIntensity(dl.getIntensity() + 1);
|
||||
for (DimmableLight dl : getOutputs()) {
|
||||
dl.setIntensity(dl.getIntensity() + DIM_INCREMENT);
|
||||
}
|
||||
}
|
||||
|
||||
/** Decreases the current intensity level of the dimmable light by 1 */
|
||||
/** Decreases the current intensity level of the dimmable light by DIM_INCREMENT */
|
||||
public void decreaseIntensity() {
|
||||
for (DimmableLight dl : lights) {
|
||||
dl.setIntensity(dl.getIntensity() - 1);
|
||||
for (DimmableLight dl : getOutputs()) {
|
||||
dl.setIntensity(dl.getIntensity() - DIM_INCREMENT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a DimmableLight to this set of DimmableLights
|
||||
*
|
||||
* @param dl The DimmableLight to be added
|
||||
*/
|
||||
public void addLight(DimmableLight dl) {
|
||||
lights.add(dl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given DimmableLight
|
||||
*
|
||||
* @param dl The DimmableLight to be removed
|
||||
*/
|
||||
public void removeLight(DimmableLight dl) {
|
||||
lights.remove(dl);
|
||||
}
|
||||
|
||||
/** Clears this set */
|
||||
public void clearSet() {
|
||||
lights.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lights
|
||||
*
|
||||
* @return duh
|
||||
*/
|
||||
public Set<DimmableLight> getLights() {
|
||||
return this.lights;
|
||||
}
|
||||
|
||||
public void setLights(Set<DimmableLight> newLights) {
|
||||
this.lights = newLights;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* A rule on how to connect an input device type to an output device type
|
||||
*
|
||||
* @param <I> the input device type
|
||||
* @param <O> the output device type
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface Connector<I extends InputDevice, O extends OutputDevice> {
|
||||
|
||||
/**
|
||||
* Connects or disconnects input to output
|
||||
*
|
||||
* @param input the input device
|
||||
* @param output the output device
|
||||
* @param connect true if connection, false if disconnection
|
||||
*/
|
||||
void connect(I input, O output, boolean connect);
|
||||
|
||||
/**
|
||||
* Produces a basic implementation of a connector, assuming there is a OneToMany relationship
|
||||
* between J and K
|
||||
*
|
||||
* @param outputsGetter the getter method of the set of outputs on the input class
|
||||
* @param inputSetter the setter method for the input id on the output class
|
||||
* @param <J> the input device type
|
||||
* @param <K> the output device type
|
||||
* @return a Connector implementation for the pair of types J and K
|
||||
*/
|
||||
static <J extends InputDevice, K extends OutputDevice> Connector<J, K> basic(
|
||||
Function<J, Set<? super K>> outputsGetter, BiConsumer<K, Long> inputSetter) {
|
||||
return (i, o, connect) -> {
|
||||
if (connect) {
|
||||
outputsGetter.apply(i).add(o);
|
||||
} else {
|
||||
outputsGetter.apply(i).remove(o);
|
||||
}
|
||||
|
||||
inputSetter.accept(o, connect ? i.getId() : null);
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.JoinColumn;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
|
@ -9,18 +11,31 @@ import javax.validation.constraints.NotNull;
|
|||
|
||||
/** Represent a dimmable light */
|
||||
@Entity
|
||||
public class DimmableLight extends Light {
|
||||
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() {
|
||||
super("light");
|
||||
}
|
||||
|
||||
@ManyToOne private Dimmer dimmer;
|
||||
@ManyToOne
|
||||
@GsonExclude
|
||||
@JoinColumn(name = "dimmer_id", updatable = false, insertable = false)
|
||||
private Dimmer dimmer;
|
||||
|
||||
@Column(name = "dimmer_id")
|
||||
private Long dimmerId;
|
||||
|
||||
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
||||
@NotNull
|
||||
@Column(nullable = false)
|
||||
@Min(1)
|
||||
@Min(0)
|
||||
@Max(100)
|
||||
private Integer intensity = 0;
|
||||
|
||||
|
@ -28,10 +43,41 @@ public class DimmableLight extends Light {
|
|||
return intensity;
|
||||
}
|
||||
|
||||
public void setIntensity(Integer intensity) throws IllegalArgumentException {
|
||||
if (intensity < 0 || intensity > 100) {
|
||||
throw new IllegalArgumentException("The intensity level can't go below 0 or above 100");
|
||||
/**
|
||||
* Sets the intensity to a certain level. Out of bound values are corrected to the respective
|
||||
* extremums. An intensity level of 0 turns the light off, but keeps the old intensity level
|
||||
* stored.
|
||||
*
|
||||
* @param intensity the intensity level (may be out of bounds)
|
||||
*/
|
||||
public void setIntensity(Integer intensity) {
|
||||
if (intensity <= 0) {
|
||||
this.intensity = 0;
|
||||
} else if (intensity > 100) {
|
||||
this.intensity = 100;
|
||||
} else {
|
||||
this.intensity = intensity;
|
||||
}
|
||||
this.intensity = intensity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOn() {
|
||||
return intensity != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOn(boolean on) {
|
||||
intensity = on ? 100 : 0;
|
||||
}
|
||||
|
||||
public void setDimmerId(Long 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;
|
||||
|
||||
public interface DimmableLightRepository extends DeviceRepository<DimmableLight> {}
|
||||
public interface DimmableLightRepository extends SwitchableRepository<DimmableLight> {}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
/** Represents a generic dimmer input device */
|
||||
@Entity
|
||||
|
@ -11,4 +13,17 @@ public abstract class Dimmer extends InputDevice {
|
|||
public Dimmer(String kind) {
|
||||
super(kind);
|
||||
}
|
||||
|
||||
@OneToMany(mappedBy = "dimmer")
|
||||
private Set<DimmableLight> lights;
|
||||
|
||||
/**
|
||||
* Get the lights connected to this dimmer
|
||||
*
|
||||
* @return duh
|
||||
*/
|
||||
@Override
|
||||
public Set<DimmableLight> getOutputs() {
|
||||
return this.lights;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,22 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import java.util.Set;
|
||||
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
|
||||
* environment (sensor) or the user (switch / dimmer).
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
|
||||
public abstract class InputDevice extends Device {
|
||||
public InputDevice(String kind) {
|
||||
super(kind, FlowType.INPUT);
|
||||
}
|
||||
|
||||
public Set<? extends OutputDevice> getOutputs() {
|
||||
return Set.of();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import java.util.Set;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
/**
|
||||
* Represents a dimmer able to set absolute intensity values (i.e. knowing the absolute intensity
|
||||
|
@ -10,62 +8,19 @@ import javax.persistence.OneToMany;
|
|||
*/
|
||||
@Entity
|
||||
public class KnobDimmer extends Dimmer {
|
||||
|
||||
public KnobDimmer() {
|
||||
super("knob-dimmer");
|
||||
}
|
||||
|
||||
@OneToMany(mappedBy = "dimmer")
|
||||
private Set<DimmableLight> lights;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* Sets absolutely the intensity level of all lights connected
|
||||
*
|
||||
* @param inc The direction the knob is turned with
|
||||
* @param intensity the intensity (must be from 0 to 100)
|
||||
*/
|
||||
public void modifyIntensity(boolean inc) {
|
||||
|
||||
for (DimmableLight dl : lights) {
|
||||
int remainder = dl.getIntensity() / 5;
|
||||
|
||||
if (inc) {
|
||||
dl.setIntensity(dl.getIntensity() - remainder);
|
||||
dl.setIntensity((dl.getIntensity() + 5) % 105);
|
||||
} else {
|
||||
dl.setIntensity(dl.getIntensity() + (5 - remainder));
|
||||
dl.setIntensity((dl.getIntensity() - 5) % 105);
|
||||
}
|
||||
public void setLightIntensity(int intensity) {
|
||||
for (DimmableLight dl : getOutputs()) {
|
||||
dl.setIntensity(intensity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a DimmableLight to this set of DimmableLights
|
||||
*
|
||||
* @param dl The DimmableLight to be added
|
||||
*/
|
||||
public void addLight(DimmableLight dl) {
|
||||
lights.add(dl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the given DimmableLight
|
||||
*
|
||||
* @param dl The DimmableLight to be removed
|
||||
*/
|
||||
public void removeLight(DimmableLight dl) {
|
||||
lights.remove(dl);
|
||||
}
|
||||
|
||||
/** Clears this set */
|
||||
public void clearSet() {
|
||||
lights.clear();
|
||||
}
|
||||
|
||||
public void setLights(Set<DimmableLight> lights) {
|
||||
this.lights = lights;
|
||||
}
|
||||
|
||||
public Set<DimmableLight> getLights() {
|
||||
return lights;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/** Represents a generic light */
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
|
||||
public abstract class Light extends OutputDevice {
|
||||
|
||||
/** Whether the light is on or not */
|
||||
@Column(name = "light_on", nullable = false)
|
||||
@NotNull
|
||||
boolean on;
|
||||
|
||||
protected Light(String kind) {
|
||||
super(kind);
|
||||
this.on = false;
|
||||
}
|
||||
|
||||
public boolean isOn() {
|
||||
return on;
|
||||
}
|
||||
|
||||
public void setOn(boolean on) {
|
||||
this.on = on;
|
||||
}
|
||||
}
|
|
@ -1,8 +1,6 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Inheritance;
|
||||
import javax.persistence.InheritanceType;
|
||||
import javax.persistence.*;
|
||||
|
||||
/**
|
||||
* Represents a generic output device, i.e. something that causes some behaviour (light, smartPlugs,
|
||||
|
|
|
@ -1,11 +1,30 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
/** Represents a standard non-dimmable light */
|
||||
@Entity
|
||||
public class RegularLight extends Light {
|
||||
public class RegularLight extends Switchable {
|
||||
|
||||
/** Whether the light is on or not */
|
||||
@Column(name = "light_on", nullable = false)
|
||||
@NotNull
|
||||
boolean on;
|
||||
|
||||
public RegularLight() {
|
||||
super("regular-light");
|
||||
this.on = false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOn() {
|
||||
return on;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOn(boolean on) {
|
||||
this.on = on;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
public interface RegularLightRepository extends DeviceRepository<RegularLight> {}
|
||||
public interface RegularLightRepository extends SwitchableRepository<RegularLight> {}
|
||||
|
|
|
@ -20,13 +20,11 @@ public class Room {
|
|||
* https://www.baeldung.com/java-base64-image-string
|
||||
* https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html
|
||||
*/
|
||||
@Lob
|
||||
@Column(name = "icon", columnDefinition = "TEXT")
|
||||
private byte[] icon;
|
||||
@Column private String icon;
|
||||
|
||||
@Lob
|
||||
@Column(name = "image", columnDefinition = "TEXT")
|
||||
private byte[] image;
|
||||
private String image;
|
||||
|
||||
/**
|
||||
* User that owns the house this room is in as a foreign key id. To use when updating and
|
||||
|
@ -65,19 +63,19 @@ public class Room {
|
|||
this.name = name;
|
||||
}
|
||||
|
||||
public byte[] getIcon() {
|
||||
public String getIcon() {
|
||||
return icon;
|
||||
}
|
||||
|
||||
public void setIcon(byte[] icon) {
|
||||
public void setIcon(String icon) {
|
||||
this.icon = icon;
|
||||
}
|
||||
|
||||
public byte[] getImage() {
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(byte[] image) {
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,8 @@ public class Sensor extends InputDevice {
|
|||
|
||||
public void setSensor(SensorType sensor) {
|
||||
this.sensor = sensor;
|
||||
|
||||
// TODO: setup hook for sockets live update
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
|
|
|
@ -6,17 +6,19 @@ import javax.validation.constraints.NotNull;
|
|||
|
||||
/** A smart plug that can be turned either on or off */
|
||||
@Entity
|
||||
public class SmartPlug extends OutputDevice {
|
||||
public class SmartPlug extends Switchable {
|
||||
|
||||
/** Whether the smart plug is on */
|
||||
@Column(name = "smart_plug_on", nullable = false)
|
||||
@NotNull
|
||||
private boolean on;
|
||||
|
||||
@Override
|
||||
public boolean isOn() {
|
||||
return on;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOn(boolean on) {
|
||||
this.on = on;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
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;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.OneToMany;
|
||||
|
||||
/** A switch input device */
|
||||
@Entity
|
||||
public class Switch extends InputDevice {
|
||||
|
||||
@OneToMany(mappedBy = "switchDevice")
|
||||
private Set<Switchable> switchables = new HashSet<>();
|
||||
|
||||
/** The state of this switch */
|
||||
@Column(nullable = false, name = "switch_on")
|
||||
private boolean on;
|
||||
|
@ -22,6 +28,15 @@ public class Switch extends InputDevice {
|
|||
*/
|
||||
public void setOn(boolean 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() {
|
||||
return on;
|
||||
}
|
||||
|
||||
public Set<Switchable> getOutputs() {
|
||||
return switchables;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
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 */
|
||||
@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)
|
||||
*
|
||||
* @return whether the device is on (true) or not (false)
|
||||
*/
|
||||
public abstract boolean isOn();
|
||||
|
||||
/**
|
||||
* Sets the on status of the device
|
||||
*
|
||||
* @param on the new on status: true for on, false for off
|
||||
*/
|
||||
public abstract void setOn(boolean on);
|
||||
|
||||
public Long getSwitchId() {
|
||||
return switchId;
|
||||
}
|
||||
|
||||
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