Skip to content

Commit 6476f55

Browse files
committed
Parse UDP message type with enum
1 parent cd2a5ff commit 6476f55

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

bundles/org.openhab.binding.haywardomnilogiclocal/src/main/java/org/openhab/binding/haywardomnilogiclocal/internal/HaywardMessageType.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.openhab.binding.haywardomnilogiclocal.internal;
22

33
import org.eclipse.jdt.annotation.NonNullByDefault;
4+
import org.eclipse.jdt.annotation.Nullable;
45

56
/**
67
* The type to request.
@@ -48,4 +49,13 @@ private HaywardMessageType(int msgInt) {
4849
public int getMsgInt() {
4950
return msgInt;
5051
}
52+
53+
public static @Nullable HaywardMessageType fromMsgInt(int msgInt) {
54+
for (HaywardMessageType type : values()) {
55+
if (type.msgInt == msgInt) {
56+
return type;
57+
}
58+
}
59+
return null;
60+
}
5161
}

bundles/org.openhab.binding.haywardomnilogiclocal/src/main/java/org/openhab/binding/haywardomnilogiclocal/internal/net/UdpClient.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.zip.InflaterInputStream;
2828

2929
import org.eclipse.jdt.annotation.NonNullByDefault;
30+
import org.eclipse.jdt.annotation.Nullable;
3031
import org.openhab.binding.haywardomnilogiclocal.internal.HaywardMessageType;
3132
import com.thoughtworks.xstream.XStream;
3233
import com.thoughtworks.xstream.io.xml.QNameMap;
@@ -37,10 +38,9 @@
3738
*/
3839
@NonNullByDefault
3940
public class UdpClient {
40-
private static final int MSG_TYPE_ACK = 1002;
41-
private static final int MSG_ACK = 1002;
42-
private static final int MSG_LEAD = 1998;
43-
private static final int MSG_BLOCK = 1999;
41+
private static final HaywardMessageType MSG_ACK = HaywardMessageType.ACK;
42+
private static final HaywardMessageType MSG_LEAD = HaywardMessageType.MSP_LEADMESSAGE;
43+
private static final HaywardMessageType MSG_BLOCK = HaywardMessageType.MSP_BLOCKMESSAGE;
4444

4545
private static final QNameMap QNAME_MAP;
4646
private static final XStream XSTREAM;
@@ -88,12 +88,11 @@ public UdpResponse send(UdpRequest request) throws IOException {
8888
int msgId = buffer.getInt();
8989
buffer.getLong();
9090
buffer.position(16);
91-
int msgType = buffer.getInt();
91+
@Nullable HaywardMessageType msgType = HaywardMessageType.fromMsgInt(buffer.getInt());
9292

9393
boolean thisCompressed = data[22] == 1;
9494

95-
if (msgType == MSG_LEAD || msgType == MSG_BLOCK
96-
|| msgType == HaywardMessageType.MSP_TELEMETRY_UPDATE.getMsgInt()) {
95+
if (msgType == MSG_LEAD || msgType == MSG_BLOCK || msgType == HaywardMessageType.MSP_TELEMETRY_UPDATE) {
9796
sendAck(socket, msgId);
9897
}
9998

@@ -118,7 +117,7 @@ public UdpResponse send(UdpRequest request) throws IOException {
118117
header.putInt(msgId);
119118
header.putLong(System.currentTimeMillis());
120119
header.put("1.22".getBytes(StandardCharsets.US_ASCII));
121-
header.putInt(msgType);
120+
header.putInt(msgType.getMsgInt());
122121
header.put((byte) 1);
123122
header.put(new byte[3]);
124123
byte[] xmlBytes = (xml + '\0').getBytes(StandardCharsets.UTF_8);
@@ -128,7 +127,7 @@ public UdpResponse send(UdpRequest request) throws IOException {
128127
response = UdpResponse.fromBytes(packetBytes, packetBytes.length);
129128
break;
130129
}
131-
} else if (msgType == HaywardMessageType.ACK.getMsgInt()) {
130+
} else if (msgType == HaywardMessageType.ACK) {
132131
continue;
133132
} else {
134133
response = UdpResponse.fromBytes(data, data.length);
@@ -151,7 +150,7 @@ private void sendAck(DatagramSocket socket, int messageId) throws IOException {
151150
header.putInt(messageId);
152151
header.putLong(System.currentTimeMillis());
153152
header.put("1.22".getBytes(StandardCharsets.US_ASCII));
154-
header.putInt(MSG_TYPE_ACK);
153+
header.putInt(MSG_ACK.getMsgInt());
155154
header.put((byte) 1);
156155
header.put(new byte[3]);
157156

@@ -163,7 +162,7 @@ private void sendAck(DatagramSocket socket, int messageId) throws IOException {
163162
DatagramPacket packet = new DatagramPacket(out, out.length, address, port);
164163
socket.send(packet);
165164

166-
UdpRequest ack = new UdpRequest(MSG_ACK, "", messageId);
165+
UdpRequest ack = new UdpRequest(MSG_ACK.getMsgInt(), "", messageId);
167166
byte[] ackBytes = ack.toBytes();
168167
DatagramPacket ackPacket = new DatagramPacket(ackBytes, ackBytes.length, address, port);
169168
socket.send(ackPacket);

0 commit comments

Comments
 (0)