From 1c28a2a38175b10b722a4698e753ee28dcbeec36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ple=C3=9F?= Date: Fri, 1 Jun 2012 21:04:35 +0200 Subject: [PATCH] completed mpd connection --- src/de/ctdo/crashtest/TestClass.java | 12 +- src/de/ctdo/crashtest/game/TheGame.java | 13 ++- src/de/ctdo/crashtest/irc/IrcClient.java | 2 +- src/de/ctdo/crashtest/log/Logger.java | 6 +- src/de/ctdo/crashtest/mpd/IMPDController.java | 4 +- src/de/ctdo/crashtest/mpd/MPDController.java | 105 +++++++++++++++--- 6 files changed, 116 insertions(+), 26 deletions(-) diff --git a/src/de/ctdo/crashtest/TestClass.java b/src/de/ctdo/crashtest/TestClass.java index 29e35cd..3520787 100644 --- a/src/de/ctdo/crashtest/TestClass.java +++ b/src/de/ctdo/crashtest/TestClass.java @@ -9,12 +9,20 @@ public class TestClass { - public static void main(String args[]) { + public static void main(String args[]) throws InterruptedException { IMPDController mpdController = new MPDController(); + mpdController.playSong("VNV Nation", "Nemesis"); + + Thread.sleep(2000); + + mpdController.playSong("Enter Shikari", "Sorry, You're Not A Winner"); + + + Thread.sleep(2000); + mpdController.playSong("Calabria", "Pump It Up (Club Mix)"); - mpdController.playSong("VNV Nation", "Perpetual"); } diff --git a/src/de/ctdo/crashtest/game/TheGame.java b/src/de/ctdo/crashtest/game/TheGame.java index d148ec6..a92f8a0 100644 --- a/src/de/ctdo/crashtest/game/TheGame.java +++ b/src/de/ctdo/crashtest/game/TheGame.java @@ -3,12 +3,15 @@ package de.ctdo.crashtest.game; import de.ctdo.crashtest.domotics.*; import de.ctdo.crashtest.gui.*; import de.ctdo.crashtest.irc.*; +import de.ctdo.crashtest.mpd.IMPDController; +import de.ctdo.crashtest.mpd.MPDController; public class TheGame implements StateChangeListener, GuiEventListener, IRCEventListener { private IGuiControl guiControl; private IIrcClient ircClient; private IStatemachine machine; private IBuntiClient bunti; + private IMPDController mpdController; private int timertSeconds = 0; private int timertSecondsLast = 0; @@ -23,6 +26,7 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL this.ircClient = new IrcClient(); this.machine = new Statemachine(); this.bunti = new BuntiClient("bunti.ctdo.de", 8080); + this.mpdController = new MPDController(); initGame(); } @@ -47,11 +51,13 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL switch (newState) { case IDLE: + mpdController.playSong("VNV Nation", "Nemesis"); bunti.setPar56(0,0,0); bunti.setLampel(false,false,false); guiControl.showCountDown(false); break; case ENTERED_ROOM: + mpdController.playSong("Blümchen", "Herz an Herz"); bunti.setLampel(false,false,false); bunti.setPar56(20,0,100); break; @@ -83,6 +89,9 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL case TABLE_GAME_SEVEN: bunti.setLampel(false,false,true); bunti.setPar56(255,100,0); + mpdController.playSong("K2", "Der berg ruft"); + ircClient.say("table game complete"); + sayScore(); break; case TABLE_FINISH: // und roket muss starten bunti.setLampel(false,false,true); @@ -132,7 +141,7 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL } else if(message.startsWith("timer")) { handleTimerCommand(message); } else if(message.startsWith("score")) { - handleScoreCommand(message); + sayScore(); } else if(message.startsWith("wall")) { guiControl.setWall(message.substring("wall".length()).trim()); } else if(message.startsWith("extra")) { @@ -199,7 +208,7 @@ public class TheGame implements StateChangeListener, GuiEventListener, IRCEventL } } - private void handleScoreCommand(final String message) { + private void sayScore() { ircClient.say("stateChangeCounter: " + machine.getStateChangeCounter()); ircClient.say("timerlast: " + timertSecondsLast / 10); } diff --git a/src/de/ctdo/crashtest/irc/IrcClient.java b/src/de/ctdo/crashtest/irc/IrcClient.java index c0a14bb..cb2b5c3 100644 --- a/src/de/ctdo/crashtest/irc/IrcClient.java +++ b/src/de/ctdo/crashtest/irc/IrcClient.java @@ -20,7 +20,7 @@ import java.util.List; */ public class IrcClient implements IIrcClient, jerklib.events.listeners.IRCEventListener, ILogger { private final List listenerListIRC = new ArrayList(); - private final static String CHANNEL = "#crashtest2"; + private final static String CHANNEL = "#crashtest"; private Session ircsession; public IrcClient() { diff --git a/src/de/ctdo/crashtest/log/Logger.java b/src/de/ctdo/crashtest/log/Logger.java index ddbc511..b1f537d 100644 --- a/src/de/ctdo/crashtest/log/Logger.java +++ b/src/de/ctdo/crashtest/log/Logger.java @@ -21,11 +21,15 @@ public class Logger { } public void log(String message) { + System.out.println("LOG: " + message); for(ILogger logger: loggerList) { - logger.log(message); + synchronized (this) { + logger.log(message); + } } + } public static void sLog(String message) { diff --git a/src/de/ctdo/crashtest/mpd/IMPDController.java b/src/de/ctdo/crashtest/mpd/IMPDController.java index a40400c..275b72c 100644 --- a/src/de/ctdo/crashtest/mpd/IMPDController.java +++ b/src/de/ctdo/crashtest/mpd/IMPDController.java @@ -5,6 +5,6 @@ package de.ctdo.crashtest.mpd; * @date: 01.06.12 10:36 */ public interface IMPDController { - void playSong(String artist, String title); - void setVolume(int volume); + void playSong(final String artist, final String title); + void setVolume(final int volume); } diff --git a/src/de/ctdo/crashtest/mpd/MPDController.java b/src/de/ctdo/crashtest/mpd/MPDController.java index e4592e5..588dad4 100644 --- a/src/de/ctdo/crashtest/mpd/MPDController.java +++ b/src/de/ctdo/crashtest/mpd/MPDController.java @@ -1,15 +1,21 @@ package de.ctdo.crashtest.mpd; +import de.ctdo.crashtest.game.TheGame; import de.ctdo.crashtest.log.Logger; import org.bff.javampd.MPD; import org.bff.javampd.MPDDatabase; import org.bff.javampd.MPDPlayer; +import org.bff.javampd.MPDPlaylist; import org.bff.javampd.exception.MPDConnectionException; import org.bff.javampd.exception.MPDDatabaseException; +import org.bff.javampd.exception.MPDPlayerException; +import org.bff.javampd.exception.MPDPlaylistException; import org.bff.javampd.objects.MPDSong; import java.net.UnknownHostException; +import java.util.ArrayList; import java.util.Collection; +import java.util.List; /** * @author: lucas @@ -18,7 +24,6 @@ import java.util.Collection; public class MPDController implements IMPDController { private MPD mpd; - private MPDPlayer player; public MPDController() { initMPD(); @@ -28,36 +33,100 @@ public class MPDController implements IMPDController { private void initMPD() { try { mpd = new MPD("dampfradio.raum.chaostreff-dortmund.de", 6600); - player = mpd.getMPDPlayer(); - - MPDDatabase database = mpd.getMPDDatabase(); - - Collection bla = database.findTitle(""); + } catch (UnknownHostException e) { Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + + + @Override + public void playSong(final String artist, final String title) { + if(mpd != null) { + + Runnable r = new Runnable() { + @Override + public void run() { + addToPlayListIfNeeded(artist, title); + + try { + + MPDPlaylist playlist = mpd.getMPDPlaylist(); + + for(MPDSong song: playlist.getSongList()) { + + if(song.getArtist() != null && song.getTitle() != null) { + if(song.getArtist().getName().toLowerCase().equals(artist.toLowerCase()) && + song.getTitle().toLowerCase().equals(title.toLowerCase())) { + + MPDPlayer player = mpd.getMPDPlayer(); + player.stop(); + player.playId(song); + break; + } + } + } + + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlayerException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + }; + + new Thread(r).start(); + } + } + + private void addToPlayListIfNeeded(final String artist, final String title) { + MPDDatabase db = mpd.getMPDDatabase(); + MPDPlaylist playlist = mpd.getMPDPlaylist(); + + try { + List tracks = new ArrayList(db.findArtist(artist)); + + for(MPDSong song: tracks) { + if(song.getName() != null && + song.getName().toLowerCase().contains(title.toLowerCase())) { + + if(!playlist.getSongList().contains(song)) { + playlist.addSong(song); + } + break; + } + } + } catch (MPDConnectionException e) { Logger.sLog("MPD error: " + e.getMessage()); } catch (MPDDatabaseException e) { Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlaylistException e) { + Logger.sLog("MPD error: " + e.getMessage()); } } - @Override - public void playSong(String artist, String title) { - if(mpd != null && player != null) { + public void setVolume(final int volume) { + if(mpd != null) { - // MPDSong finden in der DB - // dann abspielen - - } - } - - @Override - public void setVolume(int volume) { - if(mpd != null && player != null) { + Runnable r = new Runnable() { + @Override + public void run() { + try { + mpd.getMPDPlayer().setVolume(volume); + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDPlayerException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + }; + new Thread(r).start(); } } }