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.
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.";
/**
private final String nutName;
- private Parameters(final String nutName) {
+ Parameters(final String nutName) {
this.nutName = nutName;
}
private final NUTChannelTypeProvider channelTypeProvider;
- public NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
+ NUTDynamicChannelFactory(final NUTChannelTypeProvider channelTypeProvider) {
this.channelTypeProvider = channelTypeProvider;
}
* @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;
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;
* @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),
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);
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;
* @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);
* @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) {
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;
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;
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()));
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) {
import java.util.regex.Matcher;
import java.util.regex.Pattern;
-import org.apache.commons.lang.StringUtils;
import org.junit.jupiter.api.Test;
/**
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);
+ }
}