Persoenliche Mediazentrale
ulrich
2021-05-06 f70acbb491c6421623cca57292a75f1820efad4d
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
/*
  Tango - Personal Media Center
  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.tango.api;
 
import com.sun.net.httpserver.HttpExchange;
import de.uhilger.tango.App;
import de.uhilger.tango.Server;
import de.uhilger.tango.entity.Entity;
import de.uhilger.tango.entity.Geraet;
import de.uhilger.tango.store.FileStorage;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 * Steuerung von Geraeten, also Ein- und Ausschalten sowie deren Status 
 * ermitteln
 * 
 * 
 * HTTP GET /mz/api/gstrg/geraet/[name]/ein
 * HTTP GET /mz/api/gstrg/geraet/[name]/aus
 * HTTP GET /mz/api/gstrg/geraet/[name]/status
 * 
 * @author Ulrich Hilger
 */
public class GeraetSteuerung extends AbstractHandler {
 
  private static final Logger logger = Logger.getLogger(GeraetSteuerung.class.getName());
  
  public static final String KMD_EIN = "ein";
  public static final String KMD_AUS = "aus";
  public static final String KMD_STATUS = "status";
  
  private String conf;
  
  public GeraetSteuerung(String conf) {
    this.conf = conf;
  }
  
  @Override
  protected String get(HttpExchange e) {
    String response;
    String path = e.getRequestURI().toString();
    String[] elems = path.split(Server.SLASH);
    FileStorage fs = new FileStorage(conf);
    logger.fine(path);
    
    String geraetName = elems[5];
    String kommando = elems[6];
    
    Entity entity = fs.read(FileStorage.ST_GERAET, geraetName);
    if(entity instanceof Geraet) {
      Geraet g = (Geraet) entity;
      switch(kommando) {
        case KMD_EIN:
          String einschaltBefehl = g.getEinUrl();
          response = senden(einschaltBefehl);
          break;
 
        case KMD_AUS:
          String ausschaltBefehl = g.getAusUrl();
          response = senden(ausschaltBefehl);
          break;
 
        case KMD_STATUS:
          response = statusErmitteln();
          break;
 
        default:
          response = "Ungueltiges Kommando,";
          break;
      }
    } else {
      response = "Geraet nicht gefunden.";
    }
    
    return response;
  }
  
  private String statusErmitteln() {
    return "noch nicht fertig";
  }
  
  private String senden(String kommando) {
    /*
      TODO hier evtl. mit mehreren Versuchen ausgleichen, 
      dass ein einzelner Versuch nicht 'durchkommt'...
    */
    logger.info(kommando);
    try {
      HttpURLConnection conn = (HttpURLConnection) new URL(kommando).openConnection();
      conn.setRequestMethod("GET");
      conn.connect();
      int status = conn.getResponseCode();
      String msg = conn.getResponseMessage();
      logger.log(Level.INFO, "Kommando {0} mit Status {1} {2} gesendet.", new Object[]{kommando, status, msg});
      return msg;
    } catch(IOException ex) {
      logger.log(Level.INFO, ex.getMessage(), ex);
      return ex.getLocalizedMessage();
    }
  }
  
}