implemented auto-mode
This commit is contained in:
2
pom.xml
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.3.2</version>
|
<version>0.3.3</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>
|
||||||
|
|||||||
@@ -147,8 +147,8 @@ function runAction(ev){
|
|||||||
return request({action:ev.target.id,file:'default'}); // TODO: ask for name
|
return request({action:ev.target.id,file:'default'}); // TODO: ask for name
|
||||||
}
|
}
|
||||||
|
|
||||||
function train(x,y,action){
|
function train(id,mode){
|
||||||
return request({action:action+"Train",x:x,y:y});
|
return request({action:"train",id:id,mode:mode});
|
||||||
}
|
}
|
||||||
|
|
||||||
function stream(ev){
|
function stream(ev){
|
||||||
|
|||||||
@@ -110,9 +110,7 @@ public class Plan {
|
|||||||
private static final String ID = "id";
|
private static final String ID = "id";
|
||||||
private static final String ROUTE = "route";
|
private static final String ROUTE = "route";
|
||||||
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
|
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
|
||||||
private static final String ACTION_SHOW_TRAIN = "showTrain";
|
private static final String ACTION_TRAIN = "train";
|
||||||
private static final String ACTION_START_TRAIN = "startTrain";
|
|
||||||
private static final String ACTION_UPDATE_TRAIN = "updateTrain";
|
|
||||||
|
|
||||||
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
|
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
|
||||||
private HashSet<Block> blocks = new HashSet<Block>();
|
private HashSet<Block> blocks = new HashSet<Block>();
|
||||||
@@ -411,15 +409,10 @@ public class Plan {
|
|||||||
return routeProperties(params.get(ID));
|
return routeProperties(params.get(ID));
|
||||||
case ACTION_SAVE:
|
case ACTION_SAVE:
|
||||||
return saveTo(params.get(FILE));
|
return saveTo(params.get(FILE));
|
||||||
case ACTION_SHOW_TRAIN:
|
case ACTION_TRAIN:
|
||||||
return show(train(get(params.get(X),params.get(Y),true)));
|
return trainAction(params);
|
||||||
case ACTION_START_TRAIN:
|
|
||||||
return start(train(get(params.get(X),params.get(Y),true)));
|
|
||||||
case ACTION_UPDATE:
|
case ACTION_UPDATE:
|
||||||
return update(params);
|
return update(params);
|
||||||
case ACTION_UPDATE_TRAIN:
|
|
||||||
return updateTrain(params);
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
LOG.warn("Unknown action: {}",action);
|
LOG.warn("Unknown action: {}",action);
|
||||||
}
|
}
|
||||||
@@ -431,12 +424,9 @@ public class Plan {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Train train(Tile tile) {
|
private Object trainAction(HashMap<String, String> params) throws IOException {
|
||||||
if (tile instanceof Block) {
|
Object result = Train.action(params);
|
||||||
Block block = (Block) tile;
|
return result instanceof Train ? html() : result;
|
||||||
return block.train();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object routeProperties(String routeId) {
|
private Object routeProperties(String routeId) {
|
||||||
@@ -508,15 +498,6 @@ public class Plan {
|
|||||||
tile.position(x, y).plan(this);
|
tile.position(x, y).plan(this);
|
||||||
column.put(y,tile);
|
column.put(y,tile);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Tag show(Train train) {
|
|
||||||
return (train == null) ? null : train.props();
|
|
||||||
}
|
|
||||||
|
|
||||||
private String start(Train train) throws IOException {
|
|
||||||
if (train == null) return null;
|
|
||||||
return train.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public synchronized void stream(String data) {
|
public synchronized void stream(String data) {
|
||||||
data = data.replaceAll("\n", "").replaceAll("\r", "");
|
data = data.replaceAll("\n", "").replaceAll("\r", "");
|
||||||
@@ -600,11 +581,6 @@ public class Plan {
|
|||||||
Tile tile = get(x,y,true);
|
Tile tile = get(x,y,true);
|
||||||
if (tile != null) set(x,y,tile.update(params));
|
if (tile != null) set(x,y,tile.update(params));
|
||||||
}
|
}
|
||||||
|
|
||||||
private Object updateTrain(HashMap<String, String> params) throws IOException {
|
|
||||||
Train.update(params);
|
|
||||||
return this.html();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void warn(Contact contact) {
|
public void warn(Contact contact) {
|
||||||
stream(t("Warning: {}",t("Ghost train @ {}",contact)));
|
stream(t("Warning: {}",t("Ghost train @ {}",contact)));
|
||||||
|
|||||||
@@ -133,6 +133,11 @@ public class Route {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Vector<Contact> contacts() {
|
||||||
|
return new Vector<>(contacts);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void finish() throws IOException {
|
public void finish() throws IOException {
|
||||||
startBlock.train(null);
|
startBlock.train(null);
|
||||||
endBlock.train(train.heading(endDirection.inverse()));
|
endBlock.train(train.heading(endDirection.inverse()));
|
||||||
|
|||||||
@@ -18,14 +18,42 @@ import de.srsoftware.web4rail.Window;
|
|||||||
import de.srsoftware.web4rail.tags.Checkbox;
|
import de.srsoftware.web4rail.tags.Checkbox;
|
||||||
import de.srsoftware.web4rail.tags.Form;
|
import de.srsoftware.web4rail.tags.Form;
|
||||||
import de.srsoftware.web4rail.tiles.Block;
|
import de.srsoftware.web4rail.tiles.Block;
|
||||||
|
import de.srsoftware.web4rail.tiles.Contact;
|
||||||
import de.srsoftware.web4rail.tiles.Signal;
|
import de.srsoftware.web4rail.tiles.Signal;
|
||||||
|
|
||||||
public class Train {
|
public class Train {
|
||||||
|
|
||||||
|
private class Autopilot extends Thread{
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
Vector<Contact> contacts = null;
|
||||||
|
while (true) {
|
||||||
|
if (route == null) {
|
||||||
|
Train.this.start();
|
||||||
|
contacts = route == null ? new Vector<Contact>() : route.contacts();
|
||||||
|
} else {
|
||||||
|
if (!contacts.isEmpty()) {
|
||||||
|
Contact contact = contacts.remove(0);
|
||||||
|
contact.activate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Thread.sleep(1000);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
|
||||||
private static final String PUSH_PULL = "pushPull";
|
private static final String PUSH_PULL = "pushPull";
|
||||||
private static int count = 0;
|
private static int count = 0;
|
||||||
private static final HashMap<Integer, Train> trains = new HashMap<Integer, Train>();
|
private static final HashMap<Integer, Train> trains = new HashMap<Integer, Train>();
|
||||||
private static final String ID = "id";
|
public static final String ID = "id";
|
||||||
|
public static final String MODE_START = "start";
|
||||||
|
public static final String MODE_SHOW = "show";
|
||||||
|
private static final String MODE_UPDATE = "update";
|
||||||
|
private static final String MODE_AUTO = "auto";
|
||||||
private Vector<Locomotive> locos = new Vector<Locomotive>();
|
private Vector<Locomotive> locos = new Vector<Locomotive>();
|
||||||
private Vector<Car> cars = new Vector<Car>();
|
private Vector<Car> cars = new Vector<Car>();
|
||||||
private String name = null;
|
private String name = null;
|
||||||
@@ -34,13 +62,36 @@ public class Train {
|
|||||||
public int speed = 0;
|
public int speed = 0;
|
||||||
private Direction direction;
|
private Direction direction;
|
||||||
private boolean pushPull = false;
|
private boolean pushPull = false;
|
||||||
private int id;
|
public int id;
|
||||||
|
private Autopilot autopilot = null;
|
||||||
|
|
||||||
public Train(Locomotive loco) {
|
public Train(Locomotive loco) {
|
||||||
id = ++count;
|
id = ++count;
|
||||||
add(loco);
|
add(loco);
|
||||||
trains.put(id, this);
|
trains.put(id, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Object action(HashMap<String, String> params) throws IOException {
|
||||||
|
if (!params.containsKey(Train.ID)) return t("No train id passed!");
|
||||||
|
int id = Integer.parseInt(params.get(Train.ID));
|
||||||
|
Train train = trains.get(id);
|
||||||
|
if (train == null) return(t("No train with id {}!",id));
|
||||||
|
if (!params.containsKey("mode")) return t("No mode set for train action!");
|
||||||
|
String mode = params.get("mode");
|
||||||
|
switch (mode) {
|
||||||
|
case MODE_AUTO:
|
||||||
|
return train.automatic();
|
||||||
|
case MODE_SHOW:
|
||||||
|
return train.props();
|
||||||
|
case MODE_START:
|
||||||
|
return train.start();
|
||||||
|
case MODE_UPDATE:
|
||||||
|
return train.update(params);
|
||||||
|
default: return t("Unknown mode {} for {}",mode,train);
|
||||||
|
}
|
||||||
|
|
||||||
|
//return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void add(Car car) {
|
public void add(Car car) {
|
||||||
if (car == null) return;
|
if (car == null) return;
|
||||||
@@ -49,6 +100,14 @@ public class Train {
|
|||||||
} else cars.add(car);
|
} else cars.add(car);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String automatic() {
|
||||||
|
if (autopilot == null) {
|
||||||
|
autopilot = new Autopilot();
|
||||||
|
autopilot.start();
|
||||||
|
}
|
||||||
|
return t("{} now in auto-mode",this);
|
||||||
|
}
|
||||||
|
|
||||||
public void block(Block block) {
|
public void block(Block block) {
|
||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
@@ -83,8 +142,9 @@ public class Train {
|
|||||||
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName()));
|
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName()));
|
||||||
|
|
||||||
Form form = new Form();
|
Form form = new Form();
|
||||||
new Tag("input").attr("type", "hidden").attr("name","action").attr("value", "updateTrain").addTo(form);
|
new Tag("input").attr("type", "hidden").attr("name","action").attr("value", "train").addTo(form);
|
||||||
new Tag("input").attr("type", "hidden").attr("name",ID).attr("value", id).addTo(form);
|
new Tag("input").attr("type", "hidden").attr("name",ID).attr("value", id).addTo(form);
|
||||||
|
new Tag("input").attr("type", "hidden").attr("name","mode").attr("value", MODE_UPDATE).addTo(form);
|
||||||
|
|
||||||
Checkbox pp = new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull);
|
Checkbox pp = new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull);
|
||||||
pp.addTo(form);
|
pp.addTo(form);
|
||||||
@@ -99,7 +159,8 @@ public class Train {
|
|||||||
new Tag("li").content(t("Current location: {}",block)).addTo(list);
|
new Tag("li").content(t("Current location: {}",block)).addTo(list);
|
||||||
new Tag("li").content(t("Direction: heading {}",direction)).addTo(list);
|
new Tag("li").content(t("Direction: heading {}",direction)).addTo(list);
|
||||||
|
|
||||||
new Tag("li").clazz("link").attr("onclick","train("+block.x+","+block.y+",'start')").content(t("start")).addTo(list).addTo(window);
|
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_START+"')").content(t("start")).addTo(list).addTo(window);
|
||||||
|
new Tag("li").clazz("link").attr("onclick","train("+id+",'"+MODE_AUTO+"')").content(t("auto")).addTo(list).addTo(window);
|
||||||
|
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
@@ -148,16 +209,14 @@ public class Train {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void turn() throws IOException {
|
private void turn() throws IOException {
|
||||||
direction = direction.inverse();
|
if (direction != null) direction = direction.inverse();
|
||||||
if (block != null) block.train(this);
|
if (block != null) block.train(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void update(HashMap<String, String> params) {
|
public Train update(HashMap<String, String> params) {
|
||||||
LOG.debug("update({})",params);
|
LOG.debug("update({})",params);
|
||||||
int id = Integer.parseInt(params.get(ID));
|
pushPull = params.containsKey(PUSH_PULL) && params.get(PUSH_PULL).equals("on");
|
||||||
Train train = trains.get(id);
|
return this;
|
||||||
if (train == null) return;
|
|
||||||
train.pushPull = params.containsKey(PUSH_PULL) && params.get(PUSH_PULL).equals("on");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,8 +56,8 @@ public abstract class Block extends StretchableTile{
|
|||||||
|
|
||||||
if (train != null) {
|
if (train != null) {
|
||||||
new Tag("h4").content(t("Train:")).addTo(window);
|
new Tag("h4").content(t("Train:")).addTo(window);
|
||||||
new Tag("span").clazz("link").attr("onclick","train("+x+","+y+",'show')").content(train.name()).addTo(window);
|
new Tag("span").clazz("link").attr("onclick","train("+train.id+",'"+Train.MODE_SHOW+"')").content(train.name()).addTo(window);
|
||||||
new Tag("span").clazz("link").attr("onclick","train("+x+","+y+",'start')").content(" - "+t("start")).addTo(window);
|
new Tag("span").clazz("link").attr("onclick","train("+train.id+",'"+Train.MODE_START+"')").content(" - "+t("start")).addTo(window);
|
||||||
}
|
}
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ public abstract class Contact extends Tile{
|
|||||||
private boolean active = false;
|
private boolean active = false;
|
||||||
|
|
||||||
|
|
||||||
private void activate() throws IOException {
|
public void activate() throws IOException {
|
||||||
active = true;
|
active = true;
|
||||||
stream();
|
stream();
|
||||||
new Thread() {
|
new Thread() {
|
||||||
|
|||||||
Reference in New Issue
Block a user