Skip to content

Commit

Permalink
Merge pull request #217 from JdeRobot/adding-custom-universes
Browse files Browse the repository at this point in the history
Adding custom universe back
  • Loading branch information
javizqh authored Nov 17, 2024
2 parents a5a5e75 + 396f7da commit 5df7c09
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 43 deletions.
29 changes: 19 additions & 10 deletions backend/tree_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,18 +924,27 @@ def generate_app(request):
json_translator.translate(main_tree_graph, main_tree_tmp_path, bt_order)

# Copy all the subtrees to the temp folder
for subtree_file in os.listdir(subtree_path):
if subtree_file.endswith(".json"):
subtree_name = base = os.path.splitext(os.path.basename(subtree_file))[
0
]
xml_path = os.path.join(
project_path, "code", "trees", "subtrees", f"{subtree_name}.xml"
)
try:
for subtree_file in os.listdir(subtree_path):
if subtree_file.endswith(".json"):
subtree_name = base = os.path.splitext(
os.path.basename(subtree_file)
)[0]
print(os.path.join(subtree_path, subtree_file))

xml_path = os.path.join(
project_path, "code", "trees", "subtrees", f"{subtree_name}.xml"
)

with open(os.path.join(subtree_path, subtree_file), "r+") as f:
# Reading from a file
subtree_json = f.read()

json_translator.translate(subtree_file, xml_path, bt_order)
json_translator.translate(subtree_json, xml_path, bt_order)

shutil.copy(xml_path, result_trees_tmp_path)
shutil.copy(xml_path, result_trees_tmp_path)
except:
print("No subtrees")

# Generate a self-contained tree
tree_generator.generate(
Expand Down
24 changes: 10 additions & 14 deletions frontend/src/api_helper/TreeWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,29 +182,25 @@ const getCustomUniverseZip = async (

const apiUrl = "/tree_api/get_universe_zip/";
try {
// Configure the request options
const config = {
method: "POST",
url: apiUrl,
headers: {
"Content-Type": "application/json",
},
data: JSON.stringify({
// Make the request
const response = await axios.post(
apiUrl,
{
app_name: currentProjectname,
universe_name: universeName,
}),
};

// Make the request
const response = await axios(config);
},
{
responseType: "blob", // Ensure the response is treated as a Blob
},
);

// Handle unsuccessful response status (e.g., non-2xx status)
if (!isSuccessful(response)) {
throw new Error(
response.data.message || "Failed to retrieve custom universe",
); // Response error
}
return new Blob([response.data], { type: "application/octet-stream" });
return response.data;
} catch (error: unknown) {
throw error; // Rethrow
}
Expand Down
42 changes: 23 additions & 19 deletions frontend/src/components/header_menu/HeaderMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,36 @@ const HeaderMenu = ({
await manager.prepareVisualization(universe_config.visualization);
console.log("Viz ready!");
} else {
console.log("Custom universe rework underway");
console.warn("Custom universe rework underway");
const zipBlob: Blob = await getCustomUniverseZip(
configJson.name,
currentProjectname,
);
var reader = new FileReader();
reader.readAsDataURL(zipBlob);
reader.onloadend = async function () {
// Get the zip in base64
var base64data = reader.result;
const universe_config = {
name: configJson.name,
launch_file_path: configJson.ram_config.launch_file_path,
ros_version: configJson.ram_config.ros_version,
visualization: "bt_studio",
world: configJson.ram_config.world,
zip: base64data,
};
await manager.launchWorld(universe_config);
console.log("RB universe launched!");
await manager.prepareVisualization(universe_config.visualization);
console.log("Viz ready!");
};
}
} catch (error: unknown) {
throw error; // rethrow
}
};

// const launchUniverse = async (universe_name) => {
// const apiUrl = `/tree_api/get_universe_configuration?project_name=${encodeURIComponent(
// currentProjectname,
// )}&universe_name=${encodeURIComponent(universe_name)}`;

// try {
// const response = await axios.get(apiUrl);
// console.log("Stored universe config: " + response.data);
// const stored_cfg = JSON.parse(response.data);
// if (stored_cfg.type === "robotics_backend") {
// await launchBackendUniverse(stored_cfg);
// } else {
// launchCustomUniverse(stored_cfg);
// }
// } catch (error) {
// console.error("Error launching universe:", error);
// }
// };

// Project handling

const onCreateProject = async (projectName: string) => {
Expand Down

0 comments on commit 5df7c09

Please sign in to comment.