property/get/{id}/value

This commit is contained in:
Patrick Haßel 2024-11-18 13:27:40 +01:00
parent 277e321480
commit 1acd5506c6
2 changed files with 12 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package de.ph87.homeautomation.channel;
import de.ph87.homeautomation.property.Property; import de.ph87.homeautomation.property.Property;
import de.ph87.homeautomation.shared.Helpers; import de.ph87.homeautomation.shared.Helpers;
import lombok.NonNull;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -29,12 +30,11 @@ public class ChannelReader {
return findByChannel(channel).orElseThrow(RuntimeException::new); return findByChannel(channel).orElseThrow(RuntimeException::new);
} }
public Double read(final Property property) { @NonNull
final Channel channel = property.getReadChannel(); public Optional<Double> read(@NonNull final Property property) {
if (channel == null) { return Optional.ofNullable(property.getReadChannel())
return null; .map(this::getByChannel)
} .map(owner -> owner.read(property.getReadChannel().getId()));
return getByChannel(channel).read(property.getReadChannel().getId());
} }
public ChannelDto toDtoAllowNull(final Channel channel) { public ChannelDto toDtoAllowNull(final Channel channel) {

View File

@ -44,6 +44,11 @@ public class PropertyController implements ISearchController {
return propertyWriter.set(id, Property::setSlug, value == null || value.isEmpty() ? null : value); return propertyWriter.set(id, Property::setSlug, value == null || value.isEmpty() ? null : value);
} }
@PostMapping("get/{id}/value")
public Double getValue(@PathVariable final long id) {
return channelReader.read(propertyReader.getById(id)).orElse(null);
}
@PostMapping("set/{id}/value") @PostMapping("set/{id}/value")
public PropertyDto setValue(@PathVariable final long id, @RequestBody final double value) { public PropertyDto setValue(@PathVariable final long id, @RequestBody final double value) {
return propertyWriter.set(id, propertyWriter::writeToChannel, value); return propertyWriter.set(id, propertyWriter::writeToChannel, value);
@ -66,18 +71,10 @@ public class PropertyController implements ISearchController {
@PostMapping("toggle/{id}") @PostMapping("toggle/{id}")
public PropertyDto setValue(@PathVariable final long id) { public PropertyDto setValue(@PathVariable final long id) {
final boolean oldState = getOldStateBoolean(id, false); final boolean oldState = channelReader.read(propertyReader.getById(id)).map(v -> v > 0.0).orElse(false);
return propertyWriter.set(id, propertyWriter::writeToChannel, oldState ? 0.0 : 1.0); return propertyWriter.set(id, propertyWriter::writeToChannel, oldState ? 0.0 : 1.0);
} }
private boolean getOldStateBoolean(final long id, final boolean orElse) {
final Double oldValue = channelReader.read(propertyReader.getById(id));
if (oldValue == null || oldValue.isNaN()) {
return orElse;
}
return oldValue > 0.0;
}
@Override @Override
@GetMapping("searchById/{id}") @GetMapping("searchById/{id}")
public SearchResult searchById(@PathVariable final long id) { public SearchResult searchById(@PathVariable final long id) {