Skip to content

Commit

Permalink
Update lastnpe EEA to 2.4.0 (openhab#16875)
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Siepel <[email protected]>
  • Loading branch information
lsiepel authored Jul 11, 2024
1 parent 7296b38 commit 5e0e9ce
Show file tree
Hide file tree
Showing 127 changed files with 584 additions and 411 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ private enum Type {
private final String defaultValue;
private final Optional<String> mappedTo;
private final Type type;
private Optional<String> value;
private @Nullable String value;

private OptionalConfigurationElement(String defaultValue) {
this(Type.OTHER, defaultValue, null);
Expand All @@ -389,19 +389,19 @@ private OptionalConfigurationElement(Type type, String defaultValue, @Nullable S
this.type = type;
this.defaultValue = defaultValue;
this.mappedTo = Optional.ofNullable(mappedTo);
value = Optional.empty();
}

private String getValue() {
return value.orElse(defaultValue);
String value = this.value;
return value != null ? value : this.defaultValue;
}

private void setValue(String value) {
this.value = Optional.of(value);
private void setValue(@Nullable String value) {
this.value = value;
}

private void clearValue() {
this.value = Optional.empty();
this.value = null;
}

private Optional<String> mappedTo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public JSScriptFileWatcher(final @Reference(target = WatchService.CONFIG_WATCHER

@Override
protected Optional<String> getScriptType(Path scriptFilePath) {
String scriptType = super.getScriptType(scriptFilePath).orElse(null);
if (!scriptFilePath.startsWith(getWatchPath().resolve("node_modules")) && ("js".equals(scriptType))) {
return Optional.of(scriptType);
Optional<String> scriptType = super.getScriptType(scriptFilePath);
if (scriptType.isPresent() && !scriptFilePath.startsWith(getWatchPath().resolve("node_modules"))
&& ("js".equals(scriptType.get()))) {
return scriptType;
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public JythonScriptFileWatcher(

@Override
protected Optional<String> getScriptType(Path scriptFilePath) {
String scriptType = super.getScriptType(scriptFilePath).orElse(null);
if (!scriptFilePath.startsWith(getWatchPath().resolve("lib")) && ("py".equals(scriptType))) {
return Optional.of(scriptType);
Optional<String> scriptType = super.getScriptType(scriptFilePath);
if (scriptType.isPresent() && !scriptFilePath.startsWith(getWatchPath().resolve("lib"))
&& ("py".equals(scriptType.get()))) {
return scriptType;
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public int getStationId() {
*
* @return {AirQualityJsonTime}
*/
public Optional<AirQualityTime> getTime() {
return Optional.ofNullable(time);
public @Nullable AirQualityTime getTime() {
return time;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.openhab.binding.airquality.internal.api.Pollutant;
import org.openhab.binding.airquality.internal.api.Pollutant.SensitiveGroup;
import org.openhab.binding.airquality.internal.api.dto.AirQualityData;
import org.openhab.binding.airquality.internal.api.dto.AirQualityTime;
import org.openhab.binding.airquality.internal.config.AirQualityConfiguration;
import org.openhab.binding.airquality.internal.config.SensitiveGroupConfiguration;
import org.openhab.core.config.core.Configuration;
Expand Down Expand Up @@ -261,10 +262,10 @@ private State getValue(String channelId, @Nullable String groupId, AirQualityDat
double hum = data.getIaqiValue("h");
return hum != -1 ? new QuantityType<>(hum, Units.PERCENT) : UnDefType.NULL;
case TIMESTAMP:
return data.getTime()
.map(time -> (State) new DateTimeType(
time.getObservationTime().withZoneSameLocal(timeZoneProvider.getTimeZone())))
.orElse(UnDefType.NULL);
AirQualityTime time = data.getTime();
return time != null
? new DateTimeType(time.getObservationTime().withZoneSameLocal(timeZoneProvider.getTimeZone()))
: UnDefType.NULL;
case DOMINENT:
return new StringType(data.getDominentPol());
case DEW_POINT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ private void readConfigs() {
Map<String, String> configMap = OBJECT_MAPPER.readValue(configJson,
new TypeReference<HashMap<String, String>>() {
});
this.username = Optional.ofNullable(configMap.get("username")).orElse("");
this.password = Optional.ofNullable(configMap.get("password")).orElse("");
this.macAddress = Optional.ofNullable(configMap.get("macAddress")).orElse("");
this.username = configMap.getOrDefault("username", "");
this.password = configMap.getOrDefault("password", "");
this.macAddress = configMap.getOrDefault("macAddress", "");
logger.debug("Processed configJson as {} {} {}", this.username, this.password, this.macAddress);
} catch (IOException ex) {
logger.debug("IOException when reading configJson from file {}", ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.io.IOException;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -122,7 +121,8 @@ private Map<String, String> getAvailableTvChannelListFromTv() throws IOException
private String getCurrentTvChannel() throws IOException {
TvChannelDTO tvChannelDTO = OBJECT_MAPPER.readValue(connectionManager.doHttpsGet(TV_CHANNEL_PATH),
TvChannelDTO.class);
return Optional.ofNullable(tvChannelDTO.getChannel()).map(ChannelDTO::getName).orElse("NA");
ChannelDTO channel = tvChannelDTO.getChannel();
return channel != null ? channel.getName() : "NA";
}

private void switchTvChannel(Command command) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,10 @@ public final String validate() {
validateInternal();
return "";
} catch (Exception e) {
var msg = Optional.ofNullable(e.getLocalizedMessage());
var msg = e.getLocalizedMessage();
var cause = Optional.ofNullable(e.getCause());
return msg.orElse("Unknown exception, message is null") // The message theoretically can be null
// (Exception's i-face) but in practice never is, so
// keeping cryptic non-i18nized text instead of
// throwing
.concat(cause.map(c -> "\n\t[" + c.getClass().getSimpleName() + "]").orElse(""));
return Objects.requireNonNullElse(msg, "Unknown exception, message is null").concat(
Objects.requireNonNull(cause.map(c -> "\n\t[" + c.getClass().getSimpleName() + "]").orElse("")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.Objects;
import java.util.Optional;
import java.util.SortedMap;
import java.util.function.Consumer;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -142,8 +141,8 @@ protected DeviceStatus extractDeviceStatusFromResponse(String apiResponse) throw

// Group names must match regex above
var properties = new DeviceProperties(Objects.requireNonNull(matcher.group("localIP")),
Objects.requireNonNull(matcher.group("lastSeen")), Optional.of(
getWebUiUrl(Objects.requireNonNull(this.oemServerHostname.getHostName()), this.oemServerPort)));
Objects.requireNonNull(matcher.group("lastSeen")),
getWebUiUrl(Objects.requireNonNull(this.oemServerHostname.getHostName()), this.oemServerPort));

return new DeviceStatus(Objects.requireNonNull(matcher.group("commands")), properties, i18nProvider);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Collections;
import java.util.Optional;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.argoclima.internal.ArgoClimaBindingConstants;
import org.openhab.binding.argoclima.internal.ArgoClimaTranslationProvider;
import org.openhab.binding.argoclima.internal.configuration.ArgoClimaConfigurationRemote;
Expand Down Expand Up @@ -53,17 +54,29 @@ class DeviceStatus {
*/
static class DeviceProperties {
private static final Logger LOGGER = LoggerFactory.getLogger(DeviceProperties.class);
private final Optional<String> localIP;
private final Optional<OffsetDateTime> lastSeen;
private final Optional<URL> vendorUiUrl;
private Optional<String> cpuId = Optional.empty();
private Optional<String> webUiUsername = Optional.empty();
private Optional<String> webUiPassword = Optional.empty();
private Optional<String> unitFWVersion = Optional.empty();
private Optional<String> wifiFWVersion = Optional.empty();
private Optional<String> wifiSSID = Optional.empty();
private Optional<String> wifiPassword = Optional.empty();
private Optional<String> localTime = Optional.empty();

private OffsetDateTime lastSeen = OffsetDateTime.MIN;

@Nullable
private final String localIP;
@Nullable
private final URL vendorUiUrl;
@Nullable
private String cpuId;
@Nullable
private String webUiUsername;
@Nullable
private String webUiPassword;
@Nullable
private String unitFWVersion;
@Nullable
private String wifiFWVersion;
@Nullable
private String wifiSSID;
@Nullable
private String wifiPassword;
@Nullable
private String localTime;

/**
* C-tor (from remote server query response)
Expand All @@ -72,8 +85,8 @@ static class DeviceProperties {
* @param lastSeenStr The ISO-8601-formatted date/time of last update (or empty string if N/A)
* @param vendorUiAddress The optional full URL to vendor's web UI
*/
public DeviceProperties(String localIP, String lastSeenStr, Optional<URL> vendorUiAddress) {
this.localIP = localIP.isEmpty() ? Optional.empty() : Optional.of(localIP);
public DeviceProperties(String localIP, String lastSeenStr, URL vendorUiAddress) {
this.localIP = !localIP.isBlank() ? localIP : null;
this.vendorUiUrl = vendorUiAddress;
this.lastSeen = dateFromISOString(lastSeenStr, "LastSeen");
}
Expand All @@ -84,9 +97,9 @@ public DeviceProperties(String localIP, String lastSeenStr, Optional<URL> vendor
* @param lastSeen The date/time of last update (when the response got received)
*/
public DeviceProperties(OffsetDateTime lastSeen) {
this.localIP = Optional.empty();
this.lastSeen = Optional.of(lastSeen);
this.vendorUiUrl = Optional.empty();
this.localIP = null;
this.lastSeen = lastSeen;
this.vendorUiUrl = null;
}

/**
Expand All @@ -96,31 +109,32 @@ public DeviceProperties(OffsetDateTime lastSeen) {
* @param properties The intercepted device-side request (most rich with properties)
*/
public DeviceProperties(OffsetDateTime lastSeen, DeviceSideUpdateDTO properties) {
this.localIP = Optional.of(properties.setup.localIP.orElse(properties.deviceIp));
this.lastSeen = Optional.of(lastSeen);
this.vendorUiUrl = Optional.of(ArgoClimaRemoteDevice.getWebUiUrl(properties.remoteServerId, 80));
this.cpuId = Optional.of(properties.cpuId);
this.webUiUsername = Optional.of(properties.setup.username.orElse(properties.username));
this.webUiPassword = properties.setup.password;
this.unitFWVersion = Optional.of(properties.setup.unitVersionInstalled.orElse(properties.unitFirmware));
this.wifiFWVersion = Optional.of(properties.setup.wifiVersionInstalled.orElse(properties.wifiFirmware));
this.wifiSSID = properties.setup.wifiSSID;
this.wifiPassword = properties.setup.wifiPassword;
this.localTime = properties.setup.localTime;
this.localIP = Objects.requireNonNull(properties.setup.localIP.orElse(properties.deviceIp));
this.lastSeen = lastSeen;
this.vendorUiUrl = ArgoClimaRemoteDevice.getWebUiUrl(properties.remoteServerId, 80);
this.cpuId = properties.cpuId;
this.webUiUsername = properties.setup.username.orElse(properties.username);
this.webUiPassword = properties.setup.password.get();
this.unitFWVersion = Objects
.requireNonNull(properties.setup.unitVersionInstalled.orElse(properties.unitFirmware));
this.wifiFWVersion = properties.setup.wifiVersionInstalled.orElse(properties.wifiFirmware);
this.wifiSSID = properties.setup.wifiSSID.get();
this.wifiPassword = properties.setup.wifiPassword.get();
this.localTime = properties.setup.localTime.get();
}

private static Optional<OffsetDateTime> dateFromISOString(String isoDateTime, String contextualName) {
private static OffsetDateTime dateFromISOString(String isoDateTime, String contextualName) {
if (isoDateTime.isEmpty()) {
return Optional.empty();
return OffsetDateTime.MIN;
}

try {
return Optional.of(OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(isoDateTime)));
return OffsetDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse(isoDateTime));
} catch (DateTimeException ex) {
// Swallowing exception (no need to handle - proceed as if the date was never provided)
LOGGER.debug("Failed to parse [{}] timestamp: {}. Exception: {}", contextualName, isoDateTime,
ex.getMessage());
return Optional.empty();
return OffsetDateTime.MIN;
}
}

Expand All @@ -135,7 +149,7 @@ private static String dateTimeToStringLocal(OffsetDateTime toConvert, TimeZonePr
* @return Time elapsed since last device-side update
*/
Duration getLastSeenDelta() {
return Duration.between(lastSeen.orElse(OffsetDateTime.MIN).toInstant(), Instant.now());
return Duration.between(lastSeen.toInstant(), Instant.now());
}

/**
Expand All @@ -147,18 +161,50 @@ Duration getLastSeenDelta() {
SortedMap<String, String> asPropertiesRaw(TimeZoneProvider timeZoneProvider) {
var result = new TreeMap<String, String>();

this.lastSeen.map((value) -> result.put(ArgoClimaBindingConstants.PROPERTY_LAST_SEEN,
dateTimeToStringLocal(value, timeZoneProvider)));
this.localIP.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_LOCAL_IP_ADDRESS, value));
this.vendorUiUrl.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_WEB_UI, value.toString()));
this.cpuId.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_CPU_ID, value));
this.webUiUsername.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_WEB_UI_USERNAME, value));
this.webUiPassword.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_WEB_UI_PASSWORD, value));
this.unitFWVersion.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_UNIT_FW, value));
this.wifiFWVersion.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_WIFI_FW, value));
this.wifiSSID.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_WIFI_SSID, value));
this.wifiPassword.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_WIFI_PASSWORD, value));
this.localTime.map(value -> result.put(ArgoClimaBindingConstants.PROPERTY_LOCAL_TIME, value));
String localIP = this.localIP;
String cpuId = this.cpuId;
URL vendorUiUrl = this.vendorUiUrl;
String webUiUsername = this.webUiUsername;
String webUiPassword = this.webUiPassword;
String unitFWVersion = this.unitFWVersion;
String wifiFWVersion = this.wifiFWVersion;
String wifiSSID = this.wifiSSID;
String wifiPassword = this.wifiPassword;
String localTime = this.localTime;

result.put(ArgoClimaBindingConstants.PROPERTY_LAST_SEEN,
dateTimeToStringLocal(this.lastSeen, timeZoneProvider));

if (localIP != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_LOCAL_IP_ADDRESS, localIP);
}
if (cpuId != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_CPU_ID, cpuId);
}
if (vendorUiUrl != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_WEB_UI, vendorUiUrl.toString());
}
if (webUiUsername != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_WEB_UI_USERNAME, webUiUsername);
}
if (webUiPassword != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_WEB_UI_PASSWORD, webUiPassword);
}
if (unitFWVersion != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_UNIT_FW, unitFWVersion);
}
if (wifiFWVersion != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_WIFI_FW, wifiFWVersion);
}
if (wifiSSID != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_WIFI_SSID, wifiSSID);
}
if (wifiPassword != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_WIFI_PASSWORD, wifiPassword);
}
if (localTime != null) {
result.put(ArgoClimaBindingConstants.PROPERTY_LOCAL_TIME, localTime);
}
return Collections.unmodifiableSortedMap(result);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.time.Duration;
import java.time.Instant;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -413,8 +414,9 @@ public final boolean hasInFlightCommand() {

private final String getInFlightCommandsRawValueOrDefault() {
final String valueNotAvailablePlaceholder = "N/A";
return inFlightCommand.map(c -> c.deviceCommandToSend.orElse(valueNotAvailablePlaceholder))
.orElse(valueNotAvailablePlaceholder);
return Objects
.requireNonNull(inFlightCommand.map(c -> c.deviceCommandToSend.orElse(valueNotAvailablePlaceholder))
.orElse(valueNotAvailablePlaceholder));
}

/////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.openhab.binding.argoclima.internal.device.api.protocol.elements;

import java.security.InvalidParameterException;
import java.util.Objects;
import java.util.Optional;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -44,7 +45,7 @@ public OnOffParam(IArgoSettingProvider settingsProvider) {
}

private static State valueToState(Optional<Boolean> value) {
return value.<State> map(v -> OnOffType.from(v)).orElse(UnDefType.UNDEF);
return Objects.requireNonNull(value.<State> map(v -> OnOffType.from(v)).orElse(UnDefType.UNDEF));
}

@Override
Expand Down
Loading

0 comments on commit 5e0e9ce

Please sign in to comment.