[networkupstools] Some sat improvements (#10017)
Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
This commit is contained in:
parent
c4a3b1e6ba
commit
ff9454596e
|
@ -14,17 +14,10 @@ package org.openhab.binding.networkupstools.internal;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
|
|
||||||
import javax.measure.Unit;
|
|
||||||
import javax.measure.quantity.Power;
|
|
||||||
|
|
||||||
import org.eclipse.jdt.annotation.NonNullByDefault;
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.openhab.core.library.unit.Units;
|
|
||||||
import org.openhab.core.thing.ThingTypeUID;
|
import org.openhab.core.thing.ThingTypeUID;
|
||||||
import org.openhab.core.thing.type.ChannelTypeUID;
|
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
|
* The {@link NUTBindingConstants} class defines common constants, which are
|
||||||
* used across the whole binding.
|
* used across the whole binding.
|
||||||
|
@ -46,14 +39,6 @@ public class NUTBindingConstants {
|
||||||
public static final URI DYNAMIC_CHANNEL_CONFIG_QUANTITY_TYPE = URI
|
public static final URI DYNAMIC_CHANNEL_CONFIG_QUANTITY_TYPE = URI
|
||||||
.create("channel-type:ups:dynamic-channel-config-quantity-type");
|
.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.";
|
private static final String PARAMETER_PREFIX_UPS = "ups.";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -71,7 +56,7 @@ public class NUTBindingConstants {
|
||||||
|
|
||||||
private final String nutName;
|
private final String nutName;
|
||||||
|
|
||||||
private Parameters(final String nutName) {
|
Parameters(final String nutName) {
|
||||||
this.nutName = nutName;
|
this.nutName = nutName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ class NUTDynamicChannelFactory {
|
||||||
|
|
||||||
private final NUTChannelTypeProvider channelTypeProvider;
|
private final NUTChannelTypeProvider channelTypeProvider;
|
||||||
|
|
||||||
public NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
|
NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
|
||||||
this.channelTypeProvider = channelTypeProvider;
|
this.channelTypeProvider = channelTypeProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -309,7 +309,7 @@ public class NUTHandler extends BaseThingHandler {
|
||||||
* @param nutApiFunction function that will be called
|
* @param nutApiFunction function that will be called
|
||||||
* @return the value returned by the api call or null in case of an error
|
* @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 {
|
try {
|
||||||
final NUTConfiguration localConfig = config;
|
final NUTConfiguration localConfig = config;
|
||||||
|
|
||||||
|
|
|
@ -19,6 +19,7 @@ import java.util.stream.Stream;
|
||||||
|
|
||||||
import javax.measure.Unit;
|
import javax.measure.Unit;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||||
import org.eclipse.jdt.annotation.Nullable;
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.openhab.core.library.types.DecimalType;
|
import org.openhab.core.library.types.DecimalType;
|
||||||
import org.openhab.core.library.types.PercentType;
|
import org.openhab.core.library.types.PercentType;
|
||||||
|
@ -35,11 +36,12 @@ import org.openhab.core.types.UnDefType;
|
||||||
* @author Hilbrand Bouwkamp - Initial contribution
|
* @author Hilbrand Bouwkamp - Initial contribution
|
||||||
* @see https://github.com/networkupstools/nut/blob/master/docs/nut-names.txt
|
* @see https://github.com/networkupstools/nut/blob/master/docs/nut-names.txt
|
||||||
*/
|
*/
|
||||||
|
@NonNullByDefault
|
||||||
enum NutName {
|
enum NutName {
|
||||||
// UPS
|
// UPS
|
||||||
UPS_ALARM("upsAlarm", "ups.alarm", StringType.class),
|
UPS_ALARM("upsAlarm", "ups.alarm", StringType.class),
|
||||||
UPS_LOAD("upsLoad", "ups.load", Units.PERCENT),
|
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_REALPOWER("upsRealpower", "ups.realpower", Units.WATT),
|
||||||
UPS_STATUS("upsStatus", "ups.status", StringType.class),
|
UPS_STATUS("upsStatus", "ups.status", StringType.class),
|
||||||
UPS_TEMPERATURE("upsTemperature", "ups.temperature", SIUnits.CELSIUS),
|
UPS_TEMPERATURE("upsTemperature", "ups.temperature", SIUnits.CELSIUS),
|
||||||
|
@ -64,13 +66,14 @@ enum NutName {
|
||||||
BATTERY_RUNTIME("batteryRuntime", "battery.runtime", Units.SECOND),
|
BATTERY_RUNTIME("batteryRuntime", "battery.runtime", Units.SECOND),
|
||||||
BATTERY_VOLTAGE("batteryVoltage", "battery.voltage", Units.VOLT);
|
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()));
|
.collect(Collectors.toMap(NutName::getChannelId, Function.identity()));
|
||||||
|
|
||||||
private final String channelId;
|
private final String channelId;
|
||||||
private final String name;
|
private final String name;
|
||||||
private final Class<? extends State> stateClass;
|
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) {
|
NutName(final String channelId, final String name, final Class<? extends State> stateClass) {
|
||||||
this(channelId, name, stateClass, null);
|
this(channelId, name, stateClass, null);
|
||||||
|
@ -80,7 +83,8 @@ enum NutName {
|
||||||
this(channelId, name, QuantityType.class, unit);
|
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.channelId = channelId;
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.stateClass = stateClass;
|
this.stateClass = stateClass;
|
||||||
|
|
|
@ -57,7 +57,7 @@ class NutConnector {
|
||||||
* @param username username
|
* @param username username
|
||||||
* @param password password
|
* @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.login = username.isEmpty() ? "" : String.format(USERNAME, username);
|
||||||
this.password = password.isEmpty() ? "" : String.format(PASSWORD, password);
|
this.password = password.isEmpty() ? "" : String.format(PASSWORD, password);
|
||||||
inetSocketAddress = new InetSocketAddress(host, port);
|
inetSocketAddress = new InetSocketAddress(host, port);
|
||||||
|
@ -73,8 +73,8 @@ class NutConnector {
|
||||||
* @return the data read from the NUT server
|
* @return the data read from the NUT server
|
||||||
* @throws NutException Exception thrown related to the NUT server connection and/or data.
|
* @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)
|
public synchronized <R> R read(final String command,
|
||||||
throws NutException {
|
final NutFunction<NutSupplier<String>, @Nullable R> readFunction) throws NutException {
|
||||||
int retry = 0;
|
int retry = 0;
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
|
|
@ -16,8 +16,9 @@ import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -28,7 +29,6 @@ import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.openhab.core.library.CoreItemFactory;
|
import org.openhab.core.library.CoreItemFactory;
|
||||||
|
|
||||||
|
@ -121,7 +121,7 @@ public class NutNameChannelsTest {
|
||||||
final String path = getClass().getProtectionDomain().getClassLoader().getResource(".").getFile() + "../..";
|
final String path = getClass().getProtectionDomain().getClassLoader().getResource(".").getFile() + "../..";
|
||||||
|
|
||||||
try {
|
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())
|
return lines.stream().filter(line -> README_PATTERN.matcher(line).find())
|
||||||
.collect(Collectors.toMap(this::lineToNutName, Function.identity()));
|
.collect(Collectors.toMap(this::lineToNutName, Function.identity()));
|
||||||
|
@ -135,7 +135,7 @@ public class NutNameChannelsTest {
|
||||||
final String path = getClass().getProtectionDomain().getClassLoader().getResource(".").getFile()
|
final String path = getClass().getProtectionDomain().getClassLoader().getResource(".").getFile()
|
||||||
+ "../../src/main/resources/OH-INF/thing";
|
+ "../../src/main/resources/OH-INF/thing";
|
||||||
try {
|
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()
|
return lines.stream().filter(line -> pattern.matcher(line).find()).map(String::trim).sorted()
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
} catch (final IOException e) {
|
} catch (final IOException e) {
|
||||||
|
|
|
@ -18,7 +18,6 @@ import java.util.Optional;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.apache.commons.lang.StringUtils;
|
|
||||||
import org.junit.jupiter.api.Test;
|
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.");
|
assertTrue(matcher.find(), "NutName name '" + nn + "' could not be matched with expected pattern.");
|
||||||
final String expectedChannelId = matcher.group(1)
|
final String expectedChannelId = matcher.group(1)
|
||||||
+ StringUtils.capitalize(Optional.ofNullable(matcher.group(2)).orElse(""))
|
+ capitalize(Optional.ofNullable(matcher.group(2)).orElse(""))
|
||||||
+ StringUtils.capitalize(Optional.ofNullable(matcher.group(3)).orElse(""))
|
+ capitalize(Optional.ofNullable(matcher.group(3)).orElse(""))
|
||||||
+ StringUtils.capitalize(Optional.ofNullable(matcher.group(4)).orElse(""));
|
+ capitalize(Optional.ofNullable(matcher.group(4)).orElse(""));
|
||||||
assertEquals(expectedChannelId, nn.getChannelId(), "Channel name not correct");
|
assertEquals(expectedChannelId, nn.getChannelId(), "Channel name not correct");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String capitalize(String s) {
|
||||||
|
return s.isEmpty() ? "" : Character.toUpperCase(s.charAt(0)) + s.substring(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue