implemented scaled velocity
This commit is contained in:
@@ -37,6 +37,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
private static final String LENGTH = "length";
|
||||
private static final String STOCK_ID = "stock-id";
|
||||
private static final String TAGS = "tags";
|
||||
private static final String MAX_SPEED = "max_speed";
|
||||
protected HashSet<String> tags = new HashSet<String>();
|
||||
|
||||
private int id;
|
||||
@@ -45,6 +46,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
protected String stockId = "";
|
||||
private Train train;
|
||||
protected Plan plan;
|
||||
protected int maxSpeed = 0;
|
||||
|
||||
public Car(String name) {
|
||||
this(name,null);
|
||||
@@ -94,6 +96,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
json.put(ID,id);
|
||||
json.put(NAME, name);
|
||||
json.put(LENGTH, length);
|
||||
if (maxSpeed != 0) json.put(MAX_SPEED, maxSpeed);
|
||||
json.put(STOCK_ID, stockId);
|
||||
if (!tags.isEmpty()) json.put(TAGS, tags);
|
||||
return json;
|
||||
@@ -122,7 +125,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
|
||||
public static void loadAll(String filename, Plan plan) throws IOException {
|
||||
cars.clear();
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename, UTF8));
|
||||
String line = file.readLine();
|
||||
while (line != null) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
@@ -139,6 +142,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
protected Car load(JSONObject json) {
|
||||
if (json.has(ID)) id = json.getInt(ID);
|
||||
if (json.has(LENGTH)) length = json.getInt(LENGTH);
|
||||
if (json.has(MAX_SPEED)) maxSpeed = json.getInt(MAX_SPEED);
|
||||
if (json.has(STOCK_ID)) stockId = json.getString(STOCK_ID);
|
||||
if (json.has(TAGS)) json.getJSONArray(TAGS).forEach(elem -> { tags.add(elem.toString()); });
|
||||
return this;
|
||||
@@ -149,8 +153,8 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
new Tag("h4").content(t("known cars")).addTo(win);
|
||||
new Tag("p").content(t("Click on a name to edit the entry.")).addTo(win);
|
||||
|
||||
Table table = new Table().addHead(t("Stock ID"),t("Name"),t("Length"),t("Tags"));
|
||||
cars.values().stream().filter(car -> !(car instanceof Locomotive)).forEach(car -> table.addRow(car.stockId,car.link(),car.length,String.join(", ", car.tags())));
|
||||
Table table = new Table().addHead(t("Stock ID"),t("Name"),t("Max. Speed",speedUnit),t("Length"),t("Tags"));
|
||||
cars.values().stream().filter(car -> !(car instanceof Locomotive)).forEach(car -> table.addRow(car.stockId,car.maxSpeed == 0 ? "–":(car.maxSpeed+NBSP+speedUnit),car.link(),car.length,String.join(", ", car.tags())));
|
||||
table.addTo(win);
|
||||
|
||||
Form form = new Form("add-car-form");
|
||||
@@ -163,6 +167,10 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
return win;
|
||||
}
|
||||
|
||||
public int maxSpeed() {
|
||||
return maxSpeed;
|
||||
}
|
||||
|
||||
String name(){
|
||||
return name;
|
||||
}
|
||||
@@ -186,6 +194,7 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
new Input(STOCK_ID,stockId).addTo(new Label(t("Stock ID")+NBSP)).addTo(fieldset);
|
||||
new Input(LENGTH,length).attr("type", "number").addTo(new Label(t("Length")+NBSP)).addTo(fieldset);
|
||||
new Input(TAGS,String.join(", ", tags)).addTo(new Label(t("Tags")+NBSP)).addTo(fieldset);
|
||||
new Input(MAX_SPEED, maxSpeed).numeric().addTo(new Label(t("Maximum speed")+":"+NBSP)).content(NBSP+speedUnit).addTo(fieldset);
|
||||
fieldset.addTo(form);
|
||||
return form;
|
||||
}
|
||||
@@ -235,8 +244,9 @@ public class Car extends BaseClass implements Comparable<Car>{
|
||||
|
||||
public Car update(HashMap<String, String> params) {
|
||||
if (params.containsKey(NAME)) name = params.get(NAME);
|
||||
if (params.containsKey(STOCK_ID)) stockId = params.get(STOCK_ID);
|
||||
if (params.containsKey(LENGTH)) length = Integer.parseInt(params.get(LENGTH));
|
||||
if (params.containsKey(MAX_SPEED)) maxSpeed = Integer.parseInt(params.get(MAX_SPEED));
|
||||
if (params.containsKey(STOCK_ID)) stockId = params.get(STOCK_ID);
|
||||
if (params.containsKey(TAGS)) {
|
||||
String[] parts = params.get(TAGS).replace(",", " ").split(" ");
|
||||
tags.clear();
|
||||
|
||||
@@ -27,7 +27,6 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
|
||||
private static final String REVERSE = "reverse";
|
||||
public static final String LOCOMOTIVE = "locomotive";
|
||||
private static final int VMAX = 128;
|
||||
private boolean reverse = false;
|
||||
private Protocol proto = Protocol.DCC128;
|
||||
private int address = 3;
|
||||
@@ -96,7 +95,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
|
||||
Fieldset fieldset = new Fieldset(t("Control"));
|
||||
|
||||
new Tag("span").content(t("Current velocity: {} km/h",speed)).addTo(fieldset);
|
||||
new Tag("span").content(t("Current velocity: {} {}",speed)).addTo(fieldset);
|
||||
|
||||
Tag par = new Tag("p");
|
||||
Map.of("Slower (10 steps)",ACTION_SLOWER10,"Faster (10 steps)",ACTION_FASTER10).entrySet().forEach(e -> {
|
||||
@@ -146,17 +145,12 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
case MOTO:
|
||||
proto = "M 2 100 0"; break; // TODO: make configurable
|
||||
case DCC14:
|
||||
proto = "N 1 14 5"; break; // TODO: make configurable
|
||||
case DCC27:
|
||||
proto = "N 1 27 5"; break; // TODO: make configurable
|
||||
case DCC28:
|
||||
proto = "N 1 28 5"; break; // TODO: make configurable
|
||||
case DCC128:
|
||||
proto = "N 1 128 5"; break; // TODO: make configurable
|
||||
proto = "N 1 "+this.proto.steps+" 5"; break; // TODO: make configurable
|
||||
case SELECTRIX:
|
||||
proto = "S"; break;
|
||||
case ZIMO:
|
||||
proto = "Z"; break;
|
||||
}
|
||||
plan.queue(new Command("INIT {} GL "+address+" "+proto) {
|
||||
|
||||
@@ -182,8 +176,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
JSONObject loco = new JSONObject();
|
||||
loco.put(REVERSE, reverse);
|
||||
loco.put(PROTOCOL, proto);
|
||||
loco.put(ADDRESS, address);
|
||||
|
||||
loco.put(ADDRESS, address);
|
||||
json.put(LOCOMOTIVE, loco);
|
||||
return json;
|
||||
}
|
||||
@@ -214,14 +207,14 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
|
||||
new Tag("p").content(t("Click on a name to edit the entry.")).addTo(win);
|
||||
|
||||
Table table = new Table().addHead(t("Stock ID"),t("Name"),t("Protocol"),t("Address"),t("Length"),t("Tags"));
|
||||
Table table = new Table().addHead(t("Stock ID"),t("Name"),t("Max. Speed",speedUnit),t("Protocol"),t("Address"),t("Length"),t("Tags"));
|
||||
cars.values()
|
||||
.stream()
|
||||
.filter(car -> car instanceof Locomotive)
|
||||
.map(car -> (Locomotive)car)
|
||||
.sorted(Comparator.comparing(loco -> loco.address))
|
||||
.sorted(Comparator.comparing(loco -> loco.stockId))
|
||||
.forEach(loco -> table.addRow(loco.stockId,loco.link(),loco.proto,loco.address,loco.length,String.join(", ", loco.tags())));
|
||||
.forEach(loco -> table.addRow(loco.stockId,loco.link(),loco.maxSpeed == 0 ? "–":loco.maxSpeed+NBSP+speedUnit,loco.proto,loco.address,loco.length,String.join(", ", loco.tags())));
|
||||
table.addTo(win);
|
||||
|
||||
|
||||
@@ -246,6 +239,7 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
@Override
|
||||
public Form propertyForm() {
|
||||
Form form = super.propertyForm();
|
||||
|
||||
for (Tag tag : form.children()) {
|
||||
if (REALM.equals(tag.get(Input.NAME)) && REALM_CAR.equals(tag.get(Input.VALUE))) {
|
||||
tag.attr("value", REALM_LOCO);
|
||||
@@ -264,7 +258,8 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
}
|
||||
|
||||
private void queue() {
|
||||
plan.queue(new Command("SET {} GL "+address+" "+(reverse?1:0)+" "+speed+" "+VMAX+" "+(f1?1:0)+" "+(f2?1:0)+" "+(f3?1:0)+" "+(f4?1:0)) {
|
||||
int step = proto.steps * speed / maxSpeed;
|
||||
plan.queue(new Command("SET {} GL "+address+" "+(reverse?1:0)+" "+step+" "+proto.steps+" "+(f1?1:0)+" "+(f2?1:0)+" "+(f3?1:0)+" "+(f4?1:0)) {
|
||||
|
||||
@Override
|
||||
public void onFailure(Reply reply) {
|
||||
@@ -274,11 +269,16 @@ public class Locomotive extends Car implements Constants,Device{
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the speed of the locomotive to the given velocity in [plan.speedUnit]s
|
||||
* @param newSpeed
|
||||
* @return
|
||||
*/
|
||||
public String setSpeed(int newSpeed) {
|
||||
LOG.debug(this.detail()+".setSpeed({})",newSpeed);
|
||||
init();
|
||||
speed = newSpeed;
|
||||
if (speed > 128) speed = 128;
|
||||
if (speed > maxSpeed()) speed = maxSpeed();
|
||||
if (speed < 0) speed = 0;
|
||||
|
||||
queue();
|
||||
|
||||
@@ -371,7 +371,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
public static void loadAll(String filename, Plan plan) throws IOException {
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename, UTF8));
|
||||
String line = file.readLine();
|
||||
while (isSet(line)) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
@@ -439,18 +439,21 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
new Tag("p").content(t("Click on a name to edit the entry.")).addTo(win);
|
||||
|
||||
Table table = new Table().addHead(t("Name"),t("Length"),t("Tags"),t("Route"),t("Current location"),t("Destination"),t("Auto pilot"));
|
||||
list().forEach(train -> table.addRow(
|
||||
Table table = new Table().addHead(t("Name"),t("Length"),t("Max. Speed"),t("Tags"),t("Route"),t("Current location"),t("Destination"),t("Auto pilot"));
|
||||
list().forEach(train -> {
|
||||
int ms = train.maxSpeed();
|
||||
table.addRow(
|
||||
train.link(),
|
||||
train.length(),
|
||||
ms == Integer.MAX_VALUE ? "–" : ms+NBSP+speedUnit,
|
||||
String.join(", ", train.tags()),
|
||||
train.route,
|
||||
isSet(train.currentBlock) ? train.currentBlock.link() : null,
|
||||
train.destination(),
|
||||
t(isSet(train.autopilot)?"On":"Off")
|
||||
));
|
||||
);
|
||||
});
|
||||
table.addTo(win);
|
||||
|
||||
|
||||
Form form = new Form();
|
||||
new Input(ACTION, ACTION_ADD).hideIn(form);
|
||||
@@ -468,6 +471,22 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
return win;
|
||||
}
|
||||
|
||||
|
||||
private int maxSpeed() {
|
||||
int maxSpeed = Integer.MAX_VALUE;
|
||||
for (Locomotive loco : locos) {
|
||||
int max = loco.maxSpeed();
|
||||
if (max == 0) continue;
|
||||
maxSpeed = Math.min(max, maxSpeed);
|
||||
}
|
||||
for (Car car : cars) {
|
||||
int max = car.maxSpeed();
|
||||
if (max == 0) continue;
|
||||
maxSpeed = Math.min(max, maxSpeed);
|
||||
}
|
||||
return maxSpeed;
|
||||
}
|
||||
|
||||
public String name() {
|
||||
return (isSet(name) ? name : locos.firstElement().name());
|
||||
@@ -498,9 +517,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
new Input(TAGS,String.join(", ", tags)).addTo(new Label(t("Tags")+NBSP)).addTo(form);
|
||||
new Button(t("Apply")).addTo(form).addTo(fieldset);
|
||||
|
||||
HashMap<String, Object> props = new HashMap<String,Object>(Map.of(REALM,REALM_TRAIN,ID,id));
|
||||
props.put(ACTION, ACTION_TURN);
|
||||
new Button(t("Turn"), props).addTo(fieldset).addTo(window);
|
||||
fieldset.addTo(window);
|
||||
|
||||
fieldset = new Fieldset(t("other train properties"));
|
||||
|
||||
@@ -509,6 +526,10 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
locoList().addTo(propList);
|
||||
carList().addTo(propList);
|
||||
|
||||
int ms = maxSpeed();
|
||||
if (ms < Integer.MAX_VALUE) new Tag("li").content(t("Max. Speed")+": "+maxSpeed()+NBSP+speedUnit).addTo(propList);
|
||||
|
||||
HashMap<String, Object> props = new HashMap<String,Object>(Map.of(REALM,REALM_TRAIN,ID,id));
|
||||
if (isSet(currentBlock)) {
|
||||
link("li",Map.of(REALM,REALM_PLAN,ID,currentBlock.id(),ACTION,ACTION_CLICK),t("Current location: {}",currentBlock)).addTo(propList);
|
||||
Tag actions = new Tag("li").clazz().content(t("Actions:")+NBSP);
|
||||
@@ -626,13 +647,12 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
public void setSpeed(int newSpeed) {
|
||||
speed = newSpeed;
|
||||
if (speed > 128) speed = 128;
|
||||
speed = Math.min(newSpeed,maxSpeed());
|
||||
if (speed < 0) speed = 0;
|
||||
for (Locomotive loco : locos) loco.setSpeed(speed);
|
||||
plan.stream(t("Set {} to {} km/h",this,speed));
|
||||
plan.stream(t("Set {} to {} {}",this,speed,speedUnit));
|
||||
}
|
||||
|
||||
|
||||
public void setWaitTime(Range waitTime) {
|
||||
if (isNull(autopilot)) return;
|
||||
autopilot.waitTime = waitTime.random();
|
||||
|
||||
Reference in New Issue
Block a user