Browse Source

fix commandq
modify scripts for end detection

Martin Kunz 5 years ago
parent
commit
673e01e057

+ 27 - 1
src/main/java/at/acdp/urweb/RobotCommand.java

@@ -1,6 +1,9 @@
 package at.acdp.urweb;
 
-public class RobotCommand {
+import at.acdp.urweb.sclient.data.IJsonObject;
+import com.eclipsesource.json.JsonObject;
+
+public class RobotCommand implements IJsonObject {
     public String cmd;
     public int id;
     public String cpeeCallback;
@@ -8,6 +11,25 @@ public class RobotCommand {
     public int cpeeCallbackCode;
     public String cpeeCallbackMessage;
     public String cpeeCallbackError;
+    public String cpeeCallbackId;
+    public String cpeeInstanceURL;
+    public boolean doCpeeCallback;
+
+    @Override
+    public JsonObject toJSON() {
+        return new JsonObject()
+                .add("cmd", cmd)
+                .add("id", id)
+                .add("cpeeCallback", cpeeCallback)
+                .add("cpeeCallbackResult", cpeeCallbackResult)
+                .add("cpeeCallbackCode", cpeeCallbackCode)
+                .add("cpeeCallbackMessage", cpeeCallbackMessage)
+                .add("cpeeCallbackError", cpeeCallbackError)
+                .add("cpeeCallbackId", cpeeCallbackId)
+                .add("cpeeInstanceURL", cpeeInstanceURL)
+                .add("doCpeeCallback", doCpeeCallback)
+                ;
+    }
 
     public RobotCommand(int id, String cmd) {
         this.cmd = cmd;
@@ -19,4 +41,8 @@ public class RobotCommand {
         this.id = id;
         this.cpeeCallback = cpeeCallback;
     }
+
+    public RobotCommand(String cmd) {
+        this.cmd=cmd;
+    }
 }

+ 29 - 13
src/main/java/at/acdp/urweb/URBot.java

@@ -7,6 +7,7 @@ import okhttp3.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.awt.*;
 import java.util.concurrent.BlockingQueue;
 import java.util.concurrent.LinkedBlockingQueue;
 import java.util.concurrent.Semaphore;
@@ -38,25 +39,26 @@ public class URBot {
             try {
                 while (true) {
                     var c = cmdq.take();
-                    write(c.cmd);
+                    sc.writeCmd(c);
                     while (true) {
                         cmdDoneSem.acquire();
-                        if (currentAck.id == c.id  && currentAck.message.equals("URWEB_END"))
+                        if (currentAck.id == c.id && currentAck.message.equals("URWEB_END")) {
+                            if (c.cpeeCallback != null) {
+                                ackCPEE(c);
+                            }
                             break;
-                    }
-                    if (c.cpeeCallback != null) {
-                        ackCPEE(c);
+                        }
                     }
                 }
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         });
+        queueThread.start();
     }
 
-
-    private void write(String program) {
-        String[] lines = program.split("[\\r\\n]+");
+    private void write(RobotCommand rc) {
+        String[] lines = rc.cmd.split("[\\r\\n]+");
         String res = "";
         for (String line : lines) {
             line = line.trim();
@@ -68,10 +70,20 @@ public class URBot {
                 line += "\n";
             res += line;
         }
-        if (!program.endsWith("\n"))
-            program += "\n";
+        if (!res.endsWith("\n"))
+            res += "\n";
+        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);
+        }
         try {
-            this.cmdq.put(new RobotCommand(nextID.getAndIncrement(), program));
+            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);
         } catch (InterruptedException e) {
             Thread.currentThread().interrupt();
         }
@@ -117,7 +129,11 @@ public class URBot {
         return ((mb.digitalOutputBits & (1 << which)) > 0);
     }
 
-    public void sendCmd(String cmd) {
-        write(String.format("def urweb():\n{}\nend\n", cmd));
+    public void sendCmd(RobotCommand cmd) {
+        write(cmd);
+    }
+
+    public BlockingQueue<RobotCommand> getCmdq() {
+        return cmdq;
     }
 }

+ 1 - 3
src/main/java/at/acdp/urweb/sclient/SecondaryClient.java

@@ -288,11 +288,9 @@ public class SecondaryClient {
 
     public void writeCmd(String cmd) {
         try {
-            System.out.println("send cmd:" + cmd);
             os.write(cmd.getBytes(StandardCharsets.UTF_8));
-
         } catch (IOException e) {
-            e.printStackTrace();
+            log.warn("writecmd", e);
         }
     }
 

+ 2 - 2
src/main/java/at/acdp/urweb/sclient/data/ScReadThread.java

@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;
 import java.io.IOException;
 
 public class ScReadThread implements Runnable {
-    private static final Logger log = LoggerFactory.getLogger(SecondaryClient.class);
+    private static final Logger log = LoggerFactory.getLogger(ScReadThread.class);
     private final String ip;
     private final URBot urBot;
     SecondaryClient rde;
@@ -54,7 +54,7 @@ public class ScReadThread implements Runnable {
     private void writeCmd(String s) {
         if (!s.endsWith("\n"))
             s += "\n";
-        log.info("writecmd: \"" + s + "\"");
+        log.debug("writecmd: \"" + s + "\"");
         rde.writeCmd(s);
     }
 }

+ 19 - 3
src/main/java/at/acdp/urweb/web/WebServer.java

@@ -1,8 +1,10 @@
 package at.acdp.urweb.web;
 
 import at.acdp.urweb.Params;
+import at.acdp.urweb.RobotCommand;
 import at.acdp.urweb.URBot;
 import at.acdp.urweb.sclient.URLog;
+import com.eclipsesource.json.JsonArray;
 import org.slf4j.LoggerFactory;
 
 import static spark.Spark.*;
@@ -26,9 +28,16 @@ public class WebServer {
             staticFiles.location("/webroot");
 
         post("/cmd", (req, res) -> {
-            byte[] bytes = req.raw().getInputStream().readAllBytes();
-            String cmd=new String(bytes);
-            urbot.sendCmd(cmd);
+            var cmd=req.queryParams("script");
+            RobotCommand rc=new RobotCommand(cmd);
+            rc.cpeeCallback = req.headers("CPEE-CALLBACK");
+            rc.cpeeCallbackId = req.headers("CPEE-CALLBACK-ID");
+            rc.cpeeInstanceURL = req.headers("CPEE-INSTANCE-URL");
+            if (Boolean.valueOf(req.queryParams("callback"))) {
+                res.header("CPEE-CALLBACK", "true");
+                rc.doCpeeCallback = true;
+            }
+            urbot.sendCmd(rc);
             return "";
         });
         post("/freedrive",  (req, res) -> {
@@ -53,5 +62,12 @@ public class WebServer {
             var r = URLog.get(from);
             return r.toJSON();
         });
+        get("/cmdq",  (req, res) -> {
+            JsonArray jsa=new JsonArray();
+            for(var c:urbot.getCmdq()) {
+                jsa.add(c.toJSON());
+            }
+            return jsa.toString();
+        });
     }
 }

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

@@ -7,11 +7,34 @@
     <script src="js/util.js"></script>
     <script src="js/moment.js"></script>
     <script src="js/vue.js"></script>
+    <style>
+        table {
+            display: inline-table;
+            width:50%;
+        }
+        td {
+            max-width: 100px;
+            overflow: hidden;
+            text-overflow: ellipsis;
+            white-space: nowrap;
+        }
+    </style>
 </head>
 <body>
 <div id="app">
     <textarea v-model="input" cols="80" rows="20"></textarea>
     <button v-on:click="send">send</button>
+    <table border="1">
+        <tr v-for="x in cmdq">
+            <td>{{x.id}}</td>
+            <td>{{x.cmd}}</td>
+            <td>{{x.cpeeCallback}}</td>
+            <td>
+            </td>
+        </tr>
+    </table>
+
+
     <table border="1">
         <tr v-for="x in cData">
             <td>{{x.type}}</td>
@@ -43,10 +66,13 @@
             log: [],
             lastID: -1,
             doBits: [],
-            cData: {}
+            cData: {},
+            cmdq: []
         },
         created: function () {
             setInterval(this.update, 200);
+        setInterval(this.updateQ, 250);
+
         },
         watch: {
             doBits: (newValue, oldValue) => {
@@ -68,7 +94,9 @@
         },
         methods: {
             send: function (event) {
-                fetch('/cmd', {method: "POST", body: this.input})
+                var params  = new URLSearchParams();
+                params.append('script', this.input);
+                fetch('/cmd', {method: "POST", body: params})
                     .then(function (response) {
                         return response;
                     });
@@ -117,6 +145,18 @@
                         console.log('unknown package:' + package.type);
                 }
             },
+            updateQ: function (event) {
+                fetch('/cmdq', {method: "GET"})
+                    .then(handleErrors)
+                    .then((response) => {
+                        return response.json();
+                    })
+                    .then((myJson) => {
+                        this.$data.cmdq = myJson;
+                    })
+                    .catch(error => {
+                        console.log(error)
+                    })},
             update: function (event) {
                 fetch('/log/' + this.lastID, {method: "GET"})
                     .then(handleErrors)