Version 3.0.337

This commit is contained in:
Anja Müller-Meißner 2022-08-30 15:42:25 +02:00
parent fd0e63cf10
commit 573fe77e12
2336 changed files with 33688 additions and 86082 deletions

View file

@ -4,13 +4,14 @@
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>TINK</RootNamespace>
<Configurations>Debug;Release</Configurations>
<ReleaseVersion>3.0</ReleaseVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Plugin.BLE" Version="2.1.2" />
<PackageReference Include="Plugin.BLE" Version="2.1.3" />
<PackageReference Include="Polly" Version="7.2.3" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.2" />
<PackageReference Include="Serilog" Version="2.11.0" />
<PackageReference Include="Xamarin.Essentials" Version="1.7.3" />
</ItemGroup>
<ItemGroup>

View file

@ -1,19 +1,19 @@
using Plugin.BLE.Abstractions;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Plugin.BLE.Abstractions;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.Exceptions;
using Polly;
using Polly.Retry;
using Serilog;
using System;
using System.Threading.Tasks;
using TINK.Services.BluetoothLock.Crypto;
using TINK.Services.BluetoothLock.Tdo;
using System.Threading;
using System.Collections.Generic;
using TINK.Services.BluetoothLock.Exception;
using Xamarin.Essentials;
using TINK.Model.Connector;
using TINK.Model.Device;
using Polly.Retry;
using Polly;
using TINK.Services.BluetoothLock.Crypto;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.Tdo;
using Xamarin.Essentials;
namespace TINK.Services.BluetoothLock.BLE
{
@ -364,14 +364,15 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not get device state. Bluetooth code must be run on main thread");
throw new System.Exception("Can not get device state. Platform must not be unknown and bluetooth code must be run on main thread.");
}
DeviceState? state;
Log.ForContext<LockItBase>().Debug("Request to get connection state.");
try
{
state = GetDeviceState(Device.State);
state = Device?.State.GetDeviceState()
?? throw new System.Exception("Can not get bluetooth device state. State must not be null.");
}
catch (System.Exception exception)
{
@ -404,7 +405,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not reconnect to lock. Bluetooth code must be run on main thread");
throw new System.Exception("Can not reconnect to lock. Platform must not be unknown and bluetooth code must be run on main thread.");
}
// Check if key is available.
@ -477,7 +478,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not authenticate. Bluetooth code must be run on main thread");
throw new System.Exception("Can not authenticate. Platform must not be unknown and bluetooth code must be run on main thread.");
}
if (device == null)
@ -494,7 +495,8 @@ namespace TINK.Services.BluetoothLock.BLE
Log.ForContext<LockItBase>().Debug("Retrieving connection state is in context of auth.");
try
{
deviceState = GetDeviceState(device.State);
deviceState = device?.State.GetDeviceState()
?? throw new System.Exception("Can not get bluetooth device state. State must not be null.");
}
catch (System.Exception exception)
{
@ -659,20 +661,41 @@ namespace TINK.Services.BluetoothLock.BLE
Log.ForContext<LockItBase>().Debug("Encrypted access key written successfully.{Key}{Seed}{AuthCharacteristic}{CommandWritten}", ToSerilogString(crypto.KeyCopri), ToSerilogString(lockInfo.K_seed), ToSerilogString(authCharacteristic), "***");
}
/// <summary> Gets the entire lock state, like locking state (open/ close) and GUID. </summary>
/// <summary> Gets the lock state like locking state (open/ close). </summary>
/// <param name="doWaitRetry">True if to wait and retry in case of failures. </param>
/// <remarks>
/// Lock state is first byte of of value read from state characteristic ("0000baaa-1212-efde-1523-785fef13d123").
/// Values are as follows
/// Open = 0x00,
/// Closed = 0x01,
/// Unknown = 0x02,
/// CouldntCloseMoving = 0x03,
/// CouldntOpenBoldBlocked = 0x04,
/// CouldntCloseBoldBlocked = 0x05
/// TINK.Services.BluetoothLock.Tdo.LockitLockingState.
/// </remarks>
/// <returns> Lock state.</returns>
/// <exception cref="BluetoothDisconnectedException">App is not connected to lock.</exception>
/// <exception cref="CoundntGetCharacteristicException">Getting state characteristic to read from failed.</exception>
/// <exception cref="Exception">
/// Call not from main thread or unkonwn platform detected or
/// query device state (connected, disconnected, ....) failed for an unknown reason or returned an unexpected value or
/// reading state characteristic failed or reading from characteristic was empty.
/// </exception>
/// <exception> Exceptions thrown by PluginBle::ICharacteristic.ReasAsync.</exception>
public async Task<LockInfoTdo> GetLockStateAsync(bool doWaitRetry = false)
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not get lock state. Bluetooth code must be run on main thread");
throw new System.Exception("Can not get lock state. Platform must not be unknown and bluetooth code must be run on main thread.");
}
DeviceState? deviceState;
Log.ForContext<LockItBase>().Debug("Request to get connection state in context of getting locking state.");
try
{
deviceState = GetDeviceState(Device.State);
deviceState = Device?.State.GetDeviceState()
?? throw new System.Exception("Can not get bluetooth device state. State must not be null.");
}
catch (System.Exception exception)
{
@ -725,7 +748,7 @@ namespace TINK.Services.BluetoothLock.BLE
try
{
state = doWaitRetry
state = doWaitRetry
? await _retryPollicy.ExecuteAsync(async () => await readAsyncDelegate())
: await readAsyncDelegate();
}
@ -757,14 +780,15 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not get battery percentage. Bluetooth code must be run on main thread");
throw new System.Exception("Can not get battery percentage. Platform must not be unknown and bluetooth code must be run on main thread.");
}
DeviceState? deviceState;
Log.ForContext<LockItBase>().Debug("Request to get battery percentage in context of getting locking state.");
try
{
deviceState = GetDeviceState(Device.State);
deviceState = Device?.State.GetDeviceState()
?? throw new System.Exception("Can not get bluetooth device state. State must not be null.");
}
catch (System.Exception exception)
{
@ -841,7 +865,8 @@ namespace TINK.Services.BluetoothLock.BLE
Log.ForContext<LockItBase>().Debug("Request to get connection state in context of opening/ closing lock.");
try
{
deviceState = GetDeviceState(Device.State);
deviceState = Device?.State.GetDeviceState()
?? throw new System.Exception("Can not get bluetooth device state. State must not be null.");
}
catch (System.Exception exception)
{
@ -916,7 +941,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not turn alarm off. Bluetooth code must be run on main thread");
throw new System.Exception("Can not turn alarm off. Platform must not be unknown and bluetooth code must be run on main thread.");
}
var alarmCharacteristic = await GetAlarmCharacteristicAsync();
@ -959,7 +984,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not turn alarm off. Bluetooth code must be run on main thread");
throw new System.Exception("Can not turn alarm off. Platform must not be unknown and bluetooth code must be run on main thread.");
}
ICharacteristic alarmCharacteristic = await GetAlarmCharacteristicAsync();
@ -994,7 +1019,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not set sound settings. Bluetooth code must be run on main thread");
throw new System.Exception("Can not set sound settings. Platform must not be unknown and bluetooth code must be run on main thread.");
}
ICharacteristic soundCharacteristic = await GetSoundCharacteristicAsync();
@ -1028,7 +1053,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not set alarm settings. Bluetooth code must be run on main thread");
throw new System.Exception("Can not set alarm settings. Platform must not be unknown and bluetooth code must be run on main thread.");
}
ICharacteristic alarmSettingsCharacteristic = await GetAlarmSettingsCharacteristicAsync();
@ -1067,26 +1092,6 @@ namespace TINK.Services.BluetoothLock.BLE
return data;
}
private static DeviceState GetDeviceState(Plugin.BLE.Abstractions.DeviceState state)
{
switch (state)
{
case Plugin.BLE.Abstractions.DeviceState.Disconnected:
return DeviceState.Disconnected;
case Plugin.BLE.Abstractions.DeviceState.Connecting:
return DeviceState.Connecting;
case Plugin.BLE.Abstractions.DeviceState.Connected:
return DeviceState.Connected;
case Plugin.BLE.Abstractions.DeviceState.Limited:
return DeviceState.Limited;
default:
throw new ArgumentException($"Can not convert state {state}");
}
}
/// <summary> Disconnect from bluetooth lock. </summary>
public async Task Disconnect() => await Adapter.DisconnectDeviceAsync(Device);

