-
Notifications
You must be signed in to change notification settings - Fork 3
Implement the mapping API #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| #!/usr/bin/python3 | ||
| import json | ||
| import os | ||
| import threading | ||
| import rospy | ||
| import urllib.request | ||
| import urllib.error | ||
|
Comment on lines
+6
to
+7
|
||
| from std_msgs.msg import Bool, Empty, String | ||
| from hovermap_api_msgs.msg import ScanInformation, ScanInformationList, HovermapStatus | ||
|
|
||
| # Maps ip_prefix from mule.yaml to the actual Hovermap address | ||
| IP_PREFIX_TO_ADDRESS = { | ||
| "192.168.2.0": "192.168.2.115", | ||
| "192.168.3.0": "192.168.3.115", | ||
| "10.9.0.0": "10.9.0.1", | ||
| } | ||
|
|
||
| DOWNLOAD_DIR = "/data/downloads" | ||
|
|
||
| STATUS_POLL_INTERVAL = 1 # seconds | ||
| SCAN_DOWNLOAD_PREP_TIMEOUT = 450.0 # seconds to wait for zip file to be generated | ||
|
|
||
| # Payload states (from the /status "state" field) that do NOT correspond to a | ||
| # running scan. Every other state is a scan actively running or transitioning | ||
| # through the start/stop sequence, so scan_running is True for them. | ||
| NON_RUNNING_STATES = frozenset( | ||
| { | ||
| "Stopped", | ||
| "Disabled", | ||
| "Booting", | ||
| "USB Error", | ||
| "USB Mounted", | ||
| "Transferring Data", | ||
| "Processes Stopped", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| class HttpInterfaceNode: | ||
| def __init__(self): | ||
| rospy.init_node("http_interface_node") | ||
|
|
||
| ip_prefix = rospy.get_param("~ip_prefix") | ||
| assert isinstance(ip_prefix, str) # To make pyright happy | ||
| if ip_prefix not in IP_PREFIX_TO_ADDRESS: | ||
| rospy.logfatal(f"Unknown ip_prefix: {ip_prefix}") | ||
| raise RuntimeError(f"Unknown ip_prefix: {ip_prefix}") | ||
|
|
||
| self._base_url = f"http://{IP_PREFIX_TO_ADDRESS[ip_prefix]}" | ||
|
|
||
| os.makedirs(DOWNLOAD_DIR, exist_ok=True) | ||
|
|
||
| rospy.Subscriber("start_scan", Empty, self._on_start_scan) | ||
| rospy.Subscriber("stop_scan", Empty, self._on_stop_scan) | ||
| rospy.Subscriber("scan_names_request", Empty, self._on_scan_names_request) | ||
| rospy.Subscriber("download_scan", String, self._on_download_scan) | ||
| rospy.Subscriber("set_scan_prefix", String, self._on_set_scan_prefix) | ||
|
|
||
| self._scan_names_pub = rospy.Publisher( | ||
| "scan_names_response", ScanInformationList, queue_size=5 | ||
| ) | ||
| self._status_pub = rospy.Publisher( | ||
| "hovermap_status", HovermapStatus, queue_size=5 | ||
| ) | ||
| self._scan_download_successful_pub = rospy.Publisher( | ||
| "scan_download_successful", Bool, queue_size=5 | ||
| ) | ||
|
|
||
| rospy.Timer(rospy.Duration(STATUS_POLL_INTERVAL), self._poll_status) | ||
|
|
||
| rospy.loginfo(f"http_interface_node ready, target: {self._base_url}") | ||
|
|
||
| def _get(self, path, log=True): | ||
| url = f"{self._base_url}{path}" | ||
| try: | ||
| req = urllib.request.Request(url) | ||
| with urllib.request.urlopen(req, timeout=5.0) as response: | ||
| body = response.read().decode() | ||
| if log: | ||
| rospy.loginfo(f"GET {url} -> {response.status}: {body}") | ||
| return body | ||
| except urllib.error.HTTPError as e: | ||
| rospy.logerr(f"GET {url} failed: HTTP {e.code} {e.reason}") | ||
| return None | ||
| except urllib.error.URLError as e: | ||
| rospy.logerr(f"GET {url} failed: {e.reason}") | ||
| return None | ||
|
|
||
| def _on_start_scan(self, msg): | ||
| self._get("/startsystem?mission_type=1") | ||
|
|
||
| def _on_stop_scan(self, msg): | ||
| self._get("/stopsystem") | ||
|
|
||
| def _on_scan_names_request(self, msg): | ||
| body = self._get("/files") | ||
| if body is None: | ||
| return | ||
| try: | ||
| data = json.loads(body) | ||
| scans = sorted(data.get("scans", []), key=lambda s: s["time"], reverse=True) | ||
| scan_list = ScanInformationList() | ||
| for scan in scans: | ||
| info = ScanInformation() | ||
| info.name = scan["scan"] | ||
| info.size = scan["size"] | ||
| scan_list.scans.append(info) # pyright: ignore[reportOptionalMemberAccess] | ||
| self._scan_names_pub.publish(scan_list) | ||
| except (json.JSONDecodeError, KeyError) as e: | ||
| rospy.logerr(f"Failed to parse /files response: {e}") | ||
|
|
||
| def _on_download_scan(self, msg): | ||
| thread = threading.Thread( | ||
| target=self._download_scan, args=(msg.data,), daemon=True | ||
| ) | ||
| thread.start() | ||
|
|
||
| def _download_scan(self, scan_name): | ||
| """Downloads the specified scans from the Hovermap. | ||
|
|
||
| Hovermap will abort returning a HTTP 404 error if a scan is running, or | ||
| if you are already downloading a scan, so we don't need guards | ||
| """ | ||
| url = f"{self._base_url}/downloadscan?scanname={scan_name}" | ||
| output_path = os.path.join(DOWNLOAD_DIR, f"{scan_name}.zip") | ||
| rospy.loginfo(f"Downloading scan '{scan_name}' to {output_path}") | ||
|
glennswagner marked this conversation as resolved.
|
||
| result_msg = Bool() | ||
| try: | ||
| req = urllib.request.Request(url) | ||
| with urllib.request.urlopen( | ||
| req, timeout=SCAN_DOWNLOAD_PREP_TIMEOUT | ||
| ) as response: | ||
| rospy.loginfo("Zip file generation completed, starting download...") | ||
| total_size = int(response.headers.get("Content-Length", 0)) | ||
| downloaded = 0 | ||
| last_logged_pct = 0 | ||
| with open(output_path, "wb") as f: | ||
| while True: | ||
| chunk = response.read(1048576) | ||
| if not chunk: | ||
| break | ||
| f.write(chunk) | ||
|
|
||
| # Progress messages | ||
| downloaded += len(chunk) | ||
| if total_size > 0: | ||
| pct = (downloaded * 100) // total_size | ||
| if pct >= last_logged_pct + 10: | ||
| last_logged_pct = pct - (pct % 10) | ||
| rospy.loginfo( | ||
| f"Download '{scan_name}': {last_logged_pct}%" | ||
| ) | ||
| rospy.loginfo(f"Download complete: {output_path}") | ||
| result_msg.data = True | ||
| self._scan_download_successful_pub.publish(result_msg) | ||
| except urllib.error.HTTPError as e: | ||
| rospy.logerr(f"Download failed for '{scan_name}': HTTP {e.code} {e.reason}") | ||
| self._scan_download_successful_pub.publish(result_msg) | ||
| except urllib.error.URLError as e: | ||
| rospy.logerr(f"Download failed for '{scan_name}': {e.reason}") | ||
| self._scan_download_successful_pub.publish(result_msg) | ||
|
Comment on lines
+159
to
+161
|
||
|
|
||
| def _on_set_scan_prefix(self, msg): | ||
| self._get(f"/setprefix?prefix={msg.data}") | ||
|
Comment on lines
+163
to
+164
|
||
|
|
||
| def _poll_status(self, event): | ||
| body = self._get("/status", log=False) | ||
| if body is None: | ||
| return | ||
| try: | ||
| data = json.loads(body) | ||
| status = HovermapStatus() | ||
| status.scan_prefix = data["scan_name"] | ||
| status.current_scan_name = data["scan_dir"] | ||
| status.free_space = data["freeSpace"] | ||
| status.scan_running = data["state"] not in NON_RUNNING_STATES | ||
| self._status_pub.publish(status) | ||
| except (json.JSONDecodeError, KeyError) as e: | ||
| rospy.logerr(f"Failed to parse /status response: {e}") | ||
|
|
||
| def spin(self): | ||
| rospy.spin() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| node = HttpInterfaceNode() | ||
| node.spin() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| cmake_minimum_required(VERSION 3.16) | ||
| project(hovermap_api_msgs) | ||
|
|
||
| find_package(catkin REQUIRED COMPONENTS | ||
| message_generation | ||
| std_msgs | ||
| ) | ||
|
|
||
| add_message_files(FILES | ||
| ScanInformation.msg | ||
| ScanInformationList.msg | ||
| HovermapStatus.msg | ||
| ) | ||
|
|
||
| generate_messages( | ||
| DEPENDENCIES | ||
| std_msgs | ||
| ) | ||
|
|
||
| catkin_package( | ||
| CATKIN_DEPENDS | ||
| message_runtime | ||
| std_msgs | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| string scan_prefix | ||
| string current_scan_name | ||
| string free_space | ||
| bool scan_running |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| string name | ||
| int64 size # in bytes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ScanInformation[] scans |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.