From 10872d9991109723c9f02ab84b55324e39b81c1c Mon Sep 17 00:00:00 2001 From: Vadim Tkachenko Date: Wed, 13 Sep 2023 20:20:27 -0700 Subject: [PATCH] Refactored copypaste into a method call (#47, #271) --- .../config/ConfigurationContextAware.java | 71 ++++--------------- .../config/onewire/EntityProvider.java | 2 + 2 files changed, 16 insertions(+), 57 deletions(-) diff --git a/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/ConfigurationContextAware.java b/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/ConfigurationContextAware.java index 06db04f02..c2a63c53a 100644 --- a/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/ConfigurationContextAware.java +++ b/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/ConfigurationContextAware.java @@ -33,45 +33,22 @@ protected final Flux> getSensorBlocking(String address) { protected final Mono>> getSensor(String address) { return context .sensors - .getFlux() - .filter(s -> s.getKey().equals(address)) - .map(Map.Entry::getValue) - .doOnNext(s -> logger.debug("getSensor({}) = {}", address, s)) - .next(); + .getMonoById("sensors", address) + .doOnNext(s -> logger.debug("getSensor({}) = {}", address, s)); } protected final Switch getSwitch(String address) { - var result = context + return context .switches - .getFlux() - .filter(s -> s.getKey().equals(address)) - .map(Map.Entry::getValue) - .take(1) - .blockFirst(); - - if (result == null) { - throw new IllegalArgumentException("Couldn't resolve switch for id or address '" + address + "'"); - } - - logger.debug("getSwitch({}) = {}", address, result); - return result; + .getMonoById("switches", address) + .block(); } protected final Zone getZone(String address) { - var result = context + return context .zones - .getFlux() - .filter(s -> s.getKey().equals(address)) - .map(Map.Entry::getValue) - .take(1) - .blockFirst(); - - if (result == null) { - throw new IllegalArgumentException("Couldn't resolve zone for id '" + address + "'"); - } - - logger.debug("getZone({}) = {}", address, result); - return result; + .getMonoById("zones", address) + .block(); } protected final Map>, Zone> getSensorFeed2ZoneMapping(Map source) { @@ -90,37 +67,17 @@ protected final Map>, Zone> getSensorFeed2ZoneMapping( } protected final HvacDevice getHvacDevice(String address) { - var result = context + return context .hvacDevices - .getFlux() - .filter(s -> s.getKey().equals(address)) - .map(Map.Entry::getValue) - .take(1) - .blockFirst(); - - if (result == null) { - throw new IllegalArgumentException("Couldn't resolve HVAC device for id '" + address + "'"); - } - - logger.debug("getHvacDevice({}) = {}", address, result); - return result; + .getMonoById("hvac", address) + .block(); } protected final UnitController getUnitController(String address) { - var result = context + return context .units - .getFlux() - .filter(s -> s.getKey().equals(address)) - .map(Map.Entry::getValue) - .take(1) - .blockFirst(); - - if (result == null) { - throw new IllegalArgumentException("Couldn't resolve unit controller for id '" + address + "'"); - } - - logger.debug("getUnitController({}) = {}", address, result); - return result; + .getMonoById("units", address) + .block(); } protected final boolean isConfigured(String source, Set names, Map.Entry configured) { diff --git a/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/onewire/EntityProvider.java b/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/onewire/EntityProvider.java index c0a25b2c5..64295bc21 100644 --- a/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/onewire/EntityProvider.java +++ b/dz3r-bootstrap/src/main/java/net/sf/dz3r/runtime/config/onewire/EntityProvider.java @@ -73,6 +73,8 @@ public Flux> getFlux() { public T getById(String consumer, String id) { + // VT: FIXME: Swap getById() and getMonoById() logic so both are more reactive + var found = getFlux() .filter(kv -> kv.getKey().equals(id)) .blockFirst();