added SendCommand action

This commit is contained in:
Stephan Richter
2020-11-07 19:50:30 +01:00
parent 880ef561dd
commit b899572f55
9 changed files with 115 additions and 16 deletions

View File

@@ -41,6 +41,7 @@ public abstract class Action extends BaseClass {
public Context(Contact c) {
contact = c;
plan = contact.plan();
route = contact.route();
if (route == null) return;
train = route.train;
@@ -48,10 +49,12 @@ public abstract class Action extends BaseClass {
public Context(Train train) {
this.train = train;
if (isSet(train)) plan = train.locos().get(0).plan();
}
public Context(Route route) {
this.route = route;
if (isSet(route)) plan = route.path().firstElement().plan();
train = route.train;
}
@@ -101,6 +104,7 @@ public abstract class Action extends BaseClass {
public static List<Class<? extends Action>> list() {
return List.of(
SendCommand.class,
ConditionalAction.class,
SetSpeed.class,
SetSignalsToStop.class,

View File

@@ -0,0 +1,75 @@
package de.srsoftware.web4rail.actions;
import java.util.HashMap;
import org.json.JSONObject;
import de.srsoftware.tools.Tag;
import de.srsoftware.web4rail.Command;
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;
public class SendCommand extends Action{
public static final String COMMAND = "command";
private String command = "SET 1 POWER OFF";
@Override
public boolean fire(Context context) {
context.plan.queue(new Command(command) {
@Override
public void onResponse(Reply reply) {
super.onResponse(reply);
context.plan.stream(reply.message());
}
});
return false;
}
@Override
public JSONObject json() {
JSONObject json = super.json();
json.put(COMMAND, command);
return json;
}
@Override
public Action load(JSONObject json) {
super.load(json);
command = json.getString(COMMAND);
return this;
}
@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);
Label label = new Label(t("Command to send to control unit:")+NBSP);
new Input(COMMAND, command).addTo(label).addTo(form);
new Button(t("Apply"),form).addTo(form).addTo(win);
return win;
}
@Override
public String toString() {
return t("Send command \"{}\" to control unit",command);
}
@Override
protected Object update(HashMap<String, String> params) {
LOG.debug("update: {}",params);
String error = null;
command = params.get(COMMAND);
Window win = properties(params);
return new Tag("span").content(error).addTo(win);
}
}