building on statemachine

This commit is contained in:
Lucas Pleß 2012-05-10 15:53:18 +02:00
parent 3c05a00c1a
commit aa08f7e486
15 changed files with 280 additions and 26 deletions

0
.gitignore vendored Normal file
View File

View File

@ -0,0 +1,14 @@
<component name="libraryTable">
<library name="commons-codec-1.4">
<CLASSES>
<root url="jar://$PROJECT_DIR$/libs/commons-codec-1.4.jar!/" />
<root url="jar://$PROJECT_DIR$/libs/commons-logging-1.1.1.jar!/" />
<root url="jar://$PROJECT_DIR$/libs/httpclient-4.1.3.jar!/" />
<root url="jar://$PROJECT_DIR$/libs/httpclient-cache-4.1.3.jar!/" />
<root url="jar://$PROJECT_DIR$/libs/httpcore-4.1.4.jar!/" />
<root url="jar://$PROJECT_DIR$/libs/httpmime-4.1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -11,21 +11,5 @@
<output url="file://$PROJECT_DIR$/out" />
</component>
<component name="WebServicesPlugin" addRequiredLibraries="true" />
<component name="masterDetails">
<states>
<state key="ProjectJDKs.UI">
<settings>
<last-edited>1.6</last-edited>
<splitter-proportions>
<option name="proportions">
<list>
<option value="0.2" />
</list>
</option>
</splitter-proportions>
</settings>
</state>
</states>
</component>
</project>

View File

@ -8,9 +8,6 @@
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
@ -26,6 +23,9 @@
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>

View File

@ -7,6 +7,8 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="commons-codec-1.4" level="project" />
<orderEntry type="library" name="javampd-4.0" level="project" />
</component>
</module>

BIN
libs/commons-codec-1.4.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
libs/httpclient-4.1.3.jar Normal file

Binary file not shown.

Binary file not shown.

BIN
libs/httpcore-4.1.4.jar Normal file

Binary file not shown.

BIN
libs/httpmime-4.1.3.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,79 @@
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.IOException;
import java.io.UnsupportedEncodingException;
/**
* User: lpless
* Date: 10.05.12
* Time: 11:03
*/
public class BuntiClient {
String baseAddress;
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) {
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();
}
}
}

View File

@ -0,0 +1,10 @@
package de.ctdo.crashtest;
/**
* User: lpless
* Date: 10.05.12
* Time: 13:37
*/
public interface StateChangeListener {
void stateChanged(Statemachine.state newState);
}

View File

@ -1,11 +1,21 @@
package de.ctdo.crashtest;
import java.util.ArrayList;
import java.util.List;
public class Statemachine {
private final char BLUE_BUTTON = 'd';
private final char LIGHT_BARRIER = 'k';
private final char TABLE_ONE = 'c';
private final char TABLE_TWO = 'v';
private final char TABLE_THREE = 'b';
private long lastHandleInput = 0;
private final List<StateChangeListener> stateChangeListenerList = new ArrayList<StateChangeListener>();
private int stateChangeCounter = 0;
public void addStateChangedListener(StateChangeListener listener) {
stateChangeListenerList.add(listener);
}
public enum state {
IDLE,
@ -27,13 +37,64 @@ public class Statemachine {
return currentState;
}
public int getStateChangeCounter() {
return stateChangeCounter;
}
public void reset() {
stateChangeCounter = 0;
currentState = state.IDLE;
}
public void handleInput(char input) {
if(System.currentTimeMillis() - lastHandleInput < 200 ) return;
state newState = getNewState(input);
System.out.println("newState = " + newState);
if( newState != currentState ) {
stateChangeCounter++;
System.out.println("newState = " + newState);
currentState = 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) {

View File

@ -1,29 +1,133 @@
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;
public class SteuerungFrame extends JFrame {
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 SteuerungFrame() {
//setType(Type.UTILITY);
setBackground(Color.black);
setBounds(200,200, 200, 200);
setBounds(200,200, 400, 200);
machine.addStateChangedListener(this);
initGui();
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
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();
//System.out.println(e.getKeyChar());
machine.handleInput(e.getKeyChar());
//bunti.setPar56(1, 0xff, 0xff, 0xff);
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();
}
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
@Override
public void stateChanged(Statemachine.state newState) {
updateGui();
switch (newState) {
case IDLE:
bunti.setPar56(0,0,0,0);
bunti.setPar56(1,0,0,0);
bunti.setPar56(2,0,0,0);
bunti.setPar56(3,0,0,0);
bunti.setLampel(false,false,false);
break;
case ENTERED_ROOM:
bunti.setLampel(true,false,false);
break;
case TABLE_GAME_ONE:
bunti.setLampel(false,true,false);
break;
case TABLE_GAME_TWO:
bunti.setLampel(false,true,false);
break;
case TABLE_GAME_THREE:
bunti.setLampel(false,true,false);
break;
case TABLE_GAME_FOUR:
bunti.setLampel(false,true,false);
break;
case TABLE_GAME_FIVE:
bunti.setLampel(false,true,false);
break;
case TABLE_GAME_SIX:
bunti.setLampel(false,true,false);
break;
case TABLE_GAME_SEVEN:
bunti.setLampel(false,false,true);
break;
}
}
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("<html>LastKey: " + lastKey + "<br>CurrentState: " +
machine.getCurrentState() + "<br>ChangeCounter: " +
machine.getStateChangeCounter() + "</html>");
}
}