[boschshc] Fix compiler warnings and SAT/Sonar issues (#18656)

* refactored code

Signed-off-by: David Pace <dev@davidpace.de>
pull/18690/head
David Pace 2025-05-16 13:51:16 +02:00 committed by GitHub
parent c6c63bd609
commit 05b095f4e1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 30 additions and 39 deletions

View File

@ -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_.

View File

@ -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<String> candidates) {
if (cursorArgumentIndex <= 0) {
return SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
}
return false;
return cursorArgumentIndex <= 0
&& SUBCMD_COMPLETER.complete(args, cursorArgumentIndex, cursorPosition, candidates);
}
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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()));
}
}

View File

@ -57,6 +57,7 @@ public abstract class AbstractPowerSwitchHandlerWithPowerMeterTest<T extends Abs
private @Captor @NonNullByDefault({}) ArgumentCaptor<QuantityType<Energy>> energyCaptor;
@BeforeEach
@Override
public void beforeEach(TestInfo testInfo)
throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
PowerMeterServiceState powerMeterServiceState = new PowerMeterServiceState();