diff --git a/.idea/artifacts/crashteststeuerung_jar.xml b/.idea/artifacts/crashteststeuerung_jar.xml index c7465a8..a91aff4 100644 --- a/.idea/artifacts/crashteststeuerung_jar.xml +++ b/.idea/artifacts/crashteststeuerung_jar.xml @@ -13,6 +13,7 @@ + \ No newline at end of file diff --git a/.idea/libraries/commons_codec_1_41.xml b/.idea/libraries/commons_codec_1_41.xml new file mode 100644 index 0000000..227c388 --- /dev/null +++ b/.idea/libraries/commons_codec_1_41.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/jerklib.xml b/.idea/libraries/jerklib.xml new file mode 100644 index 0000000..2090b94 --- /dev/null +++ b/.idea/libraries/jerklib.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/crashteststeuerung.iml b/crashteststeuerung.iml index 2ab6dea..4e2f0f8 100644 --- a/crashteststeuerung.iml +++ b/crashteststeuerung.iml @@ -9,6 +9,8 @@ + + diff --git a/libs/jerklib.jar b/libs/jerklib.jar new file mode 100644 index 0000000..ac29b29 Binary files /dev/null and b/libs/jerklib.jar differ diff --git a/src/de/ctdo/crashtest/BuntiClient.java b/src/de/ctdo/crashtest/BuntiClient.java deleted file mode 100644 index a916284..0000000 --- a/src/de/ctdo/crashtest/BuntiClient.java +++ /dev/null @@ -1,104 +0,0 @@ -package de.ctdo.crashtest; - -import org.apache.http.HttpResponse; -import org.apache.http.client.ClientProtocolException; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.DefaultHttpClient; - -import java.io.*; -import java.net.Socket; - -/** - * User: lpless - * Date: 10.05.12 - * Time: 11:03 - */ -public class BuntiClient { - private String baseAddress; - private HttpClient client = new DefaultHttpClient(); - - public BuntiClient(String server, int port) { - baseAddress = "http://" + server + ":" + port + "/"; - } - - public void setPar56(int id, int red, int green, int blue) { - try { - HttpPost post = new HttpPost(baseAddress + "/control/devices"); - post.addHeader("Content-Type", "application/json"); - - StringEntity entity = new StringEntity( - "{ \"timeStamp\": 0, \"updates\": [ {\"deviceId\": "+id+", \"options\": { \"red\": "+ - red+", \"green\": "+green+", \"blue\": "+blue+" } } ] }" , - "UTF-8"); - - post.setEntity(entity); - - HttpResponse response = client.execute(post); - System.out.println(response); - - post.abort(); - - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } catch (ClientProtocolException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - public void setLampel(boolean red, boolean yellow, boolean green) { - int value = 0; - - if( green ) value |= 0x01; - if( yellow ) value |= 0x02; - if( red ) value |= 0x04; - - try { - Socket client = new Socket("lampel.ctdo.de", 2701); - client.setSoTimeout(2000); - - - DataOutputStream outToServer = new DataOutputStream(client.getOutputStream()); - BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream())); - - - outToServer.writeBytes("io set port 2 " + Integer.toHexString(value) + '\n'); - - String result = inFromServer.readLine(); - - client.close(); - } catch (IOException e) { - e.printStackTrace(); - } - -/* try { - HttpPost post = new HttpPost(baseAddress + "/control/devices"); - post.addHeader("Content-Type", "application/json"); - - StringEntity entity = new StringEntity( - "{ \"timeStamp\": 0, \"updates\": [ {\"deviceId\": 4, \"options\": { \"red\": "+ - red+", \"green\": "+green+", \"yellow\": "+yellow+" } } ] }" , - "UTF-8"); - - post.setEntity(entity); - - HttpResponse response = client.execute(post); - System.out.println(response); - - post.abort(); - - - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } catch (ClientProtocolException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - }*/ - } - -} diff --git a/src/de/ctdo/crashtest/Statemachine.java b/src/de/ctdo/crashtest/Statemachine.java deleted file mode 100644 index ff7137f..0000000 --- a/src/de/ctdo/crashtest/Statemachine.java +++ /dev/null @@ -1,173 +0,0 @@ -package de.ctdo.crashtest; - -import java.util.ArrayList; -import java.util.List; - -public class Statemachine { - private final char BLUE_BUTTON = 'E'; - private final char LIGHT_BARRIER = 'F'; - private final char TABLE_ONE = 'G'; - private final char TABLE_TWO = 'H'; - private final char TABLE_THREE = 'I'; - private final char ROKET_INPUT = 'B'; - private long lastHandleInput = 0; - private final List stateChangeListenerList = new ArrayList(); - private int stateChangeCounter = 0; - - public void addStateChangedListener(StateChangeListener listener) { - stateChangeListenerList.add(listener); - } - - public enum state { - IDLE, - ENTERED_ROOM, - TABLE_GAME_ONE, - TABLE_GAME_TWO, - TABLE_GAME_THREE, - TABLE_GAME_FOUR, - TABLE_GAME_FIVE, - TABLE_GAME_SIX, - TABLE_GAME_SEVEN, - TABLE_FINISH, - ROKET_DONE - } - - private state currentState = state.IDLE; - - - public state getCurrentState() { - return currentState; - } - - public int getStateChangeCounter() { - return stateChangeCounter; - } - - public void reset() { - stateChangeCounter = 0; - currentState = state.IDLE; - onStateChanged(); - } - - public void handleInput(char input) { - if(System.currentTimeMillis() - lastHandleInput < 200 ) return; - - state newState = getNewState(input); - - if( newState != currentState ) { - stateChangeCounter++; - System.out.println("newState = " + newState); - - workForState(newState); - - - currentState = newState; - onStateChanged(); - } - - - lastHandleInput = System.currentTimeMillis(); - } - - private void onStateChanged() { - for(StateChangeListener listener: stateChangeListenerList) { - listener.stateChanged(currentState); - } - } - - private void workForState(state newState) { - switch (newState) { - case IDLE: - break; - case ENTERED_ROOM: - - - break; - case TABLE_GAME_ONE: - break; - case TABLE_GAME_TWO: - break; - case TABLE_GAME_THREE: - break; - case TABLE_GAME_FOUR: - break; - case TABLE_GAME_FIVE: - break; - case TABLE_GAME_SIX: - break; - case TABLE_GAME_SEVEN: - break; - } - } - - private state getNewState(char input) { - state retVal = currentState; - - switch (currentState) { - case IDLE: - if(input == LIGHT_BARRIER) { - retVal = state.ENTERED_ROOM; - } - break; - case ENTERED_ROOM: - if(input == TABLE_ONE) { - retVal = state.TABLE_GAME_ONE; - } - break; - case TABLE_GAME_ONE: - if(input == TABLE_TWO) { - retVal = state.TABLE_GAME_TWO; - } - break; - case TABLE_GAME_TWO: - if(input == TABLE_THREE) { - retVal = state.TABLE_GAME_THREE; - } else if (input == TABLE_ONE) { - retVal = state.TABLE_GAME_ONE; - } - break; - case TABLE_GAME_THREE: - if(input == TABLE_TWO) { - retVal = state.TABLE_GAME_FOUR; - } else if (input == TABLE_ONE) { - retVal = state.TABLE_GAME_ONE; - } - break; - case TABLE_GAME_FOUR: - if(input == TABLE_THREE) { - retVal = state.TABLE_GAME_FIVE; - } else if (input == TABLE_ONE) { - retVal = state.TABLE_GAME_ONE; - } - break; - case TABLE_GAME_FIVE: - if(input == TABLE_ONE) { - retVal = state.TABLE_GAME_SIX; - } else if (input == TABLE_TWO) { - retVal = state.TABLE_GAME_ONE; - } - break; - case TABLE_GAME_SIX: - if(input == TABLE_THREE) { - retVal = state.TABLE_GAME_SEVEN; - } else if (input == TABLE_TWO) { - retVal = state.TABLE_GAME_ONE; - } - break; - case TABLE_GAME_SEVEN: - if(input == BLUE_BUTTON) { - retVal = state.TABLE_FINISH; - } - break; - case TABLE_FINISH: - if(input == ROKET_INPUT) { - retVal = state.ROKET_DONE; - } - - - } - - - return retVal; - } -} diff --git a/src/de/ctdo/crashtest/Steuerung.java b/src/de/ctdo/crashtest/Steuerung.java new file mode 100644 index 0000000..960c7a8 --- /dev/null +++ b/src/de/ctdo/crashtest/Steuerung.java @@ -0,0 +1,29 @@ +package de.ctdo.crashtest; + + +import de.ctdo.crashtest.game.TheGame; +import de.ctdo.crashtest.gui.IGuiControl; +import de.ctdo.crashtest.gui.MainGui; + +public class Steuerung { + + private TheGame game; + private IGuiControl gui; + + + public static void main(String args[]) { + + new Steuerung(); + + } + + public Steuerung() { + gui = new MainGui(); + + game = new TheGame(gui); + + + } + + +} diff --git a/src/de/ctdo/crashtest/SteuerungFrame.java b/src/de/ctdo/crashtest/SteuerungFrame.java deleted file mode 100644 index f73adda..0000000 --- a/src/de/ctdo/crashtest/SteuerungFrame.java +++ /dev/null @@ -1,175 +0,0 @@ -package de.ctdo.crashtest; - -import org.bff.javampd.MPD; -import org.bff.javampd.MPDPlayer; -import org.bff.javampd.exception.MPDConnectionException; -import org.bff.javampd.exception.MPDResponseException; - -import javax.swing.*; -import java.awt.*; -import java.awt.event.*; -import java.net.UnknownHostException; -import java.util.Random; - -public class SteuerungFrame extends JFrame implements StateChangeListener { - private JPanel pnlRoot; - private JLabel lblState; - private char lastKey = ' '; - private MPD mpd; - private MPDPlayer player; - - - Statemachine machine = new Statemachine(); - BuntiClient bunti = new BuntiClient("bunti.ctdo.de", 8080); - - public static void main(String args[]) { - new SteuerungFrame(); - } - - public SteuerungFrame() { - //setType(Type.UTILITY); - setBackground(Color.black); - setBounds(200,200, 400, 200); - - machine.addStateChangedListener(this); - - initGui(); - - setDefaultCloseOperation(EXIT_ON_CLOSE); - setVisible(true); - - machine.reset(); - - try { - mpd = new MPD("dampfradio.raum.chaostreff-dortmund.de", 6600); - player = mpd.getMPDPlayer(); - - } catch (UnknownHostException e) { - e.printStackTrace(); - return; - } catch (MPDConnectionException e) { - e.printStackTrace(); - return; - } - - addKeyListener(new KeyAdapter() { - @Override - public void keyTyped(KeyEvent e) { - lastKey = e.getKeyChar(); - - if (lastKey == KeyEvent.VK_F) { - Random r = new Random(); - setLEDs(r.nextInt(255), r.nextInt(255),r.nextInt(255)); - } - - if (lastKey == KeyEvent.VK_1) { - machine.reset(); - } else { - machine.handleInput(e.getKeyChar()); - } - - updateGui(); - } - }); - - addWindowStateListener(new WindowAdapter() { - @Override - public void windowClosing(WindowEvent e) { - try { - if(mpd != null) mpd.close(); - } catch (MPDConnectionException e1) { - e1.printStackTrace(); - } catch (MPDResponseException e1) { - e1.printStackTrace(); - } - } - }); - } - - @Override - public void stateChanged(Statemachine.state newState) { - updateGui(); - - switch (newState) { - case IDLE: - setLEDs(0x40,0,0xff); - bunti.setLampel(false,false,false); - break; - case ENTERED_ROOM: - bunti.setLampel(false,false,false); - setLEDs(255,0,100); - - // start von Mo Do - Eins, Zwei Polizei - - break; - case TABLE_GAME_ONE: - bunti.setLampel(true,false,false); - setLEDs(255,0,100); - break; - case TABLE_GAME_TWO: - bunti.setLampel(false,true,false); - setLEDs(255,0,100); - break; - case TABLE_GAME_THREE: - bunti.setLampel(false,true,false); - setLEDs(255,35,0); - - - - - break; - case TABLE_GAME_FOUR: - bunti.setLampel(false,true,false); - setLEDs(255,55,0); - break; - case TABLE_GAME_FIVE: - bunti.setLampel(false,true,false); - setLEDs(255,75,0); - break; - case TABLE_GAME_SIX: - bunti.setLampel(false,true,false); - setLEDs(255,100,0); - break; - case TABLE_GAME_SEVEN: - bunti.setLampel(false,false,true); - setLEDs(255,100,0); - break; - case TABLE_FINISH: // und roket muss starten - bunti.setLampel(false,false,true); - setLEDs(0, 255, 0); - break; - case ROKET_DONE: - - - break; - } - - } - - - private void setLEDs(int red, int green, int blue) { - bunti.setPar56(0,red,green,blue); - bunti.setPar56(1,red,green,blue); - bunti.setPar56(2,red,green,blue); - bunti.setPar56(3,red,green,blue); - } - - private void initGui() { - Container pane = getContentPane(); - - pnlRoot = new JPanel(new FlowLayout()); - lblState = new JLabel("", JLabel.LEFT); - pnlRoot.add(lblState); - - pane.add(pnlRoot); - - updateGui(); - } - - private void updateGui() { - lblState.setText("LastKey: " + lastKey + "
CurrentState: " + - machine.getCurrentState() + "
ChangeCounter: " + - machine.getStateChangeCounter() + ""); - - } -} diff --git a/src/de/ctdo/crashtest/TestClass.java b/src/de/ctdo/crashtest/TestClass.java new file mode 100644 index 0000000..878f2bf --- /dev/null +++ b/src/de/ctdo/crashtest/TestClass.java @@ -0,0 +1,19 @@ +package de.ctdo.crashtest; + +import de.ctdo.crashtest.mpd.IMPDController; +import de.ctdo.crashtest.mpd.MPDController; + + + +public class TestClass { + + + + public static void main(String args[]) throws InterruptedException { + + + + + } + +} diff --git a/src/de/ctdo/crashtest/domotics/BuntiClient.java b/src/de/ctdo/crashtest/domotics/BuntiClient.java new file mode 100644 index 0000000..c8f1776 --- /dev/null +++ b/src/de/ctdo/crashtest/domotics/BuntiClient.java @@ -0,0 +1,127 @@ +package de.ctdo.crashtest.domotics; + +import de.ctdo.crashtest.log.Logger; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.DefaultHttpClient; + +import java.io.*; +import java.net.Socket; + +public class BuntiClient implements IBuntiClient { + private String baseAddress; + private final HttpClient client = new DefaultHttpClient(); + + public BuntiClient(String server, int port) { + baseAddress = "http://" + server + ":" + port + "/"; + } + + @Override + public void setPar56(final int red, final int green, final int blue) { + + Runnable r = new Runnable() { + @Override + public void run() { + try { + HttpPost post = new HttpPost(baseAddress + "/control/devices"); + post.addHeader("Content-Type", "application/json"); + + StringBuilder jsonString = new StringBuilder(); + + jsonString.append("{ \"timeStamp\": 0, \"updates\": [ "); + + for(int i = 0; i< 4; i++) { + jsonString.append(" {\"deviceId\": "); + jsonString.append(i); + jsonString.append(", \"options\": { \"red\": "); + jsonString.append(red); + jsonString.append(", \"green\": "); + jsonString.append(green); + jsonString.append(", \"blue\": "); + jsonString.append(blue); + jsonString.append(" } } "); + if(i!=3) jsonString.append(","); + } + + jsonString.append("] }"); + + StringEntity entity = new StringEntity( jsonString.toString(), "UTF-8"); + post.setEntity(entity); + + client.execute(post); + post.abort(); + + } catch (UnsupportedEncodingException e) { + Logger.sLog("bunti error: " + e.getMessage()); + } catch (ClientProtocolException e) { + Logger.sLog("bunti error: " + e.getMessage()); + } catch (IOException e) { + Logger.sLog("bunti error: " + e.getMessage()); + } + } + }; + + new Thread(r).start(); + } + + @Override + public void setLampel(final boolean red, final boolean yellow, final boolean green) { + + Runnable r = new Runnable() { + @Override + public void run() { + int value = 0; + if( green ) value |= 0x01; + if( yellow ) value |= 0x02; + if( red ) value |= 0x04; + + try { + Socket client = new Socket("lampel.ctdo.de", 2701); + client.setSoTimeout(800); + + DataOutputStream outToServer = new DataOutputStream(client.getOutputStream()); + BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream())); + + + outToServer.writeBytes("io set port 2 " + Integer.toHexString(value) + '\n'); + + inFromServer.readLine(); + + client.close(); + } catch (IOException e) { + Logger.sLog("lampel error: " + e.getMessage()); + } + } + }; + + + new Thread(r).start(); + +/* try { + HttpPost post = new HttpPost(baseAddress + "/control/devices"); + post.addHeader("Content-Type", "application/json"); + + StringEntity entity = new StringEntity( + "{ \"timeStamp\": 0, \"updates\": [ {\"deviceId\": 4, \"options\": { \"red\": "+ + red+", \"green\": "+green+", \"yellow\": "+yellow+" } } ] }" , + "UTF-8"); + + post.setEntity(entity); + + HttpResponse response = client.execute(post); + System.out.println(response); + + post.abort(); + + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } catch (ClientProtocolException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + }*/ + } + +} diff --git a/src/de/ctdo/crashtest/domotics/IBuntiClient.java b/src/de/ctdo/crashtest/domotics/IBuntiClient.java new file mode 100644 index 0000000..bff4f75 --- /dev/null +++ b/src/de/ctdo/crashtest/domotics/IBuntiClient.java @@ -0,0 +1,11 @@ +package de.ctdo.crashtest.domotics; + +/** + * @author: lucas + * @date: 01.06.12 14:25 + */ +public interface IBuntiClient { + void setPar56(final int red, final int green, final int blue); + + void setLampel(final boolean red, final boolean yellow, final boolean green); +} diff --git a/src/de/ctdo/crashtest/domotics/IRelaisboard.java b/src/de/ctdo/crashtest/domotics/IRelaisboard.java new file mode 100644 index 0000000..a4af876 --- /dev/null +++ b/src/de/ctdo/crashtest/domotics/IRelaisboard.java @@ -0,0 +1,7 @@ +package de.ctdo.crashtest.domotics; + +public interface IRelaisboard { + + void setRelais(int relai, boolean state); + +} diff --git a/src/de/ctdo/crashtest/domotics/Relaisboard.java b/src/de/ctdo/crashtest/domotics/Relaisboard.java new file mode 100644 index 0000000..a6391b9 --- /dev/null +++ b/src/de/ctdo/crashtest/domotics/Relaisboard.java @@ -0,0 +1,12 @@ +package de.ctdo.crashtest.domotics; + + + +public class Relaisboard implements IRelaisboard { + + + @Override + public void setRelais(int relai, boolean state) { + + } +} diff --git a/src/de/ctdo/crashtest/game/IStatemachine.java b/src/de/ctdo/crashtest/game/IStatemachine.java new file mode 100644 index 0000000..ceb5f3d --- /dev/null +++ b/src/de/ctdo/crashtest/game/IStatemachine.java @@ -0,0 +1,16 @@ +package de.ctdo.crashtest.game; + +public interface IStatemachine { + void addListener(StatemachineListener listener); + void reset(); + Statemachine.state getCurrentState(); + void setNewState(Statemachine.state newState); + int getStateChangeCounter(); + void handleInput(char input); + int getTimerSecondsLast(); + + + void startTimer(int seconds); + void stopTimer(); + void pauseTimer(boolean pause); +} diff --git a/src/de/ctdo/crashtest/game/Statemachine.java b/src/de/ctdo/crashtest/game/Statemachine.java new file mode 100644 index 0000000..d2d72a8 --- /dev/null +++ b/src/de/ctdo/crashtest/game/Statemachine.java @@ -0,0 +1,240 @@ +package de.ctdo.crashtest.game; + +import de.ctdo.crashtest.log.Logger; + +import java.util.ArrayList; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +public class Statemachine implements IStatemachine { + public enum state { + IDLE, + ENTERED_ROOM, + TABLE_GAME_ONE, + TABLE_GAME_TWO, + TABLE_GAME_THREE, + TABLE_GAME_FOUR, + TABLE_GAME_FIVE, + TABLE_GAME_SIX, + TABLE_GAME_DONE, + ROKET_STARTED, + ROKET_DONE + } + + private final char RESET = '1'; + private final char BLUE_BUTTON = 'E'; + private final char LIGHT_BARRIER = 'F'; + private final char TABLE_ONE = 'G'; + private final char TABLE_TWO = 'H'; + private final char TABLE_THREE = 'I'; + private final char ROKET_INPUT = 'B'; + private final List statemachineListenerList; + + private Timer timer; + + private long lastHandleInput; + private int stateChangeCounter; + private state currentState; + private int timertSecondsLast; + + + public Statemachine() { + currentState = state.IDLE; + statemachineListenerList = new ArrayList(); + } + + @Override + public void addListener(StatemachineListener listener) { + statemachineListenerList.add(listener); + } + + @Override + public state getCurrentState() { + return currentState; + } + + @Override + public void setNewState(state newState) { + currentState = newState; + stateChangeCounter++; + onStateChanged(); + } + + @Override + public int getStateChangeCounter() { + return stateChangeCounter; + } + + @Override + public void reset() { + stateChangeCounter = 0; + currentState = state.IDLE; + stopTimer(); + onStateChanged(); + } + + @Override + public void handleInput(char input) { + if(System.currentTimeMillis() - lastHandleInput < 200 ) return; + + state newState = getNewState(input); + + if( newState != currentState ) { + stateChangeCounter++; + Logger.sLog("newState = " + newState); + + currentState = newState; + onStateChanged(); + } + + lastHandleInput = System.currentTimeMillis(); + } + + @Override + public int getTimerSecondsLast() { + return timertSecondsLast / 10; + } + + @Override + public void startTimer(int seconds) { + Logger.sLog("starting timer"); + timertSecondsLast = seconds*10; + scheduleTimer(); + } + + @Override + public void stopTimer() { + Logger.sLog("stopping timer"); + if(timer != null) timer.cancel(); + timertSecondsLast = 0; + } + + @Override + public void pauseTimer(boolean pause) { + Logger.sLog("pausing timer: " + pause); + if(pause) { + if(timer != null) timer.cancel(); + } else { + scheduleTimer(); + } + } + + + /** + * notifies all listeners about the changed state + */ + private void onStateChanged() { + for(StatemachineListener listener: statemachineListenerList) { + listener.stateChanged(currentState); + } + } + + private void onTimerTick() { + for(StatemachineListener listener: statemachineListenerList) { + listener.timerTick(timertSecondsLast); + } + } + + /** + * Calculates the new game state from the current state and new input button + * @param input the char from the input + * @return returns the new state + */ + private state getNewState(char input) { + state retVal = currentState; + + if(input == RESET) { + retVal = state.IDLE; + } else { + + switch (currentState) { + case IDLE: + if(input == LIGHT_BARRIER) { + retVal = state.ENTERED_ROOM; + } + break; + case ENTERED_ROOM: + if(input == TABLE_ONE) { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_ONE: + if(input == TABLE_TWO) { + retVal = state.TABLE_GAME_TWO; + } + break; + case TABLE_GAME_TWO: + if(input == TABLE_THREE) { + retVal = state.TABLE_GAME_THREE; + } else if (input == TABLE_ONE) { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_THREE: + if(input == TABLE_TWO) { + retVal = state.TABLE_GAME_FOUR; + } else if (input == TABLE_ONE) { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_FOUR: + if(input == TABLE_THREE) { + retVal = state.TABLE_GAME_FIVE; + } else if (input == TABLE_ONE) { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_FIVE: + if(input == TABLE_ONE) { + retVal = state.TABLE_GAME_SIX; + } else if (input == TABLE_TWO) { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_SIX: + if(input == TABLE_THREE) { + retVal = state.TABLE_GAME_DONE; + } else if (input == TABLE_TWO) { + retVal = state.TABLE_GAME_ONE; + } + break; + case TABLE_GAME_DONE: + if(input == BLUE_BUTTON) { + retVal = state.ROKET_STARTED; + } + break; + case ROKET_STARTED: + if(input == ROKET_INPUT) { + retVal = state.ROKET_DONE; + } + } + } + + return retVal; + } + + + private void scheduleTimer() { + if(timer != null) timer.cancel(); + + timer = new Timer(); + + TimerTask timerTask = new TimerTask() { + @Override + public void run() { + timertSecondsLast--; + + onTimerTick(); + + if(timertSecondsLast <= 0) { + if(timer != null) timer.cancel(); + } + } + }; + timer.scheduleAtFixedRate(timerTask, 100, 100); + } + + + +} diff --git a/src/de/ctdo/crashtest/StateChangeListener.java b/src/de/ctdo/crashtest/game/StatemachineListener.java similarity index 50% rename from src/de/ctdo/crashtest/StateChangeListener.java rename to src/de/ctdo/crashtest/game/StatemachineListener.java index f6c6ad3..bb8e3e4 100644 --- a/src/de/ctdo/crashtest/StateChangeListener.java +++ b/src/de/ctdo/crashtest/game/StatemachineListener.java @@ -1,10 +1,11 @@ -package de.ctdo.crashtest; +package de.ctdo.crashtest.game; /** * User: lpless * Date: 10.05.12 * Time: 13:37 */ -public interface StateChangeListener { +public interface StatemachineListener { void stateChanged(Statemachine.state newState); + void timerTick(int tsecondsLeft); } diff --git a/src/de/ctdo/crashtest/game/TheGame.java b/src/de/ctdo/crashtest/game/TheGame.java new file mode 100644 index 0000000..a0f9e54 --- /dev/null +++ b/src/de/ctdo/crashtest/game/TheGame.java @@ -0,0 +1,312 @@ +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 StatemachineListener, GuiEventListener, IRCEventListener { + private IGuiControl guiControl; + private IIrcClient ircClient; + private IStatemachine machine; + private IBuntiClient bunti; + private IMPDController mpdController; + + public TheGame(IGuiControl guiControl) { + this.guiControl = guiControl; + + this.ircClient = new IrcClient("crashtest", "#crashtest","irc.hackint.eu"); + this.bunti = new BuntiClient("bunti.ctdo.de", 8080); + this.mpdController = new MPDController("dampfradio.raum.ctdo.de"); + this.machine = new Statemachine(); + + initGame(); + } + + private void initGame() { + guiControl.addListener(this); + ircClient.addListener(this); + machine.addListener(this); + + machine.reset(); + } + + + /** + * Event listener for state change events from statemachine + * @param newState the new game state from statemachine + */ + @Override + public void stateChanged(Statemachine.state newState) { + + ircClient.say("New State: " + newState); + + switch (newState) { + case IDLE: + machine.stopTimer(); + + mpdController.setVolume(45); + mpdController.playSong("start", "mix"); + + + bunti.setPar56(0,0,0); + bunti.setLampel(false,false,false); + + guiControl.showCountDown(false); + break; + case ENTERED_ROOM: + mpdController.setVolume(70); + mpdController.playSong("tidirium", "welcome"); + + + bunti.setLampel(false,false,false); + bunti.setPar56(20,0,100); + + guiControl.showCountDown(true); + + machine.startTimer(60*8); + break; + case TABLE_GAME_ONE: + mpdController.setVolume(70); + mpdController.playSong("K2", "Der Berg Ruft"); + + bunti.setLampel(true,false,false); + bunti.setPar56(255,0,100); + guiControl.showCountDown(true); + break; + case TABLE_GAME_TWO: + bunti.setLampel(false,true,false); + bunti.setPar56(255,0,100); + + guiControl.showCountDown(true); + break; + case TABLE_GAME_THREE: + bunti.setLampel(false,true,false); + bunti.setPar56(255,35,0); + + guiControl.showCountDown(true); + break; + case TABLE_GAME_FOUR: + mpdController.setVolume(60); + mpdController.playSong("Mo-Do","9 Eins Zwei Polizei"); + + + bunti.setLampel(false,true,false); + bunti.setPar56(255,55,0); + + guiControl.showCountDown(true); + break; + case TABLE_GAME_FIVE: + bunti.setLampel(false,true,false); + bunti.setPar56(255,75,0); + + guiControl.showCountDown(true); + break; + case TABLE_GAME_SIX: + mpdController.setVolume(60); + mpdController.playSong("Zlatko & Jürgen","Großer Bruder"); + + + bunti.setLampel(false,true,false); + bunti.setPar56(255,100,0); + + guiControl.showCountDown(true); + break; + case TABLE_GAME_DONE: + bunti.setLampel(false,false,true); + bunti.setPar56(255,100,0); + + guiControl.showCountDown(true); + machine.pauseTimer(true); + sayScore(); + break; + case ROKET_STARTED: + mpdController.setVolume(50); + mpdController.playSong("The Underdog Project", "Summer Jam"); + + + bunti.setLampel(false,false,true); + bunti.setPar56(0, 255, 0); + + ircClient.say("table game complete, r0kets now"); + + guiControl.showCountDown(true); + machine.startTimer(7*60); + break; + case ROKET_DONE: + mpdController.setVolume(50); + mpdController.playSong("Coldplay", "Amsterdam"); + + + bunti.setLampel(true,true,true); + bunti.setPar56(255, 196, 0); + + guiControl.showCountDown(true); + machine.pauseTimer(true); + break; + } + + } + + @Override + public void timerTick(int tsecondsLeft) { + guiControl.setCountDown(tsecondsLeft); + if(tsecondsLeft == 0) ircClient.say("timer expired"); + } + + + /** + * Event listener for keyPress events from the GUI + * @param key the pressed key + */ + @Override + public void keyPress(char key) { + machine.handleInput(key); + guiControl.setExtra("btn: " + key); + + } + + /** + * Event lister for window closing event from the GUI + */ + @Override + public void windowClosing() { + machine.reset(); + bunti.setPar56(0xff,0xff,0xff); + ircClient.say("bye"); + } + + /** + * Event listener for IRC Messages + * @param message the message from the IRC user + */ + @Override + public void handleMessage(final String message) { + if(message.startsWith("help")) { + handleHelpCommand(message); + } else if(message.equals("reset")) { + machine.reset(); + } else if(message.startsWith("state")) { + handleStateCommand(message); + } else if(message.startsWith("timer")) { + handleTimerCommand(message); + } else if(message.startsWith("score")) { + sayScore(); + } else if(message.startsWith("wall")) { + guiControl.setWall(message.substring("wall".length()).trim()); + } else if(message.startsWith("extra")) { + guiControl.setExtra(message.substring("extra".length()).trim()); + } else { + ircClient.say("y u no use valid command?"); + } + } + + private void handleTimerCommand(final String message) { + String params = message.substring("timer".length()).trim().toLowerCase(); + + if(params.startsWith("start")) { + String timeStr = params.substring("start".length()).trim(); + + if(timeStr.length() > 0) { + try { + int time = Integer.parseInt(timeStr); + + guiControl.showCountDown(true); + machine.startTimer(time); + ircClient.say("got it, starting with " + time + " seconds"); + } + catch (NumberFormatException e) { + ircClient.say("your number looks a bit odd..."); + } + } else { + ircClient.say("invalid parameter"); + } + } else if(params.startsWith("stop")) { + machine.stopTimer(); + guiControl.showCountDown(false); + ircClient.say("timer stopped"); + } else if(params.startsWith("pause")) { + machine.pauseTimer(true); + } else if(params.startsWith("resume")) { + machine.pauseTimer(false); + } + } + + private void handleStateCommand(final String message) { + if(message.equals("state")) { + ircClient.say(machine.getCurrentState().name()); + } else { + String params = message.substring("state".length()).trim().toLowerCase(); + + Boolean ok = false; + for(Statemachine.state st: Statemachine.state.values()) { + String stateName = st.name().toLowerCase(); + if(stateName.equals(params)) { + ok = true; + machine.setNewState(st); + ircClient.say("consider it done, sir!"); + break; + } + } + + if(!ok) ircClient.say("ehm, impossibruu!"); + } + } + + private void handleHelpCommand(final String message) { + if(message.equals("help")) { + ircClient.say("commands: help, reset, state, timer, wall, extra, score"); + } else if(message.contains("reset")) { + ircClient.say("resets the game to IDLE"); + } else if(message.contains("state")) { + ircClient.say("get or set game state"); + ircClient.say("> state "); + ircClient.say("valid states: "); + + int counter = 0; + StringBuilder sb = new StringBuilder(); + for(Statemachine.state st: Statemachine.state.values()) { + sb.append(st.name()); + sb.append(" "); + if(++counter == 4) { + counter = 0; + ircClient.say(sb.toString()); + sb.setLength(0); + } + } + + if(sb.length() > 0) { + ircClient.say(sb.toString()); + } + } else if(message.contains("timer")) { + ircClient.say("control timer. commands are: start, stop, pause, resume."); + ircClient.say("for start give lenght in seconds as parameter"); + ircClient.say("e.g.: timer start 120"); + } else if(message.contains("wall")) { + ircClient.say("set text message on the screen"); + } else if(message.contains("extra")) { + ircClient.say("set small extra message on the screen"); + } else if(message.contains("score")) { + ircClient.say("i will tell you the current game score"); + } else { + ircClient.say("dafuq?"); + } + + } + + + private void sayScore() { + ircClient.say("stateChangeCounter: " + machine.getStateChangeCounter()); +// ircClient.say("timerlast: " + machine.getTimerSecondsLast()); + + int seconds = machine.getTimerSecondsLast(); + int mins = seconds / 60; + int secs = seconds % 60; + ircClient.say(" " + mins + ":" + secs + ""); + + } + + + +} diff --git a/src/de/ctdo/crashtest/gui/GuiEventListener.java b/src/de/ctdo/crashtest/gui/GuiEventListener.java new file mode 100644 index 0000000..c7bede4 --- /dev/null +++ b/src/de/ctdo/crashtest/gui/GuiEventListener.java @@ -0,0 +1,14 @@ +package de.ctdo.crashtest.gui; + +import java.awt.event.KeyEvent; + +/** + * @author: lucas + * @date: 01.06.12 10:29 + */ +public interface GuiEventListener { + + void keyPress(final char key); + void windowClosing(); + +} diff --git a/src/de/ctdo/crashtest/gui/IGuiControl.java b/src/de/ctdo/crashtest/gui/IGuiControl.java new file mode 100644 index 0000000..6ba4aec --- /dev/null +++ b/src/de/ctdo/crashtest/gui/IGuiControl.java @@ -0,0 +1,17 @@ +package de.ctdo.crashtest.gui; + +/** + * @author: lucas + * @date: 01.06.12 10:13 + */ +public interface IGuiControl { + + void setWall(final String message); + void setExtra(final String text); + void setCountDown(final int tseconds); + void showCountDown(final Boolean show); + + void addListener(final GuiEventListener listener); + + +} diff --git a/src/de/ctdo/crashtest/gui/MainGui.java b/src/de/ctdo/crashtest/gui/MainGui.java new file mode 100644 index 0000000..389f000 --- /dev/null +++ b/src/de/ctdo/crashtest/gui/MainGui.java @@ -0,0 +1,169 @@ +package de.ctdo.crashtest.gui; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import java.util.List; + +public class MainGui extends JFrame implements IGuiControl { + private final JTextArea textWall = new JTextArea(); + private final JLabel countDown = new JLabel(); + private final JLabel extraField = new JLabel(); + private final JPanel lowerPanel = new JPanel(); + + private final List listenerList = new ArrayList(); + + public MainGui() { + initGui(); + setDefaultCloseOperation(EXIT_ON_CLOSE); + + setBounds(0,0, 1280, 1024); + setExtendedState(Frame.MAXIMIZED_BOTH); + setUndecorated(true); + + addKeyListener(new KeyAdapter() { + @Override + public void keyTyped(KeyEvent e) { + char lastKey = e.getKeyChar(); + + for(GuiEventListener listener: listenerList) { + listener.keyPress(lastKey); + } + } + }); + + addWindowStateListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + for(GuiEventListener listener: listenerList) { + listener.windowClosing(); + } + } + }); + + setVisible(true); + } + + private void setCountDownText(final int tseconds) { + int mins = tseconds / 600; + int secs = tseconds % 600 / 10 ; + int tsecs = tseconds % 9; + countDown.setText(" " + mins + ":" + secs + "." + tsecs); + + if(tseconds < 400) { + double percentile = ((tseconds-100.0)/300.0); + double red = 255.0 - percentile * 255.0; + double green = 255.0 - red; + double blue = percentile * 100.0; + +// System.out.println("red= " + red + " green=" + green + " blue="+blue); + + if(red > 255) red = 255; + if(red < 0) red = 0; + if(green > 255) green = 255; + if(green < 0) green = 0; + if(blue > 255) blue = 255; + if(blue < 0) blue = 0; + + Color fColor = new Color((int)red,(int)green, (int)blue); + countDown.setForeground(fColor); + } + else { + countDown.setForeground(new Color(0x00, 190, 100)); + } + + } + + private void initGui() { + Container container = getContentPane(); + container.setLayout(new BorderLayout()); + container.add(textWall, BorderLayout.NORTH); + container.add(lowerPanel, BorderLayout.SOUTH); + + lowerPanel.setLayout(new BorderLayout()); + lowerPanel.add(countDown, BorderLayout.WEST); + lowerPanel.add(extraField, BorderLayout.EAST); + + Font wallFont = new Font("Arcade Interlaced", Font.PLAIN, 66); + Font lowerFont = new Font("Arcade Interlaced", Font.PLAIN, 80); + Color fontColor = new Color(0x00, 190, 100); + + textWall.setFont(wallFont); + textWall.setForeground(fontColor); + textWall.setBackground(Color.BLACK); + textWall.setText("follow the white rabbit..."); + textWall.setLineWrap(true); + textWall.setWrapStyleWord(true); + textWall.setFocusable(false); + + countDown.setFont(lowerFont); + countDown.setHorizontalAlignment(SwingConstants.LEFT); + countDown.setForeground(fontColor); + extraField.setFont(lowerFont); + extraField.setHorizontalAlignment(SwingConstants.RIGHT); + extraField.setForeground(fontColor); + + lowerPanel.setBackground(Color.BLACK); + container.setBackground(Color.BLACK); + + countDown.setText(" 15:00.0"); + extraField.setText(" "); + } + + @Override + public void setWall(final String message) { + final Runnable runnable = new Runnable() { + @Override + public void run() { + textWall.setText(message); + } + }; + + SwingUtilities.invokeLater(runnable); + } + + @Override + public void setExtra(final String text) { + final Runnable runnable = new Runnable() { + @Override + public void run() { + extraField.setText(text + " "); + } + }; + + SwingUtilities.invokeLater(runnable); + } + + @Override + public void setCountDown(final int tseconds) { + final Runnable runnable = new Runnable() { + @Override + public void run() { + setCountDownText(tseconds); + } + }; + + SwingUtilities.invokeLater(runnable); + } + + @Override + public void showCountDown(final Boolean show) { + final Runnable runnable = new Runnable() { + @Override + public void run() { + countDown.setVisible(show); + } + }; + + SwingUtilities.invokeLater(runnable); + } + + @Override + public void addListener(final GuiEventListener listener) { + listenerList.add(listener); + } + + + +} diff --git a/src/de/ctdo/crashtest/irc/IIrcClient.java b/src/de/ctdo/crashtest/irc/IIrcClient.java new file mode 100644 index 0000000..ac10e1c --- /dev/null +++ b/src/de/ctdo/crashtest/irc/IIrcClient.java @@ -0,0 +1,10 @@ +package de.ctdo.crashtest.irc; + +/** + * @author: lucas + * @date: 01.06.12 10:05 + */ +public interface IIrcClient { + void say(String message); + void addListener(IRCEventListener listenerIRC); +} diff --git a/src/de/ctdo/crashtest/irc/IRCEventListener.java b/src/de/ctdo/crashtest/irc/IRCEventListener.java new file mode 100644 index 0000000..8fc9ad9 --- /dev/null +++ b/src/de/ctdo/crashtest/irc/IRCEventListener.java @@ -0,0 +1,10 @@ +package de.ctdo.crashtest.irc; + +/** + * @author: lucas + * @date: 01.06.12 11:49 + */ +public interface IRCEventListener { + + void handleMessage(final String message); +} diff --git a/src/de/ctdo/crashtest/irc/IrcClient.java b/src/de/ctdo/crashtest/irc/IrcClient.java new file mode 100644 index 0000000..7870a37 --- /dev/null +++ b/src/de/ctdo/crashtest/irc/IrcClient.java @@ -0,0 +1,81 @@ +package de.ctdo.crashtest.irc; + +import de.ctdo.crashtest.log.ILogger; +import de.ctdo.crashtest.log.Logger; +import jerklib.Channel; +import jerklib.ConnectionManager; +import jerklib.ProfileImpl; +import jerklib.Session; +import jerklib.events.ChannelMsgEvent; +import jerklib.events.ConnectionCompleteEvent; +import jerklib.events.IRCEvent; +import jerklib.events.JoinCompleteEvent; + +import java.util.ArrayList; +import java.util.List; + +/** + * IRC Client wrapper + * simplifies communication with the irc channel + */ +public class IrcClient implements IIrcClient, jerklib.events.listeners.IRCEventListener, ILogger { + private final List listenerListIRC = new ArrayList(); + private Session ircsession; + private final String channel; + + public IrcClient(String nick, String channel, String server) { + this.channel = channel; + ConnectionManager ircConnection = new ConnectionManager(new ProfileImpl(nick,nick, nick+2, nick+3)); + ircsession = ircConnection.requestConnection(server); + ircsession.addIRCEventListener(this); + } + + @Override + public void recieveEvent(IRCEvent ircEvent) { + + if(ircEvent instanceof ConnectionCompleteEvent) { + ircEvent.getSession().joinChannel(channel); + } + else if (ircEvent instanceof JoinCompleteEvent) { + JoinCompleteEvent jce = (JoinCompleteEvent) ircEvent; + jce.getChannel().say("hello master. what's your order?"); + + Logger logger = Logger.getInstance(); + logger.addLogger(this); + } + else if(ircEvent instanceof ChannelMsgEvent) { + ChannelMsgEvent cme = (ChannelMsgEvent)ircEvent; + + String message = cme.getMessage(); + final String nick = ircsession.getNick(); + + if(message.contains(nick)) { + message = message.substring(Math.max(nick.length(), message.indexOf(":")+1)); + message = message.trim(); + + for(IRCEventListener listenerIRC : listenerListIRC) { + listenerIRC.handleMessage(message); + } + } + } + } + + @Override + public void log(String message) { + say("LOG: " + message); + } + + @Override + public void say(String message) { + if(ircsession != null) { + for(Channel chan: ircsession.getChannels()) { + chan.say(message); + } + } + } + + @Override + public void addListener(final IRCEventListener listenerIRC) { + listenerListIRC.add(listenerIRC); + } +} diff --git a/src/de/ctdo/crashtest/log/ILogger.java b/src/de/ctdo/crashtest/log/ILogger.java new file mode 100644 index 0000000..177d06b --- /dev/null +++ b/src/de/ctdo/crashtest/log/ILogger.java @@ -0,0 +1,10 @@ +package de.ctdo.crashtest.log; + +/** + * @author: lucas + * @date: 01.06.12 16:46 + */ +public interface ILogger { + + void log(String message); +} diff --git a/src/de/ctdo/crashtest/log/Logger.java b/src/de/ctdo/crashtest/log/Logger.java new file mode 100644 index 0000000..b1f537d --- /dev/null +++ b/src/de/ctdo/crashtest/log/Logger.java @@ -0,0 +1,38 @@ +package de.ctdo.crashtest.log; + +import java.util.ArrayList; +import java.util.List; + +public class Logger { + private final static Logger instance = new Logger(); + private final List loggerList = new ArrayList(); + + + public void addLogger(ILogger listener) { + loggerList.add(listener); + } + + private Logger() { + + } + + public static Logger getInstance() { + return instance; + } + + public void log(String message) { + + System.out.println("LOG: " + message); + + for(ILogger logger: loggerList) { + synchronized (this) { + logger.log(message); + } + } + + } + + public static void sLog(String message) { + getInstance().log(message); + } +} diff --git a/src/de/ctdo/crashtest/mpd/IMPDController.java b/src/de/ctdo/crashtest/mpd/IMPDController.java new file mode 100644 index 0000000..275b72c --- /dev/null +++ b/src/de/ctdo/crashtest/mpd/IMPDController.java @@ -0,0 +1,10 @@ +package de.ctdo.crashtest.mpd; + +/** + * @author: lucas + * @date: 01.06.12 10:36 + */ +public interface IMPDController { + 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 new file mode 100644 index 0000000..100675b --- /dev/null +++ b/src/de/ctdo/crashtest/mpd/MPDController.java @@ -0,0 +1,130 @@ +package de.ctdo.crashtest.mpd; + +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.List; + +/** + * A MPD abstraction to the org.bff.javampd library + */ +public class MPDController implements IMPDController { + private MPD mpd; + + public MPDController(String host) { + try { + mpd = new MPD(host, 6600); + } catch (UnknownHostException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } catch (MPDConnectionException e) { + Logger.sLog("MPD error: " + e.getMessage()); + } + } + + /** + * Play a song, defined by artist and track title + * Song gets added to current playlist and is played. + * @param artist Artist of the track to play + * @param title Title of the track to play + */ + @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()); + } + } + + /** + * Sets the current mpd volume + * @param volume the volume in percent (0-100) + */ + @Override + public void setVolume(final int volume) { + if(mpd != 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(); + } + } +}