Skip to content

Commit 1a05388

Browse files
authored
Refactor usages of deprecated methods (openhab#18084)
Signed-off-by: Michael Lobstein <[email protected]>
1 parent 8dab1ac commit 1a05388

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

bundles/org.openhab.binding.radiothermostat/src/main/java/org/openhab/binding/radiothermostat/internal/discovery/RadioThermostatDiscoveryService.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
import java.net.DatagramPacket;
1717
import java.net.Inet4Address;
1818
import java.net.InetAddress;
19-
import java.net.MalformedURLException;
19+
import java.net.InetSocketAddress;
2020
import java.net.MulticastSocket;
2121
import java.net.NetworkInterface;
2222
import java.net.SocketException;
2323
import java.net.SocketTimeoutException;
24-
import java.net.URL;
24+
import java.net.URI;
25+
import java.net.URISyntaxException;
2526
import java.net.UnknownHostException;
2627
import java.nio.charset.StandardCharsets;
2728
import java.util.Enumeration;
@@ -63,6 +64,8 @@ public class RadioThermostatDiscoveryService extends AbstractDiscoveryService {
6364
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";
6465

6566
private static final String SSDP_MATCH = "WM-NOTIFY";
67+
private static final String MULTICAST_GROUP = "239.255.255.250";
68+
private static final int MULTICAST_PORT = 1900;
6669
private static final int BACKGROUND_SCAN_INTERVAL_SECONDS = 300;
6770

6871
private @Nullable ScheduledFuture<?> scheduledFuture = null;
@@ -119,8 +122,7 @@ protected synchronized void doRunRun() {
119122
* @throws SocketException
120123
*/
121124
private void sendDiscoveryBroacast(NetworkInterface ni) throws UnknownHostException, SocketException {
122-
InetAddress m = InetAddress.getByName("239.255.255.250");
123-
final int port = 1900;
125+
InetAddress m = InetAddress.getByName(MULTICAST_GROUP);
124126
logger.debug("Sending discovery broadcast");
125127
try {
126128
Enumeration<InetAddress> addrs = ni.getInetAddresses();
@@ -143,14 +145,17 @@ private void sendDiscoveryBroacast(NetworkInterface ni) throws UnknownHostExcept
143145
// this seems to be okay on linux systems, but osx apparently prefers ipv6, so this
144146
// prevents responses from being received unless the ipv4 stack is given preference.
145147
MulticastSocket socket = new MulticastSocket(null);
148+
InetSocketAddress inetSocketAddress = new InetSocketAddress(InetAddress.getByName(MULTICAST_GROUP),
149+
MULTICAST_PORT);
150+
146151
socket.setSoTimeout(5000);
147152
socket.setReuseAddress(true);
148-
// socket.setBroadcast(true);
149153
socket.setNetworkInterface(ni);
150-
socket.joinGroup(m);
154+
socket.joinGroup(inetSocketAddress, null);
151155
logger.debug("Joined UPnP Multicast group on Interface: {}", ni.getName());
152156
byte[] requestMessage = RADIOTHERMOSTAT_DISCOVERY_MESSAGE.getBytes(StandardCharsets.UTF_8);
153-
DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, m, port);
157+
DatagramPacket datagramPacket = new DatagramPacket(requestMessage, requestMessage.length, m,
158+
MULTICAST_PORT);
154159
socket.send(datagramPacket);
155160
try {
156161
// Try to ensure that joinGroup has taken effect. Without this delay, the query
@@ -178,7 +183,7 @@ private void sendDiscoveryBroacast(NetworkInterface ni) throws UnknownHostExcept
178183
"Timed out waiting for multicast response. Presumably all devices have already responded.");
179184
}
180185
} finally {
181-
socket.leaveGroup(m);
186+
socket.leaveGroup(inetSocketAddress, null);
182187
socket.close();
183188
}
184189
} catch (IOException | InterruptedException e) {
@@ -215,8 +220,8 @@ protected void parseResponse(String response) {
215220
if ("location".equals(key)) {
216221
try {
217222
url = value;
218-
ip = new URL(value).getHost();
219-
} catch (MalformedURLException e) {
223+
ip = new URI(value).getHost();
224+
} catch (URISyntaxException e) {
220225
logger.debug("Malfored URL {}", e.getMessage());
221226
}
222227
}
@@ -265,12 +270,12 @@ protected void parseResponse(String response) {
265270

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

268-
String label = String.format("RadioThermostat (%s)", name);
273+
String label = String.format("Radio Thermostat (%s)", name);
269274
result = DiscoveryResultBuilder.create(thingUid).withLabel(label)
270275
.withRepresentationProperty(RadioThermostatBindingConstants.PROPERTY_IP)
271276
.withProperty(RadioThermostatBindingConstants.PROPERTY_IP, ip)
272277
.withProperty(RadioThermostatBindingConstants.PROPERTY_ISCT80, isCT80).build();
273-
logger.debug("New RadioThermostat discovered with ID=<{}>", uuid);
278+
logger.debug("New Radio Thermostat discovered with ID=<{}>", uuid);
274279
this.thingDiscovered(result);
275280
}
276281
}

0 commit comments

Comments
 (0)