Browse Source

Working on control unit

lookup-tables
Stephan Richter 5 years ago
parent
commit
0f30a45d16
  1. 2
      pom.xml
  2. 27
      src/main/java/de/srsoftware/web4rail/ControlUnit.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.5.1</version> <version>0.5.2</version>
<name>Web4Rail</name> <name>Web4Rail</name>
<description>Java Model Railway Control</description> <description>Java Model Railway Control</description>
<url>https://github.com/StephanRichter/Web4Rail</url> <url>https://github.com/StephanRichter/Web4Rail</url>

27
src/main/java/de/srsoftware/web4rail/ControlUnit.java

@ -15,6 +15,7 @@ public class ControlUnit extends Thread{
private static final int DEFAULT_PORT = 4303; private static final int DEFAULT_PORT = 4303;
private static final int OK_PROTO = 201; private static final int OK_PROTO = 201;
private static final int OK_MODE = 202; private static final int OK_MODE = 202;
private static final int OK = 200;
private class Reply{ private class Reply{
private long secs; private long secs;
@ -41,6 +42,7 @@ public class ControlUnit extends Thread{
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 boolean stopped = true; private boolean stopped = true;
private LinkedList<String> queue = new LinkedList<String>(); private LinkedList<String> queue = new LinkedList<String>();
private Socket socket; private Socket socket;
@ -56,13 +58,20 @@ public class ControlUnit extends Thread{
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
ControlUnit cu = new ControlUnit().setEndpoint("Modellbahn", DEFAULT_PORT).restart(); ControlUnit cu = new ControlUnit().setEndpoint("Modellbahn", DEFAULT_PORT).setBus(1).restart();
Thread.sleep(1000); Thread.sleep(1000);
cu.queue("SET 0 GL 1 0 10"); cu.queue("SET {} POWER ON");
cu.queue("SET {} GL 1 0 10 128");
Thread.sleep(1000); Thread.sleep(1000);
cu.end(); cu.end();
} }
private ControlUnit setBus(int bus) {
this.bus = bus;
return this;
}
public void queue(String command) { public void queue(String command) {
queue.add(command); queue.add(command);
} }
@ -98,12 +107,13 @@ public class ControlUnit extends Thread{
/** /**
* send command to Server * send command to Server
* @param command * @param command
* @return
* @throws IOException * @throws IOException
*/ */
private void send(String command) throws IOException { private Reply send(String command) throws IOException {
if (command == null) return; if (command == null) return null;
writeln(command); writeln(command);
Reply reply = new Reply(scanner); return new Reply(scanner);
} }
public ControlUnit setEndpoint(String newHost, int newPort){ public ControlUnit setEndpoint(String newHost, int newPort){
@ -143,12 +153,17 @@ public class ControlUnit extends Thread{
Reply reply = new Reply(scanner); Reply reply = new Reply(scanner);
if (reply.code != OK_PROTO) throw new IOException("Handshake failed: "+reply); if (reply.code != OK_PROTO) throw new IOException("Handshake failed: "+reply);
writeln("SET CONNECTIONMODE SRCP COMMAND"); writeln("SET CONNECTIONMODE SRCP COMMAND"); // preset following mode: COMMAND MODE
reply = new Reply(scanner); reply = new Reply(scanner);
if (reply.code != OK_MODE) throw new IOException("Handshake failed: "+reply); if (reply.code != OK_MODE) throw new IOException("Handshake failed: "+reply);
writeln("GO"); // switch mode
reply = new Reply(scanner);
if (reply.code != OK) throw new IOException("Handshake failed: "+reply);
} }
private void writeln(String data) throws IOException { private void writeln(String data) throws IOException {
data = data.replace("{}", ""+bus);
socket.getOutputStream().write((data+"\n").getBytes(StandardCharsets.US_ASCII)); socket.getOutputStream().write((data+"\n").getBytes(StandardCharsets.US_ASCII));
LOG.debug("sent {}.",data); LOG.debug("sent {}.",data);
} }

Loading…
Cancel
Save