diff --git a/src/main/java/frc4388/utility/DesmosServer.java b/src/main/java/frc4388/utility/DesmosServer.java new file mode 100644 index 0000000..bc82bca --- /dev/null +++ b/src/main/java/frc4388/utility/DesmosServer.java @@ -0,0 +1,110 @@ +package frc4388.utility; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.ArrayList; +import java.util.HashMap; + +import org.opencv.core.Point; + +public class DesmosServer extends Thread { + private static HashMap desmosVariables = new HashMap<>(); + private static HashMap readVariables = new HashMap<>(); + + private static boolean running = false; + + @Override + public void run() { + try { + runServer(5500); + } catch(Exception err) { + err.printStackTrace(); + } + } + + public void runServer(int port) throws IOException { + ServerSocket serverSocket = new ServerSocket(port); + running = true; + + while(true) { + Socket client = serverSocket.accept(); + handleClient(client); + } + } + + public void handleClient(Socket client) throws IOException { + InputStreamReader clientStream = new InputStreamReader(client.getInputStream()); + BufferedReader bufferedReader = new BufferedReader(clientStream); + + ArrayList requestLines = new ArrayList<>(); + + while(bufferedReader.ready()) { + String read = bufferedReader.readLine(); + requestLines.add(read + "\r\n"); + } + + sendResponse(client); + } + + public void sendResponse(Socket client) throws IOException { + OutputStream clientOutput = client.getOutputStream(); + + clientOutput.write(getJSONOutput().getBytes()); + clientOutput.flush(); + clientOutput.close(); + } + + public static String getJSONOutput() { + String json = "["; + + for(String key : desmosVariables.keySet()) { + json += "{" + + "\"name\":" + key + + "\"value\":" + desmosVariables.get(key) + + "},"; + } + + json = json.substring(0, json.length()-1); // remove comma at the end + json += "]"; + + return json; + } + + public static boolean isRunning() { + return running; + } + + public static void putInteger(String name, Integer value) { + desmosVariables.put(name, value.toString()); + } + + public static void putDecimal(String name, Double value) { + desmosVariables.put(name, value.toString()); + } + + public static void putPoint(String name, Point point) { + desmosVariables.put(name, "(" + point.x + "," + point.y + ")"); + } + + public static int readInteger(String name) { + return Integer.parseInt(readVariables.get(name)); + } + + public static double readDouble(String name) { + return Double.parseDouble(readVariables.get(name)); + } + + public static Point readPoint(String name) { + Point point = new Point(); + + String pointStr = readVariables.get(name); + point.x = Double.parseDouble(pointStr.split(",")[0]); + point.x = Double.parseDouble(pointStr.split(",")[1]); + + return point; + } +}