Skip to content

Commit

Permalink
Miele : Improve thread handling/usage (openhab#1913)
Browse files Browse the repository at this point in the history
Signed-off-by: Karel Goderis <[email protected]>
  • Loading branch information
kgoderis authored and kaikreuzer committed Feb 18, 2017
1 parent 13f6be3 commit eea7228
Showing 1 changed file with 35 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.MulticastSocket;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
Expand Down Expand Up @@ -343,44 +344,49 @@ public void run() {
while (true) {
try {
clientSocket = new MulticastSocket(JSON_RPC_PORT);
clientSocket.setSoTimeout(100);

clientSocket.setInterface(InetAddress.getByName((String) getConfig().get(INTERFACE)));
clientSocket.joinGroup(address1);
clientSocket.joinGroup(address2);

while (true) {
buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
clientSocket.receive(packet);

String event = new String(packet.getData());
logger.debug("Received a multicast event '{}' from '{}:{}'",
new Object[] { event, packet.getAddress(), packet.getPort() });

DeviceProperty dp = new DeviceProperty();
String uid = null;

String[] parts = StringUtils.split(event, "&");
for (String p : parts) {
String[] subparts = StringUtils.split(p, "=");
switch (subparts[0]) {
case "property": {
dp.Name = subparts[1];
break;
}
case "value": {
dp.Value = subparts[1];
break;
}
case "id": {
uid = subparts[1];
break;
try {
buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
clientSocket.receive(packet);

String event = new String(packet.getData());
logger.debug("Received a multicast event '{}' from '{}:{}'",
new Object[] { event, packet.getAddress(), packet.getPort() });

DeviceProperty dp = new DeviceProperty();
String uid = null;

String[] parts = StringUtils.split(event, "&");
for (String p : parts) {
String[] subparts = StringUtils.split(p, "=");
switch (subparts[0]) {
case "property": {
dp.Name = subparts[1];
break;
}
case "value": {
dp.Value = subparts[1];
break;
}
case "id": {
uid = subparts[1];
break;
}
}
}
}

for (ApplianceStatusListener listener : applianceStatusListeners) {
listener.onAppliancePropertyChanged(uid, dp);
for (ApplianceStatusListener listener : applianceStatusListeners) {
listener.onAppliancePropertyChanged(uid, dp);
}
} catch (SocketTimeoutException e) {
Thread.sleep(500);
}
}
} catch (Exception ex) {
Expand Down

0 comments on commit eea7228

Please sign in to comment.