refactored action communication
This commit is contained in:
@@ -6,6 +6,7 @@ const DIV = 'DIV';
|
||||
const SVG = 'svg';
|
||||
const PLAN = '#plan';
|
||||
const POST = 'POST';
|
||||
const PROPS = 'props';
|
||||
const CU = 'cu';
|
||||
const OPAC = 100;
|
||||
var selected = null;
|
||||
@@ -26,16 +27,16 @@ function addMessage(txt){
|
||||
}
|
||||
|
||||
function addTile(x,y){
|
||||
return request({action:mode,tile:selected.id,x:x,y:y});
|
||||
return request({realm:'plan',action:mode,tile:selected.id,x:x,y:y});
|
||||
}
|
||||
|
||||
function car(id,mode){
|
||||
return request({action:"car",id:id,mode:mode});
|
||||
function car(id,action){
|
||||
return request({realm:"car",action:action,id:id});
|
||||
}
|
||||
|
||||
function clickTile(x,y){
|
||||
var id = x+"-"+y;
|
||||
if ($('#'+id).length > 0) request({action:'click',id:id});
|
||||
if ($('#'+id).length > 0) request({realm:'plan',action:'click',id:id});
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -110,11 +111,11 @@ function keypress(ev){
|
||||
|
||||
function moveTile(x,y){
|
||||
var id = x+"-"+y;
|
||||
return request({action:mode,direction:selected.id,id:id});
|
||||
return request({realm:'plan',action:mode,direction:selected.id,id:id});
|
||||
}
|
||||
|
||||
function openRoute(id){
|
||||
request({action:'openRoute',id:id});
|
||||
request({realm:'route',action:PROPS,id:id});
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -184,8 +185,8 @@ function stream(ev){
|
||||
if (data.startsWith("dropclass")) return dropClass(data.substring(10));
|
||||
}
|
||||
|
||||
function train(id,mode){
|
||||
return request({action:"train",id:id,mode:mode});
|
||||
function train(id,action){
|
||||
return request({realm:'train',action:action,id:id});
|
||||
}
|
||||
|
||||
window.onload = function () {
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<root level="debug">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -112,7 +112,7 @@ public class Application {
|
||||
private static void sendPlan(HttpExchange client) throws IOException {
|
||||
try {
|
||||
HashMap<String, String> params = inflate(client.getRequestBody().readAllBytes());
|
||||
send(client,params.isEmpty() ? plan.html() : plan.process(params));
|
||||
send(client,params.isEmpty() ? plan.html() : plan.action(params));
|
||||
} catch (Exception e) {
|
||||
LOG.error("Error during sendPlan(): {}",e);
|
||||
send(client,new Page().append(e.getMessage()));
|
||||
|
||||
@@ -4,6 +4,7 @@ public interface Constants {
|
||||
public static final String ACTION = "action";
|
||||
public static final String ACTION_ADD = "add";
|
||||
public static final String ACTION_ANALYZE = "analyze";
|
||||
public static final String ACTION_AUTO = "auto";
|
||||
public static final String ACTION_CLICK = "click";
|
||||
public static final String ACTION_CONNECT = "connect";
|
||||
public static final String ACTION_EMERGENCY = "emergency";
|
||||
@@ -13,16 +14,19 @@ public interface Constants {
|
||||
public static final String ACTION_PROPS = "props";
|
||||
public static final String ACTION_SAVE = "save";
|
||||
public static final String ACTION_SLOWER10 = "slower10";
|
||||
public static final String ACTION_START = "start";
|
||||
public static final String ACTION_STOP = "stop";
|
||||
public static final String ACTION_TURN = "turn";
|
||||
public static final String ACTION_UPDATE = "update";
|
||||
|
||||
|
||||
|
||||
public static final String REALM = "realm";
|
||||
public static final String REALM_CAR = "car";
|
||||
public static final String REALM_CU = "cu";
|
||||
public static final String REALM_LOCO = "loco";
|
||||
public static final String REALM_ROUTE = "route";
|
||||
public static final String REALM_TILE = "tile";
|
||||
public static final String REALM_PLAN = "plan";
|
||||
public static final String REALM_TRAIN = "train";
|
||||
|
||||
public static final String ID = "id";
|
||||
|
||||
@@ -63,6 +63,7 @@ public class ControlUnit extends Thread implements Constants{
|
||||
private LinkedList<String> queue = new LinkedList<String>();
|
||||
private Socket socket;
|
||||
private Scanner scanner;
|
||||
private boolean power = false;
|
||||
|
||||
/**
|
||||
* @return stops the loop at the next interval
|
||||
@@ -105,6 +106,15 @@ public class ControlUnit extends Thread implements Constants{
|
||||
return json;
|
||||
}
|
||||
|
||||
public void load(String filename) throws IOException {
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
JSONObject json = new JSONObject(file.readLine());
|
||||
file.close();
|
||||
if (json.has(PORT)) port = json.getInt(PORT);
|
||||
if (json.has(BUS)) bus = json.getInt(BUS);
|
||||
if (json.has(HOST)) host = json.getString(HOST);
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws InterruptedException {
|
||||
ControlUnit cu = new ControlUnit().setEndpoint("Modellbahn", DEFAULT_PORT).setBus(1).restart();
|
||||
Thread.sleep(1000);
|
||||
@@ -114,6 +124,22 @@ public class ControlUnit extends Thread implements Constants{
|
||||
cu.end();
|
||||
}
|
||||
|
||||
public Object process(HashMap<String, String> params) {
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_CONNECT:
|
||||
restart();
|
||||
return t("Control unit (re)started.");
|
||||
case ACTION_POWER:
|
||||
return togglePower();
|
||||
case ACTION_PROPS:
|
||||
return properties();
|
||||
case ACTION_UPDATE:
|
||||
return update(params);
|
||||
}
|
||||
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
public Object properties() {
|
||||
Window win = new Window("cu-props", t("Properties of the control unit"));
|
||||
Form form = new Form();
|
||||
@@ -121,8 +147,8 @@ public class ControlUnit extends Thread implements Constants{
|
||||
new Input(REALM,REALM_CU).hideIn(form);
|
||||
Fieldset fieldset = new Fieldset(t("Server connection"));
|
||||
new Input(HOST,host).addTo(new Label(t("Hostname"))).addTo(fieldset);
|
||||
new Input(PORT,port).attr("type", "numeric").addTo(new Label(t("Port"))).addTo(fieldset);
|
||||
new Input(BUS,bus).attr("type", "numeric").addTo(new Label(t("Bus"))).addTo(fieldset);
|
||||
new Input(PORT,port).numeric().addTo(new Label(t("Port"))).addTo(fieldset);
|
||||
new Input(BUS,bus).numeric().addTo(new Label(t("Bus"))).addTo(fieldset);
|
||||
new Button(t("Save")).addTo(fieldset).addTo(form).addTo(win);
|
||||
|
||||
fieldset = new Fieldset("Actions");
|
||||
@@ -204,35 +230,28 @@ public class ControlUnit extends Thread implements Constants{
|
||||
super.start();
|
||||
}
|
||||
|
||||
private String t(String text,Object...fills) {
|
||||
private static String t(String text,Object...fills) {
|
||||
return Translation.get(Application.class, text, fills);
|
||||
}
|
||||
|
||||
private Object togglePower() {
|
||||
power = !power;
|
||||
String PW = power?"ON":"OFF";
|
||||
queue("SET {} POWER "+PW);
|
||||
return t("Turned power {}.",PW);
|
||||
}
|
||||
|
||||
|
||||
public String update(HashMap<String, String> params) {
|
||||
if (params.containsKey(HOST)) host = params.get(HOST);
|
||||
if (params.containsKey(PORT)) port = Integer.parseInt(params.get(PORT));
|
||||
if (params.containsKey(BUS)) bus = Integer.parseInt(params.get(BUS));
|
||||
return t("Updated control unit settings");
|
||||
}
|
||||
|
||||
private void writeln(String data) throws IOException {
|
||||
data = data.replace("{}", ""+bus);
|
||||
socket.getOutputStream().write((data+"\n").getBytes(StandardCharsets.US_ASCII));
|
||||
LOG.info("sent {}.",data);
|
||||
}
|
||||
|
||||
public void update(HashMap<String, String> params) {
|
||||
if (params.containsKey(HOST)) host = params.get(HOST);
|
||||
if (params.containsKey(PORT)) port = Integer.parseInt(params.get(PORT));
|
||||
if (params.containsKey(BUS)) bus = Integer.parseInt(params.get(BUS));
|
||||
}
|
||||
|
||||
public void load(String filename) throws IOException {
|
||||
BufferedReader file = new BufferedReader(new FileReader(filename));
|
||||
JSONObject json = new JSONObject(file.readLine());
|
||||
file.close();
|
||||
if (json.has(PORT)) port = json.getInt(PORT);
|
||||
if (json.has(BUS)) bus = json.getInt(BUS);
|
||||
if (json.has(HOST)) host = json.getString(HOST);
|
||||
}
|
||||
|
||||
public Object process(HashMap<String, String> params) {
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,6 @@ public class Plan implements Constants{
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Plan.class);
|
||||
private static final String X = "x";
|
||||
private static final String Y = "y";
|
||||
private static final String FILE = "file";
|
||||
private static final String DIRECTION = "direction";
|
||||
private static final HashMap<OutputStreamWriter,Integer> clients = new HashMap<OutputStreamWriter, Integer>();
|
||||
|
||||
@@ -101,18 +100,62 @@ public class Plan implements Constants{
|
||||
private HashSet<Block> blocks = new HashSet<Block>();
|
||||
private HashMap<Integer, Route> routes = new HashMap<Integer, Route>();
|
||||
private ControlUnit controlUnit = new ControlUnit();
|
||||
private boolean power = false;
|
||||
|
||||
public Plan() {
|
||||
new Heartbeat().start();
|
||||
}
|
||||
|
||||
public Object action(HashMap<String, String> params) {
|
||||
try {
|
||||
LOG.debug("Plan.action: {}",params);
|
||||
String realm = params.get(REALM);
|
||||
if (realm == null) throw new NullPointerException(REALM+" should not be null!");
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
|
||||
switch (realm) {
|
||||
case REALM_CAR:
|
||||
return carAction(params);
|
||||
case REALM_CU:
|
||||
return controlUnit.process(params);
|
||||
case REALM_LOCO:
|
||||
return locoAction(params);
|
||||
case REALM_PLAN:
|
||||
return planAction(params);
|
||||
case REALM_ROUTE:
|
||||
return routeAction(params);
|
||||
case REALM_TRAIN:
|
||||
Object result = Train.action(params);
|
||||
if (result instanceof Train) return html();
|
||||
return result;
|
||||
}
|
||||
return t("Unknown realm: {}",realm);
|
||||
} catch (Exception e) {
|
||||
String msg = e.getMessage();
|
||||
if (msg == null || msg.isEmpty()) msg = t("An unknown error occured!");
|
||||
LOG.debug(msg,e);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
private Object routeAction(HashMap<String, String> params) throws IOException {
|
||||
Route route = route(Integer.parseInt(params.get(ID)));
|
||||
if (route == null) return t("Unknown route: {}",params.get(ID));
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_PROPS:
|
||||
return route.properties();
|
||||
case ACTION_UPDATE:
|
||||
route.update(params);
|
||||
return html();
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private Tag actionMenu() throws IOException {
|
||||
Tag actionMenu = new Tag("div").clazz("actions").content(t("Actions"));
|
||||
Tag actions = new Tag("div").clazz("list").content("");
|
||||
new Div("power").clazz(REALM_CU).content(t("Toggle power")).addTo(actions);
|
||||
new Div("save").content(t("Save plan")).addTo(actions);
|
||||
new Div("analyze").content(t("Analyze plan")).addTo(actions);
|
||||
new Div("save").clazz(REALM_PLAN).content(t("Save plan")).addTo(actions);
|
||||
new Div("analyze").clazz(REALM_PLAN).content(t("Analyze plan")).addTo(actions);
|
||||
return actions.addTo(actionMenu);
|
||||
}
|
||||
|
||||
@@ -161,30 +204,22 @@ public class Plan implements Constants{
|
||||
}
|
||||
|
||||
private Object carAction(HashMap<String, String> params) throws IOException {
|
||||
|
||||
Car car = Car.get(params.get(Car.ID));
|
||||
if (car == null) return t("No car with id {} found!",params.get(Car.ID));
|
||||
|
||||
return car.properties();
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_UPDATE:
|
||||
car.update(params);
|
||||
return html();
|
||||
case ACTION_PROPS:
|
||||
return car.properties();
|
||||
}
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private Object click(Tile tile) throws IOException {
|
||||
if (tile == null) return null;
|
||||
return tile.click();
|
||||
}
|
||||
|
||||
private Object connect(HashMap<String, String> params) throws IOException {
|
||||
if (params.containsKey(REALM)) switch (params.get(REALM)) {
|
||||
case REALM_CU:
|
||||
controlUnit.restart();
|
||||
break;
|
||||
}
|
||||
return html();
|
||||
}
|
||||
|
||||
private Object cuProps(HashMap<String, String> params) {
|
||||
return controlUnit.properties();
|
||||
}
|
||||
|
||||
private Collection<Route> follow(Route route, Connector connector) {
|
||||
Tile tile = get(Tile.id(connector.x,connector.y),false);
|
||||
@@ -231,7 +266,7 @@ public class Plan implements Constants{
|
||||
private Tag hardwareMenu() throws IOException {
|
||||
Tag tileMenu = new Tag("div").clazz("hardware").content(t("Hardware"));
|
||||
Tag list = new Tag("div").clazz("list").content("");
|
||||
new Div(ACTION_PROPS).content(t("Control unit")).addTo(list);
|
||||
new Div(ACTION_PROPS).clazz(REALM_CU).content(t("Control unit")).addTo(list);
|
||||
return list.addTo(tileMenu);
|
||||
}
|
||||
|
||||
@@ -294,9 +329,13 @@ public class Plan implements Constants{
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD:
|
||||
new Locomotive(params.get(Locomotive.NAME));
|
||||
break;
|
||||
return html();
|
||||
case ACTION_FASTER10:
|
||||
return Locomotive.get(params.get(ID)).faster(10);
|
||||
case ACTION_PROPS:
|
||||
String id = params.get(ID);
|
||||
if (id == null) return Locomotive.manager();
|
||||
return Locomotive.get(params.get(ID)).properties();
|
||||
case ACTION_SLOWER10:
|
||||
return Locomotive.get(params.get(ID)).faster(-10);
|
||||
case ACTION_STOP:
|
||||
@@ -305,7 +344,7 @@ public class Plan implements Constants{
|
||||
return Locomotive.get(params.get(ID)).turn();
|
||||
}
|
||||
|
||||
return html();
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private Tag menu() throws IOException {
|
||||
@@ -390,75 +429,30 @@ public class Plan implements Constants{
|
||||
public void place(Tile tile) throws IOException {
|
||||
stream("place "+tile.tag(null));
|
||||
}
|
||||
|
||||
public Object process(HashMap<String, String> params) {
|
||||
try {
|
||||
String realm = params.get(REALM);
|
||||
if (realm == null) throw new NullPointerException(REALM+" should not be null!");
|
||||
String action = params.get(ACTION);
|
||||
if (action == null) throw new NullPointerException(ACTION+" should not be null!");
|
||||
switch (realm) {
|
||||
case REALM_CAR:
|
||||
return carAction(params);
|
||||
case REALM_CU:
|
||||
return controlUnit.process(params);
|
||||
case REALM_LOCO:
|
||||
return locoAction(params);
|
||||
case REALM_TILE:
|
||||
return tileAction(params);
|
||||
case REALM_TRAIN:
|
||||
return trainAction(params);
|
||||
}
|
||||
return t("Unknown realm: {}",realm);
|
||||
} catch (Exception e) {
|
||||
String msg = e.getMessage();
|
||||
if (msg == null || msg.isEmpty()) msg = t("An unknown error occured!");
|
||||
LOG.debug(msg,e);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
private Object tileAction(HashMap<String, String> params) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException {
|
||||
switch (params.get(Plan.ACTION)) {
|
||||
private Object planAction(HashMap<String, String> params) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, IOException {
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD:
|
||||
return addTile(params.get(TILE),params.get(X),params.get(Y),null);
|
||||
case ACTION_ANALYZE:
|
||||
return analyze();
|
||||
case ACTION_CLICK:
|
||||
return click(get(params.get(Tile.ID),true));
|
||||
case ACTION_MOVE:
|
||||
return moveTile(params.get(DIRECTION),params.get(Tile.ID));
|
||||
case ACTION_SAVE:
|
||||
return saveTo("default");
|
||||
case ACTION_UPDATE:
|
||||
update(get(params.get(Tile.ID),true),params);
|
||||
return html();
|
||||
}
|
||||
return null;
|
||||
return t("Unknown action: {}",params.get(ACTION));
|
||||
}
|
||||
|
||||
private Object togglePower() throws IOException {
|
||||
power = !power;
|
||||
String PW = power?"ON":"OFF";
|
||||
queue("SET {} POWER "+PW);
|
||||
return t("Turned power {}.",PW);
|
||||
}
|
||||
|
||||
private Object trainAction(HashMap<String, String> params) throws IOException {
|
||||
LOG.debug("Params: {}",params);
|
||||
switch (params.get(ACTION)) {
|
||||
case ACTION_ADD:
|
||||
Locomotive loco = (Locomotive) Locomotive.get(params.get(Train.LOCO_ID));
|
||||
if (loco == null) return t("unknown locomotive: {}",params.get(Locomotive.ID));
|
||||
new Train(loco);
|
||||
break;
|
||||
case ACTION_PROPS:
|
||||
Object result = Train.action(params);
|
||||
if (!(result instanceof Train)) return result;
|
||||
break;
|
||||
}
|
||||
return html();
|
||||
}
|
||||
|
||||
public Route route(int routeId) {
|
||||
return routes.get(routeId);
|
||||
}
|
||||
|
||||
private Object routeProperties(int id) {
|
||||
Route route = route(id);
|
||||
if (route == null) return t("Could not find route \"{}\"",id);
|
||||
return route.properties();
|
||||
}
|
||||
|
||||
Route registerRoute(Route route) {
|
||||
for (Tile tile: route.path()) tile.add(route);
|
||||
routes.put(route.id(), route);
|
||||
@@ -578,32 +572,6 @@ public class Plan implements Constants{
|
||||
return tiles.addTo(tileMenu);
|
||||
}
|
||||
|
||||
private Object update(HashMap<String, String> params) throws IOException {
|
||||
if (params.containsKey(REALM)) {
|
||||
switch (params.get(REALM)) {
|
||||
case REALM_CAR:
|
||||
Car car = Car.get(params.get(Car.ID));
|
||||
if (car == null) return t("No car with id {} found!",params.get(Car.ID));
|
||||
car.update(params);
|
||||
break;
|
||||
case REALM_CU:
|
||||
controlUnit.update(params);
|
||||
break;
|
||||
case REALM_ROUTE:
|
||||
Route route = routes.get(Integer.parseInt(params.get(ID)));
|
||||
if (route == null) return t("Unknown route: {}",params.get(ID));
|
||||
route.update(params);
|
||||
break;
|
||||
case REALM_TILE:
|
||||
update(get(params.get(Tile.ID),true),params);
|
||||
break;
|
||||
default:
|
||||
return t("Unknown realm \"{}\"",params.get(REALM));
|
||||
}
|
||||
}
|
||||
return html();
|
||||
}
|
||||
|
||||
private void update(Tile tile, HashMap<String, String> params) throws IOException {
|
||||
if (tile != null) place(tile.update(params));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public class Input extends Tag{
|
||||
}
|
||||
|
||||
public Input numeric() {
|
||||
attr("type","numeric");
|
||||
attr("type","number");
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ public abstract class Block extends StretchableTile{
|
||||
if (train != null) {
|
||||
new Tag("h4").content(t("Train:")).addTo(window);
|
||||
train.link("span").addTo(window);
|
||||
new Button(t("start"),"train("+train.id+",'"+Train.MODE_START+"')").addTo(window);
|
||||
new Button(t("start"),"train("+train.id+",'"+ACTION_START+"')").addTo(window);
|
||||
}
|
||||
return window;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import de.keawe.tools.translations.Translation;
|
||||
import de.srsoftware.tools.Tag;
|
||||
import de.srsoftware.web4rail.Application;
|
||||
import de.srsoftware.web4rail.Connector;
|
||||
import de.srsoftware.web4rail.Constants;
|
||||
import de.srsoftware.web4rail.Plan;
|
||||
import de.srsoftware.web4rail.Plan.Direction;
|
||||
import de.srsoftware.web4rail.Route;
|
||||
@@ -34,7 +35,7 @@ import de.srsoftware.web4rail.tags.Form;
|
||||
import de.srsoftware.web4rail.tags.Input;
|
||||
import de.srsoftware.web4rail.tags.Radio;
|
||||
|
||||
public abstract class Tile {
|
||||
public abstract class Tile implements Constants{
|
||||
|
||||
public static final String ID = "id";
|
||||
private static final String TYPE = "type";
|
||||
@@ -180,9 +181,9 @@ public abstract class Tile {
|
||||
|
||||
public Tag propForm() {
|
||||
Form form = new Form();
|
||||
new Input(Plan.ACTION, Plan.ACTION_UPDATE).hideIn(form);
|
||||
new Input(Plan.REALM, Plan.REALM_TILE).hideIn(form);
|
||||
new Input(Plan.ID,id()).hideIn(form);
|
||||
new Input(ACTION, ACTION_UPDATE).hideIn(form);
|
||||
new Input(REALM, REALM_PLAN).hideIn(form);
|
||||
new Input(ID,id()).hideIn(form);
|
||||
|
||||
List<Direction> pd = possibleDirections();
|
||||
if (!pd.isEmpty()) {
|
||||
|
||||
Reference in New Issue
Block a user