/*
|
Mediazentrale - 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.mediaz.api;
|
|
import com.sun.net.httpserver.HttpExchange;
|
import de.uhilger.mediaz.App;
|
import de.uhilger.mediaz.Server;
|
import de.uhilger.mediaz.entity.Entity;
|
import de.uhilger.mediaz.entity.Geraet;
|
import de.uhilger.mediaz.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";
|
|
@Override
|
protected String get(HttpExchange e) {
|
String response;
|
String path = e.getRequestURI().toString();
|
String[] elems = path.split(Server.SLASH);
|
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_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();
|
}
|
}
|
|
}
|