commit 03682d214c36c34f78c2059453871b8ad82cd26f
Author: ajournaux <ajournaux@TLS-GPS-AJOURN.inra.local>
Date:   Fri May 24 17:09:58 2024 +0200

    lecture tag RFID

diff --git a/SICPA_Connexions/Model/BytesPrtc.cs b/SICPA_Connexions/Model/BytesPrtc.cs
new file mode 100644
index 0000000..bb00b3c
--- /dev/null
+++ b/SICPA_Connexions/Model/BytesPrtc.cs
@@ -0,0 +1,16 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SICPA_Connexions.Model
+{
+    public static class BytesPrtc
+    {
+        public static byte STX = 0x02;
+        public static byte DST_TX = 0x49;
+        public static byte SRC_TX = 0x41;
+        public static byte DST_RX = 0x41;
+        public static byte SRC_RX = 0x49;
+        public static byte ETX = 0x03;
+    }
+}
diff --git a/SICPA_Connexions/Model/CRC.cs b/SICPA_Connexions/Model/CRC.cs
new file mode 100644
index 0000000..28ffdf4
--- /dev/null
+++ b/SICPA_Connexions/Model/CRC.cs
@@ -0,0 +1,54 @@
+﻿using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace SICPA_Connexions.Model
+{
+    public class CRC
+    {
+        private List<byte> data = new List<byte>();
+        public CRC() { }
+        public CRC(byte[] din) //trame entrée à la main
+        {
+            foreach (byte b in din) { data.Add(b); }
+        }
+        public void putBytes(byte[] din) //ajout trame depuis fonction
+        {
+            foreach (byte b in din) { data.Add(b); }
+        }
+        public byte getCRC()
+        {
+            byte thatCRC = 0, current_byte = 0, count, i;
+            for (i = 0; i < data.Count; i++) //calcul du CRC sur toute la trame excepté le CRC lui même et le ETX -> (frame.length-2)
+            {
+                current_byte = data[i];
+                //Console.WriteLine("CRC byte calc : {0:X}", current_byte);
+                for (count = 0; count < 8; count++)
+                {
+
+                    if (((thatCRC & 0x01) ^ (current_byte & 0x01)) != 0)//=1?
+                    {
+                        //XOR based to abr200 polygon (X^8 + X^4 + X^3 + X^2 + X^1)
+                        thatCRC ^= 0x70;
+                        thatCRC >>= 1; //bit shifting on the right
+                        thatCRC |= 0x80; //set 1 to msb bit
+                    }
+                    else
+                    {
+                        thatCRC >>= 1; //bit shifting on the right again :)
+                        thatCRC &= 0x7F; //set 0 to msb bit
+                    }
+                    //Console.WriteLine("current_crc : {0:X}", thatCRC);
+                    //bit shifting on the current byte...
+                    current_byte >>= 1;
+                }
+            }
+            //Console.WriteLine(data.Count);
+            /*for(i = 0; i < data.Count; i++)
+            {
+                data.Remove(i);
+            }*/
+            return thatCRC;
+        }
+    }
+}
diff --git a/SICPA_Connexions/Model/GestionnaireBLE.cs b/SICPA_Connexions/Model/GestionnaireBLE.cs
index dbbd0cc..b0d2bdf 100644
--- a/SICPA_Connexions/Model/GestionnaireBLE.cs
+++ b/SICPA_Connexions/Model/GestionnaireBLE.cs
@@ -20,9 +20,30 @@ namespace SICPA_Connexions.Model
         private List<ICharacteristic> charList;
         private ICharacteristic characRead;
         private ICharacteristic characWrite;
+        private String retourBLE;
+        public event EventHandler RetourBlEMofifiee;
         public IDevice DeviceConnected { get; set; }
         public IService SelectedService { get; set; }
-        public String RetourBLE { get; set; }
+        public String RetourBLE
+        {
+            get
+            {
+                return retourBLE;
+            }
+            set
+            {
+                if (retourBLE != value)
+                {
+                    retourBLE = value;
+                    OnRetourBLEMofifiee(EventArgs.Empty);
+                }
+            }
+        }
+
+        public void OnRetourBLEMofifiee(EventArgs e)
+        {
+            RetourBlEMofifiee?.Invoke(this, e);
+        }
 
         public GestionnaireBLE()
         {
@@ -173,5 +194,39 @@ namespace SICPA_Connexions.Model
             //});
         }
 
+        public  async Task LectureTag(IDevice deviceConnected)
+        {
+            try
+            {
+                String cmd = "READ";
+
+                List<byte> local_frame = new List<byte>();
+                CRC crcProcess = new CRC();
+                byte local_crc;
+
+                local_frame.Add(BytesPrtc.STX); //add STX byte
+                local_frame.Add(BytesPrtc.DST_TX); //add DST byte
+                local_frame.Add(BytesPrtc.SRC_TX); //add SRC byte
+                for (int i = 0; i < cmd.Length; i++)
+                {
+                    local_frame.Add((byte)cmd[i]); //add cmd string 
+                }
+                crcProcess.putBytes(local_frame.ToArray()); //send that buffer (without "the CRC" and ETX bytes !)
+                local_crc = crcProcess.getCRC(); //calculate CRC with that buffer
+
+                local_frame.Add(local_crc); //add CRC byte
+                local_frame.Add(BytesPrtc.ETX); //add ETX byte
+
+                /////////////////////////////////////////////
+                //And now send buffer !
+                await characWrite.WriteAsync(local_frame.ToArray());
+                await characRead.StartUpdatesAsync();
+            }
+            catch
+            {
+                await Application.Current.MainPage.DisplayAlert("Erreur de lecture", "Erreur de lecture du Tag", "OK");
+            }
+        }
+
     }
 }
diff --git a/SICPA_Connexions/View/AnimalView.xaml b/SICPA_Connexions/View/AnimalView.xaml
index 980d5c4..3341364 100644
--- a/SICPA_Connexions/View/AnimalView.xaml
+++ b/SICPA_Connexions/View/AnimalView.xaml
@@ -2,7 +2,7 @@
 <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
              x:Class="SICPA_Connexions.View.AnimalView"
-             Title="Formulaire Vivant">
+             Title="Animal">
     <Grid>
         <StackLayout BackgroundColor="{StaticResource InraePrimary}" Spacing="0">
             <ScrollView>
@@ -17,38 +17,22 @@
                             <StackLayout>
                                 <Frame BorderColor="{StaticResource InraePrimary}" Margin="0,0,0,20">
                                     <Label 
-                                        Text="Formulaire Vivant"
+                                        Text="Animal"
                                         VerticalOptions="Center" 
                                         HorizontalOptions="Center"
                                         Style="{StaticResource textTitre}"
                                         />
                                 </Frame>
-                                <Picker Title="Liste des vivants"
-                                        ItemsSource="{Binding Vivants}"
-                                        SelectedItem="{Binding VivantSelectionne}"
-                                        ItemDisplayBinding="{Binding VivantNom}"/>
-                                <Grid ColumnDefinitions="20*,80*" RowDefinitions="auto,auto">
+                                <Grid ColumnDefinitions="15*,85*" >
                                     <Label Grid.Column="0"
-                                           Grid.Row="0"
-                                           Text="Id : "
+                                           Text="Rfid"
                                            Style="{StaticResource textLabel}" />
                                     <Label
                                         Grid.Column="1"
-                                        Grid.Row="0"
-                                        Text="{Binding VivantId}"
+                                        Text="{Binding NumRfid}"
                                         VerticalOptions="Center" 
                                         HorizontalOptions="Center"
                                         Style="{StaticResource textLabel}"/>
-                                    <Label Grid.Column="0"
-                                           Grid.Row="1"
-                                           Text="Nom : "
-                                           Style="{StaticResource textLabel}"/>
-                                    <Label 
-                                        Grid.Column="1"
-                                        Grid.Row="1"
-                                        Text="{Binding VivantNom}"
-                                        VerticalOptions="Center" 
-                                        HorizontalOptions="Center"/>
                                 </Grid>
                             </StackLayout>
                         </Frame>
@@ -62,8 +46,8 @@
                     VerticalOptions="FillAndExpand">
 
                 <Button 
-                    Text="Edition"
-                    Command="{Binding EditionCommand}"
+                    Text="Scan BLE RFID"
+                    Command="{Binding LectureBLECommand}"
                     Style="{StaticResource btnNormal}"
                     VerticalOptions="End"/>
             </StackLayout>
diff --git a/SICPA_Connexions/View/AnimalView.xaml.cs b/SICPA_Connexions/View/AnimalView.xaml.cs
index 2e870d1..6b9259e 100644
--- a/SICPA_Connexions/View/AnimalView.xaml.cs
+++ b/SICPA_Connexions/View/AnimalView.xaml.cs
@@ -10,5 +10,16 @@ namespace SICPA_Connexions.View
             InitializeComponent();
             this.BindingContext = new AnimalViewModel();
         }
+
+        protected override void OnAppearing()
+        {
+            base.OnAppearing();
+            AnimalViewModel viewModel = this.BindingContext as AnimalViewModel;
+            if (viewModel != null)
+            {
+                viewModel.ActualisePage();
+            }
+        }
+
     }
 }
\ No newline at end of file
diff --git a/SICPA_Connexions/ViewModel/AnimalViewModel.cs b/SICPA_Connexions/ViewModel/AnimalViewModel.cs
index 5b32105..597c739 100644
--- a/SICPA_Connexions/ViewModel/AnimalViewModel.cs
+++ b/SICPA_Connexions/ViewModel/AnimalViewModel.cs
@@ -19,7 +19,7 @@ namespace SICPA_Connexions.ViewModel
         #endregion
 
         #region Commandes
-        public ICommand EditionCommand { get; private set; }
+        public ICommand LectureBLECommand { get; private set; }
         #endregion
 
         #region Propriétés
@@ -55,17 +55,43 @@ namespace SICPA_Connexions.ViewModel
         #region Constructeurs
         public AnimalViewModel()
         {
-            EditionCommand = new Command(async () => await Edition());
+           LectureBLECommand = new Command(async () => await LectureBLE(), CheckDeviceConnected);
         }
         #endregion
 
         #region Méthodes
-        private async Task Edition()
+        private async Task LectureBLE()
         {
             IsBusy = true;
-            await Task.Delay(1000);
+            if (GlobalApplication.GestionnaireBLE.DeviceConnected != null)
+            {
+                await GlobalApplication.GestionnaireBLE.LectureTag(GlobalApplication.GestionnaireBLE.DeviceConnected);
+            }
             IsBusy = false;
         }
+
+        private void HandleRetourBleModifiee(object sender, EventArgs e)
+        {
+            if (GlobalApplication.GestionnaireBLE != null)
+            {
+                NumRfid = GlobalApplication.GestionnaireBLE.RetourBLE;
+            }
+        }
+
+        private bool CheckDeviceConnected()
+        {
+            return (GlobalApplication.GestionnaireBLE != null && GlobalApplication.GestionnaireBLE.DeviceConnected != null);
+        }
+
+        public void ActualisePage()
+        {
+            ((Command)LectureBLECommand).ChangeCanExecute();
+            if (GlobalApplication.GestionnaireBLE != null)
+            {
+                GlobalApplication.GestionnaireBLE.RetourBlEMofifiee += HandleRetourBleModifiee;
+            }
+        }
+
         #endregion
 
     }
diff --git a/SICPA_Connexions/ViewModel/SettingsViewModel.cs b/SICPA_Connexions/ViewModel/SettingsViewModel.cs
index 35092d5..2e1d884 100644
--- a/SICPA_Connexions/ViewModel/SettingsViewModel.cs
+++ b/SICPA_Connexions/ViewModel/SettingsViewModel.cs
@@ -73,7 +73,7 @@ namespace SICPA_Connexions.ViewModel
                 ChangeConnexion(false);
                 NomBLE = "";
             }
-            if (GlobalApplication.GestionnaireBLE != null && GlobalApplication.GestionnaireBLE.DeviceConnected != null)
+            if (GlobalApplication.GestionnaireBLE.DeviceConnected != null)
             {
                 ChangeConnexion(true);
                 NomBLE = GlobalApplication.GestionnaireBLE.DeviceConnected.Name;
@@ -105,7 +105,6 @@ namespace SICPA_Connexions.ViewModel
             ConnexionNOK = !ok;
         }
 
-
         #endregion
     }
 }