View file

@ -1,18 +1,18 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions;
using System;
using System.Collections.Generic;
using System.Linq;
using TINK.Services.BluetoothLock.Tdo;
using TINK.Model.Connector;
using Serilog;
using System.Threading;
using System;
using TINK.Services.BluetoothLock.Exception;
using Xamarin.Essentials;
using TINK.Model.Device;
using System.Threading.Tasks;
using LockItShared.Services.BluetoothLock;
using Plugin.BLE;
using Plugin.BLE.Abstractions;
using Plugin.BLE.Abstractions.Contracts;
using Serilog;
using TINK.Model.Connector;
using TINK.Model.Device;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.Tdo;
using Xamarin.Essentials;
namespace TINK.Services.BluetoothLock.BLE
{
@ -20,7 +20,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
/// <summary> Constructs a LockItByGuidService object.</summary>
/// <param name="cipher">Encrpyting/ decrypting object.</param>
public LockItByGuidService(ICipher cipher) : base(cipher)
public LockItByGuidService(ICipher cipher) : base(cipher)
{
}
@ -32,7 +32,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not connect to locks by guid. Bluetooth code must be run on main thread");
throw new System.Exception("Can not connect to locks by guid. Platform must not be unknown and bluetooth code must be run on main thread.");
}
// Get list of target locks without invalid entries.
@ -84,7 +84,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not connect to lock by guid. Bluetooth code must be run on main thread");
throw new System.Exception("Can not connect to lock by guid. Platform must not be unknown and bluetooth code must be run on main thread.");
}
// Connect to device and authenticate.
@ -124,7 +124,7 @@ namespace TINK.Services.BluetoothLock.BLE
if (LockItByGuidServiceHelper.DevelGuids.ContainsKey(authInfo.Id) && LockItByGuidServiceHelper.DevelGuids[authInfo.Id] != authInfo.Guid)
throw new System.Exception($"Invalid Guid {authInfo.Guid} for lock with id {authInfo.Id} detected. Guid should be {LockItByGuidServiceHelper.DevelGuids[authInfo.Id]}.");
IDevice device;
var cts = new CancellationTokenSource(connectTimeout);
// Step 1: Perform bluetooth connect.
@ -133,7 +133,7 @@ namespace TINK.Services.BluetoothLock.BLE
device = await adapter.ConnectToKnownDeviceAsync(
authInfo.Guid,
new ConnectParameters(forceBleTransport: true), // Force BLE transport
cts.Token);
cts.Token);
}
catch (System.Exception exception)
{

View file

@ -1,17 +1,17 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Plugin.BLE;
using Plugin.BLE.Abstractions.Contracts;
using System.Linq;
using TINK.Services.BluetoothLock.Tdo;
using Serilog;
using Plugin.BLE.Abstractions;
using Plugin.BLE.Abstractions.Contracts;
using Serilog;
using TINK.Model.Connector;
using System.Threading;
using System;
using Xamarin.Essentials;
using TINK.Services.BluetoothLock.Exception;
using TINK.Model.Device;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.Tdo;
using Xamarin.Essentials;
namespace TINK.Services.BluetoothLock.BLE
{
@ -22,19 +22,19 @@ namespace TINK.Services.BluetoothLock.BLE
private IBluetoothLE BluetoothService { get; }
/// <summary> Returns true if location permission is required (Android) but on given. </summary>
private Func<Task<bool>> IsLocationPermissionMissingDelegate {get; }
private Func<Task<bool>> IsLocationPermissionMissingDelegate { get; }
/// <summary> Returns true if location service is required (Android) and off. </summary>
private Func<bool> IsLocationRequiredAndOffDelegate { get; }
private Func<IDevice, LockInfoAuthTdo, IAdapter, Task<LockItBase>> AuthenticateDelegate { get; set; }
private Func<IDevice, LockInfoAuthTdo, IAdapter, Task<LockItBase>> AuthenticateDelegate { get; set; }
public LockItByScanServiceBase(
ICipher cipher,
Func<IDevice, LockInfoAuthTdo, IAdapter, Task<LockItBase>> authenticateDelegate,
IBluetoothLE bluetoothLE,
Func<Task<bool>> isLocationPermissionMissingDelegate,
Func<bool> isLocationRequiredAndOffDelegate) : base(cipher)
Func<bool> isLocationRequiredAndOffDelegate) : base(cipher)
{
BluetoothService = bluetoothLE
?? throw new ArgumentException($"Can not instantiate {nameof(LockItByScanServiceBase)}- object. No bluetooth service available.");
@ -56,7 +56,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not connect to lock by name. Bluetooth code must be run on main thread");
throw new System.Exception("Can not connect to lock by name. Platform must not be unknown and bluetooth code must be run on main thread.");
}
Log.ForContext<LockItByScanServiceBase>().Debug($"Request to connect to device {authInfo.Id}.");
@ -66,6 +66,7 @@ namespace TINK.Services.BluetoothLock.BLE
if (lockIt != null && lockIt.GetDeviceState() == DeviceState.Connected)
{
// Nothing to do
Log.ForContext<LockItByScanServiceBase>().Debug($"Nothing to do. Lock is already connected.");
return await lockIt.GetLockStateAsync();
}
@ -78,6 +79,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
// Device has not yet been discovered.
Log.ForContext<LockItByScanServiceBase>().Verbose("Starting scan for new devices...");
var newlyDiscovertedDevice = await ScanForNewDevices(new List<int> { authInfo.Id }, connectTimeout);
var bleDevice = newlyDiscovertedDevice.FirstOrDefault(x => x.Name.GetBluetoothLockId() == authInfo.Id);
@ -92,22 +94,26 @@ namespace TINK.Services.BluetoothLock.BLE
break;
case BluetoothState.Off:
Log.ForContext<LockItByScanServiceBase>().Debug("Lock probable not found because bluetooth is off.");
throw new ConnectBluetoothNotOnException();
default:
Log.ForContext<LockItByScanServiceBase>().Debug("Lock probable not found because unexpected bluetooth state {state} detected.", bluetoothState);
throw new ConnectBluetoothNotOnException(bluetoothState);
}
if (await IsLocationPermissionMissingDelegate())
{
Log.ForContext<LockItByScanServiceBase>().Debug("Lock probable not found because missing location permission.");
throw new ConnectLocationPermissionMissingException();
}
if (IsLocationRequiredAndOffDelegate())
{
Log.ForContext<LockItByScanServiceBase>().Debug("Lock probable not found because location is off.");
throw new ConnectLocationOffException();
}
Log.ForContext<LockItByScanServiceBase>().Debug("Can not connect because device was not discovered.");
Log.ForContext<LockItByScanServiceBase>().Debug("Can not connect because device was not discovered. List of discovered devices {deviceList}.", String.Join(";", newlyDiscovertedDevice?.Select(x => x?.Id)));
throw new OutOfReachException();
}
@ -125,7 +131,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Can not connect to device by name. {Exception}", exception);
Log.ForContext<LockItByScanServiceBase>().Error("Can not connect to device by name. {Exception}", exception);
if (exception is TaskCanceledException)
{
// A timeout occurred.
@ -179,7 +185,7 @@ namespace TINK.Services.BluetoothLock.BLE
// Connect to newly discovered devices.
foreach (var lockInfo in locksInfoToScanFor)
{
var device = newlyDiscovertedDevicesList.FirstOrDefault(x => x.Name.GetBluetoothLockId() == lockInfo.Id);
var device = newlyDiscovertedDevicesList.FirstOrDefault(x => x.Name.GetBluetoothLockId() == lockInfo.Id);
if (device == null)
{
// No device found for given lock.
@ -200,7 +206,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Can not connect to lock. {Exception}", exception);
Log.ForContext<LockItByScanServiceBase>().Error("Can not connect to lock. {Exception}", exception);
continue;
}
@ -292,7 +298,7 @@ namespace TINK.Services.BluetoothLock.BLE
throw;
}
}
return newlyDiscovertedDevicesList;
}
}

