Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[harmonyhub] Harmony discovery fix #6636

Merged
merged 2 commits into from
Dec 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
Expand All @@ -29,11 +28,14 @@
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.lang.StringUtils;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the usage of Commons? We wanted to remove this lib for 3.x.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a built in replacement for isEmpty checks for Strings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s==null || s.isEmpty() 😎

Copy link
Contributor Author

@digitaldan digitaldan Dec 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough, the isBlank method (which i'm using), checks for white space as well.

 StringUtils.isBlank(null)      = true
 StringUtils.isBlank("")        = true
 StringUtils.isBlank(" ")       = true
 StringUtils.isBlank("bob")     = false
 StringUtils.isBlank("  bob  ") = false

I can of course replicate this, just hate duplicating code across bindings and classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to add a StringUtils to org.eclipse.smarthome.core.util; ? (or i guess org.openhab.core.util now) ? I'm always a little hesitant to add "utils" classes as they tend to be dumping grounds for random stuff, but this seems like a often used feature (for me at least)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Java11 (which we want to move to for 3.x) has an isBlank() method on String - so no need for a util class :-)
If you need the blank check, then leave the Commons usage in place and we can refactor it when we drop Java 8.

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.discovery.AbstractDiscoveryService;
Expand Down Expand Up @@ -245,39 +247,30 @@ private void run() {
break;
}
logger.trace("READ {}", input);
Properties properties = readProperties(input);

String friendlyName = properties.getProperty("friendlyName");
if (!responses.contains(friendlyName)) {
responses.add(friendlyName);
hubDiscovered(properties);
// response format is key1:value1;key2:value2;key3:value3;
Map<String, String> properties = Stream.of(input.split(";")).map(line -> line.split(":", 2))
.collect(Collectors.toMap(entry -> entry[0], entry -> entry[1]));
String friendlyName = properties.get("friendlyName");
String hostName = properties.get("host_name");
String ip = properties.get("ip");
if (StringUtils.isNotBlank(friendlyName) && StringUtils.isNotBlank(hostName)
&& StringUtils.isNotBlank(ip) && !responses.contains(hostName)) {
responses.add(hostName);
hubDiscovered(ip, friendlyName, hostName);
}
}
} catch (IOException e) {
} catch (IOException | IndexOutOfBoundsException e) {
if (running) {
logger.debug("Error connecting with found hub", e);
}
}
}
}

private Properties readProperties(String input) throws IOException {
String propsString = input.replaceAll(";", "\n");
propsString = propsString.replaceAll(":", "=");
Properties properties = new Properties();
properties.load(new StringReader(propsString));
return properties;
}

}

private void hubDiscovered(Properties properties) {
String ip = properties.getProperty("ip");
String friendlyName = properties.getProperty("friendlyName");
String thingId = properties.getProperty("host_name").replaceAll("[^A-Za-z0-9\\-_]", "");

private void hubDiscovered(String ip, String friendlyName, String hostName) {
String thingId = hostName.replaceAll("[^A-Za-z0-9\\-_]", "");
logger.trace("Adding HarmonyHub {} ({}) at host {}", friendlyName, thingId, ip);

ThingUID uid = new ThingUID(HARMONY_HUB_THING_TYPE, thingId);
// @formatter:off
thingDiscovered(DiscoveryResultBuilder.create(uid)
Expand Down