public void applySettingMasterGroup(JSONObject js, String pg) throws Exception { if (pg.equals(MNEMONIC_GROUP_STATUS_REPORT)) { // This is a status report master object that came in through a response object. // meaning that the response was asked for like this {"sr":""} and returned like this. // {"r":{"sr":{"line":0,"posx":0.000,"posy":0.000,"posz":0.000,"posa":0.000,"vel":0.000,"unit":1,"momo":0,"stat":3},"f":[1,0,10,885]}} // Right now its parsed down to JUST the json object for the SR like so. // {"sr":{"line":0,"posx":0.000,"posy":0.000,"posz":0.000,"posa":0.000,"vel":0.000,"unit":1,"momo":0,"stat":3},"f":[1,0,10,885]} // so we can now just pass it to the applySettingStatusReport method. applySettingStatusReport(js); } else { if (js.keySet().size() > 1) { Iterator ii = js.keySet().iterator(); // This is a special multi single value response object while (ii.hasNext()) { String key = ii.next().toString(); if (key.equals("f")) { parseFooter( js.getJSONArray( "f")); // This is very important. We break out our response footer.. error // codes.. bytes availble in hardware buffer etc. } else { responseCommand rc = TinygDriver.getInstance().mneManager.lookupSingleGroupMaster(key, pg); if (rc == null) { // This happens when a new mnemonic has been added to the tinyG firmware // but not added to tgFX's MnemonicManger // This is the error case logger.error( "Mnemonic Lookup Failed in applySettingsMasterGroup. \n\tMake sure there are not new elements added to TinyG and not to the MnemonicManager Class.\n\tMNEMONIC FAILED: " + key); } else { // This is the normal case rc.setSettingValue(js.get(key).toString()); String parentGroup = rc.getSettingParent(); _applySettings( rc.buildJsonObject(), rc.getSettingParent()); // we will supply the parent object name for each key pai } } } } } }
private void _applySettings(JSONObject js, String pg) throws Exception { switch (pg) { case (MNEMONIC_GROUP_MOTOR_1): TinygDriver.getInstance() .machine .getMotorByNumber(MNEMONIC_GROUP_MOTOR_1) .applyJsonSystemSetting( js.getJSONObject(MNEMONIC_GROUP_MOTOR_1), MNEMONIC_GROUP_MOTOR_1); setChanged(); message[0] = "CMD_GET_MOTOR_SETTINGS"; message[1] = MNEMONIC_GROUP_MOTOR_1; notifyObservers(message); break; case (MNEMONIC_GROUP_MOTOR_2): TinygDriver.getInstance() .machine .getMotorByNumber(MNEMONIC_GROUP_MOTOR_2) .applyJsonSystemSetting( js.getJSONObject(MNEMONIC_GROUP_MOTOR_2), MNEMONIC_GROUP_MOTOR_2); setChanged(); message[0] = "CMD_GET_MOTOR_SETTINGS"; message[1] = MNEMONIC_GROUP_MOTOR_2; notifyObservers(message); break; case (MNEMONIC_GROUP_MOTOR_3): TinygDriver.getInstance() .machine .getMotorByNumber(MNEMONIC_GROUP_MOTOR_3) .applyJsonSystemSetting( js.getJSONObject(MNEMONIC_GROUP_MOTOR_3), MNEMONIC_GROUP_MOTOR_3); setChanged(); message[0] = "CMD_GET_MOTOR_SETTINGS"; message[1] = MNEMONIC_GROUP_MOTOR_3; notifyObservers(message); break; case (MNEMONIC_GROUP_MOTOR_4): TinygDriver.getInstance() .machine .getMotorByNumber(MNEMONIC_GROUP_MOTOR_4) .applyJsonSystemSetting( js.getJSONObject(MNEMONIC_GROUP_MOTOR_4), MNEMONIC_GROUP_MOTOR_4); setChanged(); message[0] = "CMD_GET_MOTOR_SETTINGS"; message[1] = MNEMONIC_GROUP_MOTOR_4; notifyObservers(message); break; case (MNEMONIC_GROUP_AXIS_X): TinygDriver.getInstance() .machine .getAxisByName(MNEMONIC_GROUP_AXIS_X) .applyJsonSystemSetting(js.getJSONObject(MNEMONIC_GROUP_AXIS_X), MNEMONIC_GROUP_AXIS_X); setChanged(); message[0] = "CMD_GET_AXIS_SETTINGS"; message[1] = MNEMONIC_GROUP_AXIS_X; notifyObservers(message); break; case (MNEMONIC_GROUP_AXIS_Y): TinygDriver.getInstance() .machine .getAxisByName(MNEMONIC_GROUP_AXIS_Y) .applyJsonSystemSetting(js.getJSONObject(MNEMONIC_GROUP_AXIS_Y), MNEMONIC_GROUP_AXIS_Y); setChanged(); message[0] = "CMD_GET_AXIS_SETTINGS"; message[1] = MNEMONIC_GROUP_AXIS_Y; notifyObservers(message); break; case (MNEMONIC_GROUP_AXIS_Z): TinygDriver.getInstance() .machine .getAxisByName(MNEMONIC_GROUP_AXIS_Z) .applyJsonSystemSetting(js.getJSONObject(MNEMONIC_GROUP_AXIS_Z), MNEMONIC_GROUP_AXIS_Z); setChanged(); message[0] = "CMD_GET_AXIS_SETTINGS"; message[1] = MNEMONIC_GROUP_AXIS_Z; notifyObservers(message); break; case (MNEMONIC_GROUP_AXIS_A): TinygDriver.getInstance() .machine .getAxisByName(MNEMONIC_GROUP_AXIS_A) .applyJsonSystemSetting(js.getJSONObject(MNEMONIC_GROUP_AXIS_A), MNEMONIC_GROUP_AXIS_A); setChanged(); message[0] = "CMD_GET_AXIS_SETTINGS"; message[1] = MNEMONIC_GROUP_AXIS_A; notifyObservers(message); break; case (MNEMONIC_GROUP_AXIS_B): TinygDriver.getInstance() .machine .getAxisByName(MNEMONIC_GROUP_AXIS_B) .applyJsonSystemSetting(js.getJSONObject(MNEMONIC_GROUP_AXIS_B), MNEMONIC_GROUP_AXIS_B); setChanged(); message[0] = "CMD_GET_AXIS_SETTINGS"; message[1] = MNEMONIC_GROUP_AXIS_B; notifyObservers(message); break; case (MNEMONIC_GROUP_AXIS_C): TinygDriver.getInstance() .machine .getAxisByName(MNEMONIC_GROUP_AXIS_C) .applyJsonSystemSetting(js.getJSONObject(MNEMONIC_GROUP_AXIS_C), MNEMONIC_GROUP_AXIS_C); setChanged(); message[0] = "CMD_GET_AXIS_SETTINGS"; message[1] = MNEMONIC_GROUP_AXIS_C; notifyObservers(message); break; case ("hom"): logger.info("HOME"); break; case ("msg"): // NOP break; case (MNEMONIC_GROUP_SYSTEM): TinygDriver.getInstance() .machine .applyJsonSystemSetting(js.getJSONObject(MNEMONIC_GROUP_SYSTEM), MNEMONIC_GROUP_SYSTEM); /** UNCOMMENT THIS BELOW WHEN WE HAVE MACHINE SETTINGS THAT NEED TO UPDATE THE GU */ message[0] = "MACHINE_UPDATE"; message[1] = null; setChanged(); notifyObservers(message); break; case (MNEMONIC_GROUP_STATUS_REPORT): logger.info("Status Report"); applySettingMasterGroup(js, MNEMONIC_GROUP_STATUS_REPORT); setChanged(); message[0] = "STATUS_REPORT"; message[1] = null; notifyObservers(message); break; case (MNEMONIC_GROUP_EMERGENCY_SHUTDOWN): Platform.runLater( new Runnable() { @Override public void run() { Main.postConsoleMessage("TinyG Alarm " + line); MonologFXButton btnYes = MonologFXButtonBuilder.create() .defaultButton(true) .icon("/testmonologfx/dialog_apply.png") .type(MonologFXButton.Type.YES) .build(); MonologFXButton btnNo = MonologFXButtonBuilder.create() .cancelButton(true) .icon("/testmonologfx/dialog_cancel.png") .type(MonologFXButton.Type.CANCEL) .build(); MonologFX mono = MonologFXBuilder.create() .titleText("Error Occured") .message( "You have triggered a limit switch. TinyG is now in DISABLED mode. \n" + "Manually back your machine off of its limit switches.\n Once done, if you would like to re-enable TinyG click yes.") .button(btnYes) .button(btnNo) .type(MonologFX.Type.ERROR) .build(); MonologFXButton.Type retval = mono.showDialog(); switch (retval) { case YES: logger.info("Clicked Yes"); try { TinygDriver.getInstance().priorityWrite((byte) 0x18); } catch (Exception ex) { logger.error(ex); } break; case CANCEL: logger.info("Clicked No"); Main.postConsoleMessage( "TinyG will remain in diabled mode until you power cycle or click the reset button."); break; } } }); default: // This is for single settings xfr, 1tr etc... // This is pretty ugly but it gets the key and the value. For single values. responseCommand rc = TinygDriver.getInstance().mneManager.lookupSingleGroup(pg); // String _parent = String.valueOf(parentGroup.charAt(0)); String newJs; // String _key = parentGroup; //I changed this to deal with the fb // mnemonic.. not sure if this works all over. rc.setSettingValue(String.valueOf(js.get(js.keys().next().toString()))); logger.info( "Single Key Value: Group:" + rc.getSettingParent() + " key:" + rc.getSettingKey() + " value:" + rc.getSettingValue()); if (rc.getSettingValue().equals((""))) { logger.info(rc.getSettingKey() + " value was null"); } else { this.applySetting( rc.buildJsonObject()); // We pass the new json object we created from the string above } } }
public void applySetting(JSONObject js) { try { if (js.length() == 0) { // This is a blank object we just return and move on } else if (js.keySet().size() > 1) { // If there are more than one object in the json response Iterator ii = js.keySet().iterator(); // This is a special multi single value response object while (ii.hasNext()) { String key = ii.next().toString(); switch (key) { case "f": parseFooter(js.getJSONArray("f")); // This is very important. // We break out our response footer.. error codes.. bytes availble in hardware buffer // etc. break; case "msg": message[0] = "TINYG_USER_MESSAGE"; message[1] = (String) js.get(key) + "\n"; logger.info("[+]TinyG Message Sent: " + js.get(key) + "\n"); setChanged(); notifyObservers(message); break; case "rx": TinygDriver.getInstance().serialWriter.setBuffer(js.getInt(key)); break; default: if (TinygDriver.getInstance().mneManager.isMasterGroupObject(key)) { // logger.info("Group Status Report Detected: " + key); applySettingMasterGroup(js.getJSONObject(key), key); continue; } responseCommand rc = TinygDriver.getInstance().mneManager.lookupSingleGroup(key); rc.setSettingValue(js.get(key).toString()); _applySettings( rc.buildJsonObject(), rc.getSettingParent()); // we will supply the parent object name for each key pair break; } } } else { /* This else follows: * Contains a single object in the JSON response */ if (js.keySet().contains("f")) { /** This is a single response footer object: Like So: {"f":[1,0,5,3330]} */ parseFooter(js.getJSONArray("f")); } else { /** * Contains a single object in the json response I am not sure this else is needed any * longer. */ _applySettings(js, js.keys().next().toString()); } } } catch (JSONException ex) { logger.error("[!] Error in applySetting(JsonOBject js) : " + ex.getMessage()); logger.error("[!]JSON String Was: " + js.toString()); logger.error("Error in Line: " + js); } catch (Exception ex) { logger.error("[!] Error in applySetting(JsonOBject js) : " + ex.getMessage()); logger.error(ex.getClass().toString()); } }