]> git.basschouten.com Git - openhab-addons.git/commitdiff
[networkupstools] Some sat improvements (#10017)
authorHilbrand Bouwkamp <hilbrand@h72.nl>
Thu, 4 Feb 2021 06:04:50 +0000 (07:04 +0100)
committerGitHub <noreply@github.com>
Thu, 4 Feb 2021 06:04:50 +0000 (22:04 -0800)
Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTBindingConstants.java
bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTDynamicChannelFactory.java
bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NUTHandler.java
bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/NutName.java
bundles/org.openhab.binding.networkupstools/src/main/java/org/openhab/binding/networkupstools/internal/nut/NutConnector.java
bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameChannelsTest.java
bundles/org.openhab.binding.networkupstools/src/test/java/org/openhab/binding/networkupstools/internal/NutNameTest.java

index 3e6dc00cca11c93b65db4174bf702655899908ce..c6ad83639c8032cda52e86ca4f7ed65820ccf41a 100644 (file)
@@ -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<Power> AMPERE_PER_HOUR = new ProductUnit<>(Units.AMPERE.divide(Units.HOUR));
-    public static final Unit<Power> 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;
         }
 
index 70d001cb617ace4368ba462679cfa9c3413ae09b..01c136cc2578a85673e52bf53b75855d318c09c4 100644 (file)
@@ -42,7 +42,7 @@ class NUTDynamicChannelFactory {
 
     private final NUTChannelTypeProvider channelTypeProvider;
 
-    public NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
+    NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
         this.channelTypeProvider = channelTypeProvider;
     }
 
index da85007ba96fc6c80fc0fae3de85623d8d3d321f..9673ee9f524d613f44ebe98caa3a6743ea06a1b8 100644 (file)
@@ -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> T wrappedNutApiCall(final NutFunction<String, T> nutApiFunction, String logging) {
+    private @Nullable <T> T wrappedNutApiCall(final NutFunction<String, T> nutApiFunction, String logging) {
         try {
             final NUTConfiguration localConfig = config;
 
index 78736db72a1d30253829ba4a3d053c29fe9b6fb7..88525801c7a42c969c78bf852ae81e2bb7780702 100644 (file)
@@ -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<String, NutName> NUT_NAME_MAP = Stream.of(NutName.values())
+    static final Map<String, NutName> 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<? extends State> 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<? extends State> 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<? extends State> stateClass, final Unit<?> unit) {
+    NutName(final String channelId, final String name, final Class<? extends State> stateClass,
+            final @Nullable Unit<?> unit) {
         this.channelId = channelId;
         this.name = name;
         this.stateClass = stateClass;
index a6aa011973a18395adf3bc39020d8e97f3bb190c..a5effd3eeb91e4d71ba3d560f9601e9f0aac9537 100644 (file)
@@ -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> R read(final String command, final NutFunction<NutSupplier<String>, R> readFunction)
-            throws NutException {
+    public synchronized <R> R read(final String command,
+            final NutFunction<NutSupplier<String>, @Nullable R> readFunction) throws NutException {
         int retry = 0;
 
         while (true) {
index 00ed5d06460c782e6bedf7d9ab0a9e519f2206ce..285247256063858e15b1d9aace15dbc779fc03a8 100644 (file)
@@ -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<String> lines = FileUtils.readLines(new File(path, "README.md"));
+            final List<String> 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<String> lines = FileUtils.readLines(new File(path, filename));
+            final List<String> 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) {
index 56ba21f0f8436edd73d165aa2a7e831a4a70b338..b5af914a981d9c9b982e769b091ca6136e93df3b 100644 (file)
@@ -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);
+    }
 }