From 8bfff1ca41c7e4cecbe0805a897f6398c0fa092c Mon Sep 17 00:00:00 2001 From: pacoito123 Date: Sat, 17 Aug 2019 21:01:49 -0500 Subject: [PATCH] Moved from json-simple to jackson. [General] - Removed json-simple dependency. - Added jackson-core, databind, and annotations dependencies. [OI] - Created ControllerProfiles mapper class. - Removed Optional from Button getting (for now) - Uses jackson JSON Nodes now instead of json-simple parsing. [HyperComponents] - HyperTalons and HyperVictors now override set() method. [SuperSubsystems] - Now use jackson JSON Nodes instead of json-simple parsing. - No longer a need to call finishedJSONInit() at the very end. - Completely re-did how SuperComponents are initialized. --- build.gradle | 11 +- .../lib6647/oi/ControllerProfiles.java | 64 +++++ .../org/usfirst/lib6647/oi/JController.java | 54 +--- .../lib6647/subsystem/PIDSuperSubsystem.java | 72 ++---- .../usfirst/lib6647/subsystem/RobotMap.java | 62 +++++ .../lib6647/subsystem/SuperSubsystem.java | 33 +-- .../subsystem/hypercomponents/HyperTalon.java | 16 +- .../hypercomponents/HyperVictor.java | 16 +- .../supercomponents/SuperCompressor.java | 55 ++-- .../supercomponents/SuperDigitalInput.java | 48 ++-- .../supercomponents/SuperDoubleSolenoid.java | 56 ++-- .../supercomponents/SuperEncoder.java | 55 ++-- .../subsystem/supercomponents/SuperPDP.java | 54 ++-- .../supercomponents/SuperSolenoid.java | 71 ++---- .../subsystem/supercomponents/SuperTalon.java | 240 ++++++++---------- .../supercomponents/SuperUltrasonic.java | 58 ++--- .../supercomponents/SuperVictor.java | 161 ++++++------ .../org/usfirst/lib6647/util/MotorUtils.java | 2 - 18 files changed, 527 insertions(+), 601 deletions(-) create mode 100644 src/main/java/org/usfirst/lib6647/oi/ControllerProfiles.java create mode 100644 src/main/java/org/usfirst/lib6647/subsystem/RobotMap.java 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..ed16bac 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