View file

@ -1,6 +1,6 @@
using Plugin.BLE.Abstractions.Contracts;
using System;
using System;
using System.Threading.Tasks;
using Plugin.BLE.Abstractions.Contracts;
using TINK.Model.Device;
namespace TINK.Services.BluetoothLock.BLE
@ -12,6 +12,7 @@ namespace TINK.Services.BluetoothLock.BLE
(bleDevice, authInfo, adapter) => LockItEventBased.Authenticate(bleDevice, authInfo, adapter, cipher),
bluetoothLE,
isLocationPermissionMissingDelegate,
isLocationRequiredAndOffDelegate) { }
isLocationRequiredAndOffDelegate)
{ }
}
}

View file

@ -1,6 +1,6 @@
using Plugin.BLE.Abstractions.Contracts;
using System;
using System;
using System.Threading.Tasks;
using Plugin.BLE.Abstractions.Contracts;
using TINK.Model.Device;
namespace TINK.Services.BluetoothLock.BLE
@ -12,6 +12,7 @@ namespace TINK.Services.BluetoothLock.BLE
(bleDevice, authInfo, adapter) => LockItPolling.Authenticate(bleDevice, authInfo, adapter, cipher),
bluetoothLE,
isLocationPermissionMissingDelegate,
isLocationRequiredAndOffDelegate) { }
isLocationRequiredAndOffDelegate)
{ }
}
}

