Persoenliche Mediazentrale
ulrich
2021-05-06 f70acbb491c6421623cca57292a75f1820efad4d
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   
f70acb 51   private String conf;
U 52   
53   public GeraetSteuerung(String conf) {
54     this.conf = conf;
55   }
56   
849ee2 57   @Override
U 58   protected String get(HttpExchange e) {
59     String response;
60     String path = e.getRequestURI().toString();
61     String[] elems = path.split(Server.SLASH);
f70acb 62     FileStorage fs = new FileStorage(conf);
849ee2 63     logger.fine(path);
U 64     
65     String geraetName = elems[5];
66     String kommando = elems[6];
67     
68     Entity entity = fs.read(FileStorage.ST_GERAET, geraetName);
69     if(entity instanceof Geraet) {
70       Geraet g = (Geraet) entity;
71       switch(kommando) {
72         case KMD_EIN:
73           String einschaltBefehl = g.getEinUrl();
74           response = senden(einschaltBefehl);
75           break;
76
77         case KMD_AUS:
78           String ausschaltBefehl = g.getAusUrl();
79           response = senden(ausschaltBefehl);
80           break;
81
82         case KMD_STATUS:
83           response = statusErmitteln();
84           break;
85
86         default:
87           response = "Ungueltiges Kommando,";
88           break;
89       }
90     } else {
91       response = "Geraet nicht gefunden.";
92     }
93     
94     return response;
95   }
96   
97   private String statusErmitteln() {
98     return "noch nicht fertig";
99   }
100   
101   private String senden(String kommando) {
102     /*
103       TODO hier evtl. mit mehreren Versuchen ausgleichen, 
104       dass ein einzelner Versuch nicht 'durchkommt'...
105     */
106     logger.info(kommando);
107     try {
108       HttpURLConnection conn = (HttpURLConnection) new URL(kommando).openConnection();
109       conn.setRequestMethod("GET");
110       conn.connect();
111       int status = conn.getResponseCode();
112       String msg = conn.getResponseMessage();
113       logger.log(Level.INFO, "Kommando {0} mit Status {1} {2} gesendet.", new Object[]{kommando, status, msg});
114       return msg;
115     } catch(IOException ex) {
116       logger.log(Level.INFO, ex.getMessage(), ex);
117       return ex.getLocalizedMessage();
118     }
119   }
120   
121 }