* added action that allows to switch relays
* added action list to route setup procedure
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -4,7 +4,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.srsoftware</groupId>
|
||||
<artifactId>web4rail</artifactId>
|
||||
<version>0.9.3</version>
|
||||
<version>0.9.4</version>
|
||||
<name>Web4Rail</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Java Model Railway Control</description>
|
||||
|
||||
@@ -67,6 +67,7 @@ public class Route implements Constants{
|
||||
private static final String START_DIRECTION = "direction_start";
|
||||
private static final String END_DIRECTION = "direction_end";
|
||||
private Vector<Condition> conditions = new Vector<Condition>();
|
||||
private ActionList setupActions = new ActionList();
|
||||
|
||||
public Direction startDirection;
|
||||
private Direction endDirection;
|
||||
@@ -195,8 +196,12 @@ public class Route implements Constants{
|
||||
|
||||
private void addContactsTo(Window win) {
|
||||
if (!contacts.isEmpty()) {
|
||||
new Tag("h4").content(t("Contacts and actions")).addTo(win);
|
||||
new Tag("h4").content(t("Actions and Contacts")).addTo(win);
|
||||
Tag list = new Tag("ol");
|
||||
|
||||
Tag setup = new Tag("li").content("Setup actions");
|
||||
setupActions.addTo(setup, context());
|
||||
setup.addTo(list);
|
||||
for (Contact c : contacts) {
|
||||
Tag link = Plan.addLink(c,c.toString(),list);
|
||||
ActionList actions = triggers.get(c.trigger());
|
||||
@@ -204,7 +209,7 @@ public class Route implements Constants{
|
||||
actions = new ActionList();
|
||||
triggers.put(c.trigger(), actions);
|
||||
}
|
||||
actions.addTo(link,REALM_ROUTE+":"+id());
|
||||
actions.addTo(link,context());
|
||||
}
|
||||
list.addTo(win);
|
||||
}
|
||||
@@ -299,6 +304,10 @@ public class Route implements Constants{
|
||||
return new Vector<>(contacts);
|
||||
}
|
||||
|
||||
public String context() {
|
||||
return REALM_ROUTE+":"+id();
|
||||
}
|
||||
|
||||
public Block endBlock() {
|
||||
return endBlock;
|
||||
}
|
||||
@@ -311,6 +320,11 @@ public class Route implements Constants{
|
||||
train = null;
|
||||
}
|
||||
|
||||
|
||||
public void fireSetupActions(Context context) {
|
||||
setupActions.fire(context);
|
||||
}
|
||||
|
||||
public boolean free() {
|
||||
for (int i=1; i<path.size(); i++) {
|
||||
if (!path.get(i).free()) return false;
|
||||
@@ -378,6 +392,7 @@ public class Route implements Constants{
|
||||
|
||||
}
|
||||
if (!jTriggers.isEmpty()) json.put(ACTION_LISTS, jTriggers);
|
||||
if (!setupActions.isEmpty()) json.put(ACTIONS, setupActions.json());
|
||||
|
||||
String name = name();
|
||||
if (name != null) json.put(NAME, name);
|
||||
@@ -414,6 +429,9 @@ public class Route implements Constants{
|
||||
}
|
||||
if (json.has(ACTION_LISTS)) loadActions(json.getJSONArray(ACTION_LISTS));
|
||||
if (json.has(CONDITIONS)) loadConditions(json.getJSONArray(CONDITIONS));
|
||||
if (json.has(ACTIONS)) {
|
||||
setupActions = ActionList.load(json.getJSONArray(ACTIONS));
|
||||
}
|
||||
return plan.registerRoute(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public abstract class Action implements Constants {
|
||||
|
||||
public static Action load(JSONObject json) {
|
||||
String clazz = json.getString(TYPE);
|
||||
switch (clazz) {
|
||||
switch (clazz) { // TODO: das kann generisch mittels Reflection implementiert werden!
|
||||
case "ActivateRoute":
|
||||
return new ActivateRoute();
|
||||
case "ConditionalAction":
|
||||
@@ -75,9 +75,12 @@ public abstract class Action implements Constants {
|
||||
return new SetSignalsToStop();
|
||||
case "SetSpeed":
|
||||
return SetSpeed.load(json);
|
||||
case "SetRelay":
|
||||
return SetRelay.load(json);
|
||||
case "TurnTrain":
|
||||
return new TurnTrain();
|
||||
}
|
||||
LOG.error("Found unknwon action \"{}\" in json!",clazz);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -63,7 +63,8 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
FinishRoute.class,
|
||||
TurnTrain.class,
|
||||
StopAuto.class,
|
||||
PowerOff.class
|
||||
PowerOff.class,
|
||||
SetRelay.class
|
||||
);
|
||||
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName());
|
||||
select.addTo(new Label("Action type:")).addTo(typeForm);
|
||||
@@ -76,7 +77,7 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
String context = params.get(CONTEXT);
|
||||
if (type == null) return actionTypeForm(win,context);
|
||||
|
||||
switch (type) {
|
||||
switch (type) { // TODO: das kann man mit Reflection generischer lösen
|
||||
case "ConditionalAction":
|
||||
add(new ConditionalAction());
|
||||
break;
|
||||
@@ -98,6 +99,9 @@ public class ActionList extends Vector<Action> implements Constants{
|
||||
case "PowerOff":
|
||||
add(new PowerOff());
|
||||
break;
|
||||
case "SetRelay":
|
||||
add(new SetRelay());
|
||||
break;
|
||||
default:
|
||||
actionTypeForm(win,context);
|
||||
new Tag("span").content(t("Unknown action type: {}",type)).addTo(win);
|
||||
|
||||
92
src/main/java/de/srsoftware/web4rail/actions/SetRelay.java
Normal file
92
src/main/java/de/srsoftware/web4rail/actions/SetRelay.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package de.srsoftware.web4rail.actions;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Select;
|
||||
import de.srsoftware.web4rail.tiles.Relay;
|
||||
|
||||
public class SetRelay extends Action {
|
||||
|
||||
private static final String RELAY = "relay";
|
||||
private Relay relay = null;
|
||||
private boolean state = false;
|
||||
|
||||
@Override
|
||||
public boolean fire(Context context) throws IOException {
|
||||
if (relay != null) {
|
||||
relay.state(state);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject json() {
|
||||
JSONObject json = super.json();
|
||||
if (relay != null) {
|
||||
json.put(RELAY, relay.id());
|
||||
json.put(Relay.STATE, state);
|
||||
}
|
||||
return json;
|
||||
}
|
||||
|
||||
public static SetRelay load(JSONObject json) {
|
||||
String relayId = json.getString(RELAY);
|
||||
SetRelay result = new SetRelay();
|
||||
if (relayId != null) {
|
||||
result.relay = Relay.get(relayId);
|
||||
result.state = json.getBoolean(Relay.STATE);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Window properties(HashMap<String, String> params) {
|
||||
Window win = super.properties(params);
|
||||
Form form = new Form("action-prop-form-"+id);
|
||||
new Input(REALM,REALM_ACTIONS).hideIn(form);
|
||||
new Input(ID,params.get(ID)).hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||
|
||||
Select select = new Select(RELAY);
|
||||
for (Relay relay : Relay.list()) {
|
||||
Tag option = select.addOption(relay.id(),relay.title());
|
||||
if (relay == this.relay) option.attr("selected", "selected");
|
||||
}
|
||||
select.addTo(new Label(t("Select relay:")+NBSP)).addTo(form);
|
||||
|
||||
Select state = new Select(Relay.STATE);
|
||||
state.addOption(true,relay == null?Relay.DEFAULT_LABEL_A:relay.stateLabelA);
|
||||
state.addOption(false,relay == null?Relay.DEFAULT_LABEL_B:relay.stateLabelB);
|
||||
state.addTo(new Label(t("Select state:")+NBSP)).addTo(form);
|
||||
|
||||
|
||||
new Button(t("Apply"),form).addTo(form).addTo(win);
|
||||
return win;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
if (relay == null) return t("[click here to setup relay]");
|
||||
return t("Set "+relay+" to "+(state?relay.stateLabelA:relay.stateLabelB));
|
||||
};
|
||||
|
||||
@Override
|
||||
protected Object update(HashMap<String, String> params) {
|
||||
LOG.debug("update: {}",params);
|
||||
String relayId = params.get(RELAY);
|
||||
relay = Relay.get(relayId);
|
||||
String st = params.get(Relay.STATE);
|
||||
if (st != null) state = st.equals("true");
|
||||
return properties(params);
|
||||
}
|
||||
}
|
||||
@@ -51,7 +51,7 @@ public class SetSpeed extends Action{
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
|
||||
Label label = new Label(t("Set speed to")+NBSP);
|
||||
new Input(MAX_SPEED, maxSpeed).addTo(label).content(NBSP+t("km/h"));
|
||||
new Input(MAX_SPEED, maxSpeed).numeric().addTo(label).content(NBSP+t("km/h"));
|
||||
label.addTo(form);
|
||||
new Button(t("Apply"),form).addTo(form).addTo(win);
|
||||
return win;
|
||||
|
||||
@@ -431,6 +431,7 @@ public class Train implements Constants {
|
||||
if (!route.lock()) return t("Was not able to lock {}",route);
|
||||
String error = null;
|
||||
if (!route.setTurnouts()) error = t("Was not able to set all turnouts!");
|
||||
route.fireSetupActions(context);
|
||||
if (error == null && !route.setSignals(null)) error = t("Was not able to set all signals!");
|
||||
if (error == null && !route.train(this)) error = t("Was not able to assign {} to {}!",this,route);
|
||||
if (error == null) {
|
||||
|
||||
@@ -96,6 +96,11 @@ public abstract class Block extends StretchableTile{
|
||||
return tag;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String title() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return getClass().getSimpleName()+"("+name+") @ ("+x+","+y+")";
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package de.srsoftware.web4rail.tiles;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
@@ -22,6 +23,8 @@ public class Relay extends Tile implements Device{
|
||||
private static final String PORT_A = "port_a";
|
||||
private static final String PORT_B = "port_b";
|
||||
protected static final String STRAIGHT = "straight";
|
||||
public static final String DEFAULT_LABEL_A = "A";
|
||||
public static final String DEFAULT_LABEL_B = "B";
|
||||
|
||||
private Protocol protocol = Protocol.DCC128;
|
||||
protected int address = 0;
|
||||
@@ -29,13 +32,16 @@ public class Relay extends Tile implements Device{
|
||||
protected int delay = 400;
|
||||
protected boolean initialized = false;
|
||||
protected boolean error = false;
|
||||
private String stateLabelA = "A";
|
||||
private String stateLabelB = "B";
|
||||
public String stateLabelA = DEFAULT_LABEL_A;
|
||||
public String stateLabelB = DEFAULT_LABEL_B;
|
||||
private String name = t("Relay");
|
||||
protected boolean state = true;
|
||||
|
||||
private static final HashMap<String,Relay> relays = new HashMap<String, Relay>();
|
||||
public static final boolean STATE_A = true,STATE_B=false;
|
||||
private static final String LABEL_A = "label_a";
|
||||
private static final String LABEL_B = "label_b";
|
||||
private static final String NAME = "name";
|
||||
|
||||
@Override
|
||||
public Object click() throws IOException {
|
||||
@@ -93,6 +99,7 @@ public class Relay extends Tile implements Device{
|
||||
json.put(PROTOCOL, protocol);
|
||||
json.put(LABEL_A, stateLabelA);
|
||||
json.put(LABEL_B, stateLabelB);
|
||||
json.put(NAME, name);
|
||||
return json;
|
||||
}
|
||||
|
||||
@@ -104,9 +111,17 @@ public class Relay extends Tile implements Device{
|
||||
if (json.has(LABEL_A)) stateLabelA = json.getString(LABEL_A);
|
||||
if (json.has(LABEL_B)) stateLabelB = json.getString(LABEL_B);
|
||||
if (json.has(PROTOCOL)) protocol = Protocol.valueOf(json.getString(PROTOCOL));
|
||||
if (json.has(NAME)) name = json.getString(NAME);
|
||||
return super.load(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tile position(int x, int y) {
|
||||
super.position(x, y);
|
||||
relays.put(id(), this);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Tag propForm(String id) {
|
||||
Tag form = super.propForm(id);
|
||||
@@ -123,6 +138,8 @@ public class Relay extends Tile implements Device{
|
||||
new Input(PORT_B, portB).numeric().addTo(new Label(t("Port for state B"))).addTo(fieldset);
|
||||
new Input(LABEL_B, stateLabelB).addTo(new Label(t("Label for state B"))).addTo(fieldset);
|
||||
fieldset.addTo(form);
|
||||
fieldset = new Fieldset(t("Name"));
|
||||
new Input(NAME,name).addTo(new Label(t("Name"))).addTo(fieldset).addTo(form);
|
||||
return form;
|
||||
}
|
||||
|
||||
@@ -205,7 +222,7 @@ public class Relay extends Tile implements Device{
|
||||
|
||||
@Override
|
||||
public String title() {
|
||||
return getClass().getSimpleName()+t("(Address: {}, Ports {} and {})",address,portA,portB);
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -216,6 +233,15 @@ public class Relay extends Tile implements Device{
|
||||
if (params.containsKey(PORT_B)) portB = Integer.parseInt(params.get(PORT_B));
|
||||
if (params.containsKey(LABEL_A)) stateLabelA = params.get(LABEL_A);
|
||||
if (params.containsKey(LABEL_B)) stateLabelB = params.get(LABEL_B);
|
||||
if (params.containsKey(NAME)) name = params.get(NAME);
|
||||
return super.update(params);
|
||||
}
|
||||
|
||||
public static Collection<Relay> list() {
|
||||
return relays.values();
|
||||
}
|
||||
|
||||
public static Relay get(String relayId) {
|
||||
return relays.get(relayId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,7 +198,7 @@ public abstract class Tile implements Constants{
|
||||
}
|
||||
|
||||
public Tag propMenu() {
|
||||
Window window = new Window("tile-properties",t("Properties of {} @ ({},{})",getClass().getSimpleName(),x,y));
|
||||
Window window = new Window("tile-properties",t("Properties of {} @ ({},{})",title(),x,y));
|
||||
String formId = "tile-properties-"+id();
|
||||
Tag form = propForm(formId);
|
||||
if (form!=null && form.children().size()>3) {
|
||||
@@ -337,7 +337,7 @@ public abstract class Tile implements Constants{
|
||||
}
|
||||
|
||||
public String title() {
|
||||
return null;
|
||||
return getClass().getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user