re-implemented Delayed Action

This commit is contained in:
Stephan Richter
2020-11-01 16:06:46 +01:00
parent 7eabaf9449
commit a89d522ea2
4 changed files with 79 additions and 99 deletions
+1 -1
View File
@@ -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.9.5</version> <version>0.9.6</version>
<name>Web4Rail</name> <name>Web4Rail</name>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Java Model Railway Control</description> <description>Java Model Railway Control</description>
@@ -113,6 +113,10 @@ public class ActionList extends Vector<Action> implements Constants{
ConditionalAction ca = (ConditionalAction) action; ConditionalAction ca = (ConditionalAction) action;
ca.children().addTo(act, context); ca.children().addTo(act, context);
} }
if (action instanceof DelayedAction) {
DelayedAction da = (DelayedAction) action;
da.children().addTo(act, context);
}
act.addTo(ul); act.addTo(ul);
first = false; first = false;
} }
@@ -130,7 +134,7 @@ public class ActionList extends Vector<Action> implements Constants{
return false; return false;
} }
public void fire(Context context) { public boolean fire(Context context) {
LOG.debug("Firing {}",this); LOG.debug("Firing {}",this);
for (Action action : this) { for (Action action : this) {
@@ -140,6 +144,7 @@ public class ActionList extends Vector<Action> implements Constants{
LOG.warn("Action did not fire properly: {}",action,e); LOG.warn("Action did not fire properly: {}",action,e);
} }
} }
return true;
} }
public int id() { public int id() {
@@ -60,20 +60,11 @@ public class ConditionalAction extends Action {
@Override @Override
public boolean fire(Context context) throws IOException { public boolean fire(Context context) throws IOException {
for (Condition condition : conditions) { for (Condition condition : conditions) {
if (condition.fulfilledBy(context) != condition.inverted) return fireActions(context); if (condition.fulfilledBy(context) != condition.inverted) return actions.fire(context);
} }
return false; return false;
} }
private boolean fireActions(Context context) {
for (Action action : actions) try {
action.fire(context);
} catch (IOException e) {
LOG.warn("Was not able to fire {}",action);
}
return true;
}
@Override @Override
public JSONObject json() { public JSONObject json() {
JSONObject json = super.json(); JSONObject json = super.json();
@@ -2,122 +2,106 @@ package de.srsoftware.web4rail.actions;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import org.json.JSONObject; import org.json.JSONObject;
import de.srsoftware.tools.Tag; import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Window; import de.srsoftware.web4rail.Window;
import de.srsoftware.web4rail.tags.Button; import de.srsoftware.web4rail.tags.Button;
import de.srsoftware.web4rail.tags.Fieldset;
import de.srsoftware.web4rail.tags.Form; import de.srsoftware.web4rail.tags.Form;
import de.srsoftware.web4rail.tags.Input; import de.srsoftware.web4rail.tags.Input;
import de.srsoftware.web4rail.tags.Label; import de.srsoftware.web4rail.tags.Label;
import de.srsoftware.web4rail.tags.Select;
public class DelayedAction extends Action{ public class DelayedAction extends Action {
private static final String ACTIONS = "actions";
public static final String DELAY = "delay"; public static final String DELAY = "delay";
private static final int DEFAULT_DELAY = 1000; private static final int DEFAULT_DELAY = 1000;
private int delay = DEFAULT_DELAY; private int delay = DEFAULT_DELAY;
private Action action = null;
@Override private ActionList actions = new ActionList();
public boolean fire(Context context) {
new Thread() { private Tag actionsForm(HashMap<String, String> params) {
public void run() { Fieldset fieldset = new Fieldset(t("Actions"));
try { actions.addTo(fieldset, params.get(CONTEXT));
Thread.sleep(delay); return fieldset;
} catch (InterruptedException e) {
}
if (action != null) try {
action.fire(context);
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
return true;
} }
@Override public ActionList children() {
public JSONObject json() { return actions;
JSONObject json = super.json();
json.put(DELAY, delay);
json.put(ACTION, action.json());
return json;
} }
public static DelayedAction load(JSONObject json) { private Tag delayForm(HashMap<String, String> params) {
DelayedAction da = new DelayedAction(); Fieldset fieldset = new Fieldset(t("Delay"));
da.delay = json.getInt(DELAY);
da.action = Action.load(json.getJSONObject(ACTION));
return da;
}
@Override
public Window properties(HashMap<String, String> params) {
Window win = super.properties(params);
Form form = new Form("action-prop-form-"+id); Form form = new Form("action-prop-form-"+id);
new Input(REALM,REALM_ACTIONS).hideIn(form); new Input(REALM,REALM_ACTIONS).hideIn(form);
new Input(ID,params.get(ID)).hideIn(form); new Input(ID,params.get(ID)).hideIn(form);
new Input(ACTION,ACTION_UPDATE).hideIn(form); new Input(ACTION,ACTION_UPDATE).hideIn(form);
new Input(CONTEXT,params.get(CONTEXT)).hideIn(form); new Input(CONTEXT,params.get(CONTEXT)).hideIn(form);
if (action == null) { new Input(DELAY,delay).numeric().addTo(new Label(t("Delay")+NBSP)).content(" ms").addTo(form);
Select select = new Select(TYPE); return new Button(t("Apply"),form).addTo(form).addTo(fieldset);
List<Class<? extends Action>> classes = List.of( }
ConditionalAction.class,
SetSpeed.class, @Override
SetSignalsToStop.class, public boolean fire(Context context) throws IOException {
FinishRoute.class, new Thread() {
TurnTrain.class, public void run() {
StopAuto.class, try {
PowerOff.class, Thread.sleep(delay);
SetRelay.class, } catch (InterruptedException e) {
DelayedAction.class LOG.warn("Interrupted Exception thrown while waiting:",e);
); }
for (Class<? extends Action> clazz : classes) select.addOption(clazz.getSimpleName()); actions.fire(context);
select.addTo(new Label(t("Action type:")+NBSP)).addTo(form); };
} else { }.start();
action.link(0, params.get(CONTEXT)).addTo(new Label(t("Action: "))).addTo(form); return true;
} }
Label label = new Label(t("Delay:")+NBSP);
new Input(DELAY, delay).numeric().addTo(label).content(NBSP+t("miliseconds"));
label.addTo(form);
new Button(t("Apply"),form).addTo(form).addTo(win); @Override
public JSONObject json() {
JSONObject json = super.json();
json.put(DELAY, delay);
json.put(ACTIONS, actions.json());
return json;
}
public static DelayedAction load(JSONObject json) {
DelayedAction action = new DelayedAction();
action.delay = json.getInt(DELAY);
if (json.has(ACTIONS)) action.actions = ActionList.load(json.getJSONArray(ACTIONS));
return action;
}
@Override
public Window properties(HashMap<String, String> params) {
Window win = super.properties(params);
delayForm(params).addTo(win);
actionsForm(params).addTo(win);
return win; return win;
} }
@Override @Override
public String toString() { public String toString() {
if (action == null) return t("Click here to set up delayed action!"); return t("Wait {} ms, then:",delay);
return t("Wait {} ms, then {}",delay,action);
} }
@Override @Override
protected Object update(HashMap<String, String> params) { protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params); String d = params.get(DELAY);
if (d != null) try {
String type = params.get(TYPE); int ms = Integer.parseInt(d);
if (type != null) action = Action.create(type); if (ms < 0) throw new NumberFormatException(t("Delay must not be less than zero!"));
delay = ms;
String error = null; } catch (NumberFormatException nfe) {
String ms = params.get(DELAY); Window props = properties(params);
if (ms == null) { props.children().insertElementAt(new Tag("div").content(nfe.getMessage()), 2);
ms = ""+DEFAULT_DELAY; return props;
} else {
try {
int d = Integer.parseInt(ms);
if (d<0) error = t("Delay must not be less than zero!");
if (error == null) {
this.delay = d;
return t("Action updated!");
}
} catch (NumberFormatException e) {
error = t("Not a valid number!");
}
} }
Window win = properties(params); return super.update(params);
return new Tag("span").content(error).addTo(win);
} }
} }