src/de/uhilger/mediaz/Server.java | ●●●●● patch | view | raw | blame | history | |
src/de/uhilger/mediaz/api/GeraetSteuerung.java | ●●●●● patch | view | raw | blame | history | |
src/mediaz_de_DE.properties | ●●●●● patch | view | raw | blame | history | |
www/ui/data/tpl/geraet_schalt_liste.txt | ●●●●● patch | view | raw | blame | history | |
www/ui/js/app.js | ●●●●● patch | view | raw | blame | history |
src/de/uhilger/mediaz/Server.java
@@ -19,6 +19,7 @@ import com.sun.net.httpserver.HttpServer; import de.uhilger.mediaz.api.FileHandler; import de.uhilger.mediaz.api.GeraetSteuerung; import de.uhilger.mediaz.api.ListFileHandler; import de.uhilger.mediaz.api.ListHandler; import de.uhilger.mediaz.api.MediaSteuerung; @@ -51,6 +52,7 @@ public static final String RB_WEBROOT = "webroot"; public static final String RB_STORE = "store"; public static final String RB_STRG = "strg"; public static final String RB_GSTRG = "gstrg"; public static final String RB_ALIST= "alist"; //public static final String RB_UI_ROOT = "uiroot"; public static final String RB_STOP_SERVER = "stopServer"; @@ -113,6 +115,7 @@ ablageorteEinklinken(server); server.createContext(ctx + App.getRs(RB_STORE), new StorageHandler()); server.createContext(ctx + App.getRs(RB_STRG), new MediaSteuerung()); server.createContext(ctx + App.getRs(RB_GSTRG), new GeraetSteuerung()); server.createContext(ctx + App.getRs(RB_ALIST), new ListHandler()); server.createContext(ctx + App.getRs(RB_STOP_SERVER), new StopServerHandler()); server.setExecutor(Executors.newFixedThreadPool(20)); src/de/uhilger/mediaz/api/GeraetSteuerung.java
New file @@ -0,0 +1,115 @@ /* 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(); } } } src/mediaz_de_DE.properties
@@ -18,6 +18,7 @@ store=/api/store alist=/api/alist strg=/api/strg gstrg=/api/gstrg epliste=liste stopServer=/api/server/stop testAblage=/api/test/ablage www/ui/data/tpl/geraet_schalt_liste.txt
@@ -1,8 +1,8 @@ <div class='entity-formular'> {{#ArrayList}} <div class='geraet-schalt-eintrag'> <button class="button schalt-btn" id="ein-btn">Ein</button> <button class="button schalt-btn" id="aus-btn">Aus</button> <button class="button schalt-btn" id="ein-btn" gname="{{.}}">Ein</button> <button class="button schalt-btn" id="aus-btn" gname="{{.}}">Aus</button> <div class='schalt-geraet-name entity-eintrag'>{{.}}</div> </div> {{/ArrayList}} www/ui/js/app.js
@@ -161,6 +161,21 @@ "self.form_geraet_status", function(responseText) { var geraet = JSON.parse(responseText); self.geraet_status_form(geraet); }, function() { self.addEvtListener('#ein-btn', 'click', function (event) { var geraetName = event.target.attributes.gname.nodeValue; //var geraetName = document.querySelector('.schalt-geraet-name').textContent; self.http_get('../api/gstrg/geraet/' + geraetName + "/ein", function(responseText) { console.log(responseText); }); }); self.addEvtListener('#aus-btn', 'click', function (event) { var geraetName = event.target.attributes.gname.nodeValue; //var geraetName = document.querySelector('.schalt-geraet-name').textContent; self.http_get('../api/gstrg/geraet/' + geraetName + "/aus", function(responseText) { console.log(responseText); }); }); }); }; @@ -493,7 +508,7 @@ * self.ablageort_form(ablageort); * }); */ this.entitaet_liste = function(bname, listUrl, tpl, storeUrl, formFunc, cb) { this.entitaet_liste = function(bname, listUrl, tpl, storeUrl, formFunc, cb, customListCode) { self.reset_top_buttons(); document.querySelector('.bereich-name').textContent = bname; var bb = document.querySelector('.breadcrumb-behaelter'); @@ -508,7 +523,12 @@ //self.addEvtListener('#neu-btn', 'click', function (event) { self.addEvtListener('#top-neu-btn', 'click', function(event) { eval(formFunc + "(this)"); }); }); if(typeof(customListCode) !== 'function') { // .. } else { customListCode(); } }); }); };