use of jmri.server.json.JSON in project JMRI by JMRI.
the class JsonUtilHttpService method getSystemConnections.
/**
*
* @param locale the client's Locale.
* @return the JSON systemConnections message.
*/
public ArrayNode getSystemConnections(Locale locale) {
ArrayNode root = mapper.createArrayNode();
ArrayList<String> prefixes = new ArrayList<>();
for (ConnectionConfig config : InstanceManager.getDefault(ConnectionConfigManager.class)) {
if (!config.getDisabled()) {
ObjectNode connection = mapper.createObjectNode().put(JSON.TYPE, JSON.SYSTEM_CONNECTION);
ObjectNode data = connection.putObject(JSON.DATA);
data.put(JSON.NAME, config.getConnectionName());
data.put(JSON.MFG, config.getManufacturer());
data.put(JSON.PREFIX, config.getAdapter().getSystemConnectionMemo().getSystemPrefix());
data.put(JSON.DESCRIPTION, Bundle.getMessage(locale, "ConnectionSucceeded", config.getConnectionName(), config.name(), config.getInfo()));
prefixes.add(config.getAdapter().getSystemConnectionMemo().getSystemPrefix());
root.add(connection);
}
}
InstanceManager.getList(SystemConnectionMemo.class).stream().map((instance) -> instance).filter((memo) -> (!memo.getDisabled() && !prefixes.contains(memo.getSystemPrefix()))).forEach((memo) -> {
ObjectNode connection = mapper.createObjectNode().put(JSON.TYPE, JSON.SYSTEM_CONNECTION);
ObjectNode data = connection.putObject(JSON.DATA);
data.put(JSON.NAME, memo.getUserName());
data.put(JSON.PREFIX, memo.getSystemPrefix());
data.putNull(JSON.MFG);
data.putNull(JSON.DESCRIPTION);
prefixes.add(memo.getSystemPrefix());
root.add(connection);
});
// Following is required because despite there being a SystemConnectionMemo
// for the default internal connection, it is not used for the default internal
// connection. This allows a client to map the server's internal objects.
String prefix = "I";
if (!prefixes.contains(prefix)) {
ObjectNode connection = mapper.createObjectNode().put(JSON.TYPE, JSON.SYSTEM_CONNECTION);
ObjectNode data = connection.putObject(JSON.DATA);
data.put(JSON.NAME, ConnectionNameFromSystemName.getConnectionName(prefix));
data.put(JSON.PREFIX, prefix);
data.putNull(JSON.MFG);
data.putNull(JSON.DESCRIPTION);
root.add(connection);
}
return root;
}
Aggregations