View file

@ -1,10 +1,10 @@
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.EventArgs;
using Serilog;
using System;
using System;
using System.Threading;
using System.Threading.Tasks;
using TINK.Model.Bike.BluetoothLock;
using Plugin.BLE.Abstractions.Contracts;
using Plugin.BLE.Abstractions.EventArgs;
using Serilog;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
using TINK.Model.Device;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.Tdo;
@ -53,7 +53,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not open lock. Bluetooth code must be run on main thread");
throw new System.Exception("Can not open lock. Platform must not be unknown and bluetooth code must be run on main thread.");
}
LockitLockingState? lockingState = (await GetLockStateAsync())?.State;
@ -88,7 +88,8 @@ namespace TINK.Services.BluetoothLock.BLE
{
lockStateCharacteristic.ValueUpdated += lockStateCharcteristicChanged;
batteryStateCharacteristic.ValueUpdated += batteryStateCharcteristicChanged;
} catch (System.Exception ex)
}
catch (System.Exception ex)
{
Log.ForContext<LockItEventBased>().Error("Subscribing to events when opening lock failed.{Exception}", ex);
return await Task.FromResult((LockitLockingState?)null);
@ -122,13 +123,14 @@ namespace TINK.Services.BluetoothLock.BLE
finally
{
try
{
{
await lockStateCharacteristic.StopUpdatesAsync();
await batteryStateCharacteristic.StopUpdatesAsync();
lockStateCharacteristic.ValueUpdated -= lockStateCharcteristicChanged;
batteryStateCharacteristic.ValueUpdated -= batteryStateCharcteristicChanged;
} catch (System.Exception ex)
}
catch (System.Exception ex)
{
Log.ForContext<LockItEventBased>().Error("Sopping updates/ unsubscribing from events when opening lock failed.{Exception}", ex);
}
@ -168,7 +170,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not close lock. Bluetooth code must be run on main thread");
throw new System.Exception("Can not close lock. Platform must not be unknown and bluetooth code must be run on main thread.");
}
// Get current state
@ -196,8 +198,8 @@ namespace TINK.Services.BluetoothLock.BLE
EventHandler<CharacteristicUpdatedEventArgs> lockStateCharcteristicChanged = (sender, args) => GetStateValue(tcs, args.Characteristic.Value);
try
{
try
{
lockStateCharacteristic.ValueUpdated += lockStateCharcteristicChanged;
}
catch (System.Exception ex)
@ -214,7 +216,7 @@ namespace TINK.Services.BluetoothLock.BLE
Log.ForContext<LockItEventBased>().Error("Starting update when closing lock failed.{Exception}", ex);
return await Task.FromResult((LockitLockingState?)null);
}
try
{
Log.ForContext<LockItEventBased>().Debug($"Request to closed lock. Current state is {lockingState}, counter value {ActivateLockWriteCounter}.");
@ -232,11 +234,12 @@ namespace TINK.Services.BluetoothLock.BLE
finally
{
try
{
{
await lockStateCharacteristic.StopUpdatesAsync();
lockStateCharacteristic.ValueUpdated -= lockStateCharcteristicChanged;
} catch (System.Exception ex)
}
catch (System.Exception ex)
{
Log.ForContext<LockItEventBased>().Error("Sopping update/ unsubscribing from events when closing lock failed.{Exception}", ex);
}

View file

@ -1,9 +1,9 @@
using Plugin.BLE.Abstractions.Contracts;
using Serilog;
using System;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using TINK.Model.Bike.BluetoothLock;
using Plugin.BLE.Abstractions.Contracts;
using Serilog;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
using TINK.Model.Device;
using TINK.Services.BluetoothLock.Exception;
using TINK.Services.BluetoothLock.Tdo;
@ -46,13 +46,12 @@ namespace TINK.Services.BluetoothLock.BLE
cipher,
() => new LockItPolling(device, adapter, cipher));
/// <summary> Opens lock. </summary>
/// <returns> Locking state.</returns>
/// <summary> Opens the lock. </summary>
public async override Task<LockitLockingState?> OpenAsync()
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not open lock. Bluetooth code must be run on main thread");
throw new System.Exception("Can not open lock. Platform must not be unknown and bluetooth code must be run on main thread.");
}
var info = await GetLockStateAsync();
@ -136,7 +135,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not close lock. Bluetooth code must be run on main thread");
throw new System.Exception("Can not close lock. Platform must not be unknown and bluetooth code must be run on main thread.");
}
// Get current state

View file

@ -1,13 +1,13 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using System.Linq;
using TINK.Services.BluetoothLock.Tdo;
using TINK.Model.Connector;
using System;
using System.Threading.Tasks;
using Serilog;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
using TINK.Model.Connector;
using TINK.Model.Device;
using TINK.Services.BluetoothLock.Tdo;
using Xamarin.Essentials;
using TINK.Model.Bike.BluetoothLock;
namespace TINK.Services.BluetoothLock.BLE
{
@ -35,7 +35,7 @@ namespace TINK.Services.BluetoothLock.BLE
/// <param name="connectTimeout">Timeout for connect operation.</param>
public static async Task CheckReconnect(
IEnumerable<ILockService> deviceList,
IEnumerable<LockInfoAuthTdo> locksInfo,
IEnumerable<LockInfoAuthTdo> locksInfo,
TimeSpan connectTimeout)
{
// Get list of target locks without invalid entries.
@ -52,7 +52,7 @@ namespace TINK.Services.BluetoothLock.BLE
continue;
}
var lockInfo = validLocksInfo.FirstOrDefault(x => x.Id == device. Name.GetBluetoothLockId() || x.Guid == device.Guid);
var lockInfo = validLocksInfo.FirstOrDefault(x => x.Id == device.Name.GetBluetoothLockId() || x.Guid == device.Guid);
if (lockInfo == null)
{
@ -83,7 +83,7 @@ namespace TINK.Services.BluetoothLock.BLE
/// <param name="locksInfo">Locks toget info for.</param>
/// <param name="connectTimeout">Timeout for connect operation of a single lock.</param>
public async Task<IEnumerable<LockInfoTdo>> GetLocksStateAsync(
IEnumerable<LockInfoAuthTdo> locksInfo,
IEnumerable<LockInfoAuthTdo> locksInfo,
TimeSpan connectTimeout)
{
if (locksInfo.Count() == 0)
@ -126,7 +126,7 @@ namespace TINK.Services.BluetoothLock.BLE
/// <param name="locksInfo">Locks to reconnect.</param>
/// <param name="connectTimeout">Timeout for connect operation of a single lock.</param>
protected abstract Task<IEnumerable<ILockService>> CheckConnectMissing(
IEnumerable<LockInfoAuthTdo> locksInfo,
IEnumerable<LockInfoAuthTdo> locksInfo,
TimeSpan connectTimeout);
/// <summary>Gets a lock by bike Id.</summary>
@ -154,7 +154,7 @@ namespace TINK.Services.BluetoothLock.BLE
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not disconnect from lock. Bluetooth code must be run on main thread");
throw new System.Exception("Can not disconnect from lock. Platform must not be unknown and bluetooth code must be run on main thread.");
}
Log.ForContext<LockItByScanServiceBase>().Debug($"Request to disconnect from device {bikeId}/ {bikeGuid}.");

View file

@ -0,0 +1,35 @@
using System;
namespace TINK.Services.BluetoothLock.BLE
{
public static class PluginBleHelper
{
/// <summary>
/// Maps Plugin.BLE device state to LockItShared device state.
/// </summary>
/// <param name="state">State to convert</param>
/// <returns></returns>
/// <exception cref="ArgumentException">Unexpected state detected.</exception>
public static DeviceState GetDeviceState(this Plugin.BLE.Abstractions.DeviceState state)
{
switch (state)
{
case Plugin.BLE.Abstractions.DeviceState.Disconnected:
return DeviceState.Disconnected;
case Plugin.BLE.Abstractions.DeviceState.Connecting:
return DeviceState.Connecting;
case Plugin.BLE.Abstractions.DeviceState.Connected:
return DeviceState.Connected;
case Plugin.BLE.Abstractions.DeviceState.Limited:
return DeviceState.Limited;
default:
throw new ArgumentException($"Can not convert state Plugin.BLE-{state}.");
}
}
}
}

View file

@ -1,6 +1,6 @@
using Plugin.BLE.Abstractions.Contracts;
using System;
using System;
using System.Threading.Tasks;
using Plugin.BLE.Abstractions.Contracts;
namespace TINK.Services.BluetoothLock
{