Browse Source

pass currently executing cmd to webif

Martin Kunz 5 years ago
parent
commit
23a2e85ca2

+ 3 - 0
src/main/java/at/acdp/urweb/Params.java

@@ -12,5 +12,8 @@ public class Params {
 
     @picocli.CommandLine.Option(names = { "-rp", "--robotport" }, description = "Robot tcp port", defaultValue = "30001")
     public int robotPort = 30001;
+
+    @picocli.CommandLine.Option(names = { "-rt", "--rtport" }, description = "rtde tcp port", defaultValue = "30004")
+    public int rtPort;
 }
 

+ 18 - 10
src/main/java/at/acdp/urweb/URBot.java

@@ -7,6 +7,7 @@ import okhttp3.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.Optional;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.Semaphore;
@@ -21,6 +22,7 @@ public class URBot {
     private AtomicInteger nextID = new AtomicInteger(1);
     private Semaphore cmdDoneSem = new Semaphore(0);
     private RobotProgramLabel currentAck;
+    private Optional<RobotCommand> runningScript = Optional.empty();
 
     public URBot(String ip, int port) {
         sc = new ScReadThread(ip, port, this);
@@ -37,14 +39,16 @@ public class URBot {
         Thread queueThread = new Thread(() -> {
             try {
                 while (true) {
+                    runningScript = Optional.of(cmdq.take());
+                    var rs = runningScript.get();
+                    sc.writeCmd(rs);
                     cmdDoneSem.acquire();
-                    var c = cmdq.take();
-                    sc.writeCmd(c);
                     while (true) {
-                        if (currentAck.id == c.id && currentAck.message.equals("URWEB_END")) {
-                            if (c.cpeeCallback != null) {
-                                ackCPEE(c);
+                        if (currentAck.id == rs.id && currentAck.message.equals("URWEB_END")) {
+                            if (rs.cpeeCallback != null) {
+                                ackCPEE(rs);
                             }
+                            runningScript = Optional.empty();
                             break;
                         }
                     }
@@ -71,14 +75,14 @@ public class URBot {
         }
         if (!res.endsWith("\n"))
             res += "\n";
-        res=res.trim();
+        res = res.trim();
         int id = nextID.getAndIncrement();
-        if(res.endsWith("end")) {
-            res=res.substring(0, res.length()-3);
-            res+=String.format("$ %s \"URWEB_END\"\nend\n", id);
+        if (res.endsWith("end")) {
+            res = res.substring(0, res.length() - 3);
+            res += String.format("$ %s \"URWEB_END\"\nend\n", id);
         }
         try {
-            rc.id=id;
+            rc.id = id;
             // rc.cmd = String.format("def urweb():\n%s\n$ %s \"URWEB_END\"\nend\n", res, id);
             rc.cmd = res;
             this.cmdq.put(rc);
@@ -134,4 +138,8 @@ public class URBot {
     public BlockingQueue<RobotCommand> getCmdq() {
         return cmdq;
     }
+
+    public Optional<RobotCommand> getRunningScript() {
+        return runningScript;
+    }
 }

+ 6 - 1
src/main/java/at/acdp/urweb/web/GetRobotRealtimeData.java

@@ -1,4 +1,4 @@
-package at.acdp.urweb.web;
+package at.acdp.urweb.rt;
 
 import java.io.DataInputStream;
 import java.io.IOException;
@@ -25,6 +25,11 @@ public class GetRobotRealtimeData implements Runnable {
 
     }
 
+    public void start() {
+        Thread readThread = new Thread(this);
+        readThread.start();
+    }
+
     // Internal method that actually reads the data
     private void readSocket() throws IOException {
         try(Socket rt = new Socket(ip, port);){

+ 1 - 1
src/main/java/at/acdp/urweb/rtdeclient/RTDEClient.java

@@ -1,4 +1,4 @@
-package at.acdp.urweb.rtdeclient;
+package at.acdp.urweb.rt;
 
 public class RTDEClient {
     private final String ip;

+ 11 - 0
src/main/java/at/acdp/urweb/web/WebServer.java

@@ -3,6 +3,7 @@ package at.acdp.urweb.web;
 import at.acdp.urweb.Params;
 import at.acdp.urweb.RobotCommand;
 import at.acdp.urweb.URBot;
+import at.acdp.urweb.rt.GetRobotRealtimeData;
 import at.acdp.urweb.sclient.URLog;
 import com.eclipsesource.json.JsonArray;
 import org.slf4j.LoggerFactory;
@@ -13,6 +14,7 @@ public class WebServer {
     private final static org.slf4j.Logger logger = LoggerFactory.getLogger(WebServer.class);
     private URBot urbot;
     private final Params params;
+    private GetRobotRealtimeData rtbot;
 
     public WebServer(Params params) {
         this.params = params;
@@ -21,6 +23,8 @@ public class WebServer {
     public void start() {
         this.urbot=new URBot(params.robotIP, params.robotPort);
         this.urbot.start();
+        // this.rtbot=new GetRobotRealtimeData(params.robotIP, params.rtPort);
+        // this.rtbot.start();
         port(params.port);
         if (!params.webroot.isEmpty())
             staticFileLocation(params.webroot);
@@ -69,5 +73,12 @@ public class WebServer {
             }
             return jsa.toString();
         });
+        get("/running",  (req, res) -> {
+            var rc=urbot.getRunningScript();
+            if(rc.isPresent()) {
+                return rc.get().toJSON();
+            }
+            return "[{}]";
+        });
     }
 }

+ 16 - 2
src/main/resources/webroot/index.html

@@ -24,6 +24,7 @@
 <div id="app">
     <textarea v-model="input" cols="80" rows="20"></textarea>
     <button v-on:click="send">send</button>
+    Running: {{ $data.running }}
     <table border="1">
         <tr v-for="x in cmdq">
             <td>{{x.id}}</td>
@@ -70,8 +71,9 @@
             cmdq: []
         },
         created: function () {
-            setInterval(this.update, 200);
-        setInterval(this.updateQ, 250);
+        setInterval(this.update, 210);
+        setInterval(this.updateQ, 220);
+        setInterval(this.updateCurrent, 230);
 
         },
         watch: {
@@ -145,6 +147,18 @@
                         console.log('unknown package:' + package.type);
                 }
             },
+            updateCurrent: function (event) {
+                fetch('/running', {method: "GET"})
+                    .then(handleErrors)
+                    .then((response) => {
+                        return response.json();
+                    })
+                    .then((myJson) => {
+                        this.$data.running = myJson;
+                    })
+                    .catch(error => {
+                        console.log(error)
+                    })},
             updateQ: function (event) {
                 fetch('/cmdq', {method: "GET"})
                     .then(handleErrors)