2424import java .net .UnknownHostException ;
2525import java .nio .ByteBuffer ;
2626import java .nio .charset .StandardCharsets ;
27+ import java .util .List ;
2728import java .util .zip .InflaterInputStream ;
2829
2930import org .eclipse .jdt .annotation .NonNullByDefault ;
31+ import org .eclipse .jdt .annotation .Nullable ;
3032import org .openhab .binding .haywardomnilogiclocal .internal .HaywardMessageType ;
33+ import com .thoughtworks .xstream .XStream ;
34+ import com .thoughtworks .xstream .annotations .XStreamAlias ;
35+ import com .thoughtworks .xstream .annotations .XStreamAsAttribute ;
36+ import com .thoughtworks .xstream .annotations .XStreamImplicit ;
37+ import com .thoughtworks .xstream .annotations .XStreamValue ;
38+ import com .thoughtworks .xstream .io .xml .StaxDriver ;
3139
3240/**
3341 * Simple UDP client used to communicate with the OmniLogic controller.
@@ -39,6 +47,30 @@ public class UdpClient {
3947 private static final int MSG_LEAD = 1998 ;
4048 private static final int MSG_BLOCK = 1999 ;
4149
50+ private static final XStream XSTREAM = new XStream (new StaxDriver ());
51+
52+ static {
53+ XSTREAM .allowTypes (new Class [] { Message .class , Parameter .class });
54+ XSTREAM .setClassLoader (UdpClient .class .getClassLoader ());
55+ XSTREAM .ignoreUnknownElements ();
56+ XSTREAM .processAnnotations (Message .class );
57+ }
58+
59+ @ XStreamAlias ("Message" )
60+ private static class Message {
61+ @ XStreamImplicit (itemFieldName = "Parameter" )
62+ private @ Nullable List <Parameter > parameters ;
63+ }
64+
65+ @ XStreamAlias ("Parameter" )
66+ private static class Parameter {
67+ @ XStreamAsAttribute
68+ private @ Nullable String name ;
69+
70+ @ XStreamValue
71+ private @ Nullable String value ;
72+ }
73+
4274 private final InetAddress address ;
4375 private final int port ;
4476
@@ -131,21 +163,24 @@ public UdpResponse send(UdpRequest request) throws IOException {
131163 }
132164
133165 private static int parseIntParameter (String xml , String name ) {
134- String search = "<Parameter name=\" " + name + "\" >" ;
135- int start = xml .indexOf (search );
136- if (start == -1 ) {
137- return 0 ;
138- }
139- start += search .length ();
140- int end = xml .indexOf ("</Parameter>" , start );
141- if (end == -1 ) {
142- return 0 ;
143- }
166+ Object obj ;
144167 try {
145- return Integer . parseInt (xml . substring ( start , end ) );
146- } catch (NumberFormatException e ) {
168+ obj = XSTREAM . fromXML (xml );
169+ } catch (RuntimeException e ) {
147170 return 0 ;
148171 }
172+ if (obj instanceof Message msg && msg .parameters != null ) {
173+ for (Parameter p : msg .parameters ) {
174+ if (name .equals (p .name ) && p .value != null ) {
175+ try {
176+ return Integer .parseInt (p .value );
177+ } catch (NumberFormatException e ) {
178+ return 0 ;
179+ }
180+ }
181+ }
182+ }
183+ return 0 ;
149184 }
150185
151186 private void sendAck (DatagramSocket socket , int messageId ) throws IOException {
0 commit comments