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

[radiothermostat] Refactor usages of deprecated methods #18084

Merged
merged 1 commit into from
Jan 10, 2025
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 @@ -16,12 +16,13 @@
import java.net.DatagramPacket;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.InetSocketAddress;
import java.net.MulticastSocket;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
Expand Down Expand Up @@ -63,6 +64,8 @@ public class RadioThermostatDiscoveryService extends AbstractDiscoveryService {
private static final String RADIOTHERMOSTAT_DISCOVERY_MESSAGE = "TYPE: WM-DISCOVER\r\nVERSION: 1.0\r\n\r\nservices:com.marvell.wm.system*\r\n\r\n";

private static final String SSDP_MATCH = "WM-NOTIFY";
private static final String MULTICAST_GROUP = "239.255.255.250";
private static final int MULTICAST_PORT = 1900;
private static final int BACKGROUND_SCAN_INTERVAL_SECONDS = 300;

private @Nullable ScheduledFuture<?> scheduledFuture = null;
Expand Down Expand Up @@ -119,8 +122,7 @@ protected synchronized void doRunRun() {
* @throws SocketException
*/
private void sendDiscoveryBroacast(NetworkInterface ni) throws UnknownHostException, SocketException {
InetAddress m = InetAddress.getByName("239.255.255.250");
final int port = 1900;
InetAddress m = InetAddress.getByName(MULTICAST_GROUP);
logger.debug("Sending discovery broadcast");
try {
Enumeration<InetAddress> addrs = ni.getInetAddresses();
Expand All @@ -143,14 +145,17 @@ private void sendDiscoveryBroacast(NetworkInterface ni) throws UnknownHostExcept
// this seems to be okay on linux systems, but osx apparently prefers ipv6, so this
// prevents responses from being received unless the ipv4 stack is given preference.
MulticastSocket socket = new MulticastSocket(null);
InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getByName(MULTICAST_GROUP),
MULTICAST_PORT);

socket.setSoTimeout(5000);
socket.setReuseAddress(true);
// socket.setBroadcast(true);
socket.setNetworkInterface(ni);
socket.joinGroup(m);
socket.joinGroup(inetSocketAddress, null);
logger.debug("Joined UPnP Multicast group on Interface: {}", ni.getName());
byte[] requestMessage = RADIOTHERMOSTAT_DISCOVERY_MESSAGE.getBytes(StandardCharsets.UTF_8);
DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, m, port);
DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, m,
MULTICAST_PORT);
socket.send(datagramPacket);
try {
// Try to ensure that joinGroup has taken effect. Without this delay, the query
Expand Down Expand Up @@ -178,7 +183,7 @@ private void sendDiscoveryBroacast(NetworkInterface ni) throws UnknownHostExcept
"Timed out waiting for multicast response. Presumably all devices have already responded.");
}
} finally {
socket.leaveGroup(m);
socket.leaveGroup(inetSocketAddress, null);
socket.close();
}
} catch (IOException | InterruptedException e) {
Expand Down Expand Up @@ -215,8 +220,8 @@ protected void parseResponse(String response) {
if ("location".equals(key)) {
try {
url = value;
ip = new URL(value).getHost();
} catch (MalformedURLException e) {
ip = new URI(value).getHost();
} catch (URISyntaxException e) {
logger.debug("Malfored URL {}", e.getMessage());
}
}
Expand Down Expand Up @@ -265,12 +270,12 @@ protected void parseResponse(String response) {

logger.debug("Got discovered device.");

String label = String.format("RadioThermostat (%s)", name);
String label = String.format("Radio Thermostat (%s)", name);
result = DiscoveryResultBuilder.create(thingUid).withLabel(label)
.withRepresentationProperty(RadioThermostatBindingConstants.PROPERTY_IP)
.withProperty(RadioThermostatBindingConstants.PROPERTY_IP, ip)
.withProperty(RadioThermostatBindingConstants.PROPERTY_ISCT80, isCT80).build();
logger.debug("New RadioThermostat discovered with ID=<{}>", uuid);
logger.debug("New Radio Thermostat discovered with ID=<{}>", uuid);
this.thingDiscovered(result);
}
}