Browse Source

working on new storage format

lookup-tables
Stephan Richter 5 years ago
parent
commit
72858b4aba
  1. 1
      .gitignore
  2. 2
      pom.xml
  3. 56
      src/main/java/de/srsoftware/web4rail/Plan.java
  4. 5
      src/main/java/de/srsoftware/web4rail/Route.java
  5. 4
      src/main/java/de/srsoftware/web4rail/actions/ActivateRoute.java
  6. 40
      src/main/java/de/srsoftware/web4rail/moving/Car.java
  7. 15
      src/main/java/de/srsoftware/web4rail/moving/Locomotive.java
  8. 83
      src/main/java/de/srsoftware/web4rail/moving/Train.java
  9. 10
      src/main/java/de/srsoftware/web4rail/tags/Div.java
  10. 13
      src/main/java/de/srsoftware/web4rail/tags/TagWithId.java
  11. 16
      src/main/java/de/srsoftware/web4rail/tiles/Block.java
  12. 7
      src/main/java/de/srsoftware/web4rail/tiles/Signal.java
  13. 107
      src/main/java/de/srsoftware/web4rail/tiles/Tile.java

1
.gitignore vendored

@ -2,3 +2,4 @@ @@ -2,3 +2,4 @@
*.pyc
/bin/
/target/
/default.*

2
pom.xml

@ -4,7 +4,7 @@ @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.srsoftware</groupId>
<artifactId>web4rail</artifactId>
<version>0.3.3</version>
<version>0.3.4</version>
<name>Web4Rail</name>
<description>Java Model Railway Control</description>
<url>https://github.com/StephanRichter/Web4Rail</url>

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

