From 8af23ebfd973156fdf3ee12c3bcfc7747774a19f Mon Sep 17 00:00:00 2001 From: bharnden Date: Wed, 21 Nov 2018 15:41:00 -0800 Subject: [PATCH] core rest - consolidate config group logic and added endpoints for all mobility/emane configurations --- webapp/core_utils.py | 24 +++++++++++++++ webapp/emane_routes.py | 63 ++++++++++++--------------------------- webapp/mobility_routes.py | 19 +++++++++++- 3 files changed, 61 insertions(+), 45 deletions(-) diff --git a/webapp/core_utils.py b/webapp/core_utils.py index 4fc8685f..48da997c 100644 --- a/webapp/core_utils.py +++ b/webapp/core_utils.py @@ -19,6 +19,30 @@ def synchronized(function): return wrapper +def get_config_groups(model, config): + config_options = [] + for configuration in model.configurations(): + value = config[configuration.id] + config_options.append({ + "label": configuration.label, + "name": configuration.id, + "value": value, + "type": configuration.type.value, + "select": configuration.options + }) + + response = [] + for config_group in model.config_groups(): + start = config_group.start - 1 + stop = config_group.stop + response.append({ + "name": config_group.name, + "options": config_options[start: stop] + }) + + return response + + def convert_value(value): if value is None: return value diff --git a/webapp/emane_routes.py b/webapp/emane_routes.py index a386b11f..b61b3cb3 100644 --- a/webapp/emane_routes.py +++ b/webapp/emane_routes.py @@ -31,32 +31,28 @@ def set_emane_model_config(session_id): return jsonify() +@api.route("/sessions//emane/model/configs") +def get_emane_model_configs(session_id): + session = core_utils.get_session(coreemu, session_id) + response = {} + for node_id, model_config in session.emane.node_configurations.iteritems(): + if node_id == -1: + continue + + for model_name in model_config.iterkeys(): + model = session.emane.models[model_name] + config = session.emane.get_model_config(node_id, model_name) + config_groups = core_utils.get_config_groups(model, config) + node_configurations = response.setdefault(node_id, {}) + node_configurations[model_name] = config_groups + return jsonify(configurations=response) + + @api.route("/sessions//emane/config") def get_emane_config(session_id): session = core_utils.get_session(coreemu, session_id) - config = session.emane.get_configs() - - config_options = [] - for configuration in session.emane.emane_config.configurations(): - value = config[configuration.id] - config_options.append({ - "label": configuration.label, - "name": configuration.id, - "value": value, - "type": configuration.type.value, - "select": configuration.options - }) - - response = [] - for config_group in session.emane.emane_config.config_groups(): - start = config_group.start - 1 - stop = config_group.stop - response.append({ - "name": config_group.name, - "options": config_options[start: stop] - }) - + response = core_utils.get_config_groups(session.emane.emane_config, config) return jsonify(groups=response) @@ -64,31 +60,10 @@ def get_emane_config(session_id): def get_emane_model_config(session_id): session = core_utils.get_session(coreemu, session_id) node_id = core_utils.get_node_id(request.args["node"]) - model_name = request.args["name"] model = session.emane.models[model_name] config = session.emane.get_model_config(node_id, model_name) - - config_options = [] - for configuration in model.configurations(): - value = config[configuration.id] - config_options.append({ - "label": configuration.label, - "name": configuration.id, - "value": value, - "type": configuration.type.value, - "select": configuration.options - }) - - response = [] - for config_group in model.config_groups(): - start = config_group.start - 1 - stop = config_group.stop - response.append({ - "name": config_group.name, - "options": config_options[start: stop] - }) - + response = core_utils.get_config_groups(model, config) return jsonify(groups=response) diff --git a/webapp/mobility_routes.py b/webapp/mobility_routes.py index cbe62e2d..40d8493e 100644 --- a/webapp/mobility_routes.py +++ b/webapp/mobility_routes.py @@ -1,17 +1,34 @@ import os +from core.mobility import Ns2ScriptedMobility from flask import Blueprint from flask import jsonify from flask import request import core_utils -from core.mobility import Ns2ScriptedMobility api = Blueprint("mobility_api", __name__) coreemu = None +@api.route("/sessions//mobility/configs") +def get_mobility_configs(session_id): + session = core_utils.get_session(coreemu, session_id) + configs = {} + for node_id, model_config in session.mobility.node_configurations.iteritems(): + if node_id == -1: + continue + + for model_name in model_config.iterkeys(): + if model_name != Ns2ScriptedMobility.name: + continue + + config = session.mobility.get_model_config(node_id, model_name) + configs[node_id] = config + return jsonify(configurations=configs) + + @api.route("/sessions//nodes//mobility", methods=["POST"]) def set_mobility_config(session_id, node_id): session = core_utils.get_session(coreemu, session_id)