Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -73,10 +73,7 @@ public UdpResponse send(UdpRequest request) throws IOException {
buffer.getLong();
buffer.position(16);
int msgType = buffer.getInt();
buffer.get();
int blockCount = Byte.toUnsignedInt(buffer.get());
boolean thisCompressed = buffer.get() != 0;
buffer.get();
buffer.position(24);

if (!ackSent && (msgType == MSG_LEAD || msgType == MSG_BLOCK
|| msgType == HaywardMessageType.MSP_TELEMETRY_UPDATE.getMsgInt())) {
Expand All @@ -85,8 +82,9 @@ public UdpResponse send(UdpRequest request) throws IOException {
}

if (msgType == MSG_LEAD) {
expectedBlocks = blockCount;
compressed = thisCompressed;
String xml = new String(data, 24, data.length - 24, StandardCharsets.UTF_8).trim();
expectedBlocks = parseIntParameter(xml, "MsgBlockCount");
compressed = parseIntParameter(xml, "Type") == 1;
} else if (msgType == MSG_BLOCK) {
blocks.write(data, 24, data.length - 24);
expectedBlocks--;
Expand Down Expand Up @@ -128,6 +126,24 @@ public UdpResponse send(UdpRequest request) throws IOException {
return response;
}

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;
}
try {
return Integer.parseInt(xml.substring(start, end));
} catch (NumberFormatException e) {
Comment on lines +133 to +146

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] Parse lead parameters without assuming attribute order

The new parseIntParameter helper searches for parameters using the literal substring <Parameter name="…">. XML attribute order is not guaranteed, and the OmniLogic examples in API.md include additional attributes (e.g. dataType="int" name="MsgBlockCount"). When the lead message contains any other attributes or a different ordering, this method returns 0, leaving expectedBlocks at 0 and compressed false so the loop will wait until it times out instead of assembling the received blocks. Please parse the XML element without depending on attribute ordering or additional attributes.

Useful? React with 👍 / 👎.

return 0;
}
}

private void sendAck(DatagramSocket socket, int messageId) throws IOException {
ByteBuffer header = ByteBuffer.allocate(24);
header.putInt(messageId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,20 @@ private static byte[] createAckPacket(int messageId) throws Exception {
}

private static byte[] createLeadPacket(int messageId, int blocks, boolean compressed) {
ByteBuffer buffer = ByteBuffer.allocate(24);
buffer.putInt(messageId);
buffer.putLong(System.currentTimeMillis());
buffer.put("1.22".getBytes(StandardCharsets.US_ASCII));
buffer.putInt(HaywardMessageType.MSP_LEADMESSAGE.getMsgInt());
buffer.put((byte) 1);
buffer.put((byte) blocks);
buffer.put((byte) (compressed ? 1 : 0));
buffer.put((byte) 0);
return buffer.array();
String xml = "<Message><Parameter name=\"MsgBlockCount\">" + blocks
+ "</Parameter><Parameter name=\"Type\">" + (compressed ? 1 : 0) + "</Parameter></Message>";
byte[] xmlBytes = (xml + '\0').getBytes(StandardCharsets.UTF_8);
ByteBuffer header = ByteBuffer.allocate(24);
header.putInt(messageId);
header.putLong(System.currentTimeMillis());
header.put("1.22".getBytes(StandardCharsets.US_ASCII));
header.putInt(HaywardMessageType.MSP_LEADMESSAGE.getMsgInt());
header.put((byte) 1);
header.put(new byte[3]);
byte[] packet = new byte[24 + xmlBytes.length];
System.arraycopy(header.array(), 0, packet, 0, 24);
System.arraycopy(xmlBytes, 0, packet, 24, xmlBytes.length);
return packet;
}

private static byte[] createBlockPacket(int messageId, String xml) {
Expand All @@ -128,9 +132,7 @@ private static byte[] createBlockPacket(int messageId, String xml) {
header.put("1.22".getBytes(StandardCharsets.US_ASCII));
header.putInt(HaywardMessageType.MSP_BLOCKMESSAGE.getMsgInt());
header.put((byte) 1);
header.put((byte) 0);
header.put((byte) 0);
header.put((byte) 0);
header.put(new byte[3]);
byte[] packet = new byte[24 + xmlBytes.length];
System.arraycopy(header.array(), 0, packet, 0, 24);
System.arraycopy(xmlBytes, 0, packet, 24, xmlBytes.length);
Expand Down