Browse Source

Moved InABox.Avalonia.Platform.Desktop

Kenric Nugteren 4 months ago
parent
commit
b144484f46

+ 79 - 0
InABox.Avalonia.Platform.Desktop/Bluetooth/Desktop_Bluetooth.cs

@@ -0,0 +1,79 @@
+using BluetoothLENet;
+using InABox.Core;
+
+namespace InABox.Avalonia.Platform.Desktop;
+
+public class Desktop_Bluetooth : IBluetooth
+{
+    public Logger? Logger { get; set; }
+
+    private BLE _adapter;
+    
+    public Action<IBluetoothDevice>? DeviceFound { get; set; }
+    public CoreObservableCollection<IBluetoothDevice> Devices { get; } = new();
+
+    public event EventHandler? Changed;
+    
+    public Desktop_Bluetooth()
+    {
+        Devices.CollectionChanged += (_,_) => Changed?.Invoke(this, EventArgs.Empty);
+        
+        _adapter = new BLE();
+        _adapter.Changed += (_,_) =>
+        {
+            var found = _adapter.Devices.Select(x => x.MacAddress).ToArray();
+            var cache = Devices.OfType<Desktop_BluetoothDevice>().Select(x => x.ID).ToArray();
+            if (found.Except(cache).Any() || cache.Except(found).Any())
+            {
+                var devices = _adapter.Devices
+                    .ToArray()
+                    .Select(x => new Desktop_BluetoothDevice(x));
+                Devices.ReplaceRange(devices);
+            }
+        };
+    }
+
+    public async Task<bool> IsAvailable()
+    {
+        return await Task.FromResult(true);   
+    }
+    
+    public async Task<bool> StartScanningAsync(Guid configServiceId)
+    {
+        if (await IsAvailable())
+            return await _adapter.StartScanningAsync(configServiceId);
+        return false;
+    }
+
+    public async Task<bool> StopScanningAsync()
+    {
+        if (await IsAvailable())
+            return await _adapter.StopScanningAsync();
+        return false;
+    }
+
+    public async Task<IConnectedBluetoothDevice?> Connect(IBluetoothDevice device)
+    {
+        if (await IsAvailable())
+        {
+            if (device is Desktop_BluetoothDevice d)
+            {
+                var result = await _adapter.Connect(d.Device);
+                if (result == ConnectDeviceResult.Ok)
+                    return new Desktop_ConnectedBluetoothDevice(d.Device);
+            }
+        }
+        return null;
+        
+    }
+
+    public async Task<bool> Disconnect(IConnectedBluetoothDevice device)
+    {
+        if (await IsAvailable())
+        {
+            if (device is Desktop_BluetoothDevice d)
+                _adapter.Disconnect(d.Device);
+        }
+        return true;
+    }
+}

+ 12 - 0
InABox.Avalonia.Platform.Desktop/Bluetooth/Desktop_BluetoothDevice.cs

@@ -0,0 +1,12 @@
+using BluetoothLENet;
+
+namespace InABox.Avalonia.Platform.Desktop;
+
+public class Desktop_BluetoothDevice(BLEDevice device) : IBluetoothDevice
+{
+    public BLEDevice? Device { get; } = device;
+    public string ID => Device?.MacAddress ?? string.Empty;
+    public string Name => Device?.Native?.Name ?? "Unknown Device";
+    public Guid[] AvailableServices  => Device?.AvailableServices ?? [];
+    public DateTime LastSeen { get; set; } = device.LastSeen;
+}

+ 54 - 0
InABox.Avalonia.Platform.Desktop/Bluetooth/Desktop_ConnectedBluetoothDevice.cs

@@ -0,0 +1,54 @@
+using System.Text;
+using BluetoothLENet;
+
+namespace InABox.Avalonia.Platform.Desktop;
+
+public class Desktop_ConnectedBluetoothDevice(BLEDevice device)
+    : Desktop_BluetoothDevice(device), IConnectedBluetoothDevice
+{
+    public async Task<bool> WriteBytesAsync(Guid serviceid, Guid characteristicid, byte[] data)
+    {
+        var service = Device.Services.FirstOrDefault(x=>x.Native.Uuid == serviceid);
+        if (service != null)
+        {
+            var characteristic = service.Characteristics.FirstOrDefault(x=>x.Native.Uuid == characteristicid);
+            if (characteristic != null)
+                return await characteristic.WriteAsync(data);
+        }
+        return false;
+    }
+
+    public async Task<bool> WriteStringAsync(Guid serviceid, Guid characteristicid, string data)
+    {
+        try
+        {
+            var encoded = Encoding.UTF8.GetBytes(data);
+            var result = await WriteBytesAsync(serviceid, characteristicid, encoded);
+            return result;
+        }
+        catch (Exception e)
+        {
+            Console.WriteLine(e);
+            return false;
+        }
+       
+    }
+
+    public async Task<byte[]?> ReadBytesAsync(Guid serviceid, Guid characteristicid)
+    {
+        var service = Device.Services.FirstOrDefault(x=>x.Native.Uuid == serviceid);
+        if (service != null)
+        {
+            var characteristic = service.Characteristics.FirstOrDefault(x=>x.Native.Uuid == characteristicid);
+            if (characteristic != null)
+                return await characteristic.ReadAsync();
+        }
+        return [];
+    }
+    
+    public async Task<String?> ReadStringAsync(Guid serviceid, Guid characteristicid)
+    {
+        var data = await ReadBytesAsync(serviceid,characteristicid);
+        return data != null ? System.Text.Encoding.UTF8.GetString(data) : null;
+    }
+}

+ 24 - 0
InABox.Avalonia.Platform.Desktop/Desktop.PdfRenderer.cs

@@ -0,0 +1,24 @@
+using InABox.Core;
+using PDFtoImage;
+using SkiaSharp;
+
+namespace InABox.Avalonia.Platform.Desktop;
+
+public class Desktop_PdfRenderer : IPdfRenderer
+{
+    public byte[]? RenderPdf(byte[]? pdf, int page, int dpi)
+    {
+        if (pdf?.Any() != true)
+            return null;
+        
+        var result = Conversion.ToImage(pdf, page, options: new RenderOptions(Dpi: dpi));
+        using var ms = new MemoryStream();
+        result.Encode(ms, SKEncodedImageFormat.Jpeg, 65);
+        return ms.ToArray();
+    }
+
+    public Task<byte[]?> RenderPdfAsync(byte[]? pdf, int page, int dpi)
+        => Task.Run(() => RenderPdf(pdf, page, dpi));
+
+    public Logger? Logger { get; set; }
+}

+ 20 - 0
InABox.Avalonia.Platform.Desktop/InABox.Avalonia.Platform.Desktop.csproj

@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+    <PropertyGroup>
+        <TargetFramework>net8.0-windows10.0.19041.0</TargetFramework>
+        <ImplicitUsings>enable</ImplicitUsings>
+        <Nullable>enable</Nullable>
+        <RootNamespace>InABox.Avalonia.Platform.Desktop2</RootNamespace>
+    </PropertyGroup>
+
+    <ItemGroup>
+      <ProjectReference Include="..\..\3rdpartylibs\BluetoothLENet-master\BluetoothLeNet\BluetoothLeNet.csproj" />
+      <ProjectReference Include="..\InABox.Avalonia.Platform\InABox.Avalonia.Platform.csproj" />
+    </ItemGroup>
+
+    <ItemGroup>
+      <PackageReference Include="PDFtoImage" />
+      <PackageReference Include="SkiaSharp" />
+    </ItemGroup>
+
+</Project>

+ 2 - 2
InABox.Avalonia/Dialogs/MobileDialog.cs

@@ -10,7 +10,7 @@ public static class MobileDialog
             Message = message,
             Message = message,
             ButtonText = "OK"
             ButtonText = "OK"
         };
         };
-       var result = dialog.ShowAsync();
-       result.Wait();
+        var result = dialog.ShowAsync();
+        result.Wait();
     }
     }
 }
 }