From 05b095f4e1f80637aa779c0eeb9e3b23b692a272 Mon Sep 17 00:00:00 2001 From: David Pace Date: Fri, 16 May 2025 13:51:16 +0200 Subject: [PATCH] [boschshc] Fix compiler warnings and SAT/Sonar issues (#18656) * refactored code Signed-off-by: David Pace --- .../org.openhab.binding.boschshc/README.md | 1 + .../console/BoschShcCommandExtension.java | 14 +++---- .../devices/BoschSHCDeviceHandler.java | 6 +-- .../devices/BoschSHCHandlerFactory.java | 2 +- .../internal/devices/bridge/BoschSslUtil.java | 3 +- .../shuttercontrol/ShutterControlHandler.java | 38 +++++++++---------- .../console/BoschShcCommandExtensionTest.java | 4 +- ...tPowerSwitchHandlerWithPowerMeterTest.java | 1 + 8 files changed, 30 insertions(+), 39 deletions(-) diff --git a/bundles/org.openhab.binding.boschshc/README.md b/bundles/org.openhab.binding.boschshc/README.md index d9966b27b38..d538cef5c20 100644 --- a/bundles/org.openhab.binding.boschshc/README.md +++ b/bundles/org.openhab.binding.boschshc/README.md @@ -410,6 +410,7 @@ A keystore file with a self-signed certificate is created automatically. This certificate is used for pairing between the Bridge and the Bosch Smart Home Controller. On the Smart Home Controller Bridge, paring mode must be enabled after the `shc` Thing was created: + - Smart Home Controller: _Press and hold the button until the LED starts blinking to enable pairing mode_. - Smart Home Controller II: _Press the button briefly to enable pairing mode_. diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtension.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtension.java index 0edfe22dc1a..a0b902ed289 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtension.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtension.java @@ -182,13 +182,15 @@ public class BoschShcCommandExtension extends AbstractConsoleCommandExtension im StringBuilder builder = new StringBuilder(); builder.append(String.format(" deviceID: %1s%n", device.id)); builder.append(String.format(" type: %1s -> ", device.deviceModel)); - if (DEVICEMODEL_TO_THINGTYPE_MAP.containsKey(device.deviceModel)) { - builder.append(DEVICEMODEL_TO_THINGTYPE_MAP.get(device.deviceModel).getId()); + + ThingTypeUID thingTypeUID = DEVICEMODEL_TO_THINGTYPE_MAP.get(device.deviceModel); + if (thingTypeUID != null) { + builder.append(thingTypeUID.getId()); } else { builder.append("!UNSUPPORTED!"); } - builder.append(String.format("%n")); + builder.append(String.format("%n")); builder.append(buildDeviceServices(device.deviceServiceIds)); return builder.toString(); } @@ -260,9 +262,7 @@ public class BoschShcCommandExtension extends AbstractConsoleCommandExtension im @Override public boolean complete(String[] args, int cursorArgumentIndex, int cursorPosition, List candidates) { - if (cursorArgumentIndex <= 0) { - return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates); - } - return false; + return cursorArgumentIndex <= 0 + && SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates); } } diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java index d8d49ac19eb..5f67dff2670 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCDeviceHandler.java @@ -166,10 +166,6 @@ public abstract class BoschSHCDeviceHandler extends BoschSHCHandler { */ @Override public @Nullable String getBoschID() { - if (config != null) { - return config.id; - } - - return null; + return config != null ? config.id : null; } } diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java index 2c4c1e887f8..3e9b0580b91 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/BoschSHCHandlerFactory.java @@ -117,6 +117,6 @@ public class BoschSHCHandlerFactory extends BaseThingHandlerFactory { // Search for mapping for thing type and return handler for it if found. Otherwise return null. return supportedThingTypes.stream().filter(mapping -> mapping.thingTypeUID.equals(thingTypeUID)).findFirst() - .<@Nullable BaseThingHandler> map(mapping -> mapping.handlerSupplier.apply(thing)).orElse(null); + .map(mapping -> mapping.handlerSupplier.apply(thing)).orElse(null); } } diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/bridge/BoschSslUtil.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/bridge/BoschSslUtil.java index 67f78df4c85..a177e61fc75 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/bridge/BoschSslUtil.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/bridge/BoschSslUtil.java @@ -142,7 +142,6 @@ public class BoschSslUtil { return keyStore; } } catch (OperatorCreationException | GeneralSecurityException | IOException e) { - logger.debug("Exception during keystore creation {}", e.getMessage()); throw new PairingFailedException("Can not create or load keystore file: " + keystorePath + ". Check path, write access and JKS content.", e); } @@ -154,7 +153,7 @@ public class BoschSslUtil { logger.debug("Creating a new self signed certificate: {}", dirName); final Instant now = Instant.now(); final Date notBefore = Date.from(now); - final Date notAfter = Date.from(now.plus(Duration.ofDays(365 * 10))); + final Date notAfter = Date.from(now.plus(Duration.ofDays(365 * 10l))); X500Name name = new X500Name(dirName); // create the certificate diff --git a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/shuttercontrol/ShutterControlHandler.java b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/shuttercontrol/ShutterControlHandler.java index 77c9c024e01..6f644ef0fd9 100644 --- a/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/shuttercontrol/ShutterControlHandler.java +++ b/bundles/org.openhab.binding.boschshc/src/main/java/org/openhab/binding/boschshc/internal/devices/shuttercontrol/ShutterControlHandler.java @@ -38,18 +38,6 @@ import org.slf4j.LoggerFactory; */ @NonNullByDefault public class ShutterControlHandler extends BoschSHCDeviceHandler { - /** - * Utility functions to convert data between Bosch things and openHAB items - */ - static final class DataConversion { - public static int levelToOpenPercentage(double level) { - return (int) Math.round((1 - level) * 100); - } - - public static double openPercentageToLevel(double openPercentage) { - return (100 - openPercentage) / 100.0; - } - } private final Logger logger = LoggerFactory.getLogger(getClass()); @@ -74,13 +62,13 @@ public class ShutterControlHandler extends BoschSHCDeviceHandler { if (command instanceof UpDownType upDownCommand) { // Set full close/open as target state ShutterControlServiceState state = new ShutterControlServiceState(); - if (upDownCommand == UpDownType.UP) { - state.level = 1.0; - } else if (upDownCommand == UpDownType.DOWN) { - state.level = 0.0; - } else { - logger.warn("Received unknown UpDownType command: {}", upDownCommand); - return; + switch (upDownCommand) { + case UpDownType.UP -> state.level = 1.0; + case UpDownType.DOWN -> state.level = 0.0; + default -> { + logger.warn("Received unknown UpDownType command: {}", upDownCommand); + return; + } } this.updateServiceState(this.shutterControlService, state); } else if (command instanceof StopMoveType stopMoveCommand) { @@ -92,16 +80,24 @@ public class ShutterControlHandler extends BoschSHCDeviceHandler { } } else if (command instanceof PercentType percentCommand) { // Set specific level - double level = DataConversion.openPercentageToLevel(percentCommand.doubleValue()); + double level = openPercentageToLevel(percentCommand.doubleValue()); this.updateServiceState(this.shutterControlService, new ShutterControlServiceState(level)); } } + private double openPercentageToLevel(double openPercentage) { + return (100 - openPercentage) / 100.0; + } + private void updateChannels(ShutterControlServiceState state) { if (state.level != null) { // Convert level to open ratio - int openPercentage = DataConversion.levelToOpenPercentage(state.level); + int openPercentage = levelToOpenPercentage(state.level); updateState(CHANNEL_LEVEL, new PercentType(openPercentage)); } } + + private int levelToOpenPercentage(double level) { + return (int) Math.round((1 - level) * 100); + } } diff --git a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtensionTest.java b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtensionTest.java index fbd8c12ae8b..20f74e09601 100644 --- a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtensionTest.java +++ b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/console/BoschShcCommandExtensionTest.java @@ -34,7 +34,6 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeoutException; -import java.util.stream.Collectors; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; @@ -246,8 +245,7 @@ class BoschShcCommandExtensionTest { .walk(Paths.get("src/main/java/org/openhab/binding/boschshc/internal/services").toAbsolutePath(), 1) .filter(Files::isDirectory).map(Path::getFileName).map(Path::toString) // exclude folders which no service implementation - .filter(name -> !name.equals("dto")).filter(name -> !name.equals("services")).sorted() - .collect(Collectors.toList()); + .filter(name -> !name.equals("dto")).filter(name -> !name.equals("services")).sorted().toList(); assertThat(services, is(fixture.getAllBoschShcServices())); } } diff --git a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/AbstractPowerSwitchHandlerWithPowerMeterTest.java b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/AbstractPowerSwitchHandlerWithPowerMeterTest.java index fe401253620..22faa10e7f3 100644 --- a/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/AbstractPowerSwitchHandlerWithPowerMeterTest.java +++ b/bundles/org.openhab.binding.boschshc/src/test/java/org/openhab/binding/boschshc/internal/devices/AbstractPowerSwitchHandlerWithPowerMeterTest.java @@ -57,6 +57,7 @@ public abstract class AbstractPowerSwitchHandlerWithPowerMeterTest> energyCaptor; @BeforeEach + @Override public void beforeEach(TestInfo testInfo) throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException { PowerMeterServiceState powerMeterServiceState = new PowerMeterServiceState();