overhauled storing and loading code for routes, actions and conditions

This commit is contained in:
Stephan Richter
2020-10-30 11:03:39 +01:00
parent 05784f94ce
commit b68b1168c7
8 changed files with 72 additions and 31 deletions

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.7.14</version> <version>0.8.1</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>

View File

@@ -1,11 +1,9 @@
package de.srsoftware.web4rail; package de.srsoftware.web4rail;
import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileReader; import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
@@ -15,8 +13,8 @@ import java.util.Map.Entry;
import java.util.Vector; import java.util.Vector;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONTokener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -71,6 +69,7 @@ public class Route implements Constants{
private static final String TRIGGER = "trigger"; private static final String TRIGGER = "trigger";
private static final String ACTIONS = "actions"; private static final String ACTIONS = "actions";
private static final String ACTION_LISTS = "action_lists"; private static final String ACTION_LISTS = "action_lists";
private static final String ROUTES = "routes";
/** /**
* process commands from the client * process commands from the client
@@ -355,7 +354,6 @@ public class Route implements Constants{
if (json.has(SIGNALS)) { if (json.has(SIGNALS)) {
for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get((String) signalId, false)); for (Object signalId : json.getJSONArray(SIGNALS)) addSignal((Signal) plan.get((String) signalId, false));
} }
if (json.has(ACTIONS)) loadActions(json.getJSONArray(ACTIONS));
if (json.has(ACTION_LISTS)) loadActions(json.getJSONArray(ACTION_LISTS)); if (json.has(ACTION_LISTS)) loadActions(json.getJSONArray(ACTION_LISTS));
return plan.registerRoute(this); return plan.registerRoute(this);
} }
@@ -364,30 +362,21 @@ public class Route implements Constants{
for (int i=0; i<arr.length(); i++) { for (int i=0; i<arr.length(); i++) {
JSONObject json = arr.getJSONObject(i); JSONObject json = arr.getJSONObject(i);
String trigger = json.getString(TRIGGER); String trigger = json.getString(TRIGGER);
JSONArray actions = json.getJSONArray("actions"); ActionList actionList = ActionList.load(json.getJSONArray(ACTIONS));
for (int k=0; k<actions.length(); k++) { triggers.put(trigger, actionList);
try {
Action action = Action.load(actions.getJSONObject(k));
LOG.debug("Loaded {}",action);
add(trigger, action);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException| InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException | JSONException e) {
LOG.warn("Was not able to load action: ",e);
}
}
} }
} }
public static void loadAll(String filename, Plan plan) throws IOException { public static void loadAll(String filename, Plan plan) throws IOException {
BufferedReader file = new BufferedReader(new FileReader(filename)); FileInputStream fis = new FileInputStream(filename);
String line = file.readLine(); JSONTokener tokener = new JSONTokener(fis);
while (line != null) { JSONObject json = new JSONObject(tokener);
JSONObject json = new JSONObject(line); JSONArray routes = json.getJSONArray(ROUTES);
new Route().load(json,plan); for (Object o : routes) {
if (o instanceof JSONObject) new Route().load((JSONObject)o, plan);
line = file.readLine();
} }
file.close(); fis.close();
LOG.debug("json: {}",json.getClass());
} }
public boolean lock() { public boolean lock() {
@@ -442,14 +431,14 @@ public class Route implements Constants{
public static void saveAll(Collection<Route> routes, String filename) throws IOException { public static void saveAll(Collection<Route> routes, String filename) throws IOException {
BufferedWriter file = new BufferedWriter(new FileWriter(filename)); BufferedWriter file = new BufferedWriter(new FileWriter(filename));
file.write("[\n"); file.write("{\""+ROUTES+"\":[\n");
int count = 0; int count = 0;
for (Route route : routes) { for (Route route : routes) {
file.write(route.json()); file.write(route.json());
if (++count < routes.size()) file.write(","); if (++count < routes.size()) file.write(",");
file.write("\n"); file.write("\n");
} }
file.write("]"); file.write("]}");
file.close(); file.close();
} }

View File

@@ -1,7 +1,6 @@
package de.srsoftware.web4rail.actions; package de.srsoftware.web4rail.actions;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -57,17 +56,21 @@ public abstract class Action implements Constants {
return new Tag("span").content(toString()+NBSP).attr("onclick", action); return new Tag("span").content(toString()+NBSP).attr("onclick", action);
} }
public static Action load(JSONObject json) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, ClassNotFoundException { public static Action load(JSONObject json) {
String clazz = json.getString(TYPE); String clazz = json.getString(TYPE);
switch (clazz) { switch (clazz) {
case "ActivateRoute": case "ActivateRoute":
return new ActivateRoute(); return new ActivateRoute();
case "ConditionalAction":
return ConditionalAction.load(json);
case "FinishRoute": case "FinishRoute":
return new FinishRoute(); return new FinishRoute();
case "PowerOff":
return new PowerOff();
case "SetSignalsToStop": case "SetSignalsToStop":
return new SetSignalsToStop(); return new SetSignalsToStop();
case "SpeedReduction": case "SetSpeed":
return new SetSpeed(json.getInt(SetSpeed.MAX_SPEED)); return SetSpeed.load(json);
case "TurnTrain": case "TurnTrain":
return new TurnTrain(); return new TurnTrain();
} }

View File

@@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Vector; import java.util.Vector;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -176,6 +177,17 @@ public class ActionList extends Vector<Action> implements Constants{
for (Action action : this) result.put(action.json()); for (Action action : this) result.put(action.json());
return result; return result;
} }
public static ActionList load(JSONArray list) {
ActionList actionList = new ActionList();
for (Object o : list) {
if (o instanceof JSONObject) {
Action action = Action.load((JSONObject) o);
if (action != null) actionList.add(action);
}
}
return actionList;
}
public boolean moveUp(int actionId) { public boolean moveUp(int actionId) {
for (int i=1; i<size(); i++) { for (int i=1; i<size(); i++) {

View File

@@ -83,6 +83,18 @@ public class ConditionalAction extends Action {
json.put(ACTIONS, actions.json()); json.put(ACTIONS, actions.json());
return json; return json;
} }
public static ConditionalAction load(JSONObject json) {
ConditionalAction action = new ConditionalAction();
for (Object o : json.getJSONArray(CONDITIONS)) {
if (o instanceof JSONObject) {
Condition condition = Condition.load((JSONObject)o);
if (condition != null) action.conditions.add(condition);
}
}
action.actions = ActionList.load(json.getJSONArray(ACTIONS));
return action;
}
@Override @Override
public Window properties(HashMap<String, String> params) { public Window properties(HashMap<String, String> params) {

View File

@@ -37,6 +37,11 @@ public class SetSpeed extends Action{
return json; return json;
} }
public static SetSpeed load(JSONObject json) {
int s = json.getInt(MAX_SPEED);
return new SetSpeed(s);
}
@Override @Override
public Window properties(HashMap<String, String> params) { public Window properties(HashMap<String, String> params) {
Window win = super.properties(params); Window win = super.properties(params);

View File

@@ -52,6 +52,15 @@ public abstract class Condition implements Constants {
return new JSONObject().put(TYPE, getClass().getSimpleName()); return new JSONObject().put(TYPE, getClass().getSimpleName());
} }
public static Condition load(JSONObject json) {
String type = json.getString(TYPE);
switch (type) {
case "TrainSelect":
return TrainSelect.load(json);
}
return null;
}
public Tag link(String tagClass,String context) { public Tag link(String tagClass,String context) {
String json = new JSONObject(Map.of(REALM,REALM_CONDITION,ID,id,ACTION,ACTION_PROPS,CONTEXT,context)).toString().replace("\"", "'"); String json = new JSONObject(Map.of(REALM,REALM_CONDITION,ID,id,ACTION,ACTION_PROPS,CONTEXT,context)).toString().replace("\"", "'");
return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(toString()); return new Tag(tagClass).clazz("link").attr("onclick","request("+json+")").content(toString());

View File

@@ -27,6 +27,11 @@ public class TrainSelect extends Condition {
return super.json().put(REALM_TRAIN, train.id); return super.json().put(REALM_TRAIN, train.id);
} }
public static TrainSelect load(JSONObject json) {
int trainId = json.getInt(REALM_TRAIN);
return new TrainSelect().train(Train.get(trainId));
}
@Override @Override
protected Window properties(HashMap<String, String> params) { protected Window properties(HashMap<String, String> params) {
Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName())); Window win = new Window("condition-props", t("Properties of {}",getClass().getSimpleName()));
@@ -46,6 +51,12 @@ public class TrainSelect extends Condition {
if (train == null) return super.toString(); if (train == null) return super.toString();
return t("Train = {}",train); return t("Train = {}",train);
} }
private TrainSelect train(Train train) {
this.train = train;
return this;
}
@Override @Override
protected Object update(HashMap<String, String> params) { protected Object update(HashMap<String, String> params) {