From 653179fb7e1c37f3b149fb96b42dbc1006bb16c9 Mon Sep 17 00:00:00 2001
From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com>
Date: Wed, 30 Oct 2024 18:13:07 +0000
Subject: [PATCH] Added the drawable pane, now I need to make it draw graphs
 very nicely.

---
 App/RobobinApp/RobobinApp.csproj              |  7 +-
 App/RobobinApp/RobobinApp.csproj.user         |  3 +
 .../ViewModels/ConnectionPageViewModel.cs     | 66 +++++++++++--------
 App/RobobinApp/Views/CentreGraph.xaml         | 11 ++++
 App/RobobinApp/Views/CentreGraph.xaml.cs      | 23 +++++++
 App/RobobinApp/Views/MainPage.xaml            | 44 ++++++-------
 6 files changed, 103 insertions(+), 51 deletions(-)
 create mode 100644 App/RobobinApp/Views/CentreGraph.xaml
 create mode 100644 App/RobobinApp/Views/CentreGraph.xaml.cs

diff --git a/App/RobobinApp/RobobinApp.csproj b/App/RobobinApp/RobobinApp.csproj
index 269654ee..294a17f8 100644
--- a/App/RobobinApp/RobobinApp.csproj
+++ b/App/RobobinApp/RobobinApp.csproj
@@ -20,7 +20,7 @@
     </PropertyGroup>
 
     <ItemGroup>
-        <MauiIcon Include="Resources\AppIcon\robobinlogo.png"/>
+        <MauiIcon Include="Resources\AppIcon\robobinlogo.png" />
         <MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
         <MauiImage Include="Resources\Images\*" />
         <MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />
@@ -39,6 +39,8 @@
         <PackageReference Include="Shiny" Version="2.7.3" />
         <PackageReference Include="Shiny.BluetoothLE" Version="3.3.3" />
         <PackageReference Include="Shiny.Hosting.Maui" Version="3.3.3" />
+        <PackageReference Include="SkiaSharp.Views.Maui.Controls" Version="2.88.8" />
+        <PackageReference Include="SkiaSharp.Views.Maui.Core" Version="2.88.8" />
     </ItemGroup>
 
     <ItemGroup>
@@ -54,6 +56,9 @@
         <MauiXaml Update="LeftBox.xaml">
             <Generator>MSBuild:Compile</Generator>
         </MauiXaml>
+        <MauiXaml Update="Views\CentreGraph.xaml">
+          <Generator>MSBuild:Compile</Generator>
+        </MauiXaml>
         <MauiXaml Update="Views\ConnectionPage.xaml">
             <Generator>MSBuild:Compile</Generator>
         </MauiXaml>
diff --git a/App/RobobinApp/RobobinApp.csproj.user b/App/RobobinApp/RobobinApp.csproj.user
index 8a145f6b..db9abdfa 100644
--- a/App/RobobinApp/RobobinApp.csproj.user
+++ b/App/RobobinApp/RobobinApp.csproj.user
@@ -20,6 +20,9 @@
     </AppxManifest>
   </ItemGroup>
   <ItemGroup>
+    <MauiXaml Update="Views\CentreGraph.xaml">
+      <SubType>Designer</SubType>
+    </MauiXaml>
     <MauiXaml Update="Views\ConnectionPage.xaml">
       <SubType>Designer</SubType>
     </MauiXaml>
diff --git a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
index ab467fd2..57804e7a 100644
--- a/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
+++ b/App/RobobinApp/ViewModels/ConnectionPageViewModel.cs
@@ -150,7 +150,7 @@ namespace RobobinApp.ViewModels
             CheckAndRequestBluetoothPermissions();
         }
 
-        private void OnSendWifiInfo(object obj)
+        private async void OnSendWifiInfo(object obj)
         {
             try
             {
@@ -162,7 +162,18 @@ namespace RobobinApp.ViewModels
                 var SSID = SelectedWifiNetwork.SSID;
 
                 var result = WriteOperationAsync("CONNECT", SSID + "," + password);
-                Debug.WriteLine($"Result: {result}");
+
+                await Task.Delay(5000);
+                //var successfulConnect = await ReadOperationAsync();
+
+                var successfulConnect = "SUCCESS";
+                Debug.WriteLine($"Result: {successfulConnect}");
+
+                if (successfulConnect.Equals("SUCCESS"))
+                {
+                    Application.Current.MainPage.DisplayAlert("SUCCESS",$"RoboBin is now connected to {SelectedWifiNetwork.SSID}!","Great :)");
+                    await OnGoHome();
+                }
             }
             catch (Exception e)
             {
@@ -172,15 +183,20 @@ namespace RobobinApp.ViewModels
 
         private async Task PingPiASyncAsync()
         {
-            if (tempUnit.Equals("C"))
+      
+            await WriteOperationAsync("PING");
+            await Task.Delay(5000);
+            var result = await ReadOperationAsync();
+            Debug.WriteLine($"Result: {result}");
+            if (result == "PING_RETURN")
             {
-                tempUnit = "F";
+                Debug.WriteLine("Ping verify succeeded");
+
             }
             else
             {
-                tempUnit = "C";
+                Debug.WriteLine("Ping return could not be verified, please check the connection");
             }
-            await WriteOperationAsync(tempUnit);
 
         }
 
@@ -273,6 +289,7 @@ namespace RobobinApp.ViewModels
         public async Task OnGoHome()
         {
             Debug.WriteLine("Navigating to home page.");
+            ResetState();
             await Application.Current.MainPage.Navigation.PushAsync(new MainPage());
         }
 
@@ -311,18 +328,21 @@ namespace RobobinApp.ViewModels
         private void OnDeviceDiscovered(object? sender, DeviceEventArgs e)
         {
             Debug.WriteLine($"Discovered device: {e.Device.Name ?? e.Device.Id.ToString()}");
-
-            var bluetoothDevice = new BluetoothDevice(
-                string.IsNullOrWhiteSpace(e.Device.Name) ? e.Device.Id.ToString() : e.Device.Name,
-                e.Device.Id.ToString()
-            );
-
-            // Check if the device is already in the list
-            if (!BluetoothDevices.Any(d => d.MacAddress == bluetoothDevice.MacAddress))
+            if (e.Device.Name.Equals("RoboBin-BT"))
             {
-                // Device is new, add it
-                Microsoft.Maui.Controls.Device.BeginInvokeOnMainThread(() => BluetoothDevices.Add(bluetoothDevice));
+                var bluetoothDevice = new BluetoothDevice(
+                            string.IsNullOrWhiteSpace(e.Device.Name) ? e.Device.Id.ToString() : e.Device.Name,
+                            e.Device.Id.ToString()
+                        );
+
+                // Check if the device is already in the list
+                if (!BluetoothDevices.Any(d => d.MacAddress == bluetoothDevice.MacAddress))
+                {
+                    // Device is new, add it
+                    Microsoft.Maui.Controls.Device.BeginInvokeOnMainThread(() => BluetoothDevices.Add(bluetoothDevice));
+                }
             }
+        
         }
 
         private void RemoveUnavailableDevices(IEnumerable<IDevice> availableDevices)
@@ -441,10 +461,10 @@ namespace RobobinApp.ViewModels
                 try
                 {
                     Debug.WriteLine($"Attempt {attempt}: Reading from characteristic");
-                    var temperatureData = await ReadCharacteristic.ReadAsync();
-                    var temperatureValue = Encoding.UTF8.GetString(temperatureData.data);
-                    Debug.WriteLine($"Temperature Value: {temperatureValue}");
-                    return temperatureValue;
+                    var retrievedData = await ReadCharacteristic.ReadAsync();
+                    var retrievedValue = Encoding.UTF8.GetString(retrievedData.data);
+                    Debug.WriteLine($"Read Value: {retrievedValue}");
+                    return retrievedValue;
                 }
                 catch (Exception ex)
                 {
@@ -460,9 +480,6 @@ namespace RobobinApp.ViewModels
                     Debug.WriteLine("Reconnection failed. Returning failure.");
                     return "Failure";
                 }
-
-                // Wait before the next attempt
-                await Task.Delay(1000);
             }
             return "Failure"; // This will only be reached if all attempts failed without reconnection
         }
@@ -501,9 +518,6 @@ namespace RobobinApp.ViewModels
                     Debug.WriteLine("Reconnection failed. Returning failure.");
                     return "Failure";
                 }
-
-                // Wait before the next attempt
-                await Task.Delay(1000);
             }
             return "Failure"; // This will only be reached if all attempts failed without reconnection
         }
diff --git a/App/RobobinApp/Views/CentreGraph.xaml b/App/RobobinApp/Views/CentreGraph.xaml
new file mode 100644
index 00000000..4a6fa8d4
--- /dev/null
+++ b/App/RobobinApp/Views/CentreGraph.xaml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
+             x:Class="Robobin.Views.CentreGraph">
+    <VerticalStackLayout>
+        <Label 
+            Text="Welcome to .NET MAUI!"
+            VerticalOptions="Center" 
+            HorizontalOptions="Center" />
+    </VerticalStackLayout>
+</ContentView>
diff --git a/App/RobobinApp/Views/CentreGraph.xaml.cs b/App/RobobinApp/Views/CentreGraph.xaml.cs
new file mode 100644
index 00000000..201b4a14
--- /dev/null
+++ b/App/RobobinApp/Views/CentreGraph.xaml.cs
@@ -0,0 +1,23 @@
+using Microsoft.Maui.Graphics;
+
+namespace RobobinApp.Views
+{
+    public class GraphicsDrawable : IDrawable
+    {
+        public void Draw(ICanvas canvas, RectF dirtyRect)
+        {
+            // Set the background color
+            canvas.FillColor = Colors.LightPink; // Change to your desired color
+            canvas.FillRectangle(dirtyRect);
+
+            // Draw some graphics (for example, a circle)
+            canvas.FillColor = Colors.Red;
+            canvas.FillCircle(dirtyRect.Center.X, dirtyRect.Center.Y, 50);
+
+            // Example of drawing text
+            canvas.FontSize = 24;
+            canvas.FillColor = Colors.Black;
+            canvas.DrawString("Hello, GraphicsView!", dirtyRect.Center.X, dirtyRect.Center.Y, HorizontalAlignment.Center);
+        }
+    }
+}
diff --git a/App/RobobinApp/Views/MainPage.xaml b/App/RobobinApp/Views/MainPage.xaml
index 3e6da28f..0504ed59 100644
--- a/App/RobobinApp/Views/MainPage.xaml
+++ b/App/RobobinApp/Views/MainPage.xaml
@@ -2,10 +2,9 @@
 <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              xmlns:local="clr-namespace:RobobinApp.Views"  
-      
-
              xmlns:viewModels="clr-namespace:RobobinApp.ViewModels"
-             x:Class="RobobinApp.Views.MainPage"
+             xmlns:drawable="clr-namespace:RobobinApp.Views"
+    x:Class="RobobinApp.Views.MainPage"
              Title="">
     <ContentPage.BindingContext>
         <viewModels:MainPageViewModel />
@@ -13,11 +12,11 @@
 
     <ContentPage.Resources>
         <ResourceDictionary>
+            <drawable:GraphicsDrawable x:Key="drawable" />
             <StyleSheet Source="/Resources/Styles/appstyle.css" />
         </ResourceDictionary>
     </ContentPage.Resources>
 
-
     <Grid VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
         <Grid.RowDefinitions>
             <RowDefinition Height="*"/>
@@ -29,37 +28,34 @@
         </Grid.ColumnDefinitions>
 
         <VerticalStackLayout StyleClass="sideFrame" 
-               HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
-               Grid.Column="0" Grid.Row="0">
+                             HorizontalOptions="FillAndExpand" 
+                             VerticalOptions="FillAndExpand"
+                             Grid.Column="0" Grid.Row="0">
             <HorizontalStackLayout HorizontalOptions="Start" VerticalOptions="End">
-                <Button Text="Admin" HorizontalOptions="Start" VerticalOptions="Start" Command="{Binding ConnectToRobobinCommand}"/>
-                <Button Text="Setup" HorizontalOptions="Start" VerticalOptions="Start" Command="{Binding ConnectToRobobinCommand}"/>
-
+                <Button Text="Admin" Command="{Binding ConnectToRobobinCommand}"/>
+                <Button Text="Setup" Command="{Binding ConnectToRobobinCommand}"/>
             </HorizontalStackLayout>
 
-
-
-            <local:SideBox Grid.Column="0" Grid.Row="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
-            <local:SideBox Grid.Column="0" Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeaderTitle= "Status:" />
-            
-
+            <local:SideBox HeaderTitle="Status:" />
         </VerticalStackLayout>
+
         <Frame StyleClass="mainFrame" 
-               HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
+               HorizontalOptions="FillAndExpand" 
+               VerticalOptions="FillAndExpand"
                Grid.Column="1" Grid.Row="0">
-            <Label Text="{Binding StatusMessage}" HorizontalOptions="Center" VerticalOptions="Center"/>
+            <GraphicsView Drawable="{StaticResource drawable}"
+                          HeightRequest="300"
+                          WidthRequest="400" />
         </Frame>
 
         <VerticalStackLayout StyleClass="sideFrame" 
-               HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"
-               Grid.Column="2" Grid.Row="0">
+                             HorizontalOptions="FillAndExpand" 
+                             VerticalOptions="FillAndExpand"
+                             Grid.Column="2" Grid.Row="0">
             <HorizontalStackLayout HorizontalOptions="Start" VerticalOptions="End">
-                <Button Text="Info" HorizontalOptions="Start" VerticalOptions="Start"  />
-
+                <Button Text="Info" />
             </HorizontalStackLayout>
-
-            <local:SideBox Grid.Column="0" Grid.Row="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeaderTitle= "Mode:" />
-            <local:SideBox Grid.Column="0" Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeaderTitle= "Admin:" />
+            <local:SideBox HeaderTitle="Mode:" />
         </VerticalStackLayout>
     </Grid>
 </ContentPage>
-- 
GitLab