From 0f30a45d163dbee4c2952113f5e7a27d22ca4ebe Mon Sep 17 00:00:00 2001
From: Stephan Richter <github@keawe.de>
Date: Thu, 1 Oct 2020 22:10:22 +0200
Subject: [PATCH] Working on control unit

---
 pom.xml                                       |  2 +-
 .../de/srsoftware/web4rail/ControlUnit.java   | 27 ++++++++++++++-----
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/pom.xml b/pom.xml
index 7d88195..be8d44b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
 	<modelVersion>4.0.0</modelVersion>
 	<groupId>de.srsoftware</groupId>
 	<artifactId>web4rail</artifactId>
-	<version>0.5.1</version>
+	<version>0.5.2</version>
 	<name>Web4Rail</name>
 	<description>Java Model Railway Control</description>
 	<url>https://github.com/StephanRichter/Web4Rail</url>
diff --git a/src/main/java/de/srsoftware/web4rail/ControlUnit.java b/src/main/java/de/srsoftware/web4rail/ControlUnit.java
index c02ca6d..295e250 100644
--- a/src/main/java/de/srsoftware/web4rail/ControlUnit.java
+++ b/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 OK_PROTO = 201;
 	private static final int OK_MODE = 202;
+	private static final int OK = 200;
 	
 	private class Reply{
 		private long secs;
@@ -41,6 +42,7 @@ public class ControlUnit extends Thread{
 	
 	private String host = DEFAULT_HOST;
 	private int port = DEFAULT_PORT;
+	private int bus = 0;
 	private boolean stopped = true;
 	private LinkedList<String> queue = new LinkedList<String>();
 	private Socket socket;
@@ -56,13 +58,20 @@ public class ControlUnit extends Thread{
 
 	
 	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);
-		cu.queue("SET 0 GL 1 0 10");
+		cu.queue("SET {} POWER ON");
+		cu.queue("SET {} GL 1 0 10 128");
 		Thread.sleep(1000);
 		cu.end();
 	}
 	
+	private ControlUnit setBus(int bus) {
+		this.bus = bus;
+		return this;
+	}
+
+
 	public void queue(String command) {
 		queue.add(command);
 	}
@@ -98,12 +107,13 @@ public class ControlUnit extends Thread{
 	/**
 	 * send command to Server
 	 * @param command
+	 * @return 
 	 * @throws IOException 
 	 */
-	private void send(String command) throws IOException {
-		if (command == null) return;
+	private Reply send(String command) throws IOException {
+		if (command == null) return null;
 		writeln(command);
-		Reply reply = new Reply(scanner);
+		return new Reply(scanner);
 	}
 	
 	public ControlUnit setEndpoint(String newHost, int newPort){
@@ -143,12 +153,17 @@ public class ControlUnit extends Thread{
 		Reply reply = new Reply(scanner);
 		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);
 		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 {
+		data = data.replace("{}", ""+bus);
 		socket.getOutputStream().write((data+"\n").getBytes(StandardCharsets.US_ASCII));
 		LOG.debug("sent {}.",data);
 	}