refactored action communication
This commit is contained in:
@@ -16,6 +16,7 @@ import org.slf4j.LoggerFactory;
|
||||
import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
@@ -24,14 +25,12 @@ import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
|
||||
public class Car {
|
||||
public class Car implements Constants {
|
||||
protected static final Logger LOG = LoggerFactory.getLogger(Car.class);
|
||||
static HashMap<String,Car> cars = new HashMap<String, Car>();
|
||||
|
||||
public static final String ID = "id";
|
||||
public static final String NAME = "name";
|
||||
private static final String LENGTH = "length";
|
||||
private static final String SHOW = "show";
|
||||
private static final String STOCK_ID = "stock-id";
|
||||
|
||||
private String id;
|
||||
@@ -86,7 +85,7 @@ public class Car {
|
||||
}
|
||||
|
||||
public Tag link(String tagClass) {
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","car("+id+",'"+Car.SHOW+"')").content(name());
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","car("+id+",'"+ACTION_PROPS+"')").content(name());
|
||||
}
|
||||
|
||||
public static void loadAll(String filename, Plan plan) throws IOException {
|
||||
@@ -96,7 +95,7 @@ public class Car {
|
||||
while (line != null) {
|
||||
JSONObject json = new JSONObject(line);
|
||||
String name = json.getString(Car.NAME);
|
||||
String id = json.getString(Car.ID);
|
||||
String id = json.getString(ID);
|
||||
Car car = json.has(Locomotive.LOCOMOTIVE) ? new Locomotive(name, id) : new Car(name,id);
|
||||
car.load(json).plan(plan);
|
||||
|
||||
@@ -123,9 +122,9 @@ public class Car {
|
||||
|
||||
public Tag propertyForm() {
|
||||
Form form = new Form();
|
||||
new Input(Plan.ACTION, Plan.ACTION_UPDATE).hideIn(form);
|
||||
new Input(Plan.REALM,Plan.REALM_CAR).hideIn(form);
|
||||
new Input(Plan.ID,id()).hideIn(form);
|
||||
new Input(ACTION, ACTION_UPDATE).hideIn(form);
|
||||
new Input(REALM,REALM_CAR).hideIn(form);
|
||||
new Input(ID,id()).hideIn(form);
|
||||
Fieldset fieldset = new Fieldset("Basic properties");
|
||||
new Input(NAME,name).addTo(new Label(t("Name"))).addTo(fieldset);
|
||||
new Input(STOCK_ID,stockId).addTo(new Label(t("Stock ID"))).addTo(fieldset);
|
||||
|
||||
@@ -6,8 +6,8 @@ import java.util.Vector;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Device;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Protocol;
|
||||
import de.srsoftware.web4rail.Window;
|
||||
import de.srsoftware.web4rail.tags.Button;
|
||||
@@ -17,7 +17,7 @@ import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Label;
|
||||
import de.srsoftware.web4rail.tags.Radio;
|
||||
|
||||
public class Locomotive extends Car implements Device{
|
||||
public class Locomotive extends Car implements Constants,Device{
|
||||
|
||||
private static final String REVERSE = "reverse";
|
||||
public static final String LOCOMOTIVE = "locomotive";
|
||||
@@ -38,10 +38,10 @@ public class Locomotive extends Car implements Device{
|
||||
protected Tag cockpit(String realm) {
|
||||
Fieldset fieldset = new Fieldset(t("Control"));
|
||||
String request = "return request({realm:'"+realm+"',id:"+id()+",action:'{}'})";
|
||||
new Button(t("Turn"), request.replace("{}", Plan.ACTION_TURN)) .addTo(fieldset);
|
||||
new Button(t("Faster (10 steps)"), request.replace("{}", Plan.ACTION_FASTER10)).addTo(fieldset);
|
||||
new Button(t("Slower (10 steps)"), request.replace("{}", Plan.ACTION_SLOWER10)).addTo(fieldset);
|
||||
new Button(t("Stop"), request.replace("{}", Plan.ACTION_STOP)).addTo(fieldset);
|
||||
new Button(t("Turn"), request.replace("{}", ACTION_TURN)) .addTo(fieldset);
|
||||
new Button(t("Faster (10 steps)"), request.replace("{}", ACTION_FASTER10)).addTo(fieldset);
|
||||
new Button(t("Slower (10 steps)"), request.replace("{}", ACTION_SLOWER10)).addTo(fieldset);
|
||||
new Button(t("Stop"), request.replace("{}", ACTION_STOP)).addTo(fieldset);
|
||||
return fieldset;
|
||||
}
|
||||
|
||||
@@ -126,7 +126,8 @@ public class Locomotive extends Car implements Device{
|
||||
list.addTo(win);
|
||||
|
||||
Form form = new Form();
|
||||
new Input(Plan.ACTION, Plan.ACTION_ADD_LOCO).hideIn(form);
|
||||
new Input(ACTION, ACTION_ADD).hideIn(form);
|
||||
new Input(REALM,REALM_LOCO).hideIn(form);
|
||||
Fieldset fieldset = new Fieldset(t("add new locomotive"));
|
||||
new Input(Locomotive.NAME, t("new locomotive")).addTo(new Label(t("Name:")+" ")).addTo(fieldset);
|
||||
new Button(t("save")).addTo(fieldset);
|
||||
|
||||
@@ -93,13 +93,6 @@ public class Train implements Constants {
|
||||
}
|
||||
}
|
||||
|
||||
public static final String MODE_START = "start";
|
||||
public static final String MODE_SHOW = "show";
|
||||
private static final String MODE_UPDATE = "update";
|
||||
private static final String MODE_AUTO = "auto";
|
||||
|
||||
private static final String MODE_STOP = "stop";
|
||||
|
||||
public static final String LOCO_ID = "locoId";
|
||||
|
||||
public int speed = 0;
|
||||
@@ -117,27 +110,35 @@ public class Train implements Constants {
|
||||
}
|
||||
|
||||
public static Object action(HashMap<String, String> params) throws IOException {
|
||||
if (!params.containsKey(Train.ID)) return t("No train id passed!");
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) return t("No action passed to Train.action!");
|
||||
if (!params.containsKey(Train.ID)) {
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return manager();
|
||||
case ACTION_ADD:
|
||||
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
|
||||
if (loco == null) return t("unknown locomotive: {}",params.get(ID));
|
||||
return new Train(loco);
|
||||
}
|
||||
return t("No train id passed!");
|
||||
}
|
||||
long id = Long.parseLong(params.get(Train.ID));
|
||||
Train train = trains.get(id);
|
||||
if (train == null) return(t("No train with id {}!",id));
|
||||
if (!params.containsKey("mode")) return t("No mode set for train action!");
|
||||
String mode = params.get("mode");
|
||||
switch (mode) {
|
||||
case MODE_AUTO:
|
||||
return train.automatic();
|
||||
case MODE_SHOW:
|
||||
return train.props();
|
||||
case MODE_START:
|
||||
return train.start();
|
||||
case MODE_STOP:
|
||||
return train.stop();
|
||||
case MODE_UPDATE:
|
||||
return train.update(params);
|
||||
default: return t("Unknown mode {} for {}",mode,train);
|
||||
switch (action) {
|
||||
case ACTION_PROPS:
|
||||
return train.props();
|
||||
case ACTION_AUTO:
|
||||
return train.automatic();
|
||||
case ACTION_START:
|
||||
return train.start();
|
||||
case ACTION_STOP:
|
||||
return train.stop();
|
||||
case ACTION_UPDATE:
|
||||
return train.update(params);
|
||||
}
|
||||
|
||||
//return null;
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
public void add(Car car) {
|
||||
@@ -194,7 +195,7 @@ public class Train implements Constants {
|
||||
}
|
||||
|
||||
public Tag link(String tagClass) {
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","train("+id+",'"+Train.MODE_SHOW+"')").content(name());
|
||||
return new Tag(tagClass).clazz("link").attr("onclick","train("+id+",'"+ACTION_PROPS+"')").content(name());
|
||||
}
|
||||
|
||||
public static Collection<Train> list() {
|
||||
@@ -267,9 +268,9 @@ public class Train implements Constants {
|
||||
Window window = new Window("train-properties",t("Properties of {}",getClass().getSimpleName()));
|
||||
|
||||
Form form = new Form();
|
||||
new Input("action","train").hideIn(form);
|
||||
new Input(ACTION,ACTION_UPDATE).hideIn(form);
|
||||
new Input(REALM,REALM_TRAIN).hideIn(form);
|
||||
new Input(ID,id).hideIn(form);
|
||||
new Input("mode",MODE_UPDATE).hideIn(form);
|
||||
|
||||
new Checkbox(PUSH_PULL, t("Push-pull train"), pushPull).addTo(form);
|
||||
new Button(t("save")).addTo(form).addTo(window);
|
||||
@@ -285,11 +286,11 @@ public class Train implements Constants {
|
||||
if (block != null) {
|
||||
new Tag("li").content(t("Current location: {}",block)).addTo(list);
|
||||
Tag actions = new Tag("li").clazz().content(t("Actions: "));
|
||||
new Button(t("start"),"train("+id+",'"+MODE_START+"')").addTo(actions);
|
||||
new Button(t("start"),"train("+id+",'"+ACTION_START+"')").addTo(actions);
|
||||
if (autopilot == null) {
|
||||
new Button(t("auto"),"train("+id+",'"+MODE_AUTO+"')").addTo(actions);
|
||||
new Button(t("auto"),"train("+id+",'"+ACTION_AUTO+"')").addTo(actions);
|
||||
} else {
|
||||
new Button(t("stop"),"train("+id+",'"+MODE_STOP+"')").addTo(actions);
|
||||
new Button(t("stop"),"train("+id+",'"+ACTION_STOP+"')").addTo(actions);
|
||||
}
|
||||
actions.addTo(list);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user