From 0c8d27c8a1708d1c315aca89db47d48488fd44ca Mon Sep 17 00:00:00 2001
From: ulrich
Date: Mon, 22 Mar 2021 07:53:34 +0000
Subject: [PATCH] Play/Stop in Arbeit

---
 src/de/uhilger/avdirektor/handler/CmdHandler.java  |   43 ++++++++++++++
 src/logging.properties                             |    1 
 src/de/uhilger/avdirektor/Server.java              |    8 ++
 src/de/uhilger/avdirektor/handler/PingHandler.java |   39 +++++++++++++
 src/de/uhilger/avdirektor/handler/PlayHandler.java |   20 +-----
 src/de/uhilger/avdirektor/handler/StopHandler.java |   19 ++++++
 src/de/uhilger/avdirektor/handler/OMXPlayer.java   |   29 +++++++++
 7 files changed, 144 insertions(+), 15 deletions(-)

diff --git a/src/de/uhilger/avdirektor/Server.java b/src/de/uhilger/avdirektor/Server.java
index 018fe3c..7a56d94 100644
--- a/src/de/uhilger/avdirektor/Server.java
+++ b/src/de/uhilger/avdirektor/Server.java
@@ -1,7 +1,9 @@
 package de.uhilger.avdirektor;
 
 import com.sun.net.httpserver.HttpServer;
+import de.uhilger.avdirektor.handler.PingHandler;
 import de.uhilger.avdirektor.handler.PlayHandler;
+import de.uhilger.avdirektor.handler.StopHandler;
 import de.uhilger.avdirektor.handler.StopServerHandler;
 import java.io.IOException;
 import java.util.logging.Logger;
@@ -28,8 +30,14 @@
   
   public void start() throws IOException {
     logger.info("Server starting on port " + port);
+
+    StopHandler sh = new StopHandler();
+    sh.setCmd(App.CMD_STOP);
+    
     HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
     server.createContext("/avd/play", new PlayHandler());
+    server.createContext("/avd/stop", sh);
+    server.createContext("/avd/ping", new PingHandler());
     server.createContext("/avd/server/stop", new StopServerHandler());
     server.setExecutor(null); // creates a default executor
     server.start();
diff --git a/src/de/uhilger/avdirektor/handler/CmdHandler.java b/src/de/uhilger/avdirektor/handler/CmdHandler.java
new file mode 100644
index 0000000..ac108f4
--- /dev/null
+++ b/src/de/uhilger/avdirektor/handler/CmdHandler.java
@@ -0,0 +1,43 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package de.uhilger.avdirektor.handler;
+
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author ulrich
+ */
+public abstract class CmdHandler extends OMXPlayer implements HttpHandler {
+
+  private static final Logger logger = Logger.getLogger(CmdHandler.class.getName());
+  
+  protected String cmd;
+  
+  public abstract void setCmd(String cmd);
+
+  @Override
+  public void handle(HttpExchange t) throws IOException {
+    logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
+    Map map = getQueryMap(t);
+    
+    String antwort = this.kommando(t, cmd);
+    logger.log(Level.FINE, antwort);
+    
+    String response = getResponseString(map, "cmd", antwort);
+    t.sendResponseHeaders(200, response.length());
+    OutputStream os = t.getResponseBody();
+    os.write(response.getBytes());
+    os.close();    
+  }
+  
+}
diff --git a/src/de/uhilger/avdirektor/handler/OMXPlayer.java b/src/de/uhilger/avdirektor/handler/OMXPlayer.java
index 82acdde..dc0a797 100644
--- a/src/de/uhilger/avdirektor/handler/OMXPlayer.java
+++ b/src/de/uhilger/avdirektor/handler/OMXPlayer.java
@@ -18,6 +18,7 @@
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -28,6 +29,15 @@
 public abstract class OMXPlayer implements ProzessLauscher {
   
   private static final Logger logger = Logger.getLogger(OMXPlayer.class.getName());
+  
+  protected String getParam(Map map, String key) {
+    Object o = map.get(key);
+    if(o != null) {
+      return o.toString();
+    } else {
+      return null;
+    }
+  }
   
   protected Map getQueryMap(HttpExchange t) {
     HashMap map = new HashMap();
@@ -41,6 +51,24 @@
       }
     }
     return map;
+  }
+  
+  protected String getResponseString(Map map, String cmd, String antwort) {
+    Set keys = map.keySet();
+    StringBuilder buf = new StringBuilder();
+    buf.append("play");
+    buf.append(System.lineSeparator());
+    keys.forEach((Object key) -> {
+      buf.append("key: ");
+      buf.append(key);
+      buf.append(System.lineSeparator());
+      buf.append("value: "); 
+      buf.append(map.get(key));
+      buf.append(System.lineSeparator());
+      //logger.log(Level.FINE, "key {0} value {1}", new Object[]{key, map.get(key)});
+    });
+    buf.append(antwort);
+    return buf.toString();
   }
   
   /**
@@ -119,6 +147,7 @@
         mt.start();
       }
       //servletContext.setAttribute(App.PI_PLAYER, player_process);
+      t.setAttribute(App.PI_PLAYER, player_process);
       //Runtime.getRuntime().exec("killall dbus-daemon");
       antwort = "Abspielen gestartet, url: " + urlStr;
     }
diff --git a/src/de/uhilger/avdirektor/handler/PingHandler.java b/src/de/uhilger/avdirektor/handler/PingHandler.java
new file mode 100644
index 0000000..1e7bde1
--- /dev/null
+++ b/src/de/uhilger/avdirektor/handler/PingHandler.java
@@ -0,0 +1,39 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package de.uhilger.avdirektor.handler;
+
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpHandler;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author ulrich
+ */
+public class PingHandler implements HttpHandler {
+  
+  private static final Logger logger = Logger.getLogger(PingHandler.class.getName());
+  
+
+  @Override
+  public void handle(HttpExchange t) throws IOException {
+    logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
+    
+    String antwort = "ping Antwort";    
+    
+    String response = antwort;
+    t.sendResponseHeaders(200, response.length());
+    OutputStream os = t.getResponseBody();
+    os.write(response.getBytes());
+    os.close();    
+    
+
+  }
+  
+}
diff --git a/src/de/uhilger/avdirektor/handler/PlayHandler.java b/src/de/uhilger/avdirektor/handler/PlayHandler.java
index 80b5214..304d867 100644
--- a/src/de/uhilger/avdirektor/handler/PlayHandler.java
+++ b/src/de/uhilger/avdirektor/handler/PlayHandler.java
@@ -54,23 +54,13 @@
 
   @Override
   public void handle(HttpExchange t) throws IOException {
-    logger.log(Level.FINE, "RequestURI.getPath: {0}", t.getRequestURI().getPath());
+    logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
     Map map = getQueryMap(t);
-    Set keys = map.keySet();
-    StringBuffer buf = new StringBuffer();
-    buf.append("play");
-    buf.append(System.lineSeparator());
-    keys.forEach((Object key) -> {
-      buf.append("key: ");
-      buf.append(key);
-      buf.append(System.lineSeparator());
-      buf.append("value: "); 
-      buf.append(map.get(key));
-      buf.append(System.lineSeparator());
-      logger.log(Level.FINE, "key {0} value {1}", new Object[]{key, map.get(key)});
-    });
     
-    String response = buf.toString();
+    String antwort = abspielenMitParameternUndRueckmeldung(t, 
+            getParam(map, "t"), getParam(map, "p"), getParam(map, "r"), "1");    
+    
+    String response = getResponseString(map, "play", antwort);
     t.sendResponseHeaders(200, response.length());
     OutputStream os = t.getResponseBody();
     os.write(response.getBytes());
diff --git a/src/de/uhilger/avdirektor/handler/StopHandler.java b/src/de/uhilger/avdirektor/handler/StopHandler.java
new file mode 100644
index 0000000..460d6dd
--- /dev/null
+++ b/src/de/uhilger/avdirektor/handler/StopHandler.java
@@ -0,0 +1,19 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package de.uhilger.avdirektor.handler;
+
+/**
+ *
+ * @author ulrich
+ */
+public class StopHandler extends CmdHandler {
+
+  @Override
+  public void setCmd(String cmd) {
+    this.cmd = cmd;
+  }
+  
+}
diff --git a/src/logging.properties b/src/logging.properties
index 3f0ec2e..6b0ab55 100644
--- a/src/logging.properties
+++ b/src/logging.properties
@@ -68,4 +68,5 @@
 # de.uhilger.filecms.level = FINEST
 # de.uhilger.wbx.handlers = java.util.logging.ConsoleHandler
 # de.uhilger.wbx.level = FINEST
+de.uhilger.avdirektor.handlers = java.util.logging.FileHandler, java.util.logging.ConsoleHandler
 de.uhilger.avdirektor.level = INFO
\ No newline at end of file

--
Gitblit v1.9.3