68 lines
1.5 KiB
Java
68 lines
1.5 KiB
Java
package de.ph87.home.device;
|
|
|
|
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 DeviceDto implements IWebSocketMessage {
|
|
|
|
@ToString.Exclude
|
|
private final List<Object> websocketTopic = List.of("Device");
|
|
|
|
@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;
|
|
|
|
@Nullable
|
|
@ToString.Exclude
|
|
private final PropertyDto<Boolean> stateProperty;
|
|
|
|
public DeviceDto(@NonNull final Device device, @Nullable final PropertyDto<Boolean> stateProperty) {
|
|
this.area = new AreaDto(device.getArea());
|
|
this.uuid = device.getUuid();
|
|
this.name = device.getName();
|
|
this.slug = device.getSlug();
|
|
this.statePropertyId = device.getStatePropertyId();
|
|
this.stateProperty = stateProperty;
|
|
}
|
|
|
|
@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);
|
|
}
|
|
|
|
}
|