Home4/src/main/java/de/ph87/home/tunable/TunableDto.java
2024-11-27 14:32:25 +01:00

102 lines
2.5 KiB
Java

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.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<Object> 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<Boolean> stateProperty;
@Nullable
@ToString.Exclude
private final PropertyDto<Double> brightnessProperty;
@Nullable
@ToString.Exclude
private final PropertyDto<Double> coldnessProperty;
public TunableDto(@NonNull final Tunable tunable, @Nullable final PropertyDto<Boolean> stateProperty, @Nullable final PropertyDto<Double> brightnessProperty, @Nullable final PropertyDto<Double> 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;
}
@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);
}
}