From 172ee3a337d60edd42ee6feacb634e5223c80b3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20Ha=C3=9Fel?= Date: Thu, 17 Mar 2022 09:18:36 +0100 Subject: [PATCH] property toggle --- .../property/PropertyController.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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) {