Pārlūkot izejas kodu

Bump version and cleanup how response commands are detected

Baglio Tabifata 4 gadi atpakaļ
vecāks
revīzija
fb15895693

+ 1 - 1
pom.xml

@@ -6,7 +6,7 @@
 
   <groupId>info.hkzlab.dupal.analyzer</groupId>
   <artifactId>dupal-analyzer</artifactId>
-  <version>0.0.1</version>
+  <version>0.0.2</version>
 
   <name>dupal-analyzer</name>
   <!-- FIXME change it to the project's website -->

+ 1 - 1
src/main/java/info/hkzlab/dupal/analyzer/board/boardio/DuPALManager.java

@@ -93,7 +93,7 @@ public class DuPALManager {
                     else {
                         respBuf.append(resp);
                         retries = SERIAL_READ_RETRIES; // Reset the retries counter
-                        if(resp.trim().endsWith(DuPALProto.RESP_END)) break; // If we end with a character that could terminate the response, exit from here
+                        if(DuPALProto.isStringResponseCommand(respBuf.toString())) break; // If we end with a character that could terminate the response, exit from here
                     }
                 }
 

+ 12 - 7
src/main/java/info/hkzlab/dupal/analyzer/board/dupalproto/DuPALProto.java

@@ -6,8 +6,8 @@ public class DuPALProto {
     private final static String CMD_START = ">";
     private final static String CMD_END = "<";
    
-    public final static String RESP_START = "[";
-    public final static String RESP_END = "]";
+    private final static String RESP_START = "[";
+    private final static String RESP_END = "]";
 
     private final static char CMD_WRITE = 'W';
     private final static char CMD_READ = 'R';
@@ -20,6 +20,11 @@ public class DuPALProto {
         return CMD_START+CMD_READ+CMD_END;
     }
 
+    public static boolean isStringResponseCommand(String cmd) {
+        cmd = cmd.trim();
+        return cmd.startsWith(RESP_START) && cmd.endsWith(RESP_END);
+    }
+
     /**
      * The command will toggle the following pins on the DuPAL, from LSB to MSB
      * 1, 2, 3, 4, 5, 6, 7, 8, 9, 11 (these are connected directly to the PAL)
@@ -27,7 +32,7 @@ public class DuPALProto {
      * @param address the combination of output to compose on the DuPAL
      * @return The generated command
      */
-    public static String buildWRITECommand(int address) {
+    public static String buildWRITECommand(final int address) {
         return ""+CMD_START+CMD_WRITE+" "+String.format("%08X", address & 0x3FFFF)+CMD_END;
     }
 
@@ -45,10 +50,10 @@ public class DuPALProto {
      * @param response String containing the response received by the DuPAL
      * @return Returns an integer containing the state of the PAL pins
      */
-    public static int handleREADResponse(String response) {
+    public static int handleREADResponse(final String response) {
         String[] readRes = parseResponse(response);
 
-        if((readRes == null) || readRes.length != 2 || readRes[0].charAt(0) != CMD_READ) return -1;
+        if((readRes == null) || readRes.length != 2 || !isStringResponseCommand(response) || readRes[0].charAt(0) != CMD_READ) return -1;
         
         try {
             return Integer.parseInt(readRes[1], 16);
@@ -57,10 +62,10 @@ public class DuPALProto {
         }
     }
 
-    public static int handleWRITEResponse(String response) {
+    public static int handleWRITEResponse(final String response) {
          String[] readRes = parseResponse(response);
 
-        if((readRes == null) || readRes.length != 2 || readRes[0].charAt(0) != CMD_WRITE) return -1;
+        if((readRes == null) || readRes.length != 2 || !isStringResponseCommand(response) || readRes[0].charAt(0) != CMD_WRITE) return -1;
         
         try {
             return Integer.parseInt(readRes[1], 16);