From ff9454596e165d9c25659ced4698171fb556cc94 Mon Sep 17 00:00:00 2001 From: Hilbrand Bouwkamp Date: Thu, 4 Feb 2021 07:04:50 +0100 Subject: [PATCH] [networkupstools] Some sat improvements (#10017) Signed-off-by: Hilbrand Bouwkamp --- .../internal/NUTBindingConstants.java | 17 +---------------- .../internal/NUTDynamicChannelFactory.java | 2 +- .../networkupstools/internal/NUTHandler.java | 2 +- .../networkupstools/internal/NutName.java | 12 ++++++++---- .../internal/nut/NutConnector.java | 6 +++--- .../internal/NutNameChannelsTest.java | 8 ++++---- .../networkupstools/internal/NutNameTest.java | 11 +++++++---- 7 files changed, 25 insertions(+), 33 deletions(-) diff --git a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTBindingConstants.java b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTBindingConstants.java index 3e6dc00cc..c6ad83639 100644 --- a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTBindingConstants.java +++ b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTBindingConstants.java @@ -14,17 +14,10 @@ package org.openhab.binding.networkupstools.internal; import java.net.URI; -import javax.measure.Unit; -import javax.measure.quantity.Power; - import org.eclipse.jdt.annotation.NonNullByDefault; -import org.openhab.core.library.unit.Units; import org.openhab.core.thing.ThingTypeUID; import org.openhab.core.thing.type.ChannelTypeUID; -import tec.uom.se.format.SimpleUnitFormat; -import tec.uom.se.unit.ProductUnit; - /** * The {@link NUTBindingConstants} class defines common constants, which are * used across the whole binding. @@ -46,14 +39,6 @@ public class NUTBindingConstants { public static final URI DYNAMIC_CHANNEL_CONFIG_QUANTITY_TYPE = URI .create("channel-type:ups:dynamic-channel-config-quantity-type"); - public static final Unit AMPERE_PER_HOUR = new ProductUnit<>(Units.AMPERE.divide(Units.HOUR)); - public static final Unit VOLT_AMPERE = new ProductUnit<>(Units.VOLT.multiply(Units.AMPERE)); - - static { - SimpleUnitFormat.getInstance().label(AMPERE_PER_HOUR, "Ah"); - SimpleUnitFormat.getInstance().label(VOLT_AMPERE, "VA"); - } - private static final String PARAMETER_PREFIX_UPS = "ups."; /** @@ -71,7 +56,7 @@ public class NUTBindingConstants { private final String nutName; - private Parameters(final String nutName) { + Parameters(final String nutName) { this.nutName = nutName; } diff --git a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTDynamicChannelFactory.java b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTDynamicChannelFactory.java index 70d001cb6..01c136cc2 100644 --- a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTDynamicChannelFactory.java +++ b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTDynamicChannelFactory.java @@ -42,7 +42,7 @@ class NUTDynamicChannelFactory { private final NUTChannelTypeProvider channelTypeProvider; - public NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) { + NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) { this.channelTypeProvider = channelTypeProvider; } diff --git a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTHandler.java b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTHandler.java index da85007ba..9673ee9f5 100644 --- a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTHandler.java +++ b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTHandler.java @@ -309,7 +309,7 @@ public class NUTHandler extends BaseThingHandler { * @param nutApiFunction function that will be called * @return the value returned by the api call or null in case of an error */ - private T wrappedNutApiCall(final NutFunction nutApiFunction, String logging) { + private @Nullable T wrappedNutApiCall(final NutFunction nutApiFunction, String logging) { try { final NUTConfiguration localConfig = config; diff --git a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NutName.java b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NutName.java index 78736db72..88525801c 100644 --- a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NutName.java +++ b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NutName.java @@ -19,6 +19,7 @@ import java.util.stream.Stream; import javax.measure.Unit; +import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.PercentType; @@ -35,11 +36,12 @@ import org.openhab.core.types.UnDefType; * @author Hilbrand Bouwkamp - Initial contribution * @see https://github.com/networkupstools/nut/blob/master/docs/nut-names.txt */ +@NonNullByDefault enum NutName { // UPS UPS_ALARM("upsAlarm", "ups.alarm", StringType.class), UPS_LOAD("upsLoad", "ups.load", Units.PERCENT), - UPS_POWER("upsPower", "ups.power", NUTBindingConstants.VOLT_AMPERE), + UPS_POWER("upsPower", "ups.power", Units.VOLT_AMPERE), UPS_REALPOWER("upsRealpower", "ups.realpower", Units.WATT), UPS_STATUS("upsStatus", "ups.status", StringType.class), UPS_TEMPERATURE("upsTemperature", "ups.temperature", SIUnits.CELSIUS), @@ -64,13 +66,14 @@ enum NutName { BATTERY_RUNTIME("batteryRuntime", "battery.runtime", Units.SECOND), BATTERY_VOLTAGE("batteryVoltage", "battery.voltage", Units.VOLT); - private static final Map NUT_NAME_MAP = Stream.of(NutName.values()) + static final Map NUT_NAME_MAP = Stream.of(NutName.values()) .collect(Collectors.toMap(NutName::getChannelId, Function.identity())); private final String channelId; private final String name; private final Class stateClass; - private final Unit unit; + // unit only as a value if using a QuantityType. + private final @NonNullByDefault({}) Unit unit; NutName(final String channelId, final String name, final Class stateClass) { this(channelId, name, stateClass, null); @@ -80,7 +83,8 @@ enum NutName { this(channelId, name, QuantityType.class, unit); } - NutName(final String channelId, final String name, final Class stateClass, final Unit unit) { + NutName(final String channelId, final String name, final Class stateClass, + final @Nullable Unit unit) { this.channelId = channelId; this.name = name; this.stateClass = stateClass; diff --git a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/nut/NutConnector.java b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/nut/NutConnector.java index a6aa01197..a5effd3ee 100644 --- a/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/nut/NutConnector.java +++ b/bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/nut/NutConnector.java @@ -57,7 +57,7 @@ class NutConnector { * @param username username * @param password password */ - public NutConnector(final String host, final int port, final String username, final String password) { + NutConnector(final String host, final int port, final String username, final String password) { this.login = username.isEmpty() ? "" : String.format(USERNAME, username); this.password = password.isEmpty() ? "" : String.format(PASSWORD, password); inetSocketAddress = new InetSocketAddress(host, port); @@ -73,8 +73,8 @@ class NutConnector { * @return the data read from the NUT server * @throws NutException Exception thrown related to the NUT server connection and/or data. */ - public synchronized R read(final String command, final NutFunction, R> readFunction) - throws NutException { + public synchronized R read(final String command, + final NutFunction, @Nullable R> readFunction) throws NutException { int retry = 0; while (true) { diff --git a/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameChannelsTest.java b/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameChannelsTest.java index 00ed5d064..285247256 100644 --- a/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameChannelsTest.java +++ b/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameChannelsTest.java @@ -16,8 +16,9 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.*; -import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -28,7 +29,6 @@ import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.apache.commons.io.FileUtils; import org.junit.jupiter.api.Test; import org.openhab.core.library.CoreItemFactory; @@ -121,7 +121,7 @@ public class NutNameChannelsTest { final String path = getClass().getProtectionDomain().getClassLoader().getResource(".").getFile() + "../.."; try { - final List lines = FileUtils.readLines(new File(path, "README.md")); + final List lines = Files.readAllLines(Paths.get(path, "README.md")); return lines.stream().filter(line -> README_PATTERN.matcher(line).find()) .collect(Collectors.toMap(this::lineToNutName, Function.identity())); @@ -135,7 +135,7 @@ public class NutNameChannelsTest { final String path = getClass().getProtectionDomain().getClassLoader().getResource(".").getFile() + "../../src/main/resources/OH-INF/thing"; try { - final List lines = FileUtils.readLines(new File(path, filename)); + final List lines = Files.readAllLines(Paths.get(path, filename)); return lines.stream().filter(line -> pattern.matcher(line).find()).map(String::trim).sorted() .collect(Collectors.toList()); } catch (final IOException e) { diff --git a/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameTest.java b/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameTest.java index 56ba21f0f..b5af914a9 100644 --- a/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameTest.java +++ b/bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameTest.java @@ -18,7 +18,6 @@ import java.util.Optional; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.commons.lang.StringUtils; import org.junit.jupiter.api.Test; /** @@ -40,10 +39,14 @@ public class NutNameTest { assertTrue(matcher.find(), "NutName name '" + nn + "' could not be matched with expected pattern."); final String expectedChannelId = matcher.group(1) - + StringUtils.capitalize(Optional.ofNullable(matcher.group(2)).orElse("")) - + StringUtils.capitalize(Optional.ofNullable(matcher.group(3)).orElse("")) - + StringUtils.capitalize(Optional.ofNullable(matcher.group(4)).orElse("")); + + capitalize(Optional.ofNullable(matcher.group(2)).orElse("")) + + capitalize(Optional.ofNullable(matcher.group(3)).orElse("")) + + capitalize(Optional.ofNullable(matcher.group(4)).orElse("")); assertEquals(expectedChannelId, nn.getChannelId(), "Channel name not correct"); } } + + private String capitalize(String s) { + return s.isEmpty() ? "" : Character.toUpperCase(s.charAt(0)) + s.substring(1); + } }