commit | author | age
|
849ee2
|
1 |
/* |
94b1c2
|
2 |
Tango - Personal Media Center |
849ee2
|
3 |
Copyright (C) 2021 Ulrich Hilger |
U |
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
94b1c2
|
18 |
package de.uhilger.tango.api; |
849ee2
|
19 |
|
U |
20 |
import com.sun.net.httpserver.HttpExchange; |
94b1c2
|
21 |
import de.uhilger.tango.App; |
U |
22 |
import de.uhilger.tango.Server; |
|
23 |
import de.uhilger.tango.entity.Entity; |
|
24 |
import de.uhilger.tango.entity.Geraet; |
|
25 |
import de.uhilger.tango.store.FileStorage; |
849ee2
|
26 |
import java.io.IOException; |
U |
27 |
import java.net.HttpURLConnection; |
|
28 |
import java.net.URL; |
|
29 |
import java.util.logging.Level; |
|
30 |
import java.util.logging.Logger; |
|
31 |
|
|
32 |
/** |
|
33 |
* Steuerung von Geraeten, also Ein- und Ausschalten sowie deren Status |
|
34 |
* ermitteln |
|
35 |
* |
|
36 |
* |
|
37 |
* HTTP GET /mz/api/gstrg/geraet/[name]/ein |
|
38 |
* HTTP GET /mz/api/gstrg/geraet/[name]/aus |
|
39 |
* HTTP GET /mz/api/gstrg/geraet/[name]/status |
|
40 |
* |
|
41 |
* @author Ulrich Hilger |
|
42 |
*/ |
|
43 |
public class GeraetSteuerung extends AbstractHandler { |
|
44 |
|
|
45 |
private static final Logger logger = Logger.getLogger(GeraetSteuerung.class.getName()); |
|
46 |
|
|
47 |
public static final String KMD_EIN = "ein"; |
|
48 |
public static final String KMD_AUS = "aus"; |
|
49 |
public static final String KMD_STATUS = "status"; |
|
50 |
|
|
51 |
@Override |
|
52 |
protected String get(HttpExchange e) { |
|
53 |
String response; |
|
54 |
String path = e.getRequestURI().toString(); |
|
55 |
String[] elems = path.split(Server.SLASH); |
|
56 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
57 |
logger.fine(path); |
|
58 |
|
|
59 |
String geraetName = elems[5]; |
|
60 |
String kommando = elems[6]; |
|
61 |
|
|
62 |
Entity entity = fs.read(FileStorage.ST_GERAET, geraetName); |
|
63 |
if(entity instanceof Geraet) { |
|
64 |
Geraet g = (Geraet) entity; |
|
65 |
switch(kommando) { |
|
66 |
case KMD_EIN: |
|
67 |
String einschaltBefehl = g.getEinUrl(); |
|
68 |
response = senden(einschaltBefehl); |
|
69 |
break; |
|
70 |
|
|
71 |
case KMD_AUS: |
|
72 |
String ausschaltBefehl = g.getAusUrl(); |
|
73 |
response = senden(ausschaltBefehl); |
|
74 |
break; |
|
75 |
|
|
76 |
case KMD_STATUS: |
|
77 |
response = statusErmitteln(); |
|
78 |
break; |
|
79 |
|
|
80 |
default: |
|
81 |
response = "Ungueltiges Kommando,"; |
|
82 |
break; |
|
83 |
} |
|
84 |
} else { |
|
85 |
response = "Geraet nicht gefunden."; |
|
86 |
} |
|
87 |
|
|
88 |
return response; |
|
89 |
} |
|
90 |
|
|
91 |
private String statusErmitteln() { |
|
92 |
return "noch nicht fertig"; |
|
93 |
} |
|
94 |
|
|
95 |
private String senden(String kommando) { |
|
96 |
/* |
|
97 |
TODO hier evtl. mit mehreren Versuchen ausgleichen, |
|
98 |
dass ein einzelner Versuch nicht 'durchkommt'... |
|
99 |
*/ |
|
100 |
logger.info(kommando); |
|
101 |
try { |
|
102 |
HttpURLConnection conn = (HttpURLConnection) new URL(kommando).openConnection(); |
|
103 |
conn.setRequestMethod("GET"); |
|
104 |
conn.connect(); |
|
105 |
int status = conn.getResponseCode(); |
|
106 |
String msg = conn.getResponseMessage(); |
|
107 |
logger.log(Level.INFO, "Kommando {0} mit Status {1} {2} gesendet.", new Object[]{kommando, status, msg}); |
|
108 |
return msg; |
|
109 |
} catch(IOException ex) { |
|
110 |
logger.log(Level.INFO, ex.getMessage(), ex); |
|
111 |
return ex.getLocalizedMessage(); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
} |