Example #1
0
  @Override
  public void run() {
    logger.info("Response Parser Running");
    if (!Main.LOGLEVEL.equals("OFF")) {
      Main.print("[+]Response Parser Thread Running...");
    }
    while (RUN) {
      try {
        line = TinygDriver.jsonQueue.take();
        if (line.equals("")) {
          continue;
        }
        if (line.startsWith("{")) {
          if (isTEXT_MODE()) {
            setTEXT_MODE(false);
            // This checks to see if we WERE in textmode.  If we were we notify the user that we are
            // not longer and update the system state.
            setChanged();
            message[0] = "TEXTMODE_REPORT";
            message[1] =
                "[+]JSON Response Detected... Leaving Text mode..  Querying System State....\n";
            notifyObservers(message);
            try {
              TinygDriver.getInstance().cmdManager.queryAllMachineSettings();
              TinygDriver.getInstance().cmdManager.queryAllHardwareAxisSettings();
              TinygDriver.getInstance().cmdManager.queryAllMotorSettings();
            } catch (Exception ex) {
              logger.error(
                  "Error leaving Text mode and querying Motor, Machine and Axis Settings.");
            }
          }
          parseJSON(line); // Take a line from the response queue when its ready and parse it.

        } else {
          // Text Mode Response
          if (!isTEXT_MODE()) {
            // We are just entering text mode and need to alert the user.
            // This will fire the every time user is entering text mode.
            setTEXT_MODE(true);
            setChanged();
            message[0] = "TEXTMODE_REPORT";
            message[1] = "[+]User has entered text mode.  To exit type \"{\" and hit enter.\n";
            notifyObservers(message);
          }
          setChanged();
          message[0] = "TEXTMODE_REPORT";
          message[1] = line + "\n";
          notifyObservers(message);
        }
      } catch (InterruptedException | JSONException ex) {
        logger.error("[!]Error in responseParser run(): " + ex.getMessage());
      }
    }
  }
Example #2
0
  public synchronized void parseJSON(String line) throws JSONException {

    // logger.info("Got Line: " + line + " from TinyG.");
    if (!Main.LOGLEVEL.equals("OFF")) {
      Main.print("-" + line);
    }

    final JSONObject js = new JSONObject(line);

    if (js.has("r")
        || (js.has("sr"))
        || (js.has("tgfx"))) { // tgfx is for messages like timeout connections
      Platform.runLater(
          new Runnable() {
            @Override
            public void run() {
              try {

                if (js.has("tgfx")) {
                  // This is for when tgfx times out when trying to connect to TinyG.
                  // tgFX puts a message in the response parser queue to be parsed here.
                  setChanged();
                  message[0] = "TINYG_CONNECTION_TIMEOUT";
                  message[1] = (String) js.get("tgfx") + "\n";
                  notifyObservers(message);

                } else if (js.has("f")) {
                  // The new version of TinyG's footer has a footer element in each response.
                  // We parse it here
                  parseFooter(js.getJSONArray("f"));
                  if (js.has("r")) {
                    applySetting(js.getJSONObject("r"));
                  } else if (js.has("sr")) {
                    applySettingStatusReport(js.getJSONObject("sr"));
                  }

                } else { // This is where the old footer style is dealt with

                  // These are the 2 types of responses we will get back.
                  switch (js.keys().next().toString()) {
                    case ("r"):
                      applySetting(js.getJSONObject("r"));
                      break;
                    case ("sr"):
                      applySettingStatusReport(js.getJSONObject("sr"));
                      break;
                  }
                }

              } catch (JSONException ex) {
                logger.error(ex);
              }
            }
          });

    } else if (js.has("qr")) {
      TinygDriver.getInstance().qr.parse(js);
    } else if (js.has("er")) {
      applySetting(js);
    }
  }