package de.ph87.home.tunable; import de.ph87.home.area.AreaDto; import de.ph87.home.property.PropertyDto; import de.ph87.home.property.PropertyTypeMismatch; import de.ph87.home.tag.Tag; import de.ph87.home.web.IWebSocketMessage; import jakarta.annotation.Nullable; import lombok.Getter; import lombok.NonNull; import lombok.ToString; import java.util.List; @Getter @ToString public class TunableDto implements IWebSocketMessage { @ToString.Exclude private final List websocketTopic = List.of("Tunable"); @NonNull private final AreaDto area; @NonNull private final String uuid; @NonNull private final String name; @NonNull private final String slug; @NonNull private final String statePropertyId; @NonNull private final String brightnessPropertyId; @NonNull private final String coldnessPropertyId; @Nullable @ToString.Exclude private final PropertyDto stateProperty; @Nullable @ToString.Exclude private final PropertyDto brightnessProperty; @Nullable @ToString.Exclude private final PropertyDto coldnessProperty; @NonNull private final List tagList; public TunableDto(@NonNull final Tunable tunable, @Nullable final PropertyDto stateProperty, @Nullable final PropertyDto brightnessProperty, @Nullable final PropertyDto coldnessProperty) { this.area = new AreaDto(tunable.getArea()); this.uuid = tunable.getUuid(); this.name = tunable.getName(); this.slug = tunable.getSlug(); this.statePropertyId = tunable.getStatePropertyId(); this.brightnessPropertyId = tunable.getBrightnessPropertyId(); this.coldnessPropertyId = tunable.getColdnessPropertyId(); this.stateProperty = stateProperty; this.brightnessProperty = brightnessProperty; this.coldnessProperty = coldnessProperty; this.tagList = tunable.getTagList().stream().map(Tag::getName).toList(); } @Nullable @ToString.Include public String state() { try { return "" + getStateValue(); } catch (PropertyTypeMismatch e) { return "[PropertyTypeMismatch]"; } } @Nullable public Boolean getStateValue() throws PropertyTypeMismatch { if (stateProperty == null) { return null; } return stateProperty.getStateValueAs(Boolean.class); } @Nullable public Double getBrightnessValue() throws PropertyTypeMismatch { if (brightnessProperty == null) { return null; } return brightnessProperty.getStateValueAs(Double.class); } @Nullable public Double getColdnessValue() throws PropertyTypeMismatch { if (coldnessProperty == null) { return null; } return coldnessProperty.getStateValueAs(Double.class); } }