diff --git a/build.gradle b/build.gradle index f9c27a2..7d176b0 100644 --- a/build.gradle +++ b/build.gradle @@ -42,9 +42,14 @@ dependencies { // Use JUnit test framework testImplementation 'junit:junit:4.12' - - // https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple - compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1.1' + + // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind + compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.0.pr1' + // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core + compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.10.0.pr1' + // https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations + compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.10.0.pr1' + //Phoenix dependencies. compile group: 'com.ctre.phoenix', name: 'api-java', version: '5.14.1' diff --git a/src/main/java/org/usfirst/lib6647/oi/ControllerProfiles.java b/src/main/java/org/usfirst/lib6647/oi/ControllerProfiles.java new file mode 100644 index 0000000..80bcca5 --- /dev/null +++ b/src/main/java/org/usfirst/lib6647/oi/ControllerProfiles.java @@ -0,0 +1,64 @@ +package org.usfirst.lib6647.oi; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.usfirst.lib6647.subsystem.RobotMap; + +/** + * Class holding instances of objects required to read values from a JSON file, + * for {@link JController} usage. + */ +public class ControllerProfiles { + + private static ControllerProfiles m_instance = null; + + /** + * Creates static {@link ControllerProfiles} instance. + */ + public static void createInstance(String filePath) { + m_instance = new ControllerProfiles(filePath); + } + + /** + * Gets static {@link ControllerProfiles} instance. + * + * @return static {@link ControllerProfiles} instance + */ + public static ControllerProfiles getInstance() { + return m_instance; + } + + /** JSON {@link ObjectMapper} for {@link JController JControllers}. */ + private final ObjectMapper mapper; + /** Path to JSON file. */ + private final String filePath; + + /** + * Constructor for {@link RobotMap}. Initializes {@link #filePath} and + * {@link #mapper}. + * + * @param filePath + */ + public ControllerProfiles(String filePath) { + this.filePath = filePath; + mapper = new ObjectMapper(); + } + + /** + * Get {@link ObjectMapper} instance. + * + * @return mapper + */ + public ObjectMapper getMapper() { + return mapper; + } + + /** + * Get {@link #filePath} for JSON file. + * + * @return filePath + */ + public String getFilePath() { + return filePath; + } +} \ No newline at end of file diff --git a/src/main/java/org/usfirst/lib6647/oi/JController.java b/src/main/java/org/usfirst/lib6647/oi/JController.java index 6a8b65a..d5766cf 100644 --- a/src/main/java/org/usfirst/lib6647/oi/JController.java +++ b/src/main/java/org/usfirst/lib6647/oi/JController.java @@ -1,14 +1,10 @@ package org.usfirst.lib6647.oi; import java.io.FileReader; -import java.io.IOException; import java.io.Reader; import java.util.HashMap; -import java.util.Optional; -import org.json.simple.JSONObject; -import org.json.simple.parser.JSONParser; -import org.json.simple.parser.ParseException; +import com.fasterxml.jackson.databind.JsonNode; import edu.wpi.first.wpilibj.GenericHID; import edu.wpi.first.wpilibj.Joystick; @@ -32,23 +28,28 @@ public class JController extends Joystick { private int leftAxis = 1, rightAxis = 5; /** - * Location of the JSON file for {@link Button} nicknames. + * {@link JSONNode} for usage of friendly button names with JSON. */ - private String filePath; + private JsonNode profile; /** * Constructor for {@link JController}. * * Initializes each and every {@link Button} from the {@link Joystick} found at * the given port. Also initializes {@link Button Buttons} for each of the axes - * and POVs. + * and POVs. Also initializes {@link #profile} {@link JsonNode} if possible. * * @param port */ - public JController(int port, String filePath) { + public JController(int port) { super(port); - this.filePath = filePath; + try (Reader file = new FileReader(ControllerProfiles.getInstance().getFilePath())) { + profile = ControllerProfiles.getInstance().getMapper().readTree(file).get(getName()); + } catch (Exception e) { + System.out.println( + "[!] COULD NOT INITIALIZE CONTROLLER PROFILE FOR CONTROLLER '" + getName().toUpperCase() + "'."); + } // Button initialization. Starting at 1. for (int i = 1; i <= this.getButtonCount(); i++) { @@ -102,40 +103,13 @@ public double getRightAxis() { /** * Method for getting a {@link Button} with a friendly name (declared in the - * JSON configuration) from this {@link JController}. Returns an empty Optional - * if no {@link Button} is found at that specific key, or if any exception is - * thrown. + * JSON configuration) from this {@link JController}. * - * @param joystickName * @param buttonName * @return {@link Button} */ - public Optional