|
@@ -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;
|
|
|
+ }
|
|
|
}
|