@ -25,7 +25,9 @@ import org.slf4j.LoggerFactory; @@ -25,7 +25,9 @@ import org.slf4j.LoggerFactory;
import de.keawe.tools.translations.Translation;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.moving.Car;
import de.srsoftware.web4rail.moving.Train;
import de.srsoftware.web4rail.tags.Div;
import de.srsoftware.web4rail.tiles.Block;
import de.srsoftware.web4rail.tiles.BlockH;
import de.srsoftware.web4rail.tiles.BlockV;
@ -112,7 +114,7 @@ public class Plan { @@ -112,7 +114,7 @@ public class Plan {
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
private static final String ACTION_TRAIN = "train";
private HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
public HashMap<Integer,HashMap<Integer,Tile>> tiles = new HashMap<Integer,HashMap<Integer,Tile>>();
private HashSet<Block> blocks = new HashSet<Block>();
private HashMap<String, Route> routes = new HashMap<String, Route>();
@ -123,8 +125,8 @@ public class Plan { @@ -123,8 +125,8 @@ public class Plan {
private Tag actionMenu() throws IOException {
Tag tileMenu = new Tag("div").clazz("actions").content(t("Actions"));
StringBuffer tiles = new StringBuffer();
tiles.append(new Tag("div").id("save").content(t("Save plan")));
tiles.append(new Tag("div").id("analyze").content(t("Analyze plan")));
tiles.append(new Div("save").content(t("Save plan")));
tiles.append(new Div("analyze").content(t("Analyze plan")));
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
}
@ -206,11 +208,8 @@ public class Plan { @@ -206,11 +208,8 @@ public class Plan {
for (Entry<Connector, State> entry: connectors.entrySet()) {
route = routes.remove(0);
connector = entry.getKey();
State state = entry.getValue();
route.setLast(state);
if (connectors.size()>1) {
LOG.debug("RESUMING from {}",tile);
}
route.setLast(entry.getValue());
if (connectors.size()>1) LOG.debug("RESUMING from {}",tile);
results.addAll(follow(route,connector));
}
@ -229,7 +228,7 @@ public class Plan { @@ -229,7 +228,7 @@ public class Plan {
}
private Tag heartbeat() {
return new Tag("div").id("heartbeat").content("");
return new Div("heartbeat").content("");
}
public void heatbeat() {
@ -323,16 +322,16 @@ public class Plan { @@ -323,16 +322,16 @@ public class Plan {
}
private Tag messages() {
return new Tag("div").id("messages").content("");
return new Div("messages").content("");
}
private Tag moveMenu() {
Tag tileMenu = new Tag("div").clazz("move").title(t("Move tiles")).content(t("↹"));
StringBuffer tiles = new StringBuffer();
tiles.append(new Tag("div").id("west").title(t("Move west")).content("↤"));
tiles.append(new Tag("div").id("east").title(t("Move east")).content("↦"));
tiles.append(new Tag("div").id("north").title(t("Move north")).content("↥"));
tiles.append(new Tag("div").id("south").title(t("Move south")).content("↧"));
tiles.append(new Div("west").title(t("Move west")).content("↤"));
tiles.append(new Div("east").title(t("Move east")).content("↦"));
tiles.append(new Div("north").title(t("Move north")).content("↥"));
tiles.append(new Div("south").title(t("Move south")).content("↧"));
return new Tag("div").clazz("list").content(tiles.toString()).addTo(tileMenu);
}
@ -391,6 +390,10 @@ public class Plan { @@ -391,6 +390,10 @@ public class Plan {
return false;
}
public void place(Tile tile) throws IOException {
stream("place "+tile.tag(null));
}
public Object process(HashMap<String, String> params) {
try {
String action = params.get(ACTION);
@ -420,6 +423,7 @@ public class Plan { @@ -420,6 +423,7 @@ public class Plan {
} catch (Exception e) {
String msg = e.getMessage();
if (msg == null || msg.isEmpty()) msg = t("An unknown error occured!");
LOG.debug(msg,e);
return msg;
}
}
@ -455,24 +459,12 @@ public class Plan { @@ -455,24 +459,12 @@ public class Plan {
private String saveTo(String name) throws IOException {
if (name == null || name.isEmpty()) throw new NullPointerException("Name must not be empty!");
File file = new File(name+".plan");
Car.saveAll(name+".cars");
Tile.saveAll(this.tiles,name+".tiles");
Train.saveAll(name+".trains"); // refers to cars, blocks
File file = new File(name+".routes");
BufferedWriter br = new BufferedWriter(new FileWriter(file));
for (Entry<Integer, HashMap<Integer, Tile>> column : tiles.entrySet()) {
int x = column.getKey();
for (Entry<Integer, Tile> row : column.getValue().entrySet()) {
int y = row.getKey();
Tile tile = row.getValue().position(x, y);
if (tile != null && !(tile instanceof Shadow)) {
br.append(x+":"+y+":"+tile.getClass().getSimpleName());
JSONObject config = tile.config();
if (!config.isEmpty()) br.append(":"+config);
br.append("\n");
}
}
}
br.close();
file = new File(name+".routes");
br = new BufferedWriter(new FileWriter(file));
for (Route route: routes.values()) {
br.append(route.id()+"="+route.json()+"\n");
}
@ -486,7 +478,7 @@ public class Plan { @@ -486,7 +478,7 @@ public class Plan {
for (int i=1; i<tile.len(); i++) set(x+i,y,new Shadow(tile));
for (int i=1; i<tile.height(); i++) set(x,y+i,new Shadow(tile));
set_intern(x,y,tile);
stream("place "+tile.tag(null));
place(tile);
}
private void set_intern(int x, int y, Tile tile) {

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

@ -50,10 +50,11 @@ public class Route { @@ -50,10 +50,11 @@ public class Route {
/**
* Route wurde von Zug betreten
* @throws IOException
*/
public void activate() {
public void activate() throws IOException {
LOG.debug("{} aktiviert.",this);
for (Tile tile : path) tile.occupy(this);
for (Tile tile : path) tile.train(train);
}
public Tile add(Tile tile, Direction direrction) {

4
src/main/java/de/srsoftware/web4rail/actions/ActivateRoute.java

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
package de.srsoftware.web4rail.actions;
import java.io.IOException;
import de.srsoftware.web4rail.Route;
public class ActivateRoute extends Action {
@ -11,7 +13,7 @@ public class ActivateRoute extends Action { @@ -11,7 +13,7 @@ public class ActivateRoute extends Action {
}
@Override
public void fire() {
public void fire() throws IOException {
route.activate();
}
}

40
src/main/java/de/srsoftware/web4rail/moving/Car.java

@ -1,14 +1,54 @@ @@ -1,14 +1,54 @@
package de.srsoftware.web4rail.moving;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map.Entry;
import org.json.JSONObject;
public class Car {
private static final String ID = "id";
private static final String NAME = "name";
private static final String LENGTH = "length";
private static HashMap<String,Car> cars = new HashMap<String, Car>();
public int length;
private String name;
private String id;
public Car(String name) {
this(name,null);
}
public Car(String name, String id) {
this.name = name;
this.id = id == null ? ""+new Date().getTime() : id;
cars.put(this.id, this);
}
public String id() {
return id;
}
public JSONObject json() {
JSONObject json = new JSONObject();
json.put(ID,id);
json.put(NAME, name);
json.put(LENGTH, length);
return json;
}
String name(){
return name;
}
public static void saveAll(String filename) throws IOException {
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
for (Entry<String, Car> entry: cars.entrySet()) {
file.write(entry.getValue().json()+"\n");
}
file.close();
}
}

15
src/main/java/de/srsoftware/web4rail/moving/Locomotive.java

@ -1,10 +1,25 @@ @@ -1,10 +1,25 @@
package de.srsoftware.web4rail.moving;
import org.json.JSONObject;
public class Locomotive extends Car {
private static final String REVERSE = "reverse";
private static final String LOCOMOTIVE = "locomotive";
private boolean reverse = false;
public Locomotive(String name) {
super(name);
}
@Override
public JSONObject json() {
JSONObject json = super.json();
JSONObject loco = new JSONObject();
loco.put(REVERSE, reverse);
json.put(LOCOMOTIVE, loco);
return json;
}
public void setSpeed(int v) {
// TODO Auto-generated method stub

83
src/main/java/de/srsoftware/web4rail/moving/Train.java

@ -1,11 +1,16 @@ @@ -1,11 +1,16 @@
package de.srsoftware.web4rail.moving;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Vector;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -23,7 +28,35 @@ import de.srsoftware.web4rail.tiles.Signal; @@ -23,7 +28,35 @@ import de.srsoftware.web4rail.tiles.Signal;
import de.srsoftware.web4rail.tiles.Tile;
public class Train {
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
private static final HashMap<Long, Train> trains = new HashMap<>();
public static final String ID = "id";
public long id;
private static final String NAME = "name";
private String name = null;
private static final String BLOCK = "block";
private Block block = null;
private static final String ROUTE = "route";
public Route route;
private static final String DIRECTION = "direction";
private Direction direction;
private static final String PUSH_PULL = "pushPull";
private boolean pushPull = false;
private static final String CARS = "cars";
private Vector<Car> cars = new Vector<Car>();
private static final String LOCOS = "locomotives";
private Vector<Locomotive> locos = new Vector<Locomotive>();
private class Autopilot extends Thread{
@Override
public void run() {
@ -47,35 +80,42 @@ public class Train { @@ -47,35 +80,42 @@ public class Train {
}
}
}
private static final Logger LOG = LoggerFactory.getLogger(Train.class);
private static final String PUSH_PULL = "pushPull";
private static int count = 0;
private static final HashMap<Integer, Train> trains = new HashMap<Integer, Train>();
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<Car> cars = new Vector<Car>();
private String name = null;
private Block block = null;
public Route route;
public int speed = 0;
private Direction direction;
private boolean pushPull = false;
public int id;
private Autopilot autopilot = null;
public Train(Locomotive loco) {
id = ++count;
id = new Date().getTime();
add(loco);
trains.put(id, this);
}
private JSONObject json() {
JSONObject json = new JSONObject();
json.put(ID, id);
json.put(NAME,name);
if (block != null) json.put(BLOCK, block.id());
if (route != null) json.put(ROUTE, route.id());
if (direction != null) json.put(DIRECTION, direction);
json.put(PUSH_PULL, pushPull);
Vector<String> locoIds = new Vector<String>();
for (Locomotive loco : locos) locoIds.add(loco.id());
json.put(LOCOS, locoIds);
Vector<String> carIds = new Vector<String>();
for (Car car : cars) carIds.add(car.id());
json.put(CARS,carIds);
return json;
}
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));
long id = Long.parseLong(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!");
@ -118,7 +158,7 @@ public class Train { @@ -118,7 +158,7 @@ public class Train {
direction = dir;
return this;
}
public int length() {
int result = 0;
for (Locomotive loco : locos) result += loco.length;
@ -145,7 +185,7 @@ public class Train { @@ -145,7 +185,7 @@ public class Train {
Form form = new 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);
@ -167,6 +207,15 @@ public class Train { @@ -167,6 +207,15 @@ public class Train {
return window;
}
public static void saveAll(String filename) throws IOException {
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
for (Entry<Long, Train> entry:trains.entrySet()) {
Train train = entry.getValue();
file.write(train.json()+"\n");
}
file.close();
}
public void setSpeed(int v) {
LOG.debug("Setting speed to {} kmh.",v);
for (Locomotive loco : locos) loco.setSpeed(v);

10
src/main/java/de/srsoftware/web4rail/tags/Div.java

@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
package de.srsoftware.web4rail.tags;
public class Div extends TagWithId {
private static final long serialVersionUID = 749501876155859607L;
public Div(String id) {
super("div", id);
}
}

13
src/main/java/de/srsoftware/web4rail/tags/TagWithId.java

@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
package de.srsoftware.web4rail.tags;
import de.srsoftware.tools.Tag;
public class TagWithId extends Tag {
public TagWithId(String type, String id) {
super(type);
id(id);
}
private static final long serialVersionUID = 6349230653857414636L;
}

16
src/main/java/de/srsoftware/web4rail/tiles/Block.java

@ -79,22 +79,8 @@ public abstract class Block extends StretchableTile{ @@ -79,22 +79,8 @@ public abstract class Block extends StretchableTile{
}
public void train(Train train) throws IOException {
this.train = train;
if (train != null) train.block(this);
plan.stream("place "+tag(null));
}
public Train train() {
return train;
}
public void unlock() {
route = null;
classes.remove("locked");
if (train != null) {
classes.remove("occupied");
plan.stream("dropclass tile-"+x+"-"+y+" locked");
} else plan.stream("dropclass tile-"+x+"-"+y+" locked occupied");
super.train(train);
}
@Override

7
src/main/java/de/srsoftware/web4rail/tiles/Signal.java

@ -2,6 +2,7 @@ package de.srsoftware.web4rail.tiles; @@ -2,6 +2,7 @@ package de.srsoftware.web4rail.tiles;
import java.io.IOException;
import java.util.Map;
import java.util.Vector;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Plan.Direction;
@ -13,7 +14,13 @@ public abstract class Signal extends Tile{ @@ -13,7 +14,13 @@ public abstract class Signal extends Tile{
public Signal() {
super();
}
@Override
protected Vector<String> classes() {
Vector<String> classes = super.classes();
classes.add("signal");
return classes;
}
public abstract boolean isAffectedFrom(Direction dir);

107
src/main/java/de/srsoftware/web4rail/tiles/Tile.java

@ -1,6 +1,8 @@ @@ -1,6 +1,8 @@
package de.srsoftware.web4rail.tiles;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
@ -23,24 +25,54 @@ import de.srsoftware.web4rail.Plan; @@ -23,24 +25,54 @@ import de.srsoftware.web4rail.Plan;
import de.srsoftware.web4rail.Plan.Direction;
import de.srsoftware.web4rail.Route;
import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.moving.Train;
import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Radio;
public abstract class Tile {
private static final String ID = "id";
private static final String TYPE = "type";
private static final String LOCKED = "locked";
private static final String POS = "pos";
private static final String X = "x";
private static final Object Y = "y";
public int x = -1,y = -1;
protected HashSet<String> classes = new HashSet<>();
private static final String ROUTE = "route";
protected Route route;
private static final String OCCUPIED = "occupied";
private Train train;
private static final String ONEW_WAY = "one_way";
protected Direction oneWay = null;
protected HashSet<Shadow> shadows = new HashSet<>();
private HashSet<Route> routes = new HashSet<>();
protected Plan plan;
protected Route route;
protected Direction oneWay = null;
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
protected static Logger LOG = LoggerFactory.getLogger(Tile.class);
public Tile() {
protected Vector<String> classes(){
Vector<String> classes = new Vector<String>();
classes.add("tile");
classes.add(getClass().getSimpleName());
if (route != null) classes.add(LOCKED);
if (train != null) classes.add(OCCUPIED);
return classes;
}
public JSONObject json() {
JSONObject json = new JSONObject();
json.put(ID, id());
json.put(TYPE, getClass().getSimpleName());
JSONObject pos = new JSONObject(Map.of(X,x,Y,y));
json.put(POS, pos);
if (route != null) json.put(ROUTE, route.id());
if (oneWay != null) json.put(ONEW_WAY, oneWay);
return json;
}
public void add(Route route) {
@ -74,23 +106,19 @@ public abstract class Tile { @@ -74,23 +106,19 @@ public abstract class Tile {
return 1;
}
public String id() {
return "tile-"+x+"-"+y;
}
public int len() {
return 1;
}
public void lock(Route route) {
public void lock(Route route) throws IOException {
this.route = route;
classes.add("locked");
plan.stream("addclass tile-"+x+"-"+y+" locked");
plan.place(this);
}
public void occupy(Route route) {
this.route = route;
classes.add("occupied");
plan.stream("addclass tile-"+x+"-"+y+" occupied");
}
public void plan(Plan plan) {
this.plan = plan;
}
@ -168,9 +196,29 @@ public abstract class Tile { @@ -168,9 +196,29 @@ public abstract class Tile {
return line;
}
public Route route() {
return route;
}
public HashSet<Route> routes() {
return routes;
}
public static void saveAll(HashMap<Integer, HashMap<Integer, Tile>> tiles ,String filename) throws IOException {
BufferedWriter file = new BufferedWriter(new FileWriter(filename));
for (Entry<Integer, HashMap<Integer, Tile>> column : tiles.entrySet()) {
for (Entry<Integer, Tile> row : column.getValue().entrySet()) {
Tile tile = row.getValue();
if (tile == null || tile instanceof Shadow) continue;
file.append(tile.json()+"\n");
}
}
file.close();
}
protected static String t(String txt, Object...fills) {
return Translation.get(Application.class, txt, fills);
}
public Tag tag(Map<String,Object> replacements) throws IOException {
int width = 100*len();
@ -181,7 +229,7 @@ public abstract class Tile { @@ -181,7 +229,7 @@ public abstract class Tile {
String style = "";
Tag svg = new Tag("svg")
.id((x!=-1 && y!=-1)?("tile-"+x+"-"+y):(getClass().getSimpleName()))
.clazz(classes)
.clazz(classes())
.size(100,100)
.attr("name", getClass().getSimpleName())
.attr("viewbox", "0 0 "+width+" "+height);
@ -214,6 +262,8 @@ public abstract class Tile { @@ -214,6 +262,8 @@ public abstract class Tile {
break;
case WEST:
new Tag("polygon").clazz("oneway").attr("points", "0,50 25,35 25,65").addTo(svg);
break;
default:
}
}
} else {
@ -226,22 +276,25 @@ public abstract class Tile { @@ -226,22 +276,25 @@ public abstract class Tile {
return svg;
}
protected static String t(String txt, Object...fills) {
return Translation.get(Application.class, txt, fills);
}
@Override
public String toString() {
return t("{}({},{})",getClass().getSimpleName(),x,y) ;
}
public Train train() {
return train;
}
public void train(Train train) throws IOException {
this.train = train;
plan.place(this);
}
public void unlock() {
public void unlock() throws IOException {
route = null;
classes.remove("locked");
classes.remove("occupied");
plan.stream("dropclass tile-"+x+"-"+y+" locked occupied");
train = null;
plan.place(this);
}
public Tile update(HashMap<String, String> params) {
@ -256,8 +309,4 @@ public abstract class Tile { @@ -256,8 +309,4 @@ public abstract class Tile {
}
return this;
}
public Route route() {
return route;
}
}

Loading…
Cancel
Save