working on better auto mode options
This commit is contained in:
@@ -74,10 +74,10 @@ public class Store {
|
||||
intValue = null;
|
||||
for (char c : value.toCharArray()) {
|
||||
if (!Character.isDigit(c)) {
|
||||
if (isSet(intValue)) break;
|
||||
if (BaseClass.isSet(intValue)) break;
|
||||
} else {
|
||||
int add = ((byte)c-48);
|
||||
intValue = isNull(intValue) ? add : 10*intValue + add;
|
||||
intValue = BaseClass.isNull(intValue) ? add : 10*intValue + add;
|
||||
}
|
||||
}
|
||||
listeners.forEach(listener -> listener.storeUpdated(this));
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
|
||||
public class AbortActions extends Action{
|
||||
|
||||
public AbortActions(BaseClass parent) {
|
||||
super(parent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
context.invalidate();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@ public abstract class Action extends BaseClass {
|
||||
|
||||
public static List<Class<? extends Action>> classes() {
|
||||
return List.of(
|
||||
AbortActions.class,
|
||||
AddRemoveDestination.class,
|
||||
AddRemoveTag.class,
|
||||
AlterDirection.class,
|
||||
|
||||
@@ -15,15 +15,19 @@ import de.srsoftware.web4rail.tags.Checkbox;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
import de.srsoftware.web4rail.tiles.Switch;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
public class AddRemoveDestination extends Action {
|
||||
|
||||
private static final String TURN = "turn";
|
||||
private static final String SHUNTING = "shunting";
|
||||
private static final String TRIGGER = "destination_trigger";
|
||||
private Block destination;
|
||||
private boolean turnAtDestination;
|
||||
private boolean shunting;
|
||||
private Tile destinationTrigger = null;
|
||||
|
||||
public AddRemoveDestination(BaseClass parent) {
|
||||
super(parent);
|
||||
@@ -53,6 +57,7 @@ public class AddRemoveDestination extends Action {
|
||||
}
|
||||
}
|
||||
train.addTag(dest);
|
||||
train.setDestinationTrigger(destinationTrigger);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -68,6 +73,7 @@ public class AddRemoveDestination extends Action {
|
||||
if (isSet(destination)) json.put(Train.DESTINATION,destination.id().toString());
|
||||
if (turnAtDestination) json.put(TURN,true);
|
||||
if (shunting) json.put(SHUNTING, true);
|
||||
if (isSet(destinationTrigger)) json.put(TRIGGER, destinationTrigger.id());
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -81,7 +87,13 @@ public class AddRemoveDestination extends Action {
|
||||
destination = BaseClass.get(Id.from(json, Train.DESTINATION));
|
||||
}
|
||||
};
|
||||
|
||||
if (json.has(TRIGGER)) new LoadCallback() {
|
||||
|
||||
@Override
|
||||
public void afterLoad() {
|
||||
destinationTrigger = Tile.get(Id.from(json, TRIGGER));
|
||||
}
|
||||
};
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@@ -93,6 +105,7 @@ public class AddRemoveDestination extends Action {
|
||||
formInputs.add(t("Destination")+": "+(isNull(destination) ? t("Clear destinations") : destination),span);
|
||||
formInputs.add(t("Turn at destination"),new Checkbox(TURN, t("Turn"), turnAtDestination));
|
||||
formInputs.add(t("Shunting"),new Checkbox(SHUNTING, t("Shunting"), shunting));
|
||||
formInputs.add(t("Trigger Contact/Switch at destination")+": "+(isNull(destinationTrigger) ? t("unset") : destinationTrigger),button(t("Select from plan"),Map.of(ACTION,ACTION_UPDATE,ASSIGN,CONTACT)));
|
||||
return super.properties(preForm, formInputs, postForm,errors);
|
||||
}
|
||||
|
||||
@@ -119,6 +132,10 @@ public class AddRemoveDestination extends Action {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (params.containsKey(CONTACT)) {
|
||||
Tile tile = Tile.get(Id.from(params,CONTACT));
|
||||
if (tile instanceof Contact || tile instanceof Switch) destinationTrigger = tile;
|
||||
}
|
||||
turnAtDestination = "on".equals(params.getString(TURN));
|
||||
shunting = "on".equals(params.getString(SHUNTING));
|
||||
return context().properties();
|
||||
|
||||
@@ -7,6 +7,7 @@ import org.json.JSONObject;
|
||||
import de.srsoftware.web4rail.BaseClass;
|
||||
import de.srsoftware.web4rail.LoadCallback;
|
||||
import de.srsoftware.web4rail.Params;
|
||||
import de.srsoftware.web4rail.moving.Car;
|
||||
import de.srsoftware.web4rail.moving.Train;
|
||||
import de.srsoftware.web4rail.tags.Fieldset;
|
||||
import de.srsoftware.web4rail.tags.Window;
|
||||
@@ -14,6 +15,7 @@ import de.srsoftware.web4rail.tags.Window;
|
||||
public class SetContextTrain extends Action {
|
||||
|
||||
private Train train = null;
|
||||
private Car car = null;
|
||||
|
||||
public SetContextTrain(BaseClass parent) {
|
||||
super(parent);
|
||||
@@ -21,7 +23,9 @@ public class SetContextTrain extends Action {
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) {
|
||||
context.train(train);
|
||||
Train t = isSet(train) ? train : car.train();
|
||||
if (isNull(t)) return false;
|
||||
context.train(t);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -29,6 +33,7 @@ public class SetContextTrain extends Action {
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (isSet(train)) json.put(REALM_TRAIN, train.id());
|
||||
if (isSet(car)) json.put(REALM_CAR, car.id());
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -40,12 +45,19 @@ public class SetContextTrain extends Action {
|
||||
train = Train.get(Id.from(json,REALM_TRAIN));
|
||||
}
|
||||
};
|
||||
if (json.has(REALM_CAR)) new LoadCallback() {
|
||||
@Override
|
||||
public void afterLoad() {
|
||||
car = Car.get(Id.from(json,REALM_CAR));
|
||||
}
|
||||
};
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Window properties(List<Fieldset> preForm, FormInput formInputs, List<Fieldset> postForm,String...errors) {
|
||||
formInputs.add(t("Select train"),Train.selector(train, null));
|
||||
formInputs.add(t("Select car"),Car.selector(car, null));
|
||||
return super.properties(preForm, formInputs, postForm,errors);
|
||||
}
|
||||
|
||||
@@ -56,14 +68,31 @@ public class SetContextTrain extends Action {
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return isSet(train) ? t("Set {} as context",train) : "["+t("Click here to select train!")+"]";
|
||||
if (isSet(train)) return t("Set {} as context",train);
|
||||
if (isSet(car)) return t("Set train of {} as context",car);
|
||||
return "["+t("Click here to select train!")+"]";
|
||||
};
|
||||
|
||||
@Override
|
||||
protected Object update(Params params) {
|
||||
LOG.debug("update: {}",params);
|
||||
Id trainId = Id.from(params,Train.class.getSimpleName());
|
||||
if (isSet(trainId)) train = Train.get(trainId);
|
||||
if (isSet(trainId)) {
|
||||
Train newTrain = Train.get(trainId);
|
||||
if (newTrain != train) {
|
||||
train = newTrain;
|
||||
car = null;
|
||||
params.remove(Car.class.getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
Id carId = Id.from(params,Car.class.getSimpleName());
|
||||
if (isSet(carId) && !carId.equals(0)) {
|
||||
car = Car.get(carId);
|
||||
train = null;
|
||||
}
|
||||
|
||||
|
||||
return super.update(params);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,8 @@ public class BlockFree extends Condition {
|
||||
|
||||
@Override
|
||||
public boolean fulfilledBy(Context context) {
|
||||
return block.isFreeFor(context) != inverted;
|
||||
if (!inverted) return block.isFreeFor(context);
|
||||
return !block.isFreeFor(new Context(block)); // block.isFreeFor würde true liefern, wenn der Zug im Kontext gleich dem Zug im Block wäre. Da wir aber nur wissen wollen, ob der Block belegt ist, brauchen wir einen Context ohne Zug.
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,6 +70,7 @@ public class BlockFree extends Condition {
|
||||
if (tile instanceof Block) {
|
||||
block = (Block) tile;
|
||||
} else return t("Clicked tile is not a {}!",t("block"));
|
||||
return context().properties();
|
||||
}
|
||||
|
||||
return super.update(params);
|
||||
|
||||
@@ -43,6 +43,7 @@ import de.srsoftware.web4rail.threads.DelayedExecution;
|
||||
import de.srsoftware.web4rail.threads.RoutePrepper;
|
||||
import de.srsoftware.web4rail.tiles.Block;
|
||||
import de.srsoftware.web4rail.tiles.Contact;
|
||||
import de.srsoftware.web4rail.tiles.Switch;
|
||||
import de.srsoftware.web4rail.tiles.Tile;
|
||||
|
||||
/**
|
||||
@@ -101,6 +102,8 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
|
||||
private BrakeProcess brake;
|
||||
|
||||
private Tile destinationTrigger;
|
||||
|
||||
public static Object action(Params params, Plan plan) throws IOException {
|
||||
String action = params.getString(ACTION);
|
||||
if (isNull(action)) return t("No action passed to Train.action!");
|
||||
@@ -500,11 +503,18 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
if (isNull(destTag)) {
|
||||
quitAutopilot();
|
||||
Tile trigger = destinationTrigger; // quitAutopilot drops destinationTrigger
|
||||
quitAutopilot();
|
||||
plan.stream(t("{} reached it`s destination!",this));
|
||||
if (isSet(trigger)) new DelayedExecution(1000,this) {
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
if (trigger instanceof Contact) ((Contact)trigger).trigger(200);
|
||||
if (trigger instanceof Switch) ((Switch)trigger).trigger(new Context(Train.this));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
if (isSet(brake)) brake.updateTime();
|
||||
Integer waitTime = route.waitTime();
|
||||
@@ -521,7 +531,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
stuckTrace = null;
|
||||
if (autopilot) {
|
||||
if (isNull(waitTime)) waitTime = 0;
|
||||
if (waitTime>0) plan.stream(t("{} waiting {} secs",this,(int)(waitTime/1000)));
|
||||
if (waitTime>0) plan.stream(t("{} waiting {} secs.",this,(int)(waitTime/1000)));
|
||||
new DelayedExecution(waitTime,this) {
|
||||
|
||||
@Override
|
||||
@@ -789,6 +799,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
button(t("Select from plan"),Map.of(ACTION,ACTION_MOVE,ASSIGN,DESTINATION)).addTo(dest);
|
||||
|
||||
dest.addTo(propList);
|
||||
if (isSet(destinationTrigger)) new Tag("li").content(t("Triggers {} when reaching destination",destinationTrigger)).addTo(propList);
|
||||
if (isSet(route)) route.link("li", route).addTo(propList);
|
||||
int ms = maxSpeed();
|
||||
if (ms < Integer.MAX_VALUE) new Tag("li").content(t("Maximum Speed")+COL+maxSpeed()+NBSP+speedUnit).addTo(propList);
|
||||
@@ -808,6 +819,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
ul.addTo(li).addTo(propList);
|
||||
}
|
||||
carList().addTo(propList);
|
||||
|
||||
|
||||
|
||||
formInputs.add(t("Name"), new Input(NAME,name()));
|
||||
@@ -825,6 +837,7 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
}
|
||||
|
||||
public String quitAutopilot() {
|
||||
destinationTrigger = null;
|
||||
if (isSet(routePrepper)) {
|
||||
routePrepper.stop();
|
||||
routePrepper = null;
|
||||
@@ -950,6 +963,11 @@ public class Train extends BaseClass implements Comparable<Train> {
|
||||
return properties(t("{} is not a block!",tile));
|
||||
}
|
||||
|
||||
public void setDestinationTrigger(Tile destinationTrigger) {
|
||||
this.destinationTrigger = destinationTrigger;
|
||||
}
|
||||
|
||||
|
||||
public Object setFunction(int num, boolean active) {
|
||||
// TODO
|
||||
return properties();
|
||||
|
||||
@@ -162,7 +162,9 @@ public class Switch extends Tile{
|
||||
}
|
||||
|
||||
public boolean trigger(Context context) {
|
||||
return state ? actionsOn.fire(context) : actionsOff.fire(context);
|
||||
return state ?
|
||||
actionsOn.fire(context) :
|
||||
actionsOff.fire(context);
|
||||
}
|
||||
|
||||
public void state(boolean newState) {
|
||||
|
||||
@@ -74,8 +74,10 @@ public class TextDisplay extends StretchableTile implements Store.Listener {
|
||||
}
|
||||
|
||||
public TextDisplay text(String tx) {
|
||||
if (isNull(text) || !text.equals(tx)) {
|
||||
displayText = tx;
|
||||
}
|
||||
text = tx;
|
||||
displayText = tx;
|
||||
int pos = text.indexOf("{");
|
||||
Store.removeListener(this);
|
||||
while (pos > -1) {
|
||||
|
||||
Reference in New Issue
Block a user