Added new hierarchy for dimmable devices and removed duplications in controllers and dtos
This commit is contained in:
parent
7cbacd7406
commit
cde88f2ca9
24 changed files with 146 additions and 329 deletions
|
@ -18,19 +18,19 @@ import org.springframework.web.bind.annotation.*;
|
|||
@EnableAutoConfiguration
|
||||
@RequestMapping("/buttonDimmer")
|
||||
public class ButtonDimmerController
|
||||
extends InputDeviceConnectionController<ButtonDimmer, DimmableLight> {
|
||||
extends InputDeviceConnectionController<ButtonDimmer, Dimmable> {
|
||||
private ButtonDimmerRepository buttonDimmerRepository;
|
||||
private DimmableLightRepository dimmableLightRepository;
|
||||
private DimmableRepository<Dimmable> dimmableRepository;
|
||||
|
||||
@Autowired
|
||||
protected ButtonDimmerController(
|
||||
ButtonDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
||||
ButtonDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
|
||||
super(
|
||||
inputRepository,
|
||||
outputRepository,
|
||||
DimmableLight.BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR);
|
||||
DimmableLight.BUTTON_DIMMER_DIMMABLE_CONNECTOR);
|
||||
this.buttonDimmerRepository = inputRepository;
|
||||
this.dimmableLightRepository = outputRepository;
|
||||
this.dimmableRepository = outputRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
|
@ -53,7 +53,7 @@ public class ButtonDimmerController
|
|||
}
|
||||
|
||||
@PutMapping("/dim")
|
||||
public Set<DimmableLight> dim(
|
||||
public Set<Dimmable> dim(
|
||||
@Valid @RequestBody final ButtonDimmerDimRequest bd, final Principal principal)
|
||||
throws NotFoundException {
|
||||
final ButtonDimmer buttonDimmer =
|
||||
|
@ -70,7 +70,7 @@ public class ButtonDimmerController
|
|||
break;
|
||||
}
|
||||
|
||||
dimmableLightRepository.saveAll(buttonDimmer.getOutputs());
|
||||
dimmableRepository.saveAll(buttonDimmer.getOutputs());
|
||||
|
||||
return buttonDimmer.getOutputs();
|
||||
}
|
||||
|
|
|
@ -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.CurtainsSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest;
|
||||
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;
|
||||
|
@ -28,21 +28,21 @@ public class CurtainsController {
|
|||
return curtainsService.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
private Curtains save(Curtains newRL, CurtainsSaveRequest s) {
|
||||
private Curtains save(Curtains newRL, DimmableSaveRequest s) {
|
||||
newRL.setName(s.getName());
|
||||
newRL.setRoomId(s.getRoomId());
|
||||
newRL.setOpenedAmount(s.getOpening());
|
||||
newRL.setIntensity(s.getIntensity());
|
||||
|
||||
return curtainsService.save(newRL);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Curtains create(@Valid @RequestBody CurtainsSaveRequest curtain) {
|
||||
public Curtains create(@Valid @RequestBody DimmableSaveRequest curtain) {
|
||||
return save(new Curtains(), curtain);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public Curtains update(@Valid @RequestBody CurtainsSaveRequest curtain)
|
||||
public Curtains update(@Valid @RequestBody DimmableSaveRequest curtain)
|
||||
throws NotFoundException {
|
||||
return save(
|
||||
curtainsService.findById(curtain.getId()).orElseThrow(NotFoundException::new),
|
||||
|
|
|
@ -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.DimmableLightSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.DimmableSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLight;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.DimmableLightRepository;
|
||||
|
@ -30,7 +30,7 @@ public class DimmableLightController {
|
|||
return dimmableLightService.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
private DimmableLight save(DimmableLight initial, DimmableLightSaveRequest dl) {
|
||||
private DimmableLight save(DimmableLight initial, DimmableSaveRequest dl) {
|
||||
initial.setIntensity(dl.getIntensity());
|
||||
initial.setName(dl.getName());
|
||||
initial.setRoomId(dl.getRoomId());
|
||||
|
@ -39,13 +39,13 @@ public class DimmableLightController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public DimmableLight create(@Valid @RequestBody DimmableLightSaveRequest dl) {
|
||||
public DimmableLight create(@Valid @RequestBody DimmableSaveRequest dl) {
|
||||
return save(new DimmableLight(), dl);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public DimmableLight update(
|
||||
@Valid @RequestBody DimmableLightSaveRequest sp, final Principal principal)
|
||||
@Valid @RequestBody DimmableSaveRequest sp, final Principal principal)
|
||||
throws NotFoundException {
|
||||
return save(
|
||||
dimmableLightService
|
||||
|
|
|
@ -18,20 +18,20 @@ import org.springframework.web.bind.annotation.*;
|
|||
@EnableAutoConfiguration
|
||||
@RequestMapping("/knobDimmer")
|
||||
public class KnobDimmerController
|
||||
extends InputDeviceConnectionController<KnobDimmer, DimmableLight> {
|
||||
extends InputDeviceConnectionController<KnobDimmer, Dimmable> {
|
||||
|
||||
@Autowired private KnobDimmerRepository knobDimmerRepository;
|
||||
@Autowired private DimmableLightRepository dimmableLightRepository;
|
||||
@Autowired private DimmableRepository<Dimmable> dimmableRepository;
|
||||
|
||||
@Autowired
|
||||
protected KnobDimmerController(
|
||||
KnobDimmerRepository inputRepository, DimmableLightRepository outputRepository) {
|
||||
KnobDimmerRepository inputRepository, DimmableRepository<Dimmable> outputRepository) {
|
||||
super(
|
||||
inputRepository,
|
||||
outputRepository,
|
||||
DimmableLight.KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR);
|
||||
Dimmable.KNOB_DIMMER_DIMMABLE_CONNECTOR);
|
||||
this.knobDimmerRepository = inputRepository;
|
||||
this.dimmableLightRepository = outputRepository;
|
||||
this.dimmableRepository = outputRepository;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
|
@ -54,7 +54,7 @@ public class KnobDimmerController
|
|||
}
|
||||
|
||||
@PutMapping("/dimTo")
|
||||
public Set<DimmableLight> dimTo(
|
||||
public Set<Dimmable> dimTo(
|
||||
@Valid @RequestBody final KnobDimmerDimRequest bd, final Principal principal)
|
||||
throws NotFoundException {
|
||||
final KnobDimmer dimmer =
|
||||
|
@ -63,7 +63,7 @@ public class KnobDimmerController
|
|||
.orElseThrow(NotFoundException::new);
|
||||
|
||||
dimmer.setLightIntensity(bd.getIntensity());
|
||||
dimmableLightRepository.saveAll(dimmer.getOutputs());
|
||||
dimmableRepository.saveAll(dimmer.getOutputs());
|
||||
|
||||
return dimmer.getOutputs();
|
||||
}
|
||||
|
|
|
@ -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.RegularLightSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLight;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.RegularLightRepository;
|
||||
|
@ -37,7 +37,7 @@ public class RegularLightController {
|
|||
return regularLightService.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
private RegularLight save(RegularLight newRL, RegularLightSaveRequest rl) {
|
||||
private RegularLight save(RegularLight newRL, SwitchableSaveRequest rl) {
|
||||
newRL.setName(rl.getName());
|
||||
newRL.setRoomId(rl.getRoomId());
|
||||
newRL.setOn(rl.isOn());
|
||||
|
@ -46,13 +46,13 @@ public class RegularLightController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public RegularLight create(@Valid @RequestBody RegularLightSaveRequest rl) {
|
||||
public RegularLight create(@Valid @RequestBody SwitchableSaveRequest rl) {
|
||||
return save(new RegularLight(), rl);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public RegularLight update(
|
||||
@Valid @RequestBody RegularLightSaveRequest rl, final Principal principal)
|
||||
@Valid @RequestBody SwitchableSaveRequest rl, final Principal principal)
|
||||
throws NotFoundException {
|
||||
return save(
|
||||
regularLightService
|
||||
|
|
|
@ -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.SecurityCameraSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.SwitchableSaveRequest;
|
||||
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;
|
||||
|
@ -37,7 +37,7 @@ public class SecurityCameraController {
|
|||
return securityCameraService.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
private SecurityCamera save(SecurityCamera newSC, SecurityCameraSaveRequest sc) {
|
||||
private SecurityCamera save(SecurityCamera newSC, SwitchableSaveRequest sc) {
|
||||
newSC.setName(sc.getName());
|
||||
newSC.setRoomId(sc.getRoomId());
|
||||
newSC.setOn(sc.isOn());
|
||||
|
@ -46,13 +46,13 @@ public class SecurityCameraController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public SecurityCamera create(@Valid @RequestBody SecurityCameraSaveRequest sc) {
|
||||
public SecurityCamera create(@Valid @RequestBody SwitchableSaveRequest sc) {
|
||||
return save(new SecurityCamera(), sc);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public SecurityCamera update(
|
||||
@Valid @RequestBody SecurityCameraSaveRequest sc, final Principal principal)
|
||||
@Valid @RequestBody SwitchableSaveRequest sc, final Principal principal)
|
||||
throws NotFoundException {
|
||||
return save(
|
||||
securityCameraService
|
||||
|
|
|
@ -2,11 +2,10 @@ 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.SwitchableSaveRequest;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
|
||||
import java.security.Principal;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
import javax.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -30,7 +29,7 @@ public class SmartPlugController {
|
|||
return smartPlugRepository.findById(id).orElseThrow(NotFoundException::new);
|
||||
}
|
||||
|
||||
private SmartPlug save(SmartPlug newSP, SmartPlugSaveRequest sp) {
|
||||
private SmartPlug save(SmartPlug newSP, SwitchableSaveRequest sp) {
|
||||
newSP.setOn(sp.isOn());
|
||||
newSP.setId(sp.getId());
|
||||
newSP.setName(sp.getName());
|
||||
|
@ -40,12 +39,12 @@ public class SmartPlugController {
|
|||
}
|
||||
|
||||
@PostMapping
|
||||
public SmartPlug create(@Valid @RequestBody SmartPlugSaveRequest sp) {
|
||||
public SmartPlug create(@Valid @RequestBody SwitchableSaveRequest sp) {
|
||||
return save(new SmartPlug(), sp);
|
||||
}
|
||||
|
||||
@PutMapping
|
||||
public SmartPlug update(@Valid @RequestBody SmartPlugSaveRequest sp, final Principal principal)
|
||||
public SmartPlug update(@Valid @RequestBody SwitchableSaveRequest sp, final Principal principal)
|
||||
throws NotFoundException {
|
||||
return save(
|
||||
smartPlugRepository
|
||||
|
|
|
@ -1,57 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -4,10 +4,10 @@ import javax.validation.constraints.Max;
|
|||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class DimmableLightSaveRequest {
|
||||
public class DimmableSaveRequest {
|
||||
|
||||
/** Device id (used only for update requests) */
|
||||
private Long id;
|
||||
private long id;
|
||||
|
||||
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
||||
@NotNull
|
||||
|
@ -48,11 +48,11 @@ public class DimmableLightSaveRequest {
|
|||
this.intensity = intensity;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class SmartPlugSaveRequest {
|
||||
/** Whether the smart plug is on */
|
||||
@NotNull 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;
|
||||
}
|
||||
}
|
|
@ -2,12 +2,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.dto;
|
|||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
public class RegularLightSaveRequest {
|
||||
public class SwitchableSaveRequest {
|
||||
/** The state of this switch */
|
||||
private boolean on;
|
||||
|
||||
/** 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
|
|
@ -18,14 +18,14 @@ public class ButtonDimmer extends Dimmer {
|
|||
|
||||
/** Increases the current intensity level of the dimmable light by DIM_INCREMENT */
|
||||
public void increaseIntensity() {
|
||||
for (DimmableLight dl : getOutputs()) {
|
||||
for (Dimmable dl : getOutputs()) {
|
||||
dl.setIntensity(dl.getIntensity() + DIM_INCREMENT);
|
||||
}
|
||||
}
|
||||
|
||||
/** Decreases the current intensity level of the dimmable light by DIM_INCREMENT */
|
||||
public void decreaseIntensity() {
|
||||
for (DimmableLight dl : getOutputs()) {
|
||||
for (Dimmable dl : getOutputs()) {
|
||||
dl.setIntensity(dl.getIntensity() - DIM_INCREMENT);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,41 +1,14 @@
|
|||
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
|
||||
* Represents a curtain. The intensity 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;
|
||||
|
||||
@Entity
|
||||
public class Curtains extends Dimmable {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
public interface CurtainsRepository extends DeviceRepository<Curtains> {}
|
||||
public interface CurtainsRepository extends DimmableRepository<Curtains> {}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Set;
|
||||
|
||||
@Entity
|
||||
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
|
||||
public class Dimmable extends Switchable {
|
||||
|
||||
public static final Connector<KnobDimmer, Dimmable>
|
||||
KNOB_DIMMER_DIMMABLE_CONNECTOR =
|
||||
Connector.basic(KnobDimmer::getOutputs, Dimmable::getDimmers);
|
||||
|
||||
public static final Connector<ButtonDimmer, Dimmable>
|
||||
BUTTON_DIMMER_DIMMABLE_CONNECTOR =
|
||||
Connector.basic(ButtonDimmer::getOutputs, Dimmable::getDimmers);
|
||||
|
||||
protected Dimmable(String kind) {
|
||||
super(kind);
|
||||
}
|
||||
|
||||
@ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH)
|
||||
@GsonExclude
|
||||
private Set<Dimmer> dimmers;
|
||||
|
||||
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
||||
@NotNull
|
||||
@Column(nullable = false)
|
||||
@Min(0)
|
||||
@Max(100)
|
||||
private Integer intensity = 0;
|
||||
|
||||
@NotNull
|
||||
@Column(nullable = false)
|
||||
private Integer oldIntensity = 100;
|
||||
|
||||
public Integer getIntensity() {
|
||||
return intensity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
this.oldIntensity = 100;
|
||||
} else {
|
||||
this.intensity = intensity;
|
||||
this.oldIntensity = intensity;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOn() {
|
||||
return intensity != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOn(boolean on) {
|
||||
intensity = on ? oldIntensity : 0;
|
||||
}
|
||||
|
||||
public Set<Dimmer> getDimmers() {
|
||||
return this.dimmers;
|
||||
}
|
||||
}
|
|
@ -1,86 +1,11 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
import ch.usi.inf.sa4.sanmarinoes.smarthut.config.GsonExclude;
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.Max;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Set;
|
||||
|
||||
/** Represent a dimmable light */
|
||||
@Entity
|
||||
public class DimmableLight extends Switchable {
|
||||
|
||||
public static final Connector<ButtonDimmer, DimmableLight>
|
||||
BUTTON_DIMMER_DIMMABLE_LIGHT_CONNECTOR =
|
||||
Connector.basic(ButtonDimmer::getOutputs, DimmableLight::getDimmers);
|
||||
|
||||
public static final Connector<KnobDimmer, DimmableLight> KNOB_DIMMER_DIMMABLE_LIGHT_CONNECTOR =
|
||||
Connector.basic(KnobDimmer::getOutputs, DimmableLight::getDimmers);
|
||||
|
||||
public class DimmableLight extends Dimmable {
|
||||
public DimmableLight() {
|
||||
super("dimmableLight");
|
||||
}
|
||||
|
||||
@ManyToMany(mappedBy = "dimmables", cascade = CascadeType.DETACH)
|
||||
@GsonExclude
|
||||
private Set<Dimmer> dimmers;
|
||||
|
||||
/** The light intensity value. Goes from 0 (off) to 100 (on) */
|
||||
@NotNull
|
||||
@Column(nullable = false)
|
||||
@Min(0)
|
||||
@Max(100)
|
||||
private Integer intensity = 0;
|
||||
|
||||
@NotNull
|
||||
@Column(nullable = false)
|
||||
private Integer oldIntensity = 100;
|
||||
|
||||
public Integer getIntensity() {
|
||||
return intensity;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
this.oldIntensity = 100;
|
||||
} else {
|
||||
this.intensity = intensity;
|
||||
this.oldIntensity = intensity;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOn() {
|
||||
return intensity != 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOn(boolean on) {
|
||||
intensity = on ? oldIntensity : 0;
|
||||
}
|
||||
|
||||
public Set<Dimmer> getDimmers() {
|
||||
return this.dimmers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Objects.toStringHelper(this)
|
||||
.add("intensity", intensity)
|
||||
.add("oldIntensity", oldIntensity)
|
||||
.toString();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
public interface DimmableLightRepository extends SwitchableRepository<DimmableLight> {}
|
||||
public interface DimmableLightRepository extends DimmableRepository<DimmableLight> {}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
package ch.usi.inf.sa4.sanmarinoes.smarthut.models;
|
||||
|
||||
public interface DimmableRepository<T extends Dimmable> extends SwitchableRepository<T> {
|
||||
}
|
|
@ -21,7 +21,7 @@ public abstract class Dimmer extends InputDevice {
|
|||
joinColumns = @JoinColumn(name = "dimmer_id"),
|
||||
inverseJoinColumns = @JoinColumn(name = "dimmable_id")
|
||||
)
|
||||
private Set<DimmableLight> dimmables = new HashSet<>();
|
||||
private Set<Dimmable> dimmables = new HashSet<>();
|
||||
|
||||
/**
|
||||
* Get the lights connected to this dimmer
|
||||
|
@ -29,12 +29,12 @@ public abstract class Dimmer extends InputDevice {
|
|||
* @return duh
|
||||
*/
|
||||
@Override
|
||||
public Set<DimmableLight> getOutputs() {
|
||||
public Set<Dimmable> getOutputs() {
|
||||
return this.dimmables;
|
||||
}
|
||||
|
||||
/** Add a light to be controller by this dimmer */
|
||||
public void addDimmableLight(DimmableLight dimmableLight) {
|
||||
public void addDimmable(Dimmable dimmableLight) {
|
||||
dimmables.add(dimmableLight);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ public class KnobDimmer extends Dimmer {
|
|||
*/
|
||||
public void setLightIntensity(int intensity) {
|
||||
this.intensity = intensity;
|
||||
for (DimmableLight dl : getOutputs()) {
|
||||
for (Dimmable dl : getOutputs()) {
|
||||
dl.setIntensity(intensity);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ public class ButtonDimmerTests {
|
|||
dl = new DimmableLight();
|
||||
dl.setIntensity(10);
|
||||
;
|
||||
buttonDimmer.addDimmableLight(dl);
|
||||
buttonDimmer.addDimmable(dl);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -19,27 +19,27 @@ public class CurtainsTests {
|
|||
@Test
|
||||
@DisplayName("State when just created")
|
||||
public void initialState() {
|
||||
assertEquals(0, this.curtains.getOpenedAmount());
|
||||
assertEquals(0, this.curtains.getIntensity());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Check wether setting the opening works")
|
||||
public void normalSet() {
|
||||
this.curtains.setOpenedAmount(42);
|
||||
assertEquals(42, this.curtains.getOpenedAmount());
|
||||
this.curtains.setIntensity(42);
|
||||
assertEquals(42, this.curtains.getIntensity());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Set setting a negative number")
|
||||
public void setNeg() {
|
||||
this.curtains.setOpenedAmount(-1);
|
||||
assertEquals(0, this.curtains.getOpenedAmount());
|
||||
this.curtains.setIntensity(-1);
|
||||
assertEquals(0, this.curtains.getIntensity());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Setting state to a number greater than 100")
|
||||
public void setLarge() {
|
||||
this.curtains.setOpenedAmount(32768);
|
||||
assertEquals(100, this.curtains.getOpenedAmount());
|
||||
this.curtains.setIntensity(32768);
|
||||
assertEquals(100, this.curtains.getIntensity());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ public class KnobDimmerTests {
|
|||
dl = new DimmableLight();
|
||||
dl.setIntensity(10);
|
||||
;
|
||||
knobDimmer.addDimmableLight(dl);
|
||||
knobDimmer.addDimmable(dl);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue