From c6a769c1b8f61fb754b7e399e665943a160024b7 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Thu, 27 Feb 2020 17:24:08 +0100 Subject: [PATCH 1/3] updated button and knob dimmers --- .../smarthut/models/ButtonDimmer.java | 43 ++++++++++++++++ .../smarthut/models/KnobDimmer.java | 50 +++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java index e666c3e..e44e990 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java @@ -1,6 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.HashSet; +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 @@ -8,7 +11,47 @@ import javax.persistence.Entity; */ @Entity public class ButtonDimmer extends Dimmer { + @OneToMany(mappedBy = "") + private Set lights = new HashSet(); + public ButtonDimmer() { super("button-dimmer"); } + + /** Increases the current intensity level of the dimmable light by 1 */ + public void increaseIntensity() { + for (DimmableLight dl : lights) { + dl.setIntensity(dl.getIntensity() + 1); + } + } + + /** Decreases the current intensity level of the dimmable light by 1 */ + public void decreaseIntensity() { + for (DimmableLight dl : lights) { + dl.setIntensity(dl.getIntensity() - 1); + } + } + + /** + * 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(); + } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java index 80d59ab..e34ef76 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java @@ -1,6 +1,9 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import java.util.HashSet; +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 @@ -8,7 +11,54 @@ import javax.persistence.Entity; */ @Entity public class KnobDimmer extends Dimmer { + @OneToMany(mappedBy = "") + private Set lights = new HashSet(); + public KnobDimmer() { super("knob-dimmer"); } + + /** + * 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 + * + * @param inc The direction the knob is turned with + */ + 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); + } + } + } + + /** + * 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(); + } } From 01ccf6d968b09228d5c6341587f290976ca0199c Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 28 Feb 2020 15:28:55 +0100 Subject: [PATCH 2/3] wip --- .idea/codeStyles/codeStyleConfig.xml | 5 +++++ .idea/misc.xml | 2 +- .../smarthut/models/ButtonDimmer.java | 2 +- .../smarthut/models/KnobDimmer.java | 2 +- .../sanmarinoes/smarthut/models/Switch.java | 18 ++++++++++++++++++ 5 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..a55e7a1 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 7c6191b..c2c3244 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java index e44e990..23a69f8 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/ButtonDimmer.java @@ -11,7 +11,7 @@ import javax.persistence.OneToMany; */ @Entity public class ButtonDimmer extends Dimmer { - @OneToMany(mappedBy = "") + @OneToMany(mappedBy = "button_dimmer") private Set lights = new HashSet(); public ButtonDimmer() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java index e34ef76..5c5c603 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/KnobDimmer.java @@ -11,7 +11,7 @@ import javax.persistence.OneToMany; */ @Entity public class KnobDimmer extends Dimmer { - @OneToMany(mappedBy = "") + @OneToMany(mappedBy = "knob_dimmer") private Set lights = new HashSet(); public KnobDimmer() { diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index fbf1eed..cdc6a35 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -1,11 +1,29 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.models; +import javax.persistence.Column; import javax.persistence.Entity; /** A switch input device TODO: define switch behaviour (push button vs on/off state) */ @Entity public class Switch extends InputDevice { + + /** The state of this switch */ + @Column private boolean switchState = false; + public Switch() { super("switch"); } + + /** + * Setter method for Switch + * + * @param state The state to be set + */ + void setState(boolean state) { + switchState = state; + } + + boolean getState() { + return switchState; + } } From 5d1549cfa20b7d50b7f27f915d1169ace5253c70 Mon Sep 17 00:00:00 2001 From: tommi27 Date: Fri, 28 Feb 2020 17:06:42 +0100 Subject: [PATCH 3/3] room controller updated --- .../smarthut/controller/RoomController.java | 40 +++++++++++------ .../smarthut/dto/RoomSaveRequest.java | 43 +++++++++++++++++++ .../sanmarinoes/smarthut/models/Device.java | 2 +- .../sanmarinoes/smarthut/models/Switch.java | 14 ++++-- .../sa4/sanmarinoes/smarthut/models/User.java | 2 +- 5 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RoomSaveRequest.java diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java index 9ff6e2e..a25e4b5 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/controller/RoomController.java @@ -1,11 +1,12 @@ package ch.usi.inf.sa4.sanmarinoes.smarthut.controller; +import ch.usi.inf.sa4.sanmarinoes.smarthut.dto.RoomSaveRequest; import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*; +import java.security.Principal; import java.util.*; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.*; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.*; @@ -28,27 +29,38 @@ public class RoomController { return roomRepository.findById(id); } - @PostMapping - public Room save(@Valid @RequestBody Room r) { - final Object principal = - SecurityContextHolder.getContext().getAuthentication().getPrincipal(); - - if (!(principal instanceof UserDetails)) { - throw new IllegalStateException("User is not logged in"); - } + private Room save(final RoomSaveRequest r, final Principal principal, boolean setWhenNull) { + Room newRoom = new Room(); final String username = ((UserDetails) principal).getUsername(); final Long userId = userRepository.findByUsername(username).getId(); + final String img = r.getImage(); + final String icon = r.getIcon(); - r.setUserId(userId); - r.setUser(null); + newRoom.setUserId(userId); + newRoom.setUser(null); + if (img != null) { + newRoom.setImage(img.getBytes()); + } else if (setWhenNull) { + newRoom.setImage(null); + } + if (icon != null) { + newRoom.setIcon(icon.getBytes()); + } else if (setWhenNull) { + newRoom.setIcon(null); + } - return roomRepository.save(r); + return roomRepository.save(newRoom); + } + + @PostMapping + public Room create(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { + return this.save(r, principal, true); } @PutMapping - public Room update(@Valid @RequestBody Room r) { - return roomRepository.save(r); + public Room update(@Valid @RequestBody RoomSaveRequest r, final Principal principal) { + return this.save(r, principal, false); } @DeleteMapping("/{id}") diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RoomSaveRequest.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RoomSaveRequest.java new file mode 100644 index 0000000..f813b1c --- /dev/null +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/dto/RoomSaveRequest.java @@ -0,0 +1,43 @@ +package ch.usi.inf.sa4.sanmarinoes.smarthut.dto; + +import javax.persistence.Lob; +import javax.validation.constraints.NotNull; + +public class RoomSaveRequest { + /** + * Icon and image are to be given as byte[]. In order to get an encoded string from it, the + * Base64.getEncoder().encodeToString(byte[] content) should be used. For further information: + * https://www.baeldung.com/java-base64-image-string + * https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html + */ + @Lob private String icon; + + @Lob private String image; + + /** The user given name of this room (e.g. 'Master bedroom') */ + @NotNull private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIcon() { + return icon; + } + + public void setIcon(String icon) { + this.icon = icon; + } + + public String getImage() { + return image; + } + + public void setImage(String image) { + this.image = image; + } +} diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java index 834104d..0d5d959 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Device.java @@ -21,7 +21,7 @@ public abstract class Device { /** Device identifier */ @Id @GeneratedValue(strategy = GenerationType.AUTO) - @Column(name = "id", updatable = false, nullable = false) + @Column(name = "id", updatable = false, nullable = false, unique = true) private long id; /** The room this device belongs in */ diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java index cdc6a35..6f0eb99 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/Switch.java @@ -8,22 +8,28 @@ import javax.persistence.Entity; public class Switch extends InputDevice { /** The state of this switch */ - @Column private boolean switchState = false; + @Column(nullable = false, name = "switch_on") + private boolean on; public Switch() { super("switch"); } /** - * Setter method for Switch + * Setter method for this Switch * * @param state The state to be set */ void setState(boolean state) { - switchState = state; + on = state; } + /** + * Getter method for this Switch + * + * @return This Switch on state + */ boolean getState() { - return switchState; + return on; } } diff --git a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java index e4ce24e..a3b9202 100644 --- a/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java +++ b/src/main/java/ch/usi/inf/sa4/sanmarinoes/smarthut/models/User.java @@ -40,7 +40,7 @@ public class User { * The user's email (validated according to criteria used in >input type="email"<> * , technically not RFC 5322 compliant */ - @Column(nullable = false) + @Column(nullable = false, unique = true) @NotNull @NotEmpty(message = "Please provide an email") @Email(message = "Please provide a valid email address")