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

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

View File

@@ -1,7 +1,6 @@
package de.srsoftware.web4rail.actions;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
@@ -57,17 +56,21 @@ public abstract class Action implements Constants {
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);
switch (clazz) {
case "ActivateRoute":
return new ActivateRoute();
case "ConditionalAction":
return ConditionalAction.load(json);
case "FinishRoute":
return new FinishRoute();
case "PowerOff":
return new PowerOff();
case "SetSignalsToStop":
return new SetSignalsToStop();
case "SpeedReduction":
return new SetSpeed(json.getInt(SetSpeed.MAX_SPEED));
case "SetSpeed":
return SetSpeed.load(json);
case "TurnTrain":
return new TurnTrain();
}

View File

@@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Vector;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -176,6 +177,17 @@ public class ActionList extends Vector<Action> implements Constants{
for (Action action : this) result.put(action.json());
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) {
for (int i=1; i<size(); i++) {

View File

@@ -83,6 +83,18 @@ public class ConditionalAction extends Action {
json.put(ACTIONS, actions.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
public Window properties(HashMap<String, String> params) {

View File

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

View File

@@ -27,6 +27,11 @@ public class TrainSelect extends Condition {
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
protected Window properties(HashMap<String, String> params) {
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();
return t("Train = {}",train);
}
private TrainSelect train(Train train) {
this.train = train;
return this;
}
@Override
protected Object update(HashMap<String, String> params) {