Skip to content

Commit

Permalink
[freebox] Removed dependency on 'org.apache.commons.lang' (#7727)
Browse files Browse the repository at this point in the history
* [freebox] Removed dependency on 'org.apache.commons.lang'
* Use variables

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored May 23, 2020
1 parent 739b97c commit 6f129fd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
Expand Down Expand Up @@ -149,16 +148,18 @@ public void onDataFetched(ThingUID bridge, List<FreeboxLanHost> lanHosts,
// Network devices
for (FreeboxLanHost host : lanHosts) {
String mac = host.getMAC();
if (StringUtils.isNotEmpty(mac)) {
String primaryName = host.getPrimaryName();
String vendorName = host.getVendorName();
if (mac != null && !mac.isEmpty()) {
if (discoverNetDevice) {
String uid = mac.replaceAll("[^A-Za-z0-9_]", "_");
thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_NET_DEVICE, bridge, uid);
String name = StringUtils.isEmpty(host.getPrimaryName()) ? ("Freebox Network Device " + mac)
: host.getPrimaryName();
String name = (primaryName == null || primaryName.isEmpty()) ? ("Freebox Network Device " + mac)
: primaryName;
logger.trace("Adding new Freebox Network Device {} to inbox", thingUID);
Map<String, Object> properties = new HashMap<>(1);
if (StringUtils.isNotEmpty(host.getVendorName())) {
properties.put(Thing.PROPERTY_VENDOR, host.getVendorName());
if (vendorName != null && !vendorName.isEmpty()) {
properties.put(Thing.PROPERTY_VENDOR, vendorName);
}
properties.put(FreeboxNetDeviceConfiguration.MAC_ADDRESS, mac);
discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
Expand All @@ -170,18 +171,18 @@ public void onDataFetched(ThingUID bridge, List<FreeboxLanHost> lanHosts,
if (host.getL3Connectivities() != null && discoverNetInterface) {
for (FreeboxLanHostL3Connectivity l3 : host.getL3Connectivities()) {
String addr = l3.getAddr();
if (StringUtils.isNotEmpty(addr)) {
if (addr != null && !addr.isEmpty()) {
String uid = addr.replaceAll("[^A-Za-z0-9_]", "_");
thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_NET_INTERFACE,
bridge, uid);
String name = addr;
if (StringUtils.isNotEmpty(host.getPrimaryName())) {
name += " (" + (host.getPrimaryName() + ")");
if (primaryName != null && !primaryName.isEmpty()) {
name += " (" + (primaryName + ")");
}
logger.trace("Adding new Freebox Network Interface {} to inbox", thingUID);
Map<String, Object> properties = new HashMap<>(1);
if (StringUtils.isNotEmpty(host.getVendorName())) {
properties.put(Thing.PROPERTY_VENDOR, host.getVendorName());
if (vendorName != null && !vendorName.isEmpty()) {
properties.put(Thing.PROPERTY_VENDOR, vendorName);
}
properties.put(FreeboxNetInterfaceConfiguration.IP_ADDRESS, addr);
discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
Expand All @@ -203,7 +204,7 @@ public void onDataFetched(ThingUID bridge, List<FreeboxLanHost> lanHosts,
// The Freebox API allows pushing media only to receivers with photo or video capabilities
// but not to receivers with only audio capability; so receivers without video capability
// are ignored by the discovery
if (StringUtils.isNotEmpty(name) && videoCapable) {
if (name != null && !name.isEmpty() && videoCapable) {
String uid = name.replaceAll("[^A-Za-z0-9_]", "_");
thingUID = new ThingUID(FreeboxBindingConstants.FREEBOX_THING_TYPE_AIRPLAY, bridge, uid);
logger.trace("Adding new Freebox AirPlay Device {} to inbox", thingUID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang.StringUtils;
import org.eclipse.smarthome.core.library.types.DecimalType;
import org.eclipse.smarthome.core.library.types.IncreaseDecreaseType;
import org.eclipse.smarthome.core.library.types.OnOffType;
Expand Down Expand Up @@ -150,7 +149,7 @@ public void initialize() {
dataListener.applyConfig(configDiscovery);
}

if (StringUtils.isNotEmpty(configuration.fqdn)) {
if (configuration.fqdn != null && !configuration.fqdn.isEmpty()) {
updateStatus(ThingStatus.UNKNOWN);

logger.debug("Binding will schedule a job to establish a connection...");
Expand Down Expand Up @@ -207,44 +206,47 @@ private void authorize() {
if (!httpsRequestOk) {
result = apiManager.checkApi(fqdn, false);
}
String apiBaseUrl = result == null ? null : result.getApiBaseUrl();
String apiVersion = result == null ? null : result.getApiVersion();
String deviceType = result == null ? null : result.getDeviceType();
String apiDomain = result == null ? null : result.getApiDomain();
Integer httpsPort = result == null ? null : result.getHttpsPort();
boolean useHttps = false;
String errorMsg = null;
if (result == null) {
errorMsg = "Can't connect to " + fqdn;
} else if (StringUtils.isEmpty(result.getApiBaseUrl())) {
} else if (apiBaseUrl == null || apiBaseUrl.isEmpty()) {
errorMsg = fqdn + " does not deliver any API base URL";
} else if (StringUtils.isEmpty(result.getApiVersion())) {
} else if (apiVersion == null || apiVersion.isEmpty()) {
errorMsg = fqdn + " does not deliver any API version";
} else if (Boolean.TRUE.equals(result.isHttpsAvailable()) && !Boolean.TRUE.equals(configuration.useOnlyHttp)) {
if (result.getHttpsPort() == null || StringUtils.isEmpty(result.getApiDomain())) {
if (httpsPort == null || apiDomain == null || apiDomain.isEmpty()) {
if (httpsRequestOk) {
useHttps = true;
} else {
logger.debug("{} does not deliver API domain or HTTPS port; use HTTP API", fqdn);
}
} else if (apiManager.checkApi(String.format("%s:%d", result.getApiDomain(), result.getHttpsPort()),
true) != null) {
} else if (apiManager.checkApi(String.format("%s:%d", apiDomain, httpsPort), true) != null) {
useHttps = true;
fqdn = String.format("%s:%d", result.getApiDomain(), result.getHttpsPort());
fqdn = String.format("%s:%d", apiDomain, httpsPort);
}
}

if (errorMsg != null) {
logger.debug("Thing {}: bad configuration: {}", getThing().getUID(), errorMsg);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMsg);
} else if (!apiManager.authorize(useHttps, fqdn, result.getApiBaseUrl(), result.getApiVersion(),
configuration.appToken)) {
if (StringUtils.isEmpty(configuration.appToken)) {
} else if (!apiManager.authorize(useHttps, fqdn, apiBaseUrl, apiVersion, configuration.appToken)) {
if (configuration.appToken == null || configuration.appToken.isEmpty()) {
errorMsg = "App token not set in the thing configuration";
} else {
errorMsg = "Check your app token in the thing configuration; opening session with " + fqdn + " using "
+ (useHttps ? "HTTPS" : "HTTP") + " API version " + result.getApiVersion() + " failed";
+ (useHttps ? "HTTPS" : "HTTP") + " API version " + apiVersion + " failed";
}
logger.debug("Thing {}: {}", getThing().getUID(), errorMsg);
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, errorMsg);
} else {
logger.debug("Thing {}: session opened with {} using {} API version {}", getThing().getUID(), fqdn,
(useHttps ? "HTTPS" : "HTTP"), result.getApiVersion());
(useHttps ? "HTTPS" : "HTTP"), apiVersion);
if (globalJob == null || globalJob.isCancelled()) {
long pollingInterval = getConfigAs(FreeboxServerConfiguration.class).refreshInterval;
logger.debug("Scheduling server state update every {} seconds...", pollingInterval);
Expand All @@ -260,14 +262,14 @@ private void authorize() {
}

Map<String, String> properties = editProperties();
if (result != null && StringUtils.isNotEmpty(result.getApiBaseUrl())) {
properties.put(API_BASE_URL, result.getApiBaseUrl());
if (apiBaseUrl != null && !apiBaseUrl.isEmpty()) {
properties.put(API_BASE_URL, apiBaseUrl);
}
if (result != null && StringUtils.isNotEmpty(result.getApiVersion())) {
properties.put(API_VERSION, result.getApiVersion());
if (apiVersion != null && !apiVersion.isEmpty()) {
properties.put(API_VERSION, apiVersion);
}
if (result != null && StringUtils.isNotEmpty(result.getDeviceType())) {
properties.put(Thing.PROPERTY_HARDWARE_VERSION, result.getDeviceType());
if (deviceType != null && !deviceType.isEmpty()) {
properties.put(Thing.PROPERTY_HARDWARE_VERSION, deviceType);
}
updateProperties(properties);
}
Expand Down Expand Up @@ -308,11 +310,13 @@ public boolean unregisterDataListener(FreeboxDataListener dataListener) {
private boolean fetchConnectionStatus() {
try {
FreeboxConnectionStatus connectionStatus = apiManager.getConnectionStatus();
if (StringUtils.isNotEmpty(connectionStatus.getState())) {
updateChannelStringState(LINESTATUS, connectionStatus.getState());
String state = connectionStatus.getState();
if (state != null && !state.isEmpty()) {
updateChannelStringState(LINESTATUS, state);
}
if (StringUtils.isNotEmpty(connectionStatus.getIpv4())) {
updateChannelStringState(IPV4, connectionStatus.getIpv4());
String ipv4 = connectionStatus.getIpv4();
if (ipv4 != null && !ipv4.isEmpty()) {
updateChannelStringState(IPV4, ipv4);
}
updateChannelDecimalState(RATEUP, connectionStatus.getRateUp());
updateChannelDecimalState(RATEDOWN, connectionStatus.getRateDown());
Expand All @@ -328,7 +332,7 @@ private boolean fetchConnectionStatus() {
private boolean fetchxDslStatus() {
try {
String status = apiManager.getxDslStatus();
if (StringUtils.isNotEmpty(status)) {
if (status != null && !status.isEmpty()) {
updateChannelStringState(XDSLSTATUS, status);
}
return true;
Expand Down Expand Up @@ -422,18 +426,22 @@ private boolean fetchSystemConfig() {
try {
FreeboxSystemConfig config = apiManager.getSystemConfig();
Map<String, String> properties = editProperties();
if (StringUtils.isNotEmpty(config.getSerial())) {
properties.put(Thing.PROPERTY_SERIAL_NUMBER, config.getSerial());
String value = config.getSerial();
if (value != null && !value.isEmpty()) {
properties.put(Thing.PROPERTY_SERIAL_NUMBER, value);
}
if (StringUtils.isNotEmpty(config.getBoardName())) {
properties.put(Thing.PROPERTY_HARDWARE_VERSION, config.getBoardName());
value = config.getBoardName();
if (value != null && !value.isEmpty()) {
properties.put(Thing.PROPERTY_HARDWARE_VERSION, value);
}
if (StringUtils.isNotEmpty(config.getFirmwareVersion())) {
properties.put(Thing.PROPERTY_FIRMWARE_VERSION, config.getFirmwareVersion());
updateChannelStringState(FWVERSION, config.getFirmwareVersion());
value = config.getFirmwareVersion();
if (value != null && !value.isEmpty()) {
properties.put(Thing.PROPERTY_FIRMWARE_VERSION, value);
updateChannelStringState(FWVERSION, value);
}
if (StringUtils.isNotEmpty(config.getMac())) {
properties.put(Thing.PROPERTY_MAC_ADDRESS, config.getMac());
value = config.getMac();
if (value != null && !value.isEmpty()) {
properties.put(Thing.PROPERTY_MAC_ADDRESS, value);
}
updateProperties(properties);

Expand Down

0 comments on commit 6f129fd

Please sign in to comment.