From 31ce11ec985b9993aa0eb76749a29e14a9f328bd Mon Sep 17 00:00:00 2001 From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com> Date: Tue, 14 Jan 2025 20:20:31 +0000 Subject: [PATCH] Updated for unit tests part of report. --- App/RobobinApp/Networking/WifiManager.cs | 5 +- .../ViewModels/MainPageViewModel.cs | 3 ++ App/RobobinApp/Views/Sides/AdminBox.xaml.cs | 7 ++- .../robobin/helpers/message_handler.py | 47 ++++++++++++------- 4 files changed, 41 insertions(+), 21 deletions(-) diff --git a/App/RobobinApp/Networking/WifiManager.cs b/App/RobobinApp/Networking/WifiManager.cs index b034c349..1ba99cc0 100644 --- a/App/RobobinApp/Networking/WifiManager.cs +++ b/App/RobobinApp/Networking/WifiManager.cs @@ -186,9 +186,10 @@ namespace RobobinApp.Networking public async Task SendMessageAsync(string message) { + Debug.WriteLine($"Sent message: {message}"); if (!_isConnected || _tcpClient == null) { - Debug.WriteLine("Not connected. Cannot send message."); + //Debug.WriteLine("Not connected. Cannot send message."); return; } @@ -197,7 +198,7 @@ namespace RobobinApp.Networking NetworkStream stream = _tcpClient.GetStream(); byte[] data = Encoding.ASCII.GetBytes(message); await stream.WriteAsync(data, 0, data.Length); - Debug.WriteLine($"Sent message: {message}"); + } catch (Exception ex) { diff --git a/App/RobobinApp/ViewModels/MainPageViewModel.cs b/App/RobobinApp/ViewModels/MainPageViewModel.cs index a3477a1d..536ae8e3 100644 --- a/App/RobobinApp/ViewModels/MainPageViewModel.cs +++ b/App/RobobinApp/ViewModels/MainPageViewModel.cs @@ -182,6 +182,9 @@ namespace RobobinApp.ViewModels { mode = "Call"; await App.WifiManager.SendMessageAsync("SETMODE " + mode); + } else + { + Debug.WriteLine($"Can't switch while in mode " + App.WifiManager.Mode); } } diff --git a/App/RobobinApp/Views/Sides/AdminBox.xaml.cs b/App/RobobinApp/Views/Sides/AdminBox.xaml.cs index e9b1e994..473bf503 100644 --- a/App/RobobinApp/Views/Sides/AdminBox.xaml.cs +++ b/App/RobobinApp/Views/Sides/AdminBox.xaml.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using Microsoft.Maui.Controls; namespace RobobinApp.Views.Sides @@ -85,10 +86,12 @@ namespace RobobinApp.Views.Sides } private async void OnStopClicked(object sender, EventArgs e) { - + if (App.WifiManager.Mode == "Manual") + { string stopMessage = "STOP"; await App.WifiManager.SendMessageAsync(stopMessage); - + } + } private async void OnCallBPMClicked(object sender, EventArgs e) diff --git a/ros2/src/robobin/robobin/helpers/message_handler.py b/ros2/src/robobin/robobin/helpers/message_handler.py index 3182da3b..533db517 100644 --- a/ros2/src/robobin/robobin/helpers/message_handler.py +++ b/ros2/src/robobin/robobin/helpers/message_handler.py @@ -14,12 +14,15 @@ class MessageHandler: "SETMODE": self.handle_set_mode, "STOP": self.handle_stop, "CALLME": self.handle_call_me, - "BPM": self.handle_call_bpm, + "CALLBPM": self.handle_call_bpm, "REQMAP": self.handle_request_map, "LOCATION": self.handle_request_location } def handle_request_location(self, client_socket, message): """Handles the LOC command.""" + if self.testing: + print("Requesting location") + return "nav_command", "LOC" response = f"location is {self.api_node.connection_manager.location}".encode() if self.testing: print(response.decode()) @@ -44,16 +47,17 @@ class MessageHandler: print(message) location = message.strip() # Remove parentheses location = f"{location}" # Add parentheses back for proper formatting - - for existing_socket, existing_location in self.api_node.called_locations: - if existing_socket == client_socket: - client_socket.sendall(b"User already requested location") - return None - - # Append the client socket and location to the list - self.api_node.called_locations.append((client_socket, location)) + if not self.testing: + for existing_socket, existing_location in self.api_node.called_locations: + if existing_socket == client_socket: + client_socket.sendall(b"User already requested location") + return None + + # Append the client socket and location to the list + self.api_node.called_locations.append((client_socket, location)) print(f"Added {ip} with location {location} to the queue.") - + if self.testing: + return None response = f"Queue Position = {len(self.api_node.called_locations)-1} {location}".encode() client_socket.sendall(response) # print("nav_command", location) @@ -66,7 +70,8 @@ class MessageHandler: print(response.decode()) else: client_socket.sendall(response) - self.api_node.called_locations = [] # Clear the queue + + self.api_node.called_locations = [] # Clear the queue return "cmd_vel", (0.0, 0.0) def handle_ping(self, client_socket, _): @@ -85,8 +90,9 @@ class MessageHandler: print(response.decode()) else: client_socket.sendall(response) - self.api_node.mode = message - self.api_node.called_locations = [] # Clear the queue + if self.api_node is not None: + self.api_node.mode = message + self.api_node.called_locations = [] # Clear the queue return "nav_command", "SETMODE " + message def handle_time_request(self, client_socket, _): """Sends the current server time.""" @@ -104,7 +110,7 @@ class MessageHandler: print(response.decode()) else: client_socket.sendall(response) - + return "nav_command","BPM" def handle_manual_control(self, client_socket, message): """Handles manual control commands: W, A, S, D.""" @@ -138,7 +144,7 @@ class MessageHandler: """Handles the REQMAP command.""" # Relative path to the JSON file working_dir = os.getcwd() - graph_file_path = os.path.abspath(os.path.join(working_dir,"src", "robobin", "robobin", "graphs", "graph.json")) + graph_file_path = os.path.abspath(os.path.join(working_dir,"src", "robobin", "robobin", "graphs", "anchors.json")) try: # Load the graph JSON file @@ -201,6 +207,13 @@ if __name__ == "__main__": assert message_handler.handle_message(None, "MANUALCTRL D") == ("cmd_vel", (0, -0.25)) assert message_handler.handle_message(None, "STOP") == ("cmd_vel", (0.0, 0.0)) - assert message_handler.handle_message(None, "CALLME (212.9638, 283.98108)") == ("nav_command", "(212.9638, 283.98108)") + assert message_handler.handle_message(None, "CALLME (212.9638, 283.98108)") == None assert message_handler.handle_message(None, "UNKNOWN") is None - assert message_handler.handle_message(None, "REQMAP") is None \ No newline at end of file + assert message_handler.handle_message(None, "REQMAP") is None + assert message_handler.handle_message(None, "SETMODE MANUAL") == ("nav_command", "SETMODE MANUAL") + + + + + assert message_handler.handle_message(None, "CALLBPM")== ("nav_command", "BPM") + assert message_handler.handle_message(None, "LOCATION") is ("nav_command", "LOC") \ No newline at end of file -- GitLab