From d14aa2afd3da1b2750fcbfc9e7e26825a4a4f079 Mon Sep 17 00:00:00 2001 From: Anurag Kompalli Date: Tue, 29 Jan 2019 11:00:31 -0500 Subject: [PATCH 1/2] Add files via upload --- config/config.json | 49 +-------------------------------------- config/configBackup.json | 1 + config/configBackup2.json | 1 + config/configTemp.json | 1 + 4 files changed, 4 insertions(+), 48 deletions(-) create mode 100644 config/configBackup.json create mode 100644 config/configBackup2.json create mode 100644 config/configTemp.json diff --git a/config/config.json b/config/config.json index d5b83cc..39c5096 100644 --- a/config/config.json +++ b/config/config.json @@ -1,48 +1 @@ -{ - "driveTrainRightFrontTalon" : 1, - "driveTrainRightBackTalon" : 2, - "driveTrainLeftFrontTalon" : 13, - "driveTrainLeftBackTalon" : 14, - - - "driveTrainSolenoidShifter" : 1, - "driveTrainSolenoidBoolean" : true, - "driveTrainPID_PValue" : 0.025, - "driveTrainPID_IValue" : 0.0, - "driveTrainPID_DValue" : 0.06, - - - "topLimitSwitchOnElevator" : 1, - "bottomLimitSwitchOnElevator" : 2, - "elevatorLeftTalon" : 8, - "elevatorRightTalon" : 9, - "elevatorDownPower" : -0.1, - "elevatorPID_PValue" : 1, - "elevatorPID_IValue" : 0, - "elevatorPID_DValue" : 0, - "elevatorPID_FValue" : 0, - - - "elevatorSolenoidOne" : 3, - "elevatorSolenoidTwo" : 2, - - "ingestorRightTalon" : 6, - "ingestorLeftTalon" : 7, - - "ingestorCurrentConstant" : 4, - "ingestorCurrentTimer" : 15, - - "ingestorSolenoidCloseHeavy" : 0, - "ingestorSolenoidCloseLight" : 5, - - "climberTalonOne" : 5, - "climberTalonTwo" : 4, - "climberTalonThree" : 11, - - "xboxPositionDriver" : 0, - "xboxPositionManip" : 1, - - "scaleDistance": 500, - "switchDistance": 400, - "turnToAngle": 50.0 -} +{"driveTrainPID_PValue":0.025,"xboxPositionDriver":0,"ingestorRightTalon":6,"driveTrainLeftBackTalon":14,"driveTrainPID_DValue":0.06,"topLimitSwitchOnElevator":1,"ingestorCurrentConstant":4,"driveTrainRightBackTalon":2,"elevatorPID_IValue":0,"driveTrainRightFrontTalon":1,"elevatorPID_FValue":0,"ingestorLeftTalon":7,"switchDistance":400,"ingestorCurrentTimer":15,"climberTalonOne":5,"elevatorPID_PValue":1,"elevatorLeftTalon":8,"elevatorPID_DValue":0,"driveTrainSolenoidBoolean":true,"elevatorSolenoidTwo":2,"driveTrainPID_IValue":0.0,"climberTalonThree":11,"ingestorSolenoidCloseHeavy":0,"scaleDistance":500,"driveTrainSolenoidShifter":1,"bottomLimitSwitchOnElevator":2,"turnToAngle":50.0,"elevatorSolenoidOne":3,"xboxPositionManip":1,"driveTrainLeftFrontTalon":13,"climberTalonTwo":4,"elevatorRightTalon":9,"elevatorDownPower":-0.1,"ingestorSolenoidCloseLight":5} \ No newline at end of file diff --git a/config/configBackup.json b/config/configBackup.json new file mode 100644 index 0000000..39c5096 --- /dev/null +++ b/config/configBackup.json @@ -0,0 +1 @@ +{"driveTrainPID_PValue":0.025,"xboxPositionDriver":0,"ingestorRightTalon":6,"driveTrainLeftBackTalon":14,"driveTrainPID_DValue":0.06,"topLimitSwitchOnElevator":1,"ingestorCurrentConstant":4,"driveTrainRightBackTalon":2,"elevatorPID_IValue":0,"driveTrainRightFrontTalon":1,"elevatorPID_FValue":0,"ingestorLeftTalon":7,"switchDistance":400,"ingestorCurrentTimer":15,"climberTalonOne":5,"elevatorPID_PValue":1,"elevatorLeftTalon":8,"elevatorPID_DValue":0,"driveTrainSolenoidBoolean":true,"elevatorSolenoidTwo":2,"driveTrainPID_IValue":0.0,"climberTalonThree":11,"ingestorSolenoidCloseHeavy":0,"scaleDistance":500,"driveTrainSolenoidShifter":1,"bottomLimitSwitchOnElevator":2,"turnToAngle":50.0,"elevatorSolenoidOne":3,"xboxPositionManip":1,"driveTrainLeftFrontTalon":13,"climberTalonTwo":4,"elevatorRightTalon":9,"elevatorDownPower":-0.1,"ingestorSolenoidCloseLight":5} \ No newline at end of file diff --git a/config/configBackup2.json b/config/configBackup2.json new file mode 100644 index 0000000..39c5096 --- /dev/null +++ b/config/configBackup2.json @@ -0,0 +1 @@ +{"driveTrainPID_PValue":0.025,"xboxPositionDriver":0,"ingestorRightTalon":6,"driveTrainLeftBackTalon":14,"driveTrainPID_DValue":0.06,"topLimitSwitchOnElevator":1,"ingestorCurrentConstant":4,"driveTrainRightBackTalon":2,"elevatorPID_IValue":0,"driveTrainRightFrontTalon":1,"elevatorPID_FValue":0,"ingestorLeftTalon":7,"switchDistance":400,"ingestorCurrentTimer":15,"climberTalonOne":5,"elevatorPID_PValue":1,"elevatorLeftTalon":8,"elevatorPID_DValue":0,"driveTrainSolenoidBoolean":true,"elevatorSolenoidTwo":2,"driveTrainPID_IValue":0.0,"climberTalonThree":11,"ingestorSolenoidCloseHeavy":0,"scaleDistance":500,"driveTrainSolenoidShifter":1,"bottomLimitSwitchOnElevator":2,"turnToAngle":50.0,"elevatorSolenoidOne":3,"xboxPositionManip":1,"driveTrainLeftFrontTalon":13,"climberTalonTwo":4,"elevatorRightTalon":9,"elevatorDownPower":-0.1,"ingestorSolenoidCloseLight":5} \ No newline at end of file diff --git a/config/configTemp.json b/config/configTemp.json new file mode 100644 index 0000000..3484560 --- /dev/null +++ b/config/configTemp.json @@ -0,0 +1 @@ +{"driveTrainPID_PValue":0.025,"xboxPositionDriver":0,"ingestorRightTalon":6,"driveTrainLeftBackTalon":14,"driveTrainPID_DValue":0.06,"topLimitSwitchOnElevator":1,"ingestorCurrentConstant":4,"driveTrainRightBackTalon":2,"elevatorPID_IValue":0,"driveTrainRightFrontTalon":1,"elevatorPID_FValue":0,"ingestorLeftTalon":7,"switchDistance":400,"ingestorCurrentTimer":15,"climberTalonOne":5,"elevatorPID_PValue":1,"elevatorLeftTalon":8,"elevatorPID_DValue":0,"driveTrainSolenoidBoolean":true,"elevatorSolenoidTwo":2,"driveTrainPID_IValue":0.0,"climberTalonThree":11,"ingestorSolenoidCloseHeavy":0,"scaleDistance":500,"driveTrainSolenoidShifter":1,"bottomLimitSwitchOnElevator":2,"turnToAngle":50.0,"elevatorSolenoidOne":3,"xboxPositionManip":1,"driveTrainLeftFrontTalon":13,"climberTalonTwo":4,"elevatorRightTalon":9,"elevatorDownPower":-0.1,"ingestorSolenoidCloseLight":"5"} \ No newline at end of file From 971ff80a7faf61886f6e972d5ea2196968d4d2f9 Mon Sep 17 00:00:00 2001 From: Anurag Kompalli Date: Tue, 29 Jan 2019 11:03:08 -0500 Subject: [PATCH 2/2] Added Java Program to Edit config.json file --- src/main/java/frc/robot/ConfigEdit.java | 331 +++++++++++++++++++++ src/main/java/frc/robot/JSONConstants.java | 15 +- 2 files changed, 334 insertions(+), 12 deletions(-) create mode 100644 src/main/java/frc/robot/ConfigEdit.java diff --git a/src/main/java/frc/robot/ConfigEdit.java b/src/main/java/frc/robot/ConfigEdit.java new file mode 100644 index 0000000..0cab971 --- /dev/null +++ b/src/main/java/frc/robot/ConfigEdit.java @@ -0,0 +1,331 @@ +/** + * @author Jack Smith + * + * Methods for editing config.json file on-board. + * Opens base file (config.json), backs it up (configBackup.json) + * and (configBackup2.json), then writes over original + * with changes. Requires json file to already be parsed + * into a hashMap. + * + * + */ + + //Importing stuff +import java.io.FileNotFoundException; +import java.io.FileWriter; +import java.io.IOException; + +import org.json.simple.JSONObject; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +import java.util.HashMap; +import java.util.Map; +import java.util.Scanner; + + +public class ConfigEdit { + + //Constant file paths used in test + private static final String CONFIG_FILE = "config/config.json"; + + private static final String CONFIG_FILE_BACKUP = "config/configBackup.json"; + + private static final String CONFIG_FILE_BACKUP_BACKUP = "config/configBackup2.json"; + + //Scanner object used for user input + private static Scanner scan = new Scanner(System.in); + + //Stores preparsed hashMap and then stores changes + private static HashMap mapChanges = new HashMap(); + + //JSON Object used to transition hashMap for writing + private static JSONObject mapFinal = new JSONObject(); + + //File writer + private static FileWriter fileW; + + + /** + * "Main function", call for intended purposes. + */ + public static void test() { + + //declare variable used in if statement + int status2 = -1; + + //Start call timer + double copyFileTimer = System.nanoTime(); + + //Run the function + int status = copyFile(CONFIG_FILE, CONFIG_FILE_BACKUP, CONFIG_FILE_BACKUP_BACKUP); + + //End call timer + double copyFileTimerEND = System.nanoTime(); + + System.out.println("Copying files took: " + ((copyFileTimerEND - copyFileTimer) / 1000000) + " ms"); + + //Start call timer + double printMapTimer = System.nanoTime(); + + //Run the function + print(JSONConstants.getMap()); + + //End call timer + double printMapTimerEND = System.nanoTime(); + + System.out.println("Printing map took: " + ((printMapTimerEND - printMapTimer) / 1000000) + " ms"); + + //Start call timer + double putMapTimer = System.nanoTime(); + + //Run the function + mapChanges = putHashMap(JSONConstants.getMap()); + + //End call timer + double putMapTimerEND = System.nanoTime(); + + System.out.println("Editing map took: " + ((putMapTimerEND - putMapTimer) / 1000000) + " ms"); + + //Start call timer + double doSaveTimer = System.nanoTime(); + + //Run the function + boolean doSaveBool = doSave(); + + //End call timer + double doSaveTimerEND = System.nanoTime(); + + System.out.println("Asking for save took: " + ((doSaveTimerEND - doSaveTimer) / 1000000) + " ms"); + + if(doSaveBool) { + //Start call timer + double writeTimer = System.nanoTime(); + + //Run the function + status2 = writeToFile(CONFIG_FILE, mapChanges); + + //End call timer + double writeTimerEND = System.nanoTime(); + + System.out.println("Saving took: " + ((writeTimerEND - writeTimer) / 1000000) + " ms"); + } + + //What did we see? + System.out.println("Copying files returned: " + status); + System.out.println("Writing files returned: " + status2); + + scan.close(); + } + + /** + * Method puts user input to filePath for use. + * Only use when no specific filepath is given. + * + * @return filePath - returns the user input for a filepath + */ + public static String enterFile() { + + //Ask for fileName to return to base code + System.out.println("Enter the name and location of the file you would like to access."); + String filePath = scan.next(); + + return filePath; + } + + /** + * Copies backup file into second backup and then + * original file into backup file. + * + * @param FILEPATHORIGINAL - original file that is being copied. + * @param FILEPATHBACKUP - backup file to be copied to in case of mistake. + * @param FILEPATHBACKUP2 - extra backup precaution + * + * @return int - returns status of completion + */ + public static int copyFile(String FILEPATHORIGINAL, String FILEPATHBACKUP, String FILEPATHBACKUP2) { + try { + + //Copies existing file as entered file path. + Files.copy(Paths.get(FILEPATHBACKUP), Paths.get(FILEPATHBACKUP2), StandardCopyOption.REPLACE_EXISTING); + Files.copy(Paths.get(FILEPATHORIGINAL), Paths.get(FILEPATHBACKUP), StandardCopyOption.REPLACE_EXISTING); + + + + } catch(FileNotFoundException fnfe) { + + // System did not find the file in the specified path + // Check if file is defined correctly (spelling and path) + System.err.println("One or more file(s) were not found at their specified paths"); + fnfe.printStackTrace(); + + //FAIL + return -1; + } catch(IOException ioe) { + + // System was not able to perform I/O Operation(s) + // Check if the file is marked as read only or is protected in any way + // (encryption, etc.) + System.err.println("IOException Occured"); + ioe.printStackTrace(); + + //FAIL + return -1; + } + + //SUCCESS + return 0; + } + + /** + * Gives user prompts to change the values in a HashMap. + * + * @param map - Hashmap to be changed. + * + * @return HashMap - returns a changed hashMap. + */ + public static HashMap putHashMap(HashMap map) { + + //Loop that continues until user asks for termination. + boolean endLoop = false; + while(endLoop == false) { + + //Asks for input and then assigns to value + System.out.println("\nWould you like to change a key? [y/n]"); + String check = scan.nextLine(); + + if (check.equals("y")) { + //Asks user for inputs for key and value. + System.out.println("\nEnter the key of the value you would like to change."); + String key = scan.nextLine(); + + System.out.println("\nEnter the value that you would like the key to be changed to."); + String value = scan.nextLine(); + + Object valf; + + //Casts the inputted value to be the same type as the existing hashMap. + if(map.get(key) instanceof Integer) { + valf = Integer.parseInt(value); + + } else if(map.get(key) instanceof Double) { + valf = Double.parseDouble(value); + + } else if(map.get(key) instanceof Boolean) { + valf = Boolean.parseBoolean(value); + + } else if(map.get(key) instanceof Long) { + valf = Integer.parseInt(value); + + } else { + valf = value; + } + + + //Checks if key is valid. + if(map.containsKey(key)) { + + //Asks user if they are okay with changes. + System.out.println("Are you sure you want to change: "); + System.out.println("KEY: [" + key + "] = " + map.get(key) + " of TYPE " + map.get(key).getClass()); + System.out.println("To: "); + System.out.println("KEY: [" + key + "] = " + valf + " of TYPE " + valf.getClass()); + + check = scan.nextLine(); + if(check.equals("y")) { + //Saves if y + map.put(key, valf); + } + }else { + System.out.println("Key does not exist."); + } + + } else { + endLoop = true; + } + } + //Clean up + return map; + } + + + /** + * Saves a json file to specified file path + * + * @param FILEPATH - File to be saved over + * @param HashMap - Hashmap to save over the file with + * + * @return int - returns whether successful + */ + public static int writeToFile(String FILEPATH, HashMap mapOriginal) { + //Populates JSONObject used for writing file + for(Map.Entry entry : mapOriginal.entrySet()) { + mapFinal.put(entry.getKey(), entry.getValue()); + } + + try { + //Assigns FileWriter object + fileW = new FileWriter(FILEPATH); + + //Writes file and then flushes stream + fileW.write(mapFinal.toJSONString()); + fileW.flush(); + + } catch(FileNotFoundException fnfe) { + + // System did not find the file in the specified path + // Check if file is defined correctly (spelling and path) + System.err.println("The file " + FILEPATH + " was not found at its specified path"); + fnfe.printStackTrace(); + + //FAIL + return -1; + } catch(IOException ioe) { + + // System was not able to perform I/O Operation(s) + // Check if the file is marked as read only or is protected in any way + // (encryption, etc.) + System.err.println("An IOException Occured"); + ioe.printStackTrace(); + + //FAIL + return -1; + } + + //SUCCESS + return 0; + } + + /** + * Prints out a visual representation of a HashMap + * + * @param map - Hash map to be printed out + */ + public static void print(HashMap map) { + + //Prints out list of all keys and associated values + System.out.println("\n======KEYS AND CONSTANTS======"); + for (Map.Entry entry : map.entrySet()) { + System.out.println("KEY: [" + entry.getKey() + "] = " + entry.getValue() + " of TYPE " + map.get(entry.getKey()).getClass()); + } + } + + /** + * Asks user if they want to commit changes + * + * @return boolean isSaved - Boolean value of 'y' / 'n'. + */ + public static boolean doSave() { + //Asks user for input. + System.out.println("Would you like to commit changes? [y/n] "); + String isSaved = scan.nextLine(); + + //return true if y, else return false. + if(isSaved.equals("y")) { + return true; + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/frc/robot/JSONConstants.java b/src/main/java/frc/robot/JSONConstants.java index 1529da0..358e5f2 100644 --- a/src/main/java/frc/robot/JSONConstants.java +++ b/src/main/java/frc/robot/JSONConstants.java @@ -1,5 +1,3 @@ -package frc.robot; - import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; @@ -28,7 +26,7 @@ public class JSONConstants { private static HashMap configValues = new HashMap(); // defines the file path and name of the config file - private static String FILE_PATH; + private static final String FILE_PATH = "config/config.json"; /** * Tester Class: Tests whether one is able to read and update the @@ -62,13 +60,6 @@ public static void test() { System.out.println("Second populateMap() returned: " + status2); } - - public static String getFilePath(){ - return JSONConstants.FILE_PATH; - } - public static int getMapLength(){ - return configValues.size(); - } /** * The populateMap() method takes a JSON file and reads * the keys and values. @@ -174,7 +165,7 @@ public static Object get(String key) { return -1; } - public static void setFilePath(String filePath){ - FILE_PATH = filePath; + public static HashMap getMap(){ + return configValues; } }