Skip to content

Commit

Permalink
Refactored copypaste into a method call (#47, #271)
Browse files Browse the repository at this point in the history
  • Loading branch information
climategadgets committed Sep 14, 2023
1 parent ba1143a commit 10872d9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,45 +33,22 @@ protected final Flux<Signal<Double, Void>> getSensorBlocking(String address) {
protected final Mono<Flux<Signal<Double, Void>>> 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<Flux<Signal<Double, Void>>, Zone> getSensorFeed2ZoneMapping(Map<String, String> source) {
Expand All @@ -90,37 +67,17 @@ protected final Map<Flux<Signal<Double, Void>>, 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<String> names, Map.Entry<String, ?> configured) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ public Flux<Map.Entry<String, T>> 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();
Expand Down

0 comments on commit 10872d9

Please sign in to comment.