Browse Source

preparing for feedback processing

lookup-tables
Stephan Richter 5 years ago
parent
commit
9e9105b89b
  1. 2
      pom.xml
  2. 1
      resources/translations/Application.de.translation
  3. 92
      src/main/java/de/srsoftware/web4rail/ControlUnit.java
  4. 5
      src/main/java/de/srsoftware/web4rail/Plan.java

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId> <groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId> <artifactId>web4rail</artifactId>
<version>0.8.4</version> <version>0.9.1</version>
<name>Web4Rail</name> <name>Web4Rail</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Java Model Railway Control</description> <description>Java Model Railway Control</description>

1
resources/translations/Application.de.translation

@ -6,6 +6,7 @@ Apply : Übernehmen
Conditions : Bedingungen Conditions : Bedingungen
Contacts and actions : Kontakte und Aktionen Contacts and actions : Kontakte und Aktionen
Destination\: {} from {} : Ziel: {} von {} Destination\: {} from {} : Ziel: {} von {}
inverted : invertiert
length\: : Länge: length\: : Länge:
name\: : Name: name\: : Name:
Origin and destination : Start und Ziel Origin and destination : Start und Ziel

92
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 HOST = "host";
private static final String PORT = "port"; private static final String PORT = "port";
private static final String BUS = "bus"; 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 String host = DEFAULT_HOST;
private int port = DEFAULT_PORT; private int port = DEFAULT_PORT;
private int bus = 0; private int bus = 0;
private boolean stopped = true; private boolean stopped = true;
private LinkedList<Command> queue = new LinkedList<Command>(); private LinkedList<Command> queue = new LinkedList<Command>();
private Socket socket; private Socket commandSocket,infoSocket;
private Scanner scanner; private Scanner commandScanner,infoScanner;
private boolean power = false; private boolean power = false;
private Plan plan; private Plan plan;
@ -61,13 +64,16 @@ public class ControlUnit extends Thread implements Constants{
/** /**
* performs a handshake as specified in the SRCP protocol * performs a handshake as specified in the SRCP protocol
* @param mode
* @throws TimeoutException * @throws TimeoutException
* @throws IOException * @throws IOException
*/ */
private void handshake() throws TimeoutException, IOException { private void handshake(String mode) throws TimeoutException, IOException {
String proto = null; String proto = null;
if (scanner.hasNext()) { commandSocket = new Socket(host, port);
String line = scanner.nextLine(); commandScanner = new Scanner(commandSocket.getInputStream());
if (commandScanner.hasNext()) {
String line = commandScanner.nextLine();
LOG.debug("recv: "+line); LOG.debug("recv: "+line);
for (String part : line.split(";")) { for (String part : line.split(";")) {
part = part.trim(); part = part.trim();
@ -81,7 +87,7 @@ public class ControlUnit extends Thread implements Constants{
send(command); send(command);
if (!command.reply().succeeded()) throw new IOException("Handshake failed: "+command.reply()); 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); send(command);
if (!command.reply().succeeded()) throw new IOException("Handshake failed: "+command.reply()); if (!command.reply().succeeded()) throw new IOException("Handshake failed: "+command.reply());
@ -119,7 +125,7 @@ public class ControlUnit extends Thread implements Constants{
* test method * test method
* @param args * @param args
* @throws InterruptedException * @throws InterruptedException
*/ *
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
ControlUnit cu = new ControlUnit(null).setEndpoint("Modellbahn", DEFAULT_PORT).setBus(1).restart(); ControlUnit cu = new ControlUnit(null).setEndpoint("Modellbahn", DEFAULT_PORT).setBus(1).restart();
Thread.sleep(1000); Thread.sleep(1000);
@ -138,7 +144,7 @@ public class ControlUnit extends Thread implements Constants{
}); });
Thread.sleep(1000); Thread.sleep(1000);
cu.end(); cu.end();
} } //*/
/** /**
* process actions related to the SRCP daemon * process actions related to the SRCP daemon
@ -230,9 +236,10 @@ public class ControlUnit extends Thread implements Constants{
} }
} }
try { try {
socket.close(); commandSocket.close();
LOG.debug("Closed command socket.");
} catch (IOException e) { } 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 { private void send(Command command) throws IOException {
if (command == null || command.toString() == null) return; if (command == null || command.toString() == null) return;
String data = command.toString().replace("{}", ""+bus); 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); LOG.info("sent {}.",data);
command.readReplyFrom(scanner); command.readReplyFrom(commandScanner);
}
/**
* 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;
} }
/** /**
@ -286,16 +283,61 @@ public class ControlUnit extends Thread implements Constants{
@Override @Override
public synchronized void start() { public synchronized void start() {
try { try {
socket = new Socket(host, port); handshake(MODE_INFO);
scanner = new Scanner(socket.getInputStream());
handshake();
stopped = false; stopped = false;
startInfoThread();
handshake(MODE_COMMAND);
} catch (IOException | TimeoutException e) { } catch (IOException | TimeoutException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
super.start(); 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) * shorthand for Translation.get(text,fills)
* @param text * @param text

5
src/main/java/de/srsoftware/web4rail/Plan.java

@ -658,6 +658,11 @@ public class Plan implements Constants{
tile.position(x, y).plan(this); tile.position(x, y).plan(this);
tiles.put(tile.id(),tile); tiles.put(tile.id(),tile);
} }
public void sensor(int addr, boolean active) {
}
/** /**
* shows the properties of an entity specified in the params.context value * shows the properties of an entity specified in the params.context value

Loading…
Cancel
Save