diff --git a/pom.xml b/pom.xml
index 637362c..c1a676c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
4.0.0
de.srsoftware
web4rail
- 0.8.4
+ 0.9.1
Web4Rail
jar
Java Model Railway Control
diff --git a/resources/translations/Application.de.translation b/resources/translations/Application.de.translation
index 4512068..7871728 100644
--- a/resources/translations/Application.de.translation
+++ b/resources/translations/Application.de.translation
@@ -6,6 +6,7 @@ Apply : Übernehmen
Conditions : Bedingungen
Contacts and actions : Kontakte und Aktionen
Destination\: {} from {} : Ziel: {} von {}
+inverted : invertiert
length\: : Länge:
name\: : Name:
Origin and destination : Start und Ziel
diff --git a/src/main/java/de/srsoftware/web4rail/ControlUnit.java b/src/main/java/de/srsoftware/web4rail/ControlUnit.java
index 3abbd01..2236fd4 100644
--- a/src/main/java/de/srsoftware/web4rail/ControlUnit.java
+++ b/src/main/java/de/srsoftware/web4rail/ControlUnit.java
@@ -36,14 +36,17 @@ public class ControlUnit extends Thread implements Constants{
private static final String HOST = "host";
private static final String PORT = "port";
private static final String BUS = "bus";
+ private static final String MODE_INFO = "INFO";
+ private static final String MODE_COMMAND = "COMMAND";
+ protected static final String FEEDBACK = "FB";
private String host = DEFAULT_HOST;
private int port = DEFAULT_PORT;
private int bus = 0;
private boolean stopped = true;
private LinkedList queue = new LinkedList();
- private Socket socket;
- private Scanner scanner;
+ private Socket commandSocket,infoSocket;
+ private Scanner commandScanner,infoScanner;
private boolean power = false;
private Plan plan;
@@ -61,13 +64,16 @@ public class ControlUnit extends Thread implements Constants{
/**
* performs a handshake as specified in the SRCP protocol
+ * @param mode
* @throws TimeoutException
* @throws IOException
*/
- private void handshake() throws TimeoutException, IOException {
+ private void handshake(String mode) throws TimeoutException, IOException {
String proto = null;
- if (scanner.hasNext()) {
- String line = scanner.nextLine();
+ commandSocket = new Socket(host, port);
+ commandScanner = new Scanner(commandSocket.getInputStream());
+ if (commandScanner.hasNext()) {
+ String line = commandScanner.nextLine();
LOG.debug("recv: "+line);
for (String part : line.split(";")) {
part = part.trim();
@@ -81,7 +87,7 @@ public class ControlUnit extends Thread implements Constants{
send(command);
if (!command.reply().succeeded()) throw new IOException("Handshake failed: "+command.reply());
- command = new Command("SET CONNECTIONMODE SRCP COMMAND"); // preset following mode: COMMAND MODE
+ command = new Command("SET CONNECTIONMODE SRCP "+mode); // preset following mode: COMMAND MODE
send(command);
if (!command.reply().succeeded()) throw new IOException("Handshake failed: "+command.reply());
@@ -119,7 +125,7 @@ public class ControlUnit extends Thread implements Constants{
* test method
* @param args
* @throws InterruptedException
- */
+ *
public static void main(String[] args) throws InterruptedException {
ControlUnit cu = new ControlUnit(null).setEndpoint("Modellbahn", DEFAULT_PORT).setBus(1).restart();
Thread.sleep(1000);
@@ -138,7 +144,7 @@ public class ControlUnit extends Thread implements Constants{
});
Thread.sleep(1000);
cu.end();
- }
+ } //*/
/**
* process actions related to the SRCP daemon
@@ -230,9 +236,10 @@ public class ControlUnit extends Thread implements Constants{
}
}
try {
- socket.close();
+ commandSocket.close();
+ LOG.debug("Closed command socket.");
} catch (IOException e) {
- e.printStackTrace();
+ LOG.warn("Was not able to close command socket: ",e);
}
}
@@ -256,19 +263,9 @@ public class ControlUnit extends Thread implements Constants{
private void send(Command command) throws IOException {
if (command == null || command.toString() == null) return;
String data = command.toString().replace("{}", ""+bus);
- socket.getOutputStream().write((data+"\n").getBytes(StandardCharsets.US_ASCII));
+ commandSocket.getOutputStream().write((data+"\n").getBytes(StandardCharsets.US_ASCII));
LOG.info("sent {}.",data);
- command.readReplyFrom(scanner);
- }
-
- /**
- * defines the bus on the SRCP deamon, to which commands shall be assigned
- * @param bus
- * @return
- */
- private ControlUnit setBus(int bus) {
- this.bus = bus;
- return this;
+ command.readReplyFrom(commandScanner);
}
/**
@@ -286,16 +283,61 @@ public class ControlUnit extends Thread implements Constants{
@Override
public synchronized void start() {
try {
- socket = new Socket(host, port);
- scanner = new Scanner(socket.getInputStream());
- handshake();
+ handshake(MODE_INFO);
stopped = false;
+ startInfoThread();
+ handshake(MODE_COMMAND);
} catch (IOException | TimeoutException e) {
throw new IllegalStateException(e);
}
super.start();
}
+ private void startInfoThread() {
+ infoSocket = commandSocket; // handshake läuft immer über commandSocket und commandScanner
+ infoScanner = commandScanner;
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ while (!stopped) {
+ String line = infoScanner.nextLine();
+ String[] parts = line.split(" ");
+// String time = parts[0];
+// String code = parts[1];
+ String type = parts[2];
+// String bus = parts[3];
+ String device = parts[4];
+ if (type.equals("INFO")) {
+
+ switch (device) {
+ case FEEDBACK:
+ int addr = Integer.parseInt(parts[5]);
+ boolean active = !parts[6].equals("0");
+ ControlUnit.this.plan.sensor(addr,active);
+ break;
+ default:
+ LOG.debug("Info thread received: {}",line);
+ break;
+ }
+ } else {
+ LOG.warn("Unknown/unexpected message type: {}",line);
+ }
+
+ }
+ infoScanner.close();
+ LOG.info("Closed info stream.");
+ try {
+ infoSocket.close();
+ LOG.info("Closed info socket.");
+ } catch (IOException e) {
+ LOG.warn("Was not able to close info socket:",e);
+ }
+
+ }
+ }).start();
+ }
+
/**
* shorthand for Translation.get(text,fills)
* @param text
diff --git a/src/main/java/de/srsoftware/web4rail/Plan.java b/src/main/java/de/srsoftware/web4rail/Plan.java
index 5d25e47..f4aa85e 100644
--- a/src/main/java/de/srsoftware/web4rail/Plan.java
+++ b/src/main/java/de/srsoftware/web4rail/Plan.java
@@ -658,6 +658,11 @@ public class Plan implements Constants{
tile.position(x, y).plan(this);
tiles.put(tile.id(),tile);
}
+
+ public void sensor(int addr, boolean active) {
+
+ }
+
/**
* shows the properties of an entity specified in the params.context value