diff --git a/src/main/java/de/ph87/homeautomation/property/PropertyController.java b/src/main/java/de/ph87/homeautomation/property/PropertyController.java index a2bf2ac..e908aa8 100644 --- a/src/main/java/de/ph87/homeautomation/property/PropertyController.java +++ b/src/main/java/de/ph87/homeautomation/property/PropertyController.java @@ -55,6 +55,20 @@ public class PropertyController implements ISearchController { return propertyWriteService.set(id, (p, v) -> p.setWriteChannel(channelService.getById(v)), channelId); } + @PostMapping("toggle/{id}") + public PropertyDto setValue(@PathVariable final long id) { + final boolean oldState = getOldStateBoolean(id, false); + return propertyWriteService.set(id, propertyWriteService::writeToChannel, oldState ? 0.0 : 1.0); + } + + private boolean getOldStateBoolean(final long id, final boolean orElse) { + final Double oldValue = propertyReadService.getDtoById(id).getValue(); + if (oldValue == null || oldValue.isNaN()) { + return orElse; + } + return oldValue > 0.0; + } + @Override @GetMapping("getById/{id}") public SearchResult getById(@PathVariable final long id) {