This commit is contained in:
Lucas Pleß 2012-06-14 11:40:08 +02:00
parent dc6a1f318c
commit 58a8028bd5
18 changed files with 74 additions and 217 deletions

View File

@ -1,29 +1,10 @@
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();
new TheGame(new MainGui());
}
public Steuerung() {
gui = new MainGui();
game = new TheGame(gui);
}
}

View File

@ -1,49 +1,28 @@
package de.ctdo.crashtest;
import de.ctdo.crashtest.domotics.BuntiClient;
import de.ctdo.crashtest.domotics.IBuntiClient;
import de.ctdo.crashtest.domotics.IRelaisboard;
import de.ctdo.crashtest.domotics.Relaisboard;
import de.ctdo.crashtest.mpd.IMPDController;
import de.ctdo.crashtest.mpd.MPDController;
import de.ctdo.crashtest.gui.GuiEventListener;
import de.ctdo.crashtest.gui.MainGui;
public class TestClass implements GuiEventListener {
public TestClass() {
MainGui mg = new MainGui();
mg.addListener(this);
mg.setCountDown(121411);
}
public class TestClass {
public static void main(String args[]) {
new TestClass();
}
public static void main(String args[]) throws InterruptedException {
//IRelaisboard relaisboard = new Relaisboard("/dev/ttyUSB0");
//relaisboard.open();
//relaisboard.close();
IBuntiClient bunti = new BuntiClient("bunti.ctdo.de", 8080);
for(int i = 0; i < 10; i++) {
bunti.setPar56(0, 255, 0, 0);
bunti.setPar56(1, 0, 255, 0);
bunti.setPar56(2, 0, 255, 0);
bunti.setPar56(3, 255, 0, 0);
Thread.sleep(500);
bunti.setPar56(0, 0, 0, 255);
bunti.setPar56(1, 255, 255, 0);
bunti.setPar56(2, 255, 255, 0);
bunti.setPar56(3, 0, 0, 255);
Thread.sleep(500);
bunti.setPar56(0, 255, 128, 0);
bunti.setPar56(1, 0, 255, 255);
bunti.setPar56(2, 0, 255, 255);
bunti.setPar56(3, 255, 128, 0);
Thread.sleep(500);
bunti.setPar56(0, 0, 255, 255);
bunti.setPar56(1, 0, 255, 0);
bunti.setPar56(2, 0, 255, 0);
bunti.setPar56(3, 0, 255, 255);
Thread.sleep(500);
}
@Override
public void keyPress(char key) {
}
@Override
public void windowClosing() {
System.out.println("closing");
System.exit(0);
}
}

View File

@ -11,7 +11,7 @@ import java.io.*;
import java.net.Socket;
public class BuntiClient implements IBuntiClient {
private String baseAddress;
private final String baseAddress;
//private final HttpClient client = new DefaultHttpClient();
public BuntiClient(String server, int port) {

View File

@ -1,9 +1,5 @@
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 setPar56(final int par, final int red, final int green, final int blue);

View File

@ -1,13 +1,10 @@
package de.ctdo.crashtest.domotics;
public interface IRelaisboard {
void setRelais(final int relais, final boolean state);
void toggleRelais(final int relais, final int milliseconds);
void blinkRelais(final int relais, final int pause, final int count);
void blinkRelais(final int relais, final int pause);
void blinkRelaisStop(final int relais);
boolean open();
void close();
}

View File

@ -12,7 +12,7 @@ public class Relaisboard implements IRelaisboard {
private OutputStream outputStream;
private Boolean serialPortGeoeffnet = false;
private String portName = "/dev/ttyUSB0";
private Object mLock = new Object();
private final Object mLock = new Object();
private final Map<Integer,Thread> threadMap = new ConcurrentHashMap<Integer, Thread>();
private final Map<Integer,Boolean> stopMap = new ConcurrentHashMap<Integer, Boolean>();
@ -21,7 +21,7 @@ public class Relaisboard implements IRelaisboard {
}
private void sendData(final int relais, final boolean state) {
synchronized (outputStream) {
synchronized (mLock) {
if(relais >= 0 && relais < 8 && outputStream != null) {
char charsOff[] = { 'a','b','c','d','e','f','g','h' };
char charsOn[] = { 'A','B','C','D','E','F','G','H' };
@ -49,7 +49,7 @@ public class Relaisboard implements IRelaisboard {
serialPortGeoeffnet = false;
Boolean foundPort = false;
if (serialPortGeoeffnet != false) {
if (serialPortGeoeffnet) {
Logger.sLog("Serialport bereits geöffnet");
return false;
}
@ -64,7 +64,7 @@ public class Relaisboard implements IRelaisboard {
break;
}
}
if (foundPort != true) {
if (!foundPort) {
Logger.sLog("Serialport nicht gefunden: " + portName);
return false;
}
@ -128,7 +128,7 @@ public class Relaisboard implements IRelaisboard {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) { }
} catch (InterruptedException ignored) { }
sendData(relais, false);
}
@ -137,45 +137,6 @@ public class Relaisboard implements IRelaisboard {
new Thread(r).start();
}
@Override
public void blinkRelais(final int relais, final int pause, final int count) {
if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
Runnable r = new Runnable() {
@Override
public void run() {
int i;
for(i = 0; i< count; i++) {
sendData(relais, true);
try {
Thread.sleep(pause);
} catch (InterruptedException e) { }
if(stopMap.get(relais)) return;
sendData(relais, false);
try {
Thread.sleep(pause);
} catch (InterruptedException e) { }
if(stopMap.get(relais)) return;
}
if(stopMap.get(relais)) sendData(relais,false); // switch off afterwards
}
};
stopMap.put(relais, false);
Thread thread = new Thread(r);
threadMap.put(relais,thread);
thread.start();
}
@Override
public void blinkRelais(final int relais, final int pause) {
if(!serialPortGeoeffnet) return;
@ -190,7 +151,7 @@ public class Relaisboard implements IRelaisboard {
try {
Thread.sleep(pause);
} catch (InterruptedException e) { }
} catch (InterruptedException ignored) { }
if(stopMap.get(relais)) return;
@ -198,7 +159,7 @@ public class Relaisboard implements IRelaisboard {
try {
Thread.sleep(pause);
} catch (InterruptedException e) { }
} catch (InterruptedException ignored) { }
if(stopMap.get(relais)) return;
}
@ -217,7 +178,7 @@ public class Relaisboard implements IRelaisboard {
}
private void stopRelaisThread(final int relais) {
Thread thread = (Thread)threadMap.get(relais);
Thread thread = threadMap.get(relais);
if(thread != null) {
stopMap.put(relais, true);

View File

@ -9,8 +9,6 @@ public interface IStatemachine {
void handleInput(char input);
int getTimerSecondsLast();
int getTimerSeconds();
void startTimer(int seconds);
void stopTimer();
void pauseTimer(boolean pause);

View File

@ -40,7 +40,6 @@ public class Statemachine implements IStatemachine {
private int timertSecondsLast;
private int timertSeconds;
public Statemachine() {
currentState = state.IDLE;
statemachineListenerList = new ArrayList<StatemachineListener>();
@ -218,7 +217,6 @@ public class Statemachine implements IStatemachine {
return retVal;
}
private void scheduleTimer() {
if(timer != null) timer.cancel();
@ -238,7 +236,4 @@ public class Statemachine implements IStatemachine {
};
timer.scheduleAtFixedRate(timerTask, 100, 100);
}
}

View File

@ -7,26 +7,24 @@ 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;
private IRelaisboard relaisboard;
private final IGuiControl guiControl;
private final IIrcClient ircClient;
private final IStatemachine machine;
private final IBuntiClient bunti;
private final IMPDController mpdController;
private final IRelaisboard relaisboard;
private int gamerRating = 3;
private Thread discoThread;
private boolean shouldStopDisco;
private boolean gemActivated = false;
public TheGame(IGuiControl guiControl) {
this.guiControl = guiControl;
this.ircClient = new IrcClient("crashtest", "#crashtest","irc.ctdo.de");
this.bunti = new BuntiClient("bunti.ctdo.de", 8080);
this.mpdController = new MPDController("dampfradio.raum.ctdo.de");
this.relaisboard = new Relaisboard("/dev/ttyUSB0");
this.machine = new Statemachine();
public TheGame(IGuiControl guiControl1) {
guiControl = guiControl1;
ircClient = new IrcClient("crashtest", "#crashtest","irc.ctdo.de");
bunti = new BuntiClient("bunti.ctdo.de", 8080);
mpdController = new MPDController("dampfradio.raum.ctdo.de");
relaisboard = new Relaisboard("/dev/ttyUSB0");
machine = new Statemachine();
initGame();
}
@ -202,7 +200,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
}
sayScore();
disco();
discoStart();
break;
}
@ -234,8 +232,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
@Override
public void keyPress(char key) {
machine.handleInput(key);
//guiControl.setExtra("btn: " + key);
}
/**
@ -243,10 +239,12 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
*/
@Override
public void windowClosing() {
machine.reset();
discoStop();
resetDomotics();
bunti.setPar56(0xff,0xff,0xff);
ircClient.say("bye");
relaisboard.close();
System.exit(0);
}
/**
@ -272,11 +270,11 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
} else if(message.startsWith("relais")) {
handleRelaisCommand(message);
} else if(message.startsWith("disco start")) {
disco();
discoStart();
} else if(message.startsWith("disco stop")) {
discoStop();
} else if(message.startsWith("gem")) {
handleGem();
handleGemCommand();
} else {
ircClient.say("y u no use valid command?");
}
@ -305,7 +303,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
} 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")) {
@ -314,15 +311,14 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
}
private void handleStateCommand(final String message) {
if(message.equals("state")) {
if(message.trim().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)) {
if(st.name().toLowerCase().equals(params)) {
ok = true;
machine.setNewState(st);
break;
@ -334,7 +330,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
}
// Yes, it makes no sense, but I want have it anyway. For more Spass am Geraet!
private void handleGem() {
private void handleGemCommand() {
if (!gemActivated) {
java.util.Random random = new java.util.Random();
int scry = random.nextInt(100);
@ -352,7 +348,6 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
}
private void handleRelaisCommand(final String message) {
String params = message.substring("relais".length()).trim().toLowerCase();
if(params.startsWith("lamp on")) {
@ -370,13 +365,11 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
} else if(params.startsWith("lamp stop")) {
relaisboard.blinkRelaisStop(2);
}
}
private void handleHelpCommand(final String message) {
if(message.equals("help")) {
ircClient.say("commands: help, reset, state, timer, wall, extra, score");
ircClient.say("commands: help, reset, state, timer, wall, extra, score, relais, disco");
} else if(message.contains("reset")) {
ircClient.say("resets the game to IDLE");
} else if(message.contains("state")) {
@ -413,7 +406,7 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
ircClient.say("control the relais board");
ircClient.say("valid commands: lamp on, lamp off, lamp blink, lamp stop, oven on, oven off, rokets");
} else if(message.contains("disco")) {
ircClient.say("party! use: disco {on,off}");
ircClient.say("party! use: disco {start,stop}");
} else if(message.contains("gem")) {
ircClient.say("you don't say?");
} else {
@ -424,38 +417,39 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
private void sayScore() {
ircClient.say("stateChangeCounter: " + machine.getStateChangeCounter());
int seconds = machine.getTimerSecondsLast();
int seconds1 = machine.getTimerSeconds();
int seconds2 = seconds-seconds1;
int secondsLast = machine.getTimerSecondsLast();
int seconds = machine.getTimerSeconds();
int secondsLeft = seconds - secondsLast;
int mins = seconds / 60;
int secs = seconds % 60;
int mins1 = seconds1 / 60;
int secs1 = seconds1 % 60;
int mins2 = seconds2 / 60;
int secs2 = seconds2 % 60;
ircClient.say("time " + mins1 + ":" + secs1 + " last: " + mins + ":" + secs + " left: " + mins2 + ":" + secs2);
int minsLast = secondsLast / 60;
int minsLeft = secondsLeft / 60;
ircClient.say(String.format("time: %d:%02d last: %d:%02d left: %d:%02d",
mins, seconds % 60, minsLast, secondsLast % 60, minsLeft, secondsLeft % 60));
ircClient.say("gamerRating: " + gamerRating);
}
private void resetDomotics() {
relaisboard.toggleRelais(2, 2000);
relaisboard.setRelais(7, true); // enable light barrier over relais
relaisboard.setRelais(6, true); // enable third green circle
relaisboard.setRelais(7, true); // enable light barrier over relais
relaisboard.setRelais(6, true); // enable third green circle
relaisboard.setRelais(2, false); // disable the lamp
relaisboard.setRelais(3, false); // disable the oven
mpdController.clearPlaylist();
}
private void discoStop() {
if(discoThread != null) {
shouldStopDisco = true;
discoThread.interrupt();
try {
discoThread.join(500);
} catch (InterruptedException e) { }
} catch (InterruptedException ignored) { }
}
}
private void disco() {
private void discoStart() {
discoStop();
Runnable r = new Runnable() {
@ -489,14 +483,13 @@ public class TheGame implements StatemachineListener, GuiEventListener, IRCEvent
if(shouldStopDisco) return;
Thread.sleep(500);
}
} catch (InterruptedException e) { }
} catch (InterruptedException ignored) { }
}
};
discoThread = new Thread(r);
shouldStopDisco = false;
discoThread.start();
}
}

View File

@ -1,14 +1,6 @@
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();
}

View File

@ -1,17 +1,9 @@
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);
}

View File

@ -11,14 +11,11 @@ public class MainGui extends JFrame implements IGuiControl {
private final JLabel countDown = new JLabel();
private final JLabel extraField = new JLabel();
private final JPanel lowerPanel = new JPanel();
private final List<GuiEventListener> listenerList = new ArrayList<GuiEventListener>();
public MainGui() {
initGui();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(0,0, 1280, 1024);
setExtendedState(Frame.MAXIMIZED_BOTH);
setUndecorated(true);
@ -33,7 +30,7 @@ public class MainGui extends JFrame implements IGuiControl {
}
});
addWindowStateListener(new WindowAdapter() {
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
for(GuiEventListener listener: listenerList) {
@ -46,18 +43,14 @@ public class MainGui extends JFrame implements IGuiControl {
}
private void setCountDownText(final int tseconds) {
int mins = tseconds / 600;
int secs = tseconds % 600 / 10 ;
int tsecs = tseconds % 9;
countDown.setText(" " + mins + ":" + secs + "." + tsecs);
countDown.setText(String.format(" %d:%02d.%1d", tseconds / 600, tseconds % 600 / 10, tseconds % 9));
if(tseconds < 400) {
double percentile = ((tseconds-100.0)/300.0);
double red = 255.0 - percentile * 255.0;
double green = 255.0 - red;
// System.out.println("red= " + red + " green=" + green + " blue="+blue);
if(red > 255) red = 255;
if(red < 0) red = 0;
if(green > 255) green = 255;
@ -69,7 +62,6 @@ public class MainGui extends JFrame implements IGuiControl {
else {
countDown.setForeground(new Color(42, 190, 0));
}
}
private void initGui() {
@ -161,6 +153,4 @@ public class MainGui extends JFrame implements IGuiControl {
listenerList.add(listener);
}
}

View File

@ -1,9 +1,5 @@
package de.ctdo.crashtest.irc;
/**
* @author: lucas
* @date: 01.06.12 10:05
*/
public interface IIrcClient {
void say(String message);
void addListener(IRCEventListener listenerIRC);

View File

@ -1,9 +1,5 @@
package de.ctdo.crashtest.irc;
/**
* @author: lucas
* @date: 01.06.12 11:49
*/
public interface IRCEventListener {
void handleMessage(final String message);

View File

@ -20,7 +20,7 @@ import java.util.List;
*/
public class IrcClient implements IIrcClient, jerklib.events.listeners.IRCEventListener, ILogger {
private final List<IRCEventListener> listenerListIRC = new ArrayList<IRCEventListener>();
private Session ircsession;
private final Session ircsession;
private final String channel;
public IrcClient(String nick, String channel, String server) {

View File

@ -1,10 +1,5 @@
package de.ctdo.crashtest.log;
/**
* @author: lucas
* @date: 01.06.12 16:46
*/
public interface ILogger {
void log(String message);
}

View File

@ -20,7 +20,7 @@ public class Logger {
return instance;
}
public void log(String message) {
private void log(String message) {
System.out.println("LOG: " + message);

View File

@ -1,9 +1,5 @@
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);