Skip to content
Permalink
Browse files

feat: Allow more properties to be set/get by JSON client

  • Loading branch information...
rhwood committed May 23, 2019
1 parent 062c10f commit 43c7012f4fbfbe51c4a3693937e6721c7271f850
@@ -12,6 +12,7 @@ ErrorEmptyAttribute=Property "{0}" must not be empty for type {1}.
ErrorMissingAttribute=Property "{0}" is required for type {1}.
ErrorMissingPropertyPut=Property "{0}" is required to create a new {1}.
ErrorUneditableProperty=Property "{0}" of type {1} cannot be posted.
ErrorInvalidPropertyValue=Property "{0}" of type {1} has in invalid value "{2}"
UnlistableService={0} cannot be listed.
NotAClientType=No messages from clients of type {0} are allowed.
NotAServerType=No messages from servers of type {0} are allowed.
@@ -109,6 +109,10 @@
* {@value #DEFAULT}
*/
public static final String DEFAULT = "default"; // NOI18N
/**
* {@value #SPEED}
*/
public static final String SPEED = "speed"; // NOI18N

/* JSON hello and metadata */
/**
@@ -636,7 +640,8 @@
* {@value #UNKNOWN}
* <p>
* Note that this value deliberately differs from
* {@link jmri.NamedBean#UNKNOWN}.
* {@link jmri.NamedBean#UNKNOWN} so that JSON clients can treat
* all known states as true, and the unknown state as false.
*/
public static final int UNKNOWN = 0x00;

@@ -1,5 +1,7 @@
package jmri.server.json.block;

import jmri.server.json.JSON;

/**
* Constants used by the internal JMRI JSON Block service.
*
@@ -15,6 +17,22 @@
* {@value #BLOCKS}
*/
public static final String BLOCKS = "blocks"; // NOI18N
/**
* {@value #CURVATURE}
*/
public static final String CURVATURE = "curvature"; // NOI18N
/**
* {@value #DENIED}
*/
public static final String DENIED = "denied"; // NOI18N
/**
* {@value #PERMISSIVE}
*/
public static final String PERMISSIVE = "permissive"; // NOI18N
/**
* {@value #SPEED_LIMIT}
*/
public static final String SPEED_LIMIT = JSON.SPEED + "Limit"; // NOI18N

/**
* Prevent instantiation, since this class only contains static values
@@ -5,12 +5,14 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.util.Locale;
import javax.servlet.http.HttpServletResponse;
import jmri.Block;
import jmri.BlockManager;
import jmri.InstanceManager;
import jmri.JmriException;
import jmri.ProvidingManager;
import jmri.Reporter;
import jmri.ReporterManager;
@@ -48,11 +50,22 @@ public ObjectNode doGet(Block block, String name, String type, Locale locale, in
data.put(JSON.VALUE, block.getValue() != null ? block.getValue().toString() : null);
data.put(JsonSensor.SENSOR, block.getSensor() != null ? block.getSensor().getSystemName() : null);
data.put(JsonReporter.REPORTER, block.getReporter() != null ? block.getReporter().getSystemName() : null);
data.put(JSON.SPEED, block.getBlockSpeed());
data.put(JsonBlock.CURVATURE, block.getCurvature());
data.put(JSON.DIRECTION, block.getDirection());
data.put(JSON.LENGTH, block.getLengthMm());
data.put(JsonBlock.PERMISSIVE, block.getPermissiveWorking());
data.put(JsonBlock.SPEED_LIMIT, block.getSpeedLimit());
ArrayNode array = data.putArray(JsonBlock.DENIED);
block.getDeniedBlocks().forEach((denied) -> {
array.add(denied);
});
return root;
}

@Override
public ObjectNode doPost(Block block, String name, String type, JsonNode data, Locale locale, int id) throws JsonException {
public ObjectNode doPost(Block block, String name, String type, JsonNode data, Locale locale, int id)
throws JsonException {
if (!data.path(JSON.VALUE).isMissingNode()) {
if (data.path(JSON.VALUE).isNull()) {
block.setValue(null);
@@ -83,7 +96,8 @@ public ObjectNode doPost(Block block, String name, String type, JsonNode data, L
if (sensor != null) {
block.setSensor(sensor.getSystemName());
} else {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorNotFound", JsonSensor.SENSOR, node.asText()), id);
throw new JsonException(404,
Bundle.getMessage(locale, "ErrorNotFound", JsonSensor.SENSOR, node.asText()), id);
}
}
}
@@ -96,10 +110,21 @@ public ObjectNode doPost(Block block, String name, String type, JsonNode data, L
if (reporter != null) {
block.setReporter(reporter);
} else {
throw new JsonException(404, Bundle.getMessage(locale, "ErrorNotFound", JsonReporter.REPORTER, node.asText()), id);
throw new JsonException(404,
Bundle.getMessage(locale, "ErrorNotFound", JsonReporter.REPORTER, node.asText()), id);
}
}
}
String text = data.findPath(JSON.SPEED).asText(block.getBlockSpeed());
try {
block.setBlockSpeed(text);
} catch (JmriException ex) {
throw new JsonException(HttpServletResponse.SC_BAD_REQUEST, Bundle.getMessage(locale, "ErrorInvalidPropertyValue", JSON.SPEED, type, text), id);
}
block.setCurvature(data.path(JsonBlock.CURVATURE).asInt(block.getCurvature()));
block.setDirection(data.path(JSON.DIRECTION).asInt(block.getDirection()));
block.setLength(Double.valueOf(data.path(JSON.LENGTH).asDouble(block.getLengthMm())).floatValue());
block.setPermissiveWorking(data.path(JsonBlock.PERMISSIVE).asBoolean(block.getPermissiveWorking()));
return this.doGet(block, name, type, locale, id);
}

@@ -28,6 +28,7 @@
import jmri.ThrottleListener;
import jmri.ThrottleManager;
import jmri.jmrit.roster.Roster;
import jmri.server.json.JSON;
import jmri.server.json.JsonException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -54,8 +55,11 @@
public static final String IDLE = "idle"; // NOI18N
/**
* {@value #SPEED}
*
* @deprecated since 4.15.7; use {@link jmri.server.json.JSON#SPEED} instead
*/
public static final String SPEED = "speed"; // NOI18N
@Deprecated
public static final String SPEED = JSON.SPEED; // NOI18N
/**
* {@value #SPEED_STEPS}
*/

0 comments on commit 43c7012

Please sign in to comment.
You can’t perform that action at this time.