App zur Steuerung des mpv Mediaplayers auf einem Raspberry Pi über HTTP
ulrich
2021-03-25 60719cc5f0cdc13defcf73eb4a0701ee9b654289
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
    AV-Direktor - Control OMXPlayer on Raspberry Pi via HTTP
    Copyright (C) 2021  Ulrich Hilger
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as
    published by the Free Software Foundation, either version 3 of the
    License, or (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.
 
    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/
 
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.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author ulrich
 */
public abstract class AbstractHandler extends OMXPlayer implements HttpHandler {
 
  private static final Logger logger = Logger.getLogger(AbstractHandler.class.getName());
  
  protected String cmd;
  protected Map map;
  
  @Override
  public void handle(HttpExchange t) throws IOException {
    logger.log(Level.FINE, "RequestURI: {0}", t.getRequestURI().toString());
    StringBuilder params = buildParams(t);
    String antwort = process(t, params.toString());
    sendResponse(t, cmd, antwort);
  }
  
  protected abstract String process(HttpExchange t, String params);
  
  protected StringBuilder buildParams(HttpExchange t) {
    map = getQueryMap(t);
    StringBuilder params = new StringBuilder();
    return params;
  }
  
  protected void sendResponse(HttpExchange t, String cmd, String antwort) throws IOException {
    String response = getResponseString(map, cmd, antwort);
    t.sendResponseHeaders(200, response.length());
    OutputStream os = t.getResponseBody();
    os.write(response.getBytes());
    os.close();    
  }
  
  public void setCmd(String cmd) {
    this.cmd = cmd;
  }
  
  public String getCmd(String cmd) {
    return this.cmd;
  }
 
  /* --- --- */
  
  protected String getParam(Map map, String key) {
    Object o = map.get(key);
    if(o != null) {
      return o.toString();
    } else {
      return null;
    }
  }
  
  /*
    Den Query-Teil einer URL in die Parameter zerlegen
  
    Die Zerlegung erfolgt mit String.split nach 
    &amp; und dann nach =
  */
  protected Map getQueryMap(HttpExchange t) {
    HashMap map = new HashMap();
    String query = t.getRequestURI().getQuery();
    if(query != null && query.length() > 0) {
      String qParts[] = query.split("&");
      for(String qPart : qParts) {
        logger.finer("qPart: " + qPart);
        String pParts[] = qPart.split("=");
        map.put(pParts[0], pParts[1]);
        logger.finer("pParts[0]: " + pParts[0] + ", pParts[1]: " + pParts[1]);
      }
    }
    return map;
  }
  
  protected String getResponseString(Map map, String cmd, String antwort) {
    Set keys = map.keySet();
    StringBuilder buf = new StringBuilder();
    buf.append(cmd);
    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();
  }
    
  
}