From 8e13b5c69f4d069042f56232dd88b0b1123de316 Mon Sep 17 00:00:00 2001 From: 66945 <54561572+66945@users.noreply.github.com> Date: Sun, 13 Mar 2022 17:11:59 -0600 Subject: [PATCH] Added desmos server --- src/main/java/frc4388/robot/Robot.java | 5 + .../java/frc4388/utility/DesmosClient.html | 222 +++++++++++++ .../java/frc4388/utility/DesmosServer.java | 314 ++++++++++++++++++ 3 files changed, 541 insertions(+) create mode 100644 src/main/java/frc4388/utility/DesmosClient.html create mode 100644 src/main/java/frc4388/utility/DesmosServer.java diff --git a/src/main/java/frc4388/robot/Robot.java b/src/main/java/frc4388/robot/Robot.java index d04fae9..ddee6d5 100644 --- a/src/main/java/frc4388/robot/Robot.java +++ b/src/main/java/frc4388/robot/Robot.java @@ -24,6 +24,7 @@ import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard; import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.CommandScheduler; import edu.wpi.first.wpilibj2.command.RunCommand; +import frc4388.utility.DesmosServer; import frc4388.utility.RobotTime; import frc4388.utility.VelocityCorrection; @@ -46,6 +47,8 @@ public class Robot extends TimedRobot { private Pose2d selectedOdo; private double current; + private static DesmosServer desmosServer = new DesmosServer(8000); + /** * This function is run when the robot is first started up and should be * used for any initialization code. @@ -116,6 +119,8 @@ public class Robot extends TimedRobot { return "Not Running"; } }); + + desmosServer.start(); } /** diff --git a/src/main/java/frc4388/utility/DesmosClient.html b/src/main/java/frc4388/utility/DesmosClient.html new file mode 100644 index 0000000..7eaef15 --- /dev/null +++ b/src/main/java/frc4388/utility/DesmosClient.html @@ -0,0 +1,222 @@ + + +
+ + +
+ * Use this for cases when the robot is using the default port
+ *
+ * @param port The port the server will run on
+ * */
+ public DesmosServer(int port) {
+ activePort = port;
+ }
+
+ /**
+ * Creates DesmosServer running on port 5500
+ * */
+ public DesmosServer() {
+ activePort = 8000;
+ }
+
+ @Override
+ public void run() {
+ try {
+ runServer(activePort);
+ } catch(IOException err) {
+ err.printStackTrace();
+ }
+ }
+
+ /**
+ * Runs server on port
+ *
+ * @param port The port the server runs on
+ * @throws IOException
+ * */
+ public void runServer(int port) throws IOException {
+ System.out.println("Initializing DesmosServer on port " + port + "...");
+
+ serverSocket = new ServerSocket(port);
+ running = true;
+
+ System.out.println("DesmosServer is active!");
+
+ while(true) {
+ Socket client = serverSocket.accept();
+ handleClient(client);
+ }
+ }
+
+ /**
+ * Handles client requests
+ *
+ * @param client The client connection
+ * @throws IOException
+ * */
+ public void handleClient(Socket client) throws IOException {
+ InputStreamReader clientStream = new InputStreamReader(client.getInputStream());
+ BufferedReader bufferedReader = new BufferedReader(clientStream);
+
+ ArrayList
+ * [
+ * {"int": "24"},
+ * {"double": "2.4"},
+ * {"point": "(2,4)"},
+ * {"list": "[2,4]"}
+ * ]
+ *
+ * @param client The client connection
+ * @throws IOException
+ * */
+ public void sendResponse(Socket client) throws IOException {
+ OutputStream clientOutput = client.getOutputStream();
+
+ // Write Headers
+ clientOutput.write("HTTP/1.1 200 OK\r\n".getBytes());
+ clientOutput.write("Access-Control-Allow-Origin: *\r\n".getBytes());
+ clientOutput.write("Keep-Alive: timeout=2, max=100\r\n".getBytes());
+ clientOutput.write("Connection: Keep-Alive\r\n".getBytes());
+ clientOutput.write("Content-Type: application/json\r\n\r\n".getBytes());
+
+ clientOutput.write(getJSONOutput().getBytes());
+ clientOutput.flush();
+ clientOutput.close();
+ }
+
+ /**
+ * Produces JSON output containing Desmos output.
+ *
+ * @return JSON string to be read by Desmos client
+ * */
+ public static String getJSONOutput() {
+ String json = "[";
+
+ if(!desmosQueue.isEmpty()) {
+ Set if variable is a double it will be cast to int
+ * */
+ public static int readInteger(String name) {
+ if(!readVariables.containsKey(name) || !readVariables.get(name)[0].equals("number"))
+ return 0;
+
+ return (int) Double.parseDouble(readVariables.get(name)[1]);
+ }
+
+ /**
+ * Reads desmos double
+ *
+ * @param name Desmos variable name
+ * @return Numeric value, if variable is an expression it will be evaluated
+ * */
+ public static double readDouble(String name) {
+ if(!readVariables.containsKey(name) || !readVariables.get(name)[0].equals("number"))
+ return 0;
+
+ return Double.parseDouble(readVariables.get(name)[1]);
+ }
+
+ /**
+ * Reads desmos point
+ *
+ * @param name Desmos variable name
+ * @return Point, if variable contains expressions they will be evaluated
+ * */
+ public static Point readPoint(String name) {
+ Point point = new Point();
+
+ if(!readVariables.containsKey(name) || !readVariables.get(name)[0].equals("point"))
+ return point;
+
+ String pointStr = readVariables.get(name)[1];
+ point.x = Double.parseDouble(pointStr.split(",")[0]);
+ point.y = Double.parseDouble(pointStr.split(",")[1]);
+
+ return point;
+ }
+
+ /**
+ * Reads desmos array, including table columns
+ *
+ * @param name Desmos variable name
+ * @returns Array of numeric values, if array contains expressions they will be evaluated
+ * */
+ public static double[] readArray(String name) {
+ if(!readVariables.containsKey(name) || !readVariables.get(name)[0].equals("array"))
+ return new double[0];
+
+ String[] unparsed = readVariables.get(name)[1].split(",");
+ double[] arr = new double[unparsed.length];
+
+ for(int i = 0; i < arr.length; i++)
+ arr[i] = Integer.parseInt(unparsed[i]);
+
+ return arr;
+ }
+}