mirror of
https://github.com/Team4388/2022NoWayHome.git
synced 2026-06-09 00:38:05 -06:00
Added arrays + fix and docs
Also tested on localhost
This commit is contained in:
@@ -11,21 +11,52 @@ import java.util.HashMap;
|
||||
|
||||
import org.opencv.core.Point;
|
||||
|
||||
/**
|
||||
* A http server that allows the robot to communicate with Desmos Graphing Calculator
|
||||
*
|
||||
* @author Daniel McGrath
|
||||
* */
|
||||
public class DesmosServer extends Thread {
|
||||
private static HashMap<String, String> desmosVariables = new HashMap<>();
|
||||
private static HashMap<String, String> readVariables = new HashMap<>();
|
||||
|
||||
private static boolean running = false;
|
||||
|
||||
private int activePort;
|
||||
|
||||
/**
|
||||
* Creates DesmosServer running on port
|
||||
* <p>
|
||||
* 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 = 5500;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
runServer(5500);
|
||||
runServer(activePort);
|
||||
} catch(Exception err) {
|
||||
err.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs server on port
|
||||
*
|
||||
* @param port The port the server runs on
|
||||
* @throws IOException
|
||||
* */
|
||||
public void runServer(int port) throws IOException {
|
||||
ServerSocket serverSocket = new ServerSocket(port);
|
||||
running = true;
|
||||
@@ -36,6 +67,12 @@ public class DesmosServer extends Thread {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles client requests
|
||||
*
|
||||
* @param The client connection
|
||||
* @throws IOException
|
||||
* */
|
||||
public void handleClient(Socket client) throws IOException {
|
||||
InputStreamReader clientStream = new InputStreamReader(client.getInputStream());
|
||||
BufferedReader bufferedReader = new BufferedReader(clientStream);
|
||||
@@ -50,6 +87,12 @@ public class DesmosServer extends Thread {
|
||||
sendResponse(client);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends JSON response
|
||||
*
|
||||
* @param The client connection
|
||||
* @throws IOException
|
||||
* */
|
||||
public void sendResponse(Socket client) throws IOException {
|
||||
OutputStream clientOutput = client.getOutputStream();
|
||||
|
||||
@@ -58,17 +101,25 @@ public class DesmosServer extends Thread {
|
||||
clientOutput.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* Produces JSON output containing Desmos output.
|
||||
*
|
||||
* @return JSON string to be read by Desmos client
|
||||
* */
|
||||
public static String getJSONOutput() {
|
||||
String json = "[";
|
||||
|
||||
for(String key : desmosVariables.keySet()) {
|
||||
json += "{"
|
||||
+ "\"name\":" + key
|
||||
+ "\"value\":" + desmosVariables.get(key)
|
||||
+ "},";
|
||||
if(!desmosVariables.isEmpty()) {
|
||||
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 = json.substring(0, json.length()-1); // remove comma at the end
|
||||
|
||||
json += "]";
|
||||
|
||||
return json;
|
||||
@@ -77,6 +128,8 @@ public class DesmosServer extends Thread {
|
||||
public static boolean isRunning() {
|
||||
return running;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public static void putInteger(String name, Integer value) {
|
||||
desmosVariables.put(name, value.toString());
|
||||
@@ -89,6 +142,16 @@ public class DesmosServer extends Thread {
|
||||
public static void putPoint(String name, Point point) {
|
||||
desmosVariables.put(name, "(" + point.x + "," + point.y + ")");
|
||||
}
|
||||
|
||||
public static void putIntegerArray(String name, int... arr) {
|
||||
desmosVariables.put(name, Arrays.toString(arr).replace(" ", ""));
|
||||
}
|
||||
|
||||
public static void putDoubleArray(String name, double... arr) {
|
||||
desmosVariables.put(name, Arrays.toString(arr).replace(" ", ""));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------
|
||||
|
||||
public static int readInteger(String name) {
|
||||
return Integer.parseInt(readVariables.get(name));
|
||||
|
||||
Reference in New Issue
Block a user