Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 committed Mar 1, 2022
1 parent d160b22 commit 5121f2b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.ecovacs.internal;

import java.util.HashMap;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -75,7 +76,7 @@ public class EcovacsBindingConstants {
public static class StateOptionEntry<T extends Enum<T>> {
public final T enumValue;
public final String value;
public final @Nullable DeviceCapability capability;
public final Optional<DeviceCapability> capability;

StateOptionEntry(T enumValue, String value) {
this(enumValue, value, null);
Expand All @@ -84,7 +85,7 @@ public static class StateOptionEntry<T extends Enum<T>> {
StateOptionEntry(T enumValue, String value, @Nullable DeviceCapability capability) {
this.enumValue = enumValue;
this.value = value;
this.capability = capability;
this.capability = Optional.ofNullable(capability);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand Down Expand Up @@ -413,25 +415,15 @@ private void updateStateOptions(EcovacsDevice device) {

private <T extends Enum<T>> List<StateOption> createChannelOptions(EcovacsDevice device, T[] values,
StateOptionMapping<T> mapping, @Nullable Predicate<StateOptionEntry<T>> filter) {
List<StateOption> options = new ArrayList<>();
for (int i = 0; i < values.length; i++) {
T value = values[i];
@Nullable
StateOptionEntry<T> mappedValue = mapping.get(value);
if (mappedValue == null) {
continue;
}
if (filter != null && !filter.test(mappedValue)) {
continue;
}
@Nullable
DeviceCapability cap = mappedValue.capability;
if (cap != null && !device.hasCapability(cap)) {
continue;
}
options.add(new StateOption(mappedValue.value, mappedValue.value));
}
return options;
return Arrays.stream(values).map(v -> Optional.ofNullable(mapping.get(v)))
// ensure we have a mapping (should always be the case)
.filter(Optional::isPresent).map(opt -> opt.get())
// apply supplied filter
.filter(mv -> filter == null || filter.test(mv))
// apply capability filter
.filter(mv -> mv.capability.isEmpty() || device.hasCapability(mv.capability.get()))
// map to actual option
.map(mv -> new StateOption(mv.value, mv.value)).collect(Collectors.toList());
}

private synchronized void scheduleNextPoll(long initialDelaySeconds) {
Expand Down Expand Up @@ -505,7 +497,7 @@ private void pollData() {
updateState(CHANNEL_ID_LAST_CLEAN_DURATION, new QuantityType<>(record.cleaningDuration, Units.SECOND));
updateState(CHANNEL_ID_LAST_CLEAN_AREA, new QuantityType<>(record.cleanedArea, SIUnits.SQUARE_METRE));
StateOptionEntry<CleanMode> mode = CLEAN_MODE_MAPPING.get(record.mode);
updateState(CHANNEL_ID_LAST_CLEAN_MODE, mode != null ? new StringType(mode.value) : UnDefType.UNDEF);
updateState(CHANNEL_ID_LAST_CLEAN_MODE, stringToState(mode != null ? mode.value : null));

if (device.hasCapability(DeviceCapability.MAPPING)
&& !lastDownloadedCleanMapUrl.equals(record.mapImageUrl)) {
Expand Down Expand Up @@ -571,8 +563,8 @@ private void updateStateAndCommandChannels() {
String commandState = determineCommandChannelValue(charging, cleanMode);
String currentMode = determineCleaningModeChannelValue(cleanMode.isActive() ? cleanMode : lastActiveCleanMode);
updateState(CHANNEL_ID_STATE, StringType.valueOf(determineStateChannelValue(charging, cleanMode)));
updateState(CHANNEL_ID_CLEANING_MODE, currentMode != null ? StringType.valueOf(currentMode) : UnDefType.UNDEF);
updateState(CHANNEL_ID_COMMAND, commandState != null ? StringType.valueOf(commandState) : UnDefType.UNDEF);
updateState(CHANNEL_ID_CLEANING_MODE, stringToState(currentMode));
updateState(CHANNEL_ID_COMMAND, stringToState(commandState));
}

private String determineStateChannelValue(boolean charging, CleanMode cleanMode) {
Expand Down Expand Up @@ -612,6 +604,11 @@ private String determineStateChannelValue(boolean charging, CleanMode cleanMode)
return null;
}

private State stringToState(@Nullable String value) {
Optional<State> stateOpt = Optional.ofNullable(value).map(v -> StringType.valueOf(v));
return stateOpt.orElse(UnDefType.UNDEF);
}

private @Nullable AbstractNoResponseCommand determineDeviceCommand(EcovacsDevice device, String command) {
CleanMode mode = lastActiveCleanMode;

Expand Down

0 comments on commit 5121f2b

Please sign in to comment.