Skip to content
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 @@ -24,10 +24,18 @@
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.zip.InflaterInputStream;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.haywardomnilogiclocal.internal.HaywardMessageType;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamValue;
import com.thoughtworks.xstream.io.xml.StaxDriver;

/**
* Simple UDP client used to communicate with the OmniLogic controller.
Expand All @@ -39,6 +47,30 @@ public class UdpClient {
private static final int MSG_LEAD = 1998;
private static final int MSG_BLOCK = 1999;

private static final XStream XSTREAM = new XStream(new StaxDriver());

static {
XSTREAM.allowTypes(new Class[] { Message.class, Parameter.class });
XSTREAM.setClassLoader(UdpClient.class.getClassLoader());
XSTREAM.ignoreUnknownElements();
XSTREAM.processAnnotations(Message.class);
Comment on lines +50 to +56

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[P1] Register XStream annotations for Parameter elements

The new parseIntParameter relies on XStream to map <Parameter name="…">…</Parameter> blocks to the inner Parameter class, but the static initializer only calls XSTREAM.processAnnotations(Message.class). Because the annotations on the Parameter class (especially @XStreamAsAttribute and @XStreamValue) are never processed, deserialized Parameter instances keep both name and value as null. As a result, every lookup falls through and the method always returns 0, leaving expectedBlocks at 0 and causing send() to time out once blocks arrive. The initializer should also process the Parameter annotations (or enable autodetection) so that parameters are actually parsed.

Useful? React with 👍 / 👎.

}

@XStreamAlias("Message")
private static class Message {
@XStreamImplicit(itemFieldName = "Parameter")
private @Nullable List<Parameter> parameters;
}

@XStreamAlias("Parameter")
private static class Parameter {
@XStreamAsAttribute
private @Nullable String name;

@XStreamValue
private @Nullable String value;
}

private final InetAddress address;
private final int port;

Expand Down Expand Up @@ -131,21 +163,24 @@ public UdpResponse send(UdpRequest request) throws IOException {
}

private static int parseIntParameter(String xml, String name) {
String search = "<Parameter name=\"" + name + "\">";
int start = xml.indexOf(search);
if (start == -1) {
return 0;
}
start += search.length();
int end = xml.indexOf("</Parameter>", start);
if (end == -1) {
return 0;
}
Object obj;
try {
return Integer.parseInt(xml.substring(start, end));
} catch (NumberFormatException e) {
obj = XSTREAM.fromXML(xml);
} catch (RuntimeException e) {
return 0;
}
if (obj instanceof Message msg && msg.parameters != null) {
for (Parameter p : msg.parameters) {
if (name.equals(p.name) && p.value != null) {
try {
return Integer.parseInt(p.value);
} catch (NumberFormatException e) {
return 0;
}
}
}
}
return 0;
}

private void sendAck(DatagramSocket socket, int messageId) throws IOException {
Expand Down