Implemented Turnout-Commands, added logging config
This commit is contained in:
@@ -6,18 +6,13 @@
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="resources"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
|
||||
4
pom.xml
4
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.5.4</version>
|
||||
<version>0.5.5</version>
|
||||
<name>Web4Rail</name>
|
||||
<description>Java Model Railway Control</description>
|
||||
<url>https://github.com/StephanRichter/Web4Rail</url>
|
||||
@@ -33,7 +33,7 @@
|
||||
<dependency>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>tools</artifactId>
|
||||
<version>1.1.8</version>
|
||||
<version>1.1.9</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
13
resources/logback.xml
Normal file
13
resources/logback.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{5} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -10,6 +10,8 @@ import java.io.OutputStreamWriter;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -30,7 +32,7 @@ public class Application {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Application.class);
|
||||
private static final String PORT = "port";
|
||||
private static final Charset UTF8 = StandardCharsets.UTF_8;
|
||||
|
||||
|
||||
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
|
||||
Configuration config = new Configuration(Configuration.dir("Web4Rail")+"/app.config");
|
||||
LOG.debug("Config: {}",config);
|
||||
|
||||
@@ -45,7 +45,7 @@ public class ControlUnit extends Thread{
|
||||
milis = Integer.parseInt(word.substring(word.length()-3));
|
||||
code = scanner.nextInt();
|
||||
message = scanner.nextLine().trim();
|
||||
LOG.debug("recv {}.{} {} {}.",secs,milis,code,message);
|
||||
LOG.info("recv {}.{} {} {}.",secs,milis,code,message);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -211,7 +211,7 @@ public class ControlUnit extends Thread{
|
||||
private void writeln(String data) throws IOException {
|
||||
data = data.replace("{}", ""+bus);
|
||||
socket.getOutputStream().write((data+"\n").getBytes(StandardCharsets.US_ASCII));
|
||||
LOG.debug("sent {}.",data);
|
||||
LOG.info("sent {}.",data);
|
||||
}
|
||||
|
||||
public void update(HashMap<String, String> params) {
|
||||
|
||||
7
src/main/java/de/srsoftware/web4rail/Device.java
Normal file
7
src/main/java/de/srsoftware/web4rail/Device.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package de.srsoftware.web4rail;
|
||||
|
||||
public interface Device {
|
||||
public static final String ADDRESS = "address";
|
||||
public static final String PORT = "port";
|
||||
public static final String PROTOCOL = "proto";
|
||||
}
|
||||
@@ -299,11 +299,15 @@ public class Plan {
|
||||
LOG.warn("Was not able to load routes!",e);
|
||||
}
|
||||
try {
|
||||
plan.controlUnit.load(filename+".cu");
|
||||
plan.controlUnit.start();
|
||||
plan.controlUnit.load(filename+".cu");
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Was not able to load control unit settings!",e);
|
||||
}
|
||||
try {
|
||||
plan.controlUnit.start();
|
||||
} catch (Exception e) {
|
||||
LOG.warn("Was not able to establish connection to control unit!");
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
|
||||
12
src/main/java/de/srsoftware/web4rail/Protocol.java
Normal file
12
src/main/java/de/srsoftware/web4rail/Protocol.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package de.srsoftware.web4rail;
|
||||
|
||||
public enum Protocol{
|
||||
DCC14,
|
||||
DCC27,
|
||||
DCC28,
|
||||
DCC128,
|
||||
MOTO,
|
||||
FLEISCH,
|
||||
SELECTRIX,
|
||||
ZIMO;
|
||||
}
|
||||
@@ -6,7 +6,9 @@ import java.util.Vector;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
@@ -15,16 +17,10 @@ import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Radio;
|
||||
|
||||
public class Locomotive extends Car {
|
||||
|
||||
public enum Protocol{
|
||||
DCC14,DCC27,DCC28,DCC128,MOTO,FLEISCH,SELECTRIX,ZIMO;
|
||||
}
|
||||
public class Locomotive extends Car implements Device{
|
||||
|
||||
private static final String REVERSE = "reverse";
|
||||
public static final String LOCOMOTIVE = "locomotive";
|
||||
private static final String PROTOCOL = "protocol";
|
||||
private static final String ADDRESS = "address";
|
||||
private boolean reverse = false;
|
||||
private Protocol proto = Protocol.DCC128;
|
||||
private int address = 3;
|
||||
|
||||
@@ -5,9 +5,10 @@ import de.srsoftware.tools.Tag;
|
||||
public class Fieldset extends Tag {
|
||||
|
||||
private static final long serialVersionUID = -1643025934527173421L;
|
||||
public static final String TYPE = "fieldset";
|
||||
|
||||
public Fieldset(String title) {
|
||||
super("fieldset");
|
||||
super(TYPE);
|
||||
if (title != null) new Tag("legend").content(title).addTo(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,5 +18,10 @@ public class Input extends Tag{
|
||||
|
||||
public Tag hideIn(Tag form) {
|
||||
return this.attr("type", "hidden").addTo(form);
|
||||
}
|
||||
}
|
||||
|
||||
public Input numeric() {
|
||||
attr("type","numeric");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,6 +78,7 @@ public abstract class Tile {
|
||||
}
|
||||
|
||||
public Object click() throws IOException {
|
||||
LOG.debug("Tile.click()");
|
||||
return propMenu();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +1,126 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public abstract class Turnout extends Tile {
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Radio;
|
||||
|
||||
public abstract class Turnout extends Tile implements Device{
|
||||
public static final String STATE = "state";
|
||||
private static final String PORT_A = "port_a";
|
||||
private static final String PORT_B = "port_b";
|
||||
protected static final String STRAIGHT = "straight";
|
||||
|
||||
private Protocol protocol = Protocol.DCC128;
|
||||
protected int address = 0;
|
||||
protected int portA = 0, portB = 0;
|
||||
protected int delay = 400;
|
||||
protected boolean initialized;
|
||||
|
||||
public enum State{
|
||||
LEFT,STRAIGHT,RIGHT,UNDEF;
|
||||
}
|
||||
|
||||
protected State state = State.STRAIGHT;
|
||||
|
||||
@Override
|
||||
public Object click() throws IOException {
|
||||
LOG.debug("Turnout.click()");
|
||||
Object o = super.click();
|
||||
if (address != 0 && !initialized) {
|
||||
String p = null;
|
||||
switch (protocol) {
|
||||
case DCC14:
|
||||
case DCC27:
|
||||
case DCC28:
|
||||
case DCC128:
|
||||
p = "N";
|
||||
break;
|
||||
case MOTO:
|
||||
p = "M";
|
||||
break;
|
||||
case SELECTRIX:
|
||||
p = "S";
|
||||
break;
|
||||
default:
|
||||
p = "P";
|
||||
}
|
||||
plan.queue("INIT {} GA "+address+" "+p);
|
||||
initialized = true;
|
||||
}
|
||||
return o;
|
||||
}
|
||||
|
||||
protected void init() {
|
||||
if (!initialized) {
|
||||
plan.queue("INIT {} GA "+address+" "+proto());
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (portA != 0) json.put(PORT_A, portA);
|
||||
if (portB != 0) json.put(PORT_B, portB);
|
||||
if (address != 0) json.put(ADDRESS, address);
|
||||
json.put(PROTOCOL, protocol);
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Tile load(JSONObject json) throws IOException {
|
||||
if (json.has(ADDRESS)) address = json.getInt(ADDRESS);
|
||||
if (json.has(PORT_A)) portA = json.getInt(PORT_A);
|
||||
if (json.has(PORT_B)) portB = json.getInt(PORT_B);
|
||||
if (json.has(PROTOCOL)) protocol = Protocol.valueOf(json.getString(PROTOCOL));
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag propForm() {
|
||||
Tag form = super.propForm();
|
||||
Fieldset fieldset = new Fieldset(t("Decoder settings"));
|
||||
Label protocol = new Label(t("Protocol:"));
|
||||
for (Protocol proto : Protocol.values()) {
|
||||
new Radio(PROTOCOL, proto.toString(), t(proto.toString()), proto == this.protocol).addTo(protocol);
|
||||
}
|
||||
protocol.addTo(fieldset).addTo(form);
|
||||
new Input(ADDRESS, address).numeric().addTo(new Label(t("Address"))).addTo(fieldset);
|
||||
return form;
|
||||
}
|
||||
|
||||
private char proto() {
|
||||
switch (protocol) {
|
||||
case DCC14:
|
||||
case DCC27:
|
||||
case DCC28:
|
||||
case DCC128:
|
||||
return 'N';
|
||||
case MOTO:
|
||||
return 'M';
|
||||
case SELECTRIX:
|
||||
return 'S';
|
||||
default:
|
||||
return 'P';
|
||||
}
|
||||
}
|
||||
|
||||
public State state() {
|
||||
return state;
|
||||
}
|
||||
|
||||
public void state(State newState) throws IOException {
|
||||
state = newState;
|
||||
LOG.debug("Setting {} to {}",this,state);
|
||||
plan.stream("place "+tag(null));
|
||||
}
|
||||
|
||||
public abstract void state(State newState) throws IOException;
|
||||
|
||||
@Override
|
||||
public Tag tag(Map<String, Object> replacements) throws IOException {
|
||||
Tag tag = super.tag(replacements);
|
||||
@@ -32,4 +131,13 @@ public abstract class Turnout extends Tile {
|
||||
public void toggle() {
|
||||
state = State.STRAIGHT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tile update(HashMap<String, String> params) throws IOException {
|
||||
if (params.containsKey(PROTOCOL)) protocol = Protocol.valueOf(params.get(PROTOCOL));
|
||||
if (params.containsKey(ADDRESS)) address = Integer.parseInt(params.get(ADDRESS));
|
||||
if (params.containsKey(PORT_A)) portA = Integer.parseInt(params.get(PORT_A));
|
||||
if (params.containsKey(PORT_B)) portB = Integer.parseInt(params.get(PORT_B));
|
||||
return super.update(params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -26,4 +27,10 @@ public class Turnout3E extends Turnout{
|
||||
return new HashMap<>();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void state(State newState) throws IOException {
|
||||
// TODO Auto-generated method stub
|
||||
LOG.warn("Turnout3E.state({}) not implemented, yet!",newState);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,65 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class TurnoutL extends Turnout {
|
||||
|
||||
private static final String LEFT = "left";
|
||||
|
||||
@Override
|
||||
public Object click() throws IOException {
|
||||
LOG.debug("TurnoutL.click()");
|
||||
Object o = super.click();
|
||||
if (route != null) {
|
||||
plan.stream(t("{} is locked by {}!",this,route));
|
||||
} else {
|
||||
state = (state == State.STRAIGHT) ? State.LEFT : State.STRAIGHT;
|
||||
plan.stream("place "+tag(null));
|
||||
} else state(state == State.STRAIGHT ? State.LEFT : State.STRAIGHT);
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag propForm() {
|
||||
Tag form = super.propForm();
|
||||
Tag fieldset = null;
|
||||
for (Tag child : form.children()) {
|
||||
if (child.is(Fieldset.TYPE)) {
|
||||
fieldset = child;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return propMenu();
|
||||
new Input(STRAIGHT, portA).numeric().addTo(new Label(t("Straight port"))).addTo(fieldset);
|
||||
new Input(LEFT, portB).numeric().addTo(new Label(t("Left port"))).addTo(fieldset);
|
||||
return form;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tile update(HashMap<String, String> params) throws IOException {
|
||||
if (params.containsKey(STRAIGHT)) portA = Integer.parseInt(params.get(STRAIGHT));
|
||||
if (params.containsKey(LEFT)) portB = Integer.parseInt(params.get(LEFT));
|
||||
return super.update(params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void state(State newState) throws IOException {
|
||||
init();
|
||||
LOG.debug("Setting {} to {}",this,newState);
|
||||
int p = 0;
|
||||
switch (newState) {
|
||||
case LEFT:
|
||||
p = portB;
|
||||
break;
|
||||
case STRAIGHT:
|
||||
p = portA;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
if (p != 0) plan.queue("SET {} GA "+address+" "+p+" 1 "+delay);
|
||||
state = newState;
|
||||
plan.stream("place "+tag(null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,16 +1,65 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class TurnoutR extends Turnout {
|
||||
|
||||
private static final String RIGHT = "right";
|
||||
|
||||
@Override
|
||||
public Object click() throws IOException {
|
||||
LOG.debug("Turnoutr.click()");
|
||||
Object o = super.click();
|
||||
if (route != null) {
|
||||
plan.stream(t("{} is locked by {}!",this,route));
|
||||
} else {
|
||||
state = (state == State.STRAIGHT) ? State.RIGHT : State.STRAIGHT;
|
||||
plan.stream("place "+tag(null));
|
||||
} else state(state == State.STRAIGHT ? State.RIGHT : State.STRAIGHT);
|
||||
return o;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag propForm() {
|
||||
Tag form = super.propForm();
|
||||
Tag fieldset = null;
|
||||
for (Tag child : form.children()) {
|
||||
if (child.is(Fieldset.TYPE)) {
|
||||
fieldset = child;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return propMenu();
|
||||
new Input(STRAIGHT, portA).addTo(new Label(t("Straight port"))).addTo(fieldset);
|
||||
new Input(RIGHT, portB).addTo(new Label(t("Right port"))).addTo(fieldset);
|
||||
return form;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tile update(HashMap<String, String> params) throws IOException {
|
||||
if (params.containsKey(STRAIGHT)) portA = Integer.parseInt(params.get(STRAIGHT));
|
||||
if (params.containsKey(RIGHT)) portB = Integer.parseInt(params.get(RIGHT));
|
||||
return super.update(params);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void state(State newState) throws IOException {
|
||||
init();
|
||||
LOG.debug("Setting {} to {}",this,newState);
|
||||
int p = 0;
|
||||
switch (newState) {
|
||||
case RIGHT:
|
||||
p = portB;
|
||||
break;
|
||||
case STRAIGHT:
|
||||
p = portA;
|
||||
break;
|
||||
default:
|
||||
}
|
||||
if (p != 0) plan.queue("SET {} GA "+address+" "+p+" 1 "+delay);
|
||||
state = newState;
|
||||
plan.stream("place "+tag(null));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user