Browse Source

move public api to urbot.java

Martin Kunz 5 years ago
parent
commit
18ad737fbd

+ 2 - 4
src/main/java/at/acdp/urweb/Main.java

@@ -8,10 +8,8 @@ import java.io.IOException;
 public class Main {
 
     public static void main(String [ ] args) {
-        ScReadThread rt=new ScReadThread("127.0.0.1");
-        new WebServer(8080,true, rt).start();
-        Thread t=new Thread(rt);
-        t.start();
+        URBot urbot=new URBot("127.0.0.1");
+        new WebServer(8080,true, urbot).start();
 
         // rde.writeCmd("set_digital_out(2,True)\n");
         // rde.writeCmd("movej([-1.95,-1.58,-1.16,-1.15,-1.55,1.25], a=1.0, v=0.1)\n");

+ 45 - 0
src/main/java/at/acdp/urweb/URBot.java

@@ -0,0 +1,45 @@
+package at.acdp.urweb;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class URBot {
+    private ScReadThread sc;
+    private static final Logger log = LoggerFactory.getLogger(URBot.class);
+
+    public URBot(String ip) {
+        sc=new ScReadThread(ip);
+        Thread t=new Thread(sc);
+        t.start();
+    }
+
+    public void sendProgram(String program) {
+        String[] lines=program.split("[\\r\\n]+");
+        String res="";
+        for(String line:lines) {
+            line=line.trim();
+            if(line.startsWith("//"))
+                continue;
+            if(line.startsWith("#"))
+                continue;
+            if(!line.endsWith("\n"))
+                line+="\n";
+
+            res+=line;
+        }
+        if(!program.endsWith("\n"))
+            program+="\n";
+        sc.writeCmd(program);
+    }
+
+    public void sendFreedrive(int timeout) {
+        sc.writeCmd(String.format("def myProg():\n\tfreedrive_mode()\n\tsleep(%d)\nend", timeout));
+    }
+
+    public void sendMessage(String message) {
+        sc.writeCmd(String.format("textmsg(%s)", message));
+    }
+
+
+
+}

+ 21 - 24
src/main/java/at/acdp/urweb/web/WebServer.java

@@ -2,12 +2,13 @@ package at.acdp.urweb.web;
 
 import java.nio.file.Paths;
 
-import at.acdp.urweb.ScReadThread;
+import at.acdp.urweb.URBot;
 import at.acdp.urweb.sclient.URLog;
 import io.undertow.Handlers;
 import io.undertow.Undertow;
 import io.undertow.server.HttpHandler;
 import io.undertow.server.HttpServerExchange;
+import io.undertow.server.handlers.HttpTraceHandler;
 import io.undertow.server.handlers.resource.PathResourceManager;
 import org.slf4j.LoggerFactory;
 
@@ -16,11 +17,11 @@ import static io.undertow.Handlers.resource;
 public class WebServer {
     private final static org.slf4j.Logger logger = LoggerFactory.getLogger(WebServer.class);
     private final int port;
-    private final ScReadThread sc;
+    private final URBot urbot;
     private Undertow server;
 
-    public WebServer(int port, boolean debug, ScReadThread sc) {
-        this.sc = sc;
+    public WebServer(int port, boolean debug, URBot urbot) {
+        this.urbot = urbot;
         this.port = port;
     }
 
@@ -31,29 +32,18 @@ public class WebServer {
                 .post("/cmd", new HttpHandler() {
                     @Override
                     public void handleRequest(HttpServerExchange ex) throws Exception {
-                        if (ex.isInIoThread()) {
-                            ex.dispatch(this);
-                            return;
-                        }
+                        dispatch(ex, this);
                         ex.startBlocking();
                         byte[] bytes = ex.getInputStream().readAllBytes();
                         String cmd=new String(bytes);
-                        String[] lines=cmd.split("[\\r\\n]+");
-                        String res="";
-                        for(String line:lines) {
-                            line=line.trim();
-                            if(line.startsWith("//"))
-                                continue;
-                            if(line.startsWith("#"))
-                                continue;
-                            if(!line.endsWith("\n"))
-                                line+="\n";
-
-                            res+=line;
-                        }
-                        if(!cmd.endsWith("\n"))
-                            cmd+="\n";
-                        sc.writeCmd(cmd);
+                        urbot.sendProgram(cmd);
+                    }
+                })
+                .post("/freedrive", new HttpHandler() {
+                    @Override
+                    public void handleRequest(HttpServerExchange ex) throws Exception {
+                        dispatch(ex, this);
+                        urbot.sendFreedrive(1);
                     }
                 })
                 .get("/log/{from}",  ex -> {
@@ -72,4 +62,11 @@ public class WebServer {
         server = builder.build();
         server.start();
     }
+
+    private void dispatch(HttpServerExchange ex, HttpHandler handler) {
+        if (ex.isInIoThread()) {
+            ex.dispatch(handler);
+            return;
+        }
+    }
 }