diff --git a/crashteststeuerung.iml b/crashteststeuerung.iml
index 4e2f0f8..118b8b9 100644
--- a/crashteststeuerung.iml
+++ b/crashteststeuerung.iml
@@ -11,6 +11,7 @@
+
diff --git a/src/de/ctdo/crashtest/domotics/IRelaisboard.java b/src/de/ctdo/crashtest/domotics/IRelaisboard.java
index a4af876..0baee89 100644
--- a/src/de/ctdo/crashtest/domotics/IRelaisboard.java
+++ b/src/de/ctdo/crashtest/domotics/IRelaisboard.java
@@ -2,6 +2,10 @@ package de.ctdo.crashtest.domotics;
public interface IRelaisboard {
- void setRelais(int relai, boolean state);
+ void setRelais(final int relais, final boolean state);
+ void toggleRelais(final int relais, final int milliseconds);
+
+ boolean open();
+ void close();
}
diff --git a/src/de/ctdo/crashtest/domotics/Relaisboard.java b/src/de/ctdo/crashtest/domotics/Relaisboard.java
index a6391b9..1e4d921 100644
--- a/src/de/ctdo/crashtest/domotics/Relaisboard.java
+++ b/src/de/ctdo/crashtest/domotics/Relaisboard.java
@@ -1,12 +1,132 @@
package de.ctdo.crashtest.domotics;
-
+import de.ctdo.crashtest.log.Logger;
+import gnu.io.*;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.Enumeration;
public class Relaisboard implements IRelaisboard {
+ private SerialPort serialPort;
+ private OutputStream outputStream;
+ private Boolean serialPortGeoeffnet = false;
+ private String portName = "/dev/ttyUSB0";
+
+ public Relaisboard(final String port) {
+ this.portName = port;
+ }
+
+ private synchronized void send(final char ch) {
+ if (!serialPortGeoeffnet) return;
+
+ try {
+ outputStream.write(ch);
+ } catch (IOException e) {
+ Logger.sLog("Fehler beim Senden");
+ }
+ }
+
+ private void sendData(final int relais, final boolean state) {
+ if(relais >= 0 && relais < 8) {
+ char charsOff[] = { 'a','b','c','d','e','f','g','h' };
+ char charsOn[] = { 'A','B','C','D','E','F','G','H' };
+
+ if(state) {
+ send(charsOn[relais]);
+ } else {
+ send(charsOff[relais]);
+ }
+ }
+ }
@Override
- public void setRelais(int relai, boolean state) {
+ public boolean open()
+ {
+ serialPortGeoeffnet = false;
+ Boolean foundPort = false;
+ if (serialPortGeoeffnet != false) {
+ Logger.sLog("Serialport bereits geƶffnet");
+ return false;
+ }
+
+ Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
+ CommPortIdentifier serialPortId = null;
+
+ while(enumComm.hasMoreElements()) {
+ serialPortId = (CommPortIdentifier) enumComm.nextElement();
+ if (portName.contentEquals(serialPortId.getName())) {
+ foundPort = true;
+ break;
+ }
+ }
+ if (foundPort != true) {
+ Logger.sLog("Serialport nicht gefunden: " + portName);
+ return false;
+ }
+
+ try {
+ // open port and tell the OS who we are. Wait max 500ms for release if port is currently owned.
+ serialPort = (SerialPort) serialPortId.open("crashtest app", 500);
+ serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
+ serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
+
+ outputStream = serialPort.getOutputStream();
+ serialPortGeoeffnet = true;
+
+ return true;
+ } catch (PortInUseException e) {
+ Logger.sLog("Port belegt");
+ } catch (IOException e) {
+ Logger.sLog("Keinen Zugriff auf OutputStream");
+ } catch(UnsupportedCommOperationException e) {
+ Logger.sLog("Konnte Schnittstellen-Paramter nicht setzen");
+ }
+
+ return false;
+ }
+
+ @Override
+ public void close() {
+ if ( serialPortGeoeffnet ) {
+ serialPort.close();
+ serialPortGeoeffnet = false;
+ outputStream = null;
+ }
+ }
+
+ @Override
+ public void setRelais(final int relais, final boolean state) {
+ if(!serialPortGeoeffnet) return;
+
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ sendData(relais, state);
+ }
+ };
+
+ new Thread(r).start();
+ }
+
+ @Override
+ public void toggleRelais(final int relais, final int milliseconds) {
+ if(!serialPortGeoeffnet) return;
+
+ Runnable r = new Runnable() {
+ @Override
+ public void run() {
+ sendData(relais, true);
+
+ try {
+ Thread.sleep(milliseconds);
+ } catch (InterruptedException e) { }
+
+ sendData(relais, false);
+ }
+ };
+
+ new Thread(r).start();
}
}
diff --git a/src/de/ctdo/crashtest/mpd/MPDController.java b/src/de/ctdo/crashtest/mpd/MPDController.java
index 100675b..6d80daf 100644
--- a/src/de/ctdo/crashtest/mpd/MPDController.java
+++ b/src/de/ctdo/crashtest/mpd/MPDController.java
@@ -40,7 +40,6 @@ public class MPDController implements IMPDController {
@Override
public void playSong(final String artist, final String title) {
if(mpd != null) {
-
Runnable r = new Runnable() {
@Override
public void run() {
@@ -72,7 +71,9 @@ public class MPDController implements IMPDController {
}
};
- new Thread(r).start();
+ synchronized (mpd) {
+ new Thread(r).start();
+ }
}
}
@@ -124,7 +125,9 @@ public class MPDController implements IMPDController {
}
};
- new Thread(r).start();
+ synchronized (mpd) {
+ new Thread(r).start();
+ }
}
}
}