package de.uhilger.avdirektor.handler; import com.sun.net.httpserver.HttpExchange; import de.uhilger.avdirektor.App; import de.uhilger.avdirektor.OMXLogLeser; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; /** * z.B. * http://rpi4-az:9090/avd/playon?th=1&ti=240&o=local&log=true&titel=http://amd-srv:9090/srv/Filme/C/casino_royale.m4v * * @author ulrich */ public class PlayOnHandler extends PlayHandler { private static final Logger logger = Logger.getLogger(PlayOnHandler.class.getName()); public static final String LOG_DIR = "omx-logs"; public static final String LOG_EXT = "log"; public static final String DASH = "-"; public static final String DOT = "."; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss"); public PlayOnHandler(String cmd) { super(cmd); } /* 1. buildParams 2. process */ @Override protected StringBuilder buildParams(HttpExchange t) { File logDir = logSichern(); StringBuilder params = super.buildParams(t); params.append(" --pos "); try { params.append(dauerBestimmen(logDir)); } catch (IOException | ParseException ex) { logger.log(Level.SEVERE, null, ex); } return params; } private File logSichern() { String wd = System.getProperty(App.OMX_WD); File zielOrdner = new File(wd, LOG_DIR); try { // omxplayer.log umbenennen FileSystem fs = FileSystems.getDefault(); Path logDatei = fs.getPath(wd, OMXPlayer.NAME + DOT + LOG_EXT); Date now = new Date(); String neuerName = OMXPlayer.NAME + DASH + sdf.format(now) + DOT + LOG_EXT; Files.move(logDatei, logDatei.resolveSibling(neuerName)); // Unterverzeichnis ggf. erzeugen zielOrdner.mkdirs(); // omxplayer-datum.log verschieben Path quellDatei = fs.getPath(wd, neuerName); Path zielPfad = fs.getPath(wd, LOG_DIR); Files.move(quellDatei, zielPfad.resolve(quellDatei.getFileName()), REPLACE_EXISTING); } catch (IOException ex) { logger.log(Level.SEVERE, null, ex); } return zielOrdner; } private String dauerBestimmen(File logDir) throws IOException, FileNotFoundException, ParseException { OMXLogLeser leser = new OMXLogLeser(); return leser.logDirLesen(logDir); } }