crashtest/src/de/ctdo/crashtest/domotics/Relaisboard.java

186 lines
5.5 KiB
Java
Raw Normal View History

2012-06-05 22:59:09 +00:00
package de.ctdo.crashtest.domotics;
2012-06-06 10:24:09 +00:00
import de.ctdo.crashtest.log.Logger;
import gnu.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
2012-06-05 22:59:09 +00:00
public class Relaisboard implements IRelaisboard {
2012-06-06 10:24:09 +00:00
private SerialPort serialPort;
private OutputStream outputStream;
private Boolean serialPortGeoeffnet = false;
private String portName = "/dev/ttyUSB0";
2012-06-14 09:40:08 +00:00
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>();
2012-06-06 10:24:09 +00:00
public Relaisboard(final String port) {
this.portName = port;
}
private void sendData(final int relais, final boolean state) {
2012-06-14 09:40:08 +00:00
synchronized (mLock) {
2012-06-07 22:58:08 +00:00
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' };
try {
if(state) {
outputStream.write(charsOn[relais]);
} else {
outputStream.write(charsOff[relais]);
}
System.out.println("Relaisboard: sendData " + relais + " = " + state);
2012-06-07 22:58:08 +00:00
outputStream.flush();
} catch (IOException e) {
2012-06-10 19:39:42 +00:00
Logger.sLog("Relaisboard error: Fehler beim Senden");
2012-06-07 22:58:08 +00:00
}
2012-06-06 10:24:09 +00:00
}
}
}
@Override
2012-06-15 14:06:03 +00:00
public void open() {
2012-06-06 10:24:09 +00:00
serialPortGeoeffnet = false;
Boolean foundPort = false;
2012-06-14 09:40:08 +00:00
if (serialPortGeoeffnet) {
2012-06-06 10:24:09 +00:00
Logger.sLog("Serialport bereits geöffnet");
2012-06-15 14:06:03 +00:00
return;
2012-06-06 10:24:09 +00:00
}
Enumeration enumComm = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier serialPortId = null;
while(enumComm.hasMoreElements()) {
serialPortId = (CommPortIdentifier) enumComm.nextElement();
if (portName.contentEquals(serialPortId.getName())) {
foundPort = true;
break;
}
}
2012-06-14 09:40:08 +00:00
if (!foundPort) {
2012-06-06 10:24:09 +00:00
Logger.sLog("Serialport nicht gefunden: " + portName);
2012-06-15 14:06:03 +00:00
return;
2012-06-06 10:24:09 +00:00
}
2012-06-05 22:59:09 +00:00
2012-06-06 10:24:09 +00:00
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;
} catch (PortInUseException e) {
2012-06-10 19:39:42 +00:00
Logger.sLog("Relaisboard error: Port belegt " + portName);
2012-06-06 10:24:09 +00:00
} catch (IOException e) {
2012-06-10 19:39:42 +00:00
Logger.sLog("Relaisboard error: Keinen Zugriff auf OutputStream");
2012-06-06 10:24:09 +00:00
} catch(UnsupportedCommOperationException e) {
2012-06-10 19:39:42 +00:00
Logger.sLog("Relaisboard error: Konnte Schnittstellen-Paramter nicht setzen");
2012-06-06 10:24:09 +00:00
}
}
2012-06-05 22:59:09 +00:00
@Override
2012-06-06 10:24:09 +00:00
public void close() {
if ( serialPortGeoeffnet ) {
serialPort.close();
}
serialPortGeoeffnet = false;
outputStream = null;
2012-06-06 10:24:09 +00:00
}
@Override
public void setRelais(final int relais, final boolean state) {
if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
2012-06-07 22:58:08 +00:00
Runnable r = new Runnable() {
@Override
public void run() {
sendData(relais, state);
}
};
2012-06-06 10:24:09 +00:00
2012-06-07 22:58:08 +00:00
new Thread(r).start();
2012-06-06 10:24:09 +00:00
}
@Override
public void toggleRelais(final int relais, final int milliseconds) {
if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
2012-06-07 22:58:08 +00:00
Runnable r = new Runnable() {
@Override
public void run() {
sendData(relais, true);
2012-06-06 10:24:09 +00:00
2012-06-07 22:58:08 +00:00
try {
Thread.sleep(milliseconds);
2012-06-14 09:40:08 +00:00
} catch (InterruptedException ignored) { }
2012-06-07 22:58:08 +00:00
sendData(relais, false);
}
};
new Thread(r).start();
}
2012-06-05 22:59:09 +00:00
@Override
public void blinkRelais(final int relais, final int pause) {
if(!serialPortGeoeffnet) return;
stopRelaisThread(relais);
Runnable r = new Runnable() {
@Override
public void run() {
while(true) {
sendData(relais, true);
try {
Thread.sleep(pause);
2012-06-14 09:40:08 +00:00
} catch (InterruptedException ignored) { }
if(stopMap.get(relais)) return;
sendData(relais, false);
try {
Thread.sleep(pause);
2012-06-14 09:40:08 +00:00
} catch (InterruptedException ignored) { }
if(stopMap.get(relais)) return;
}
}
};
stopMap.put(relais, false);
Thread thread = new Thread(r);
threadMap.put(relais,thread);
thread.start();
}
@Override
public void blinkRelaisStop(final int relais) {
stopRelaisThread(relais);
}
private void stopRelaisThread(final int relais) {
2012-06-14 09:40:08 +00:00
Thread thread = threadMap.get(relais);
if(thread != null) {
stopMap.put(relais, true);
thread.interrupt();
threadMap.remove(relais);
}
2012-06-05 22:59:09 +00:00
}
}