Skip to content

Commit

Permalink
send rollerShutters
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreKoepke committed Feb 22, 2025
1 parent 4ddf6ee commit 84c0343
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package ch.akop.homesystem.controller.dtos;

import ch.akop.homesystem.models.color.Color;
import ch.akop.homesystem.models.devices.actor.Actor;
import ch.akop.homesystem.models.devices.actor.ColoredLight;
import ch.akop.homesystem.models.devices.actor.DimmableLight;
import ch.akop.homesystem.models.devices.actor.SimpleLight;
import java.time.ZonedDateTime;
import lombok.Data;

@Data
public class ActorDto {
public class LightDto {

private String id;
private String name;
Expand All @@ -19,26 +18,21 @@ public class ActorDto {
private boolean reachable;
private ZonedDateTime lastUpdated;


public static ActorDto from(Actor<?> light) {
return new ActorDto()
public static LightDto from(SimpleLight light) {
return new LightDto()
.setId(light.getId())
.setName(light.getName())
.setReachable(light.isReachable())
.setLastUpdated(light.getLastUpdated());
}

public static ActorDto from(SimpleLight light) {
return from((Actor<?>) light)
.setLastUpdated(light.getLastUpdated())
.setOn(light.isCurrentStateIsOn());
}

public static ActorDto from(DimmableLight light) {
public static LightDto from(DimmableLight light) {
return from((SimpleLight) light)
.setBrightness((int) (light.getCurrentBrightness() / 255f * 100));
}

public static ActorDto from(ColoredLight light) {
public static LightDto from(ColoredLight light) {
return from((DimmableLight) light)
.setColor(light.getCurrentColor());
}
Expand Down
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());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ch.akop.homesystem.controller.for_private;


import ch.akop.homesystem.controller.dtos.ActorDto;
import ch.akop.homesystem.controller.dtos.LightDto;
import ch.akop.homesystem.controller.dtos.SensorDto;
import ch.akop.homesystem.models.devices.actor.SimpleLight;
import ch.akop.homesystem.models.devices.sensor.MotionSensor;
Expand All @@ -23,17 +23,17 @@ public class DeviceController {

@Path("lights")
@GET
public Stream<ActorDto> getAllLights() {
public Stream<LightDto> getAllLights() {
return deviceService.getDevicesOfType(SimpleLight.class)
.stream()
.map(ActorDto::from);
.map(LightDto::from);
}

@Path("lights/{id}")
@GET
public ActorDto getLight(@PathParam("id") String id) {
public LightDto getLight(@PathParam("id") String id) {
return deviceService.findDeviceById(id, SimpleLight.class)
.map(ActorDto::from)
.map(LightDto::from)
.orElseThrow(() -> new NotFoundException(id));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ch.akop.homesystem.controller.for_private.websocket;

import ch.akop.homesystem.controller.dtos.ActorDto;
import ch.akop.homesystem.controller.dtos.LightDto;
import ch.akop.homesystem.models.devices.actor.SimpleLight;
import ch.akop.homesystem.services.impl.DeviceService;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -18,9 +18,9 @@

@Slf4j
@ApplicationScoped
@ServerEndpoint("/secured/ws/v1/devices/actors")
@ServerEndpoint("/secured/ws/v1/devices/lights")
@RequiredArgsConstructor
public class ActorsChangedSocket extends AbstractBaseSocket {
public class LightsChangedSocket extends AbstractBaseSocket {

private final DeviceService deviceService;

Expand All @@ -30,7 +30,7 @@ public class ActorsChangedSocket extends AbstractBaseSocket {
@ConsumeEvent(value = "devices/lights/update", blocking = true)
void updateLight(String updatedDeviceId) {
deviceService.findDeviceById(updatedDeviceId, SimpleLight.class)
.map(ActorDto::from)
.map(LightDto::from)
.ifPresent(this::broadcast);
}

Expand All @@ -57,7 +57,7 @@ public void onError(Session session, Throwable throwable) {
private void sendAllLightsToSession(String sessionId) {
deviceService.getDevicesOfType(SimpleLight.class)
.stream()
.map(ActorDto::from)
.map(LightDto::from)
.forEach(motionSensor -> sendMessage(sessionId, motionSensor));
}
}
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,21 @@ public void handleMessage(WebSocketUpdate update) {
&& update.getState() != null) {

deviceService.findDeviceById(update.getId(), Actor.class)
.ifPresent(device -> device.consumeUpdate(update.getState()));

eventBus.publish("devices/lights/update", update.getId());
.ifPresent(device -> {
device.consumeUpdate(update.getState());
eventBus.publish("devices/" + getTopicName(device.getClass()) + "/update", update.getId());
});
}
}


private static String getTopicName(Class<?> clazz) {
return switch (clazz.getSimpleName()) {
case "RollerShutter" -> "roller-shutters";
case "DimmableLight", "SimpleLight", "" -> "lights";
default -> {
log.warn("Unknown {}", clazz.getSimpleName());
yield "unknown";
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.akop.homesystem.models.devices.actor;

import ch.akop.homesystem.deconz.rest.State;
import ch.akop.homesystem.persistence.model.config.RollerShutterConfig;
import ch.akop.homesystem.util.TimedGateKeeper;
import io.reactivex.rxjava3.core.Completable;
import io.reactivex.rxjava3.core.Observable;
Expand Down Expand Up @@ -82,6 +83,8 @@ public class RollerShutter extends Actor<RollerShutter> {

private boolean isOpen;

public RollerShutterConfig config;

/**
* Set "tilt" and wait until both reached their position. After that, set lift to the given value.
*
Expand Down
Loading

0 comments on commit 84c0343

Please sign in to comment.