Browse Source

rest if digital output get/post

Martin Kunz 5 years ago
parent
commit
23e71d3bcc

+ 7 - 3
src/main/java/at/acdp/urweb/ScReadThread.java

@@ -20,6 +20,9 @@ public class ScReadThread implements Runnable {
         this.ip=ip;
     }
 
+    public SecondaryClient getRde() {
+        return rde;
+    }
 
     @Override
     public void run() {
@@ -34,7 +37,7 @@ public class ScReadThread implements Runnable {
     }
 
     private void read() throws IOException {
-        rde=new SecondaryClient(ip, 30001);
+        rde=new SecondaryClient(ip, 30002);
         VersionMessage vm = rde.connect();
         while(true) {
             rde.readPackage();
@@ -42,8 +45,9 @@ public class ScReadThread implements Runnable {
     }
 
     public void writeCmd(String s) {
-        log.info("writecmd: "+s);
+        if(!s.endsWith("\n"))
+            s+="\n";
+        log.info("writecmd: \""+s+"\"");
         rde.writeCmd(s);
-
     }
 }

+ 9 - 1
src/main/java/at/acdp/urweb/URBot.java

@@ -1,5 +1,6 @@
 package at.acdp.urweb;
 
+import at.acdp.urweb.sclient.data.MasterBoardData;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -40,6 +41,13 @@ public class URBot {
         sc.writeCmd(String.format("textmsg(%s)", message));
     }
 
+    public void setDigital(int which, boolean val) {
+        String c=String.format("digital_out[%d]=%s", which, val?"True":"False");
+        sc.writeCmd(c);
+    }
 
-
+    public boolean getDigital(int which) {
+        MasterBoardData mb = sc.getRde().getLastMB();
+        return ((mb.digitalOutputBits&(1<<which)) >0) ;
+    }
 }

+ 6 - 0
src/main/java/at/acdp/urweb/sclient/SecondaryClient.java

@@ -17,6 +17,7 @@ public class SecondaryClient {
     private Socket rt;
     private OutputStream os;
     private CountDataInputStream in;
+    private MasterBoardData lastMB=new MasterBoardData();
 
     public SecondaryClient(String ip, int port) {
         this.ip = ip;
@@ -32,6 +33,10 @@ public class SecondaryClient {
         return vm;
     }
 
+    public MasterBoardData getLastMB() {
+        return lastMB;
+    }
+
     public int readReply(CountDataInputStream di) throws IOException {
         int beforeCount=di.getCount();
         int size = di.readInt(); //4
@@ -133,6 +138,7 @@ public class SecondaryClient {
     private MasterBoardData readMasterBoardData(CountDataInputStream di, int size) throws IOException {
         MasterBoardData mb=new MasterBoardData();
         mb.read(di, size);
+        lastMB=mb;
         return mb;
     }
 

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

@@ -6,8 +6,8 @@ import com.eclipsesource.json.JsonObject;
 import java.io.IOException;
 
 public class MasterBoardData implements IRead, IJsonObject {
-    int digitalInputBits;
-    int digitalOutputBits;
+    public int digitalInputBits;
+    public int digitalOutputBits;
     int analogInputRange0;
     int analogInputRange1;
     double analogInput0;

+ 23 - 4
src/main/java/at/acdp/urweb/web/WebServer.java

@@ -32,8 +32,7 @@ public class WebServer {
                 .post("/cmd", new HttpHandler() {
                     @Override
                     public void handleRequest(HttpServerExchange ex) throws Exception {
-                        dispatch(ex, this);
-                        ex.startBlocking();
+                        if(!dispatch(ex,this)) return;
                         byte[] bytes = ex.getInputStream().readAllBytes();
                         String cmd=new String(bytes);
                         urbot.sendProgram(cmd);
@@ -46,6 +45,24 @@ public class WebServer {
                         urbot.sendFreedrive(1);
                     }
                 })
+                .post("/digital/{which}", new HttpHandler() {
+                    @Override
+                    public void handleRequest(HttpServerExchange ex) throws Exception {
+                        if(!dispatch(ex,this)) return;
+                        int which= Integer.parseInt(ex.getQueryParameters().get("which").getFirst());
+                        boolean val = Boolean.valueOf(new String(ex.getInputStream().readAllBytes()));
+                        urbot.setDigital(which, val);
+                    }
+                })
+                .get("/digital/{which}", new HttpHandler() {
+                    @Override
+                    public void handleRequest(HttpServerExchange ex) throws Exception {
+                        if(!dispatch(ex,this)) return;
+                        int which= Integer.parseInt(ex.getQueryParameters().get("which").getFirst());
+                        boolean res=urbot.getDigital(which);
+                        ex.getResponseSender().send(String.valueOf(res));
+                    }
+                })
                 .get("/log/{from}",  ex -> {
                     var x=ex.getQueryParameters().get("from").getFirst();
                     int from= Integer.parseInt(ex.getQueryParameters().get("from").getFirst());
@@ -63,10 +80,12 @@ public class WebServer {
         server.start();
     }
 
-    private void dispatch(HttpServerExchange ex, HttpHandler handler) {
+    private boolean dispatch(HttpServerExchange ex, HttpHandler handler) {
         if (ex.isInIoThread()) {
             ex.dispatch(handler);
-            return;
+            return false;
         }
+        ex.startBlocking();
+        return true;
     }
 }