-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ddf6ee
commit 84c0343
Showing
8 changed files
with
196 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
backend/src/main/java/ch/akop/homesystem/controller/dtos/RollerShutterDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package ch.akop.homesystem.controller.dtos; | ||
|
||
import ch.akop.homesystem.models.CompassDirection; | ||
import ch.akop.homesystem.models.devices.actor.RollerShutter; | ||
import ch.akop.homesystem.persistence.model.config.RollerShutterConfig; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalTime; | ||
import java.util.List; | ||
import javax.validation.constraints.Max; | ||
import javax.validation.constraints.Min; | ||
import lombok.Data; | ||
import org.springframework.lang.Nullable; | ||
|
||
@Data | ||
public class RollerShutterDto { | ||
|
||
private String id; | ||
private String name; | ||
private boolean reachable; | ||
|
||
@Min(0) | ||
@Max(100) | ||
private Integer currentLift; | ||
|
||
/** | ||
* Tilt angle | ||
*/ | ||
@Min(0) | ||
@Max(100) | ||
private Integer currentTilt; | ||
|
||
private boolean isOpen; | ||
|
||
private ConfigDto config; | ||
|
||
public static RollerShutterDto from(RollerShutter rollerShutter) { | ||
return new RollerShutterDto() | ||
.setId(rollerShutter.getId()) | ||
.setName(rollerShutter.getName()) | ||
.setReachable(rollerShutter.isReachable()) | ||
.setCurrentLift(rollerShutter.getCurrentLift()) | ||
.setCurrentTilt(rollerShutter.getCurrentTilt()) | ||
.setOpen(rollerShutter.isOpen()) | ||
.setConfig(ConfigDto.from(rollerShutter.getConfig())); | ||
} | ||
|
||
@Data | ||
public static class ConfigDto { | ||
|
||
private List<CompassDirection> compassDirection; | ||
|
||
@Nullable | ||
private LocalTime closeAt; | ||
|
||
@Nullable | ||
private LocalTime openAt; | ||
|
||
@Nullable | ||
private LocalDateTime noAutomaticsUntil; | ||
|
||
@Nullable | ||
private Boolean closeWithInterrupt; | ||
|
||
public static ConfigDto from(RollerShutterConfig config) { | ||
return new ConfigDto() | ||
.setCompassDirection(config.getCompassDirection()) | ||
.setCloseAt(config.getCloseAt()) | ||
.setOpenAt(config.getOpenAt()) | ||
.setNoAutomaticsUntil(config.getNoAutomaticsUntil()) | ||
.setCloseWithInterrupt(config.getCloseWithInterrupt()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
.../java/ch/akop/homesystem/controller/for_private/websocket/RollerShutterChangedSocket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ch.akop.homesystem.controller.for_private.websocket; | ||
|
||
import ch.akop.homesystem.controller.dtos.RollerShutterDto; | ||
import ch.akop.homesystem.models.devices.actor.RollerShutter; | ||
import ch.akop.homesystem.services.impl.DeviceService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.quarkus.vertx.ConsumeEvent; | ||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.websocket.OnClose; | ||
import javax.websocket.OnError; | ||
import javax.websocket.OnOpen; | ||
import javax.websocket.Session; | ||
import javax.websocket.server.ServerEndpoint; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@ApplicationScoped | ||
@ServerEndpoint("/secured/ws/v1/devices/roller-shutters") | ||
@RequiredArgsConstructor | ||
public class RollerShutterChangedSocket extends AbstractBaseSocket { | ||
|
||
private final DeviceService deviceService; | ||
|
||
@Getter | ||
private final ObjectMapper objectMapper; | ||
|
||
@ConsumeEvent(value = "devices/roller-shutters/update", blocking = true) | ||
void updateLight(String updatedDeviceId) { | ||
deviceService.findDeviceById(updatedDeviceId, RollerShutter.class) | ||
.map(RollerShutterDto::from) | ||
.ifPresent(this::broadcast); | ||
} | ||
|
||
@OnOpen | ||
public void onOpen(Session session) { | ||
log.info("Opening session: {}", session.getId()); | ||
registerSession(session); | ||
sendAllRollerShuttersToSession(session.getId()); | ||
} | ||
|
||
@OnClose | ||
public void onClose(Session session) { | ||
log.info("Close session: {}", session.getId()); | ||
deregisterSession(session.getId()); | ||
} | ||
|
||
@OnError | ||
public void onError(Session session, Throwable throwable) { | ||
log.error("Error on session: {}", session.getId(), throwable); | ||
deregisterSession(session.getId()); | ||
} | ||
|
||
@SneakyThrows | ||
private void sendAllRollerShuttersToSession(String sessionId) { | ||
deviceService.getDevicesOfType(RollerShutter.class) | ||
.stream() | ||
.map(RollerShutterDto::from) | ||
.forEach(rollerShutter -> sendMessage(sessionId, rollerShutter)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.