From 9d4292732c1db2de2e9545ac9d91b431aa98ebad Mon Sep 17 00:00:00 2001
From: ulrich
Date: Sun, 30 Mar 2025 17:23:05 +0000
Subject: [PATCH] Maschine Ausschalten nach einer Minute, sodass der Server zuvor herunter fahren kann.
---
src/de/uhilger/calypso/actor/DownServerActor.java | 60 ++++++++++++++++++++++++++++++
src/de/uhilger/calypso/http/ApiHandler.java | 22 +++++++++-
2 files changed, 79 insertions(+), 3 deletions(-)
diff --git a/src/de/uhilger/calypso/actor/DownServerActor.java b/src/de/uhilger/calypso/actor/DownServerActor.java
new file mode 100644
index 0000000..7f54a66
--- /dev/null
+++ b/src/de/uhilger/calypso/actor/DownServerActor.java
@@ -0,0 +1,60 @@
+/*
+ Calypso - Media Player Remote Control via HTTP for Raspberry Pi
+ Copyright (C) 2021-2023 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.calypso.actor;
+
+import com.sun.net.httpserver.HttpContext;
+import java.io.IOException;
+import java.util.Timer;
+import java.util.TimerTask;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * Ein HTTP-Handler zum Stoppen der Anwendung
+ *
+ * @author Ulrich Hilger
+ */
+public class DownServerActor extends StopServerActor {
+
+ public void run(HttpContext thisContext) {
+ Logger.getLogger(DownServerActor.class.getName()).info(
+ "Maschine wird in einer Minute ausgeschaltet.");
+ ProcessBuilder pb = new ProcessBuilder("sudo", "shutdown", "-h", "-P", "+1");
+ try {
+ Process process = pb.start();
+ } catch (IOException ex) {
+ Logger.getLogger(DownServerActor.class.getName()).log(Level.SEVERE, null, ex);
+ }
+ super.run(thisContext);
+ }
+
+ /**
+ * Die Klasse AppStopper ermöglicht das asnychrone bzw.
+ * zeitgesteuerte Stoppen der Anwendung.
+ */
+ class AppStopper extends TimerTask {
+
+ @Override
+ public void run() {
+ Logger.getLogger(DownServerActor.class.getName()).info("Calypso beendet.");
+ System.exit(0);
+ }
+ }
+
+}
diff --git a/src/de/uhilger/calypso/http/ApiHandler.java b/src/de/uhilger/calypso/http/ApiHandler.java
index bcdf2a3..ba978fe 100644
--- a/src/de/uhilger/calypso/http/ApiHandler.java
+++ b/src/de/uhilger/calypso/http/ApiHandler.java
@@ -20,6 +20,7 @@
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpExchange;
+import de.uhilger.calypso.actor.DownServerActor;
import de.uhilger.calypso.actor.PlayActor;
import de.uhilger.calypso.actor.ShellActor;
import de.uhilger.calypso.actor.StopServerActor;
@@ -53,6 +54,7 @@
public static final String STOP = "stop";
public static final String SERVER = "server";
public static final String PING = "ping";
+ public static final String DOWN = "down";
public static final String PLAYER = "player-proc";
@@ -109,7 +111,8 @@
break;
case SERVER:
- if(elems[SERVERCMD].equals(STOP)) {
+ switch (elems[SERVERCMD]) {
+ case STOP:
try {
antwort = "Calypso: Der Server wird angehalten und die App beendet.";
sendResponse(exchange, antwort);
@@ -118,10 +121,23 @@
Logger.getLogger(ApiHandler.class.getName()).log(Level.SEVERE, null, ex);
antwort = "Fehler: " + ex.getLocalizedMessage();
}
- } else {
+ break;
+ case DOWN:
+ try {
+ antwort = "Calypso: Der Server wird angehalten, die App beendet und die Maschine ausgeschaltet.";
+ sendResponse(exchange, antwort);
+ new DownServerActor().run(exchange.getHttpContext());
+ } catch (IOException ex) {
+ Logger.getLogger(ApiHandler.class.getName()).log(Level.SEVERE, null, ex);
+ antwort = "Fehler: " + ex.getLocalizedMessage();
+ }
+ break;
+ default:
antwort = elems[SERVERCMD] + " ist ein unbekanntes Serverkommando";
- }
+ break;
+ }
break;
+
case PING:
antwort = elems[PLAYERCMD];
--
Gitblit v1.9.3