Skip to content
Snippets Groups Projects
Commit e13a0437 authored by Paul-Winpenny's avatar Paul-Winpenny
Browse files

Modes now exist within the .NET

parent e1d31660
No related branches found
No related tags found
1 merge request!2Manual control user interface
......@@ -8,18 +8,46 @@ using System.Diagnostics;
using System.Text.Json;
using Newtonsoft.Json;
using JsonException = Newtonsoft.Json.JsonException;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace RobobinApp.Networking
{
public class WifiManager : IWifiService
public class WifiManager : IWifiService, INotifyPropertyChanged
{
private (float X, float Y)? _location;
private string _mode = "Unknown";
public event PropertyChangedEventHandler PropertyChanged;
private UdpClient _udpClient;
private const int BroadcastPort = 5005;
private bool _isConnected = false;
private CancellationTokenSource _cancellationTokenSource;
private TcpClient _tcpClient;
public string Location { get; private set; } = "Unknown";
public (float X, float Y)? Location
{
get => _location;
private set
{
_location = value;
OnPropertyChanged(); // Notify change
}
}
public string Mode
{
get => _mode;
private set
{
_mode = value;
OnPropertyChanged(); // Notify change
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// Event to notify the UI or other parts of the app of specific messages
public event Action<string> OnMessageReceived;
......@@ -43,6 +71,7 @@ namespace RobobinApp.Networking
// Parse JSON
var jsonMessage = JsonConvert.DeserializeObject<dynamic>(message);
Debug.WriteLine($"Message Type {jsonMessage.type}");
if (jsonMessage != null && jsonMessage.type == "ROBOBIN_PRESENT")
{
var location = jsonMessage.data?.Location;
......@@ -50,7 +79,11 @@ namespace RobobinApp.Networking
if (location != null && mode != null)
{
Location = $"({location[0]}, {location[1]})";
float x = (float)location[0];
float y = (float)location[1];
Location = (x, y);
Mode = mode.ToString();
Debug.WriteLine($"Detected Robobin presence at location: {Location}, Mode: {mode}");
SendConnectMessage(result.RemoteEndPoint.Address.ToString());
}
......@@ -90,7 +123,7 @@ namespace RobobinApp.Networking
{
if (_isConnected)
{
Debug.WriteLine("Already connected. No need to send another connect message.");
//Debug.WriteLine("Already connected. No need to send another connect message.");
return;
}
......
......@@ -53,6 +53,7 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
<PackageReference Include="Microsoft.Maui.Essentials" Version="8.0.91" />
<PackageReference Include="Microsoft.Maui.Resizetizer" Version="8.0.91" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Plugin.BLE" Version="3.1.0" />
<PackageReference Include="Shiny" Version="2.7.3" />
<PackageReference Include="Shiny.BluetoothLE" Version="3.3.3" />
......
......@@ -6,44 +6,85 @@
<Frame class="side-box-frame">
<VerticalStackLayout>
<!-- Header -->
<Grid class="side-box-header">
<Label x:Name="HeaderText"
Text="Admin:"
class="side-box-header-text"/>
</Grid>
<!-- Content -->
<ScrollView class="side-box-content">
<VerticalStackLayout>
<HorizontalStackLayout>
<!-- Send TCP Message Section -->
<VerticalStackLayout Spacing="10" Padding="10">
<Label Text="Send TCP Message:" />
<!-- Grid Layout for Entry and Button -->
<Grid ColumnDefinitions="*, Auto">
<!-- Entry Field -->
<Entry x:Name="TcpMessageEntry"
Placeholder="Type message here"
BackgroundColor="#FFFFFF"
Grid.Column="0"
HorizontalOptions="FillAndExpand"
HorizontalOptions="Start"
VerticalOptions="Center"
WidthRequest="200"
HeightRequest="40"
Margin="0" />
<!-- Send Button -->
<Button Text="Send"
Clicked="OnSendMessageClicked"
class="button-primary"
Grid.Column="1"
HorizontalOptions="End"
Margin="0"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="Latest Message:" Margin="0,10,0,5"/>
<Frame class="primaryFrame">
<Label x:Name="LatestMessageLabel" TextColor="White"
Text="No messages yet."
/>
Grid.Column="1"
HeightRequest="40"
Margin="5,0,0,0" />
</Grid>
</VerticalStackLayout>
<!-- Latest Message Section -->
<VerticalStackLayout>
<Label Text="Latest Message:"
Margin="0,10,0,5" />
<Frame class="primaryFrame"
Padding="5"
HorizontalOptions="FillAndExpand">
<Label x:Name="LatestMessageLabel"
TextColor="White"
Text="No messages yet." />
</Frame>
</HorizontalStackLayout>
</VerticalStackLayout>
<!-- Mode Selection Buttons -->
<VerticalStackLayout Spacing="10" Padding="10">
<Label Text="Mode Selection:"
FontAttributes="Bold"
HorizontalOptions="Center" />
<!-- Buttons for Modes -->
<Button Text="Manual Mode"
Clicked="OnManualModeClicked"
BackgroundColor="Red"
TextColor="White"
HeightRequest="40"
CornerRadius="5" />
<Button Text="Call Me Mode"
Clicked="OnCallMeModeClicked"
BackgroundColor="Green"
TextColor="White"
HeightRequest="40"
CornerRadius="5" />
<Button Text="Follow Me Mode"
Clicked="OnFollowMeModeClicked"
BackgroundColor="Blue"
TextColor="White"
HeightRequest="40"
CornerRadius="5" />
</VerticalStackLayout>
</VerticalStackLayout>
</ScrollView>
......
......@@ -54,6 +54,21 @@ namespace RobobinApp.Views.Sides
}
}
private void OnManualModeClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
private void OnCallMeModeClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
private void OnFollowMeModeClicked(object sender, EventArgs e)
{
throw new NotImplementedException();
}
protected static void OnHeaderTitleChanged(BindableObject bindable, object oldValue, object newValue)
{
var control = (AdminBox)bindable;
......@@ -72,5 +87,7 @@ namespace RobobinApp.Views.Sides
App.WifiManager.OnMessageReceived -= UpdateLatestMessage;
}
}
}
}
namespace RobobinApp.Views.Sides;
using RobobinApp.Networking;
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Maui.Dispatching;
namespace RobobinApp.Views.Sides
{
public partial class ModeBox : ContentView
{
public ModeBox()
{
InitializeComponent();
UpdateMode("Control");
// Subscribe to changes in Mode
App.WifiManager.PropertyChanged += WifiManager_PropertyChanged;
// Initialize mode UI
UpdateMode(App.WifiManager.Mode);
}
private void WifiManager_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Debug.WriteLine($"New change, mode = {App.WifiManager.Mode}");
// Ensure the update happens on the UI thread
MainThread.BeginInvokeOnMainThread(() =>
{
UpdateMode(App.WifiManager.Mode);
});
}
public void UpdateMode(string mode)
{
// Reset all states to default
CallMeState.BackgroundColor = Colors.LightGray;
FollowState.BackgroundColor = Colors.LightGray;
ControlState.BackgroundColor = Colors.LightGray;
......@@ -18,15 +40,18 @@ public partial class ModeBox : ContentView
switch (mode)
{
case "Call Me":
Debug.WriteLine("New mode is call me");
CallMeState.BackgroundColor = Colors.Green;
break;
case "Follow":
Debug.WriteLine("New mode is follow me");
FollowState.BackgroundColor = Colors.Blue;
break;
case "Control":
case "Manual":
Debug.WriteLine("New mode is manual");
ControlState.BackgroundColor = Colors.Red;
break;
}
}
}
}
......@@ -36,7 +36,11 @@ class ApiNode(Node):
if not data:
break
self.get_logger().info(f"Received data: {data}")
topic,message = self.message_handler.handle_message(client_socket, data)
result = self.message_handler.handle_message(client_socket, data)
# Check if the result is not None
if result is not None:
topic, message = result # Safely unpack after checking
if topic is not None:
self.get_logger().info(f"Publishing to topic: {topic}")
self.publish_to_topic(topic, message)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment