mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-04-21 20:46:28 +02:00
Version 3.0.338
This commit is contained in:
parent
573fe77e12
commit
0468955d49
751 changed files with 62747 additions and 60672 deletions
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Plugin.BLE.Abstractions.Contracts;
|
||||
|
@ -12,304 +12,312 @@ using Xamarin.Essentials;
|
|||
|
||||
namespace TINK.Services.BluetoothLock.BLE
|
||||
{
|
||||
public class LockItEventBased : LockItBase
|
||||
{
|
||||
private LockItEventBased(IDevice device, IAdapter adapter, ICipher cipher) : base(device, adapter, cipher)
|
||||
{
|
||||
}
|
||||
public class LockItEventBased : LockItBase
|
||||
{
|
||||
private LockItEventBased(IDevice device, IAdapter adapter, ICipher cipher) : base(device, adapter, cipher)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary> Reconnects to device. </summary>
|
||||
/// <remarks> Consists of a bluetooth connect plus invocation of an authentication sequence. </remarks>
|
||||
/// <param name="authInfo">Info required to connect.</param>
|
||||
/// <param name="connectTimeout">Timeout to apply when connecting to bluetooth lock.</param>
|
||||
/// <returns>True if connecting succeeded, false if not.</returns>
|
||||
public async override Task ReconnectAsync(
|
||||
LockInfoAuthTdo authInfo,
|
||||
TimeSpan connectTimeout)
|
||||
=> await ReconnectAsync(
|
||||
authInfo,
|
||||
connectTimeout,
|
||||
() => new LockItEventBased(Device, Adapter, Cipher));
|
||||
/// <summary> Reconnects to device. </summary>
|
||||
/// <remarks> Consists of a bluetooth connect plus invocation of an authentication sequence. </remarks>
|
||||
/// <param name="authInfo">Info required to connect.</param>
|
||||
/// <param name="connectTimeout">Timeout to apply when connecting to bluetooth lock.</param>
|
||||
/// <returns>True if connecting succeeded, false if not.</returns>
|
||||
public async override Task ReconnectAsync(
|
||||
LockInfoAuthTdo authInfo,
|
||||
TimeSpan connectTimeout)
|
||||
=> await ReconnectAsync(
|
||||
authInfo,
|
||||
connectTimeout,
|
||||
() => new LockItEventBased(Device, Adapter, Cipher));
|
||||
|
||||
/// <summary> Connects to device. </summary>
|
||||
/// <param name="authInfo">Info required to connect.</param>
|
||||
/// <param name="device">Device with must be connected.</param>
|
||||
/// <returns>True if connecting succeeded, false if not.</returns>
|
||||
public static async Task<LockItBase> Authenticate(
|
||||
IDevice device,
|
||||
LockInfoAuthTdo authInfo,
|
||||
IAdapter adapter,
|
||||
ICipher cipher)
|
||||
=> await Authenticate(
|
||||
device,
|
||||
authInfo,
|
||||
adapter,
|
||||
cipher,
|
||||
() => new LockItEventBased(device, adapter, cipher));
|
||||
/// <summary> Connects to device. </summary>
|
||||
/// <param name="authInfo">Info required to connect.</param>
|
||||
/// <param name="device">Device with must be connected.</param>
|
||||
/// <returns>True if connecting succeeded, false if not.</returns>
|
||||
public static async Task<LockItBase> Authenticate(
|
||||
IDevice device,
|
||||
LockInfoAuthTdo authInfo,
|
||||
IAdapter adapter,
|
||||
ICipher cipher)
|
||||
=> await Authenticate(
|
||||
device,
|
||||
authInfo,
|
||||
adapter,
|
||||
cipher,
|
||||
() => new LockItEventBased(device, adapter, cipher));
|
||||
|
||||
/// <summary> Opens lock. </summary>
|
||||
/// <returns> Locking state.</returns>
|
||||
public async override Task<LockitLockingState?> OpenAsync()
|
||||
{
|
||||
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
|
||||
{
|
||||
throw new System.Exception("Can not open lock. Platform must not be unknown and bluetooth code must be run on main thread.");
|
||||
}
|
||||
/// <summary> Opens lock. </summary>
|
||||
/// <returns> Locking state.</returns>
|
||||
public async override Task<LockitLockingState?> OpenAsync()
|
||||
{
|
||||
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
|
||||
{
|
||||
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;
|
||||
if (lockingState == null)
|
||||
{
|
||||
// Device not reachable.
|
||||
Log.ForContext<LockItEventBased>().Information("Can not open lock. Device is not reachable (get state).");
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
LockitLockingState? lockingState = (await GetLockStateAsync())?.State;
|
||||
if (lockingState == null)
|
||||
{
|
||||
// Device not reachable.
|
||||
Log.ForContext<LockItEventBased>().Information("Can not open lock. Device is not reachable (get state).");
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
|
||||
if (lockingState.Value.GetLockingState() == LockingState.Open)
|
||||
{
|
||||
// Lock is already open.
|
||||
Log.ForContext<LockItEventBased>().Information("No need to open lock. Lock is already open.");
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
if (lockingState.Value.GetLockingState() == LockingState.Open)
|
||||
{
|
||||
// Lock is already open.
|
||||
Log.ForContext<LockItEventBased>().Information("No need to open lock. Lock is already open.");
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
|
||||
Log.ForContext<LockItEventBased>().Debug($"Request to closed lock. Current lockikng state is {lockingState}, counter value {ActivateLockWriteCounter}.");
|
||||
Log.ForContext<LockItEventBased>().Debug($"Request to open lock. Current locking state is {lockingState}, counter value {ActivateLockWriteCounter}.");
|
||||
|
||||
double batteryPercentage = double.NaN;
|
||||
double batteryPercentage = double.NaN;
|
||||
|
||||
var lockStateCharacteristic = await GetStateCharacteristicAsync();
|
||||
var batteryStateCharacteristic = await GetBatteryCharacteristicAsync();
|
||||
var lockStateCharacteristic = await GetStateCharacteristicAsync();
|
||||
var batteryStateCharacteristic = await GetBatteryCharacteristicAsync();
|
||||
|
||||
TaskCompletionSource<LockitLockingState?> tcs = new TaskCompletionSource<LockitLockingState?>();
|
||||
var cts = new CancellationTokenSource(OPEN_CLOSE_TIMEOUT_MS);
|
||||
cts.Token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false);
|
||||
TaskCompletionSource<LockitLockingState?> tcs = new TaskCompletionSource<LockitLockingState?>();
|
||||
var cts = new CancellationTokenSource(OPEN_CLOSE_TIMEOUT_MS);
|
||||
cts.Token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false);
|
||||
|
||||
EventHandler<CharacteristicUpdatedEventArgs> lockStateCharcteristicChanged = (sender, args) => GetStateValue(tcs, args.Characteristic.Value);
|
||||
EventHandler<CharacteristicUpdatedEventArgs> batteryStateCharcteristicChanged = (sender, args) => batteryPercentage = GetChargeValue(args.Characteristic.Value);
|
||||
try
|
||||
{
|
||||
lockStateCharacteristic.ValueUpdated += lockStateCharcteristicChanged;
|
||||
batteryStateCharacteristic.ValueUpdated += batteryStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Subscribing to events when opening lock failed.{Exception}", ex);
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
EventHandler<CharacteristicUpdatedEventArgs> lockStateCharcteristicChanged = (sender, args) => GetStateValue(tcs, args.Characteristic.Value);
|
||||
EventHandler<CharacteristicUpdatedEventArgs> batteryStateCharcteristicChanged = (sender, args) => batteryPercentage = GetChargeValue(args.Characteristic.Value);
|
||||
try
|
||||
{
|
||||
lockStateCharacteristic.ValueUpdated += lockStateCharcteristicChanged;
|
||||
batteryStateCharacteristic.ValueUpdated += batteryStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Subscribing to events when opening lock failed.{Exception}", ex);
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StartUpdatesAsync();
|
||||
await batteryStateCharacteristic.StartUpdatesAsync();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Starting updates wen opening lock failed.{Exception}", ex);
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StartUpdatesAsync();
|
||||
await batteryStateCharacteristic.StartUpdatesAsync();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Starting updates when opening lock failed.{Exception}", ex);
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var result = await OpenCloseLock(true); // Close lock;
|
||||
try
|
||||
{
|
||||
var result = await OpenCloseLockAsync(true); // Close lock;
|
||||
|
||||
if (!result)
|
||||
{
|
||||
// State did not change. Return previous state.
|
||||
Log.ForContext<LockItEventBased>().Information($"Opening lock failed.");
|
||||
return await Task.FromResult(lockingState.Value);
|
||||
}
|
||||
if (!result)
|
||||
{
|
||||
// State did not change. Return previous state.
|
||||
Log.ForContext<LockItEventBased>().Information($"Opening lock failed.");
|
||||
return await Task.FromResult(lockingState.Value);
|
||||
}
|
||||
|
||||
// Wait until event has been received.
|
||||
lockingState = await tcs.Task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StopUpdatesAsync();
|
||||
await batteryStateCharacteristic.StopUpdatesAsync();
|
||||
// Wait until event has been received.
|
||||
lockingState = await tcs.Task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StopUpdatesAsync();
|
||||
await batteryStateCharacteristic.StopUpdatesAsync();
|
||||
|
||||
lockStateCharacteristic.ValueUpdated -= lockStateCharcteristicChanged;
|
||||
batteryStateCharacteristic.ValueUpdated -= batteryStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Sopping updates/ unsubscribing from events when opening lock failed.{Exception}", ex);
|
||||
}
|
||||
}
|
||||
lockStateCharacteristic.ValueUpdated -= lockStateCharcteristicChanged;
|
||||
batteryStateCharacteristic.ValueUpdated -= batteryStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Sopping updates/ unsubscribing from events when opening lock failed.{Exception}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (lockingState == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (lockingState == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (lockingState.Value)
|
||||
{
|
||||
case LockitLockingState.Open:
|
||||
return lockingState.Value;
|
||||
switch (lockingState.Value)
|
||||
{
|
||||
case LockitLockingState.Open:
|
||||
Log.ForContext<LockItEventBased>().Information($"Lock was opened successfully.");
|
||||
return lockingState.Value;
|
||||
|
||||
case LockitLockingState.CouldntOpenBoldBlocked:
|
||||
// Expected error. ILockIt count not be opened (Spoke blocks lock, ....)
|
||||
throw new CouldntOpenBoldIsBlockedException();
|
||||
case LockitLockingState.CouldntOpenBoldBlocked:
|
||||
// Expected error. ILockIt count not be opened (Spoke blocks lock, ....)
|
||||
Log.ForContext<LockItEventBased>().Debug($"Opening lock failed. Bold is blocked.");
|
||||
throw new CouldntOpenBoldIsBlockedException();
|
||||
|
||||
case LockitLockingState.Unknown:
|
||||
// Expected error. ILockIt count not be opened (Spoke has blocked/ blocks lock, ....)
|
||||
throw new CouldntOpenBoldWasBlockedException();
|
||||
case LockitLockingState.Unknown:
|
||||
// Expected error. ILockIt count not be opened (Spoke has blocked/ blocks lock, ....)
|
||||
Log.ForContext<LockItEventBased>().Debug($"Opening lock failed. Bold was blocked.");
|
||||
throw new CouldntOpenBoldWasBlockedException();
|
||||
|
||||
default:
|
||||
// Comprises values
|
||||
// - LockitLockingState.Closed
|
||||
// - LockitLockingState.Unknown
|
||||
// - LockitLockingState.CouldntOpenBoldBlocked
|
||||
// Internal error which sould never occure. Lock refuses to open but connection is ok.
|
||||
throw new CouldntOpenInconsistentStateExecption(lockingState.Value.GetLockingState());
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Comprises values
|
||||
// - LockitLockingState.Closed
|
||||
// - LockitLockingState.Unknown
|
||||
// - LockitLockingState.CouldntOpenBoldBlocked
|
||||
// Internal error which sould never occure. Lock refuses to open but connection is ok.
|
||||
Log.ForContext<LockItEventBased>().Debug($"Opening lock failed. Unexpected lock state {lockingState.Value.GetLockingState()} detected.");
|
||||
throw new CouldntOpenInconsistentStateExecption(lockingState.Value.GetLockingState());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Close the lock.</summary>
|
||||
/// <returns>Locking state.</returns>
|
||||
public async override Task<LockitLockingState?> CloseAsync()
|
||||
{
|
||||
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
|
||||
{
|
||||
throw new System.Exception("Can not close lock. Platform must not be unknown and bluetooth code must be run on main thread.");
|
||||
}
|
||||
/// <summary> Close the lock.</summary>
|
||||
/// <returns>Locking state.</returns>
|
||||
public async override Task<LockitLockingState?> CloseAsync()
|
||||
{
|
||||
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
|
||||
{
|
||||
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
|
||||
LockitLockingState? lockingState = (await GetLockStateAsync()).State;
|
||||
if (lockingState == null)
|
||||
{
|
||||
// Device not reachable.
|
||||
Log.ForContext<LockItEventBased>().Error("Can not close lock. Device is not reachable (get state).");
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
// Get current state
|
||||
LockitLockingState? lockingState = (await GetLockStateAsync()).State;
|
||||
if (lockingState == null)
|
||||
{
|
||||
// Device not reachable.
|
||||
Log.ForContext<LockItEventBased>().Error("Can not close lock. Device is not reachable (get state).");
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
|
||||
if (lockingState.Value.GetLockingState() == LockingState.Closed)
|
||||
{
|
||||
// Lock is already closed.
|
||||
Log.ForContext<LockItEventBased>().Error("No need to close lock. Lock is already closed.");
|
||||
return await Task.FromResult(lockingState.Value);
|
||||
}
|
||||
if (lockingState.Value.GetLockingState() == LockingState.Closed)
|
||||
{
|
||||
// Lock is already closed.
|
||||
Log.ForContext<LockItEventBased>().Error("No need to close lock. Lock is already closed.");
|
||||
return await Task.FromResult(lockingState.Value);
|
||||
}
|
||||
|
||||
lockingState = null;
|
||||
var lockStateCharacteristic = await GetStateCharacteristicAsync();
|
||||
lockingState = null;
|
||||
var lockStateCharacteristic = await GetStateCharacteristicAsync();
|
||||
|
||||
TaskCompletionSource<LockitLockingState?> tcs = new TaskCompletionSource<LockitLockingState?>();
|
||||
var cts = new CancellationTokenSource(OPEN_CLOSE_TIMEOUT_MS);
|
||||
cts.Token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false);
|
||||
TaskCompletionSource<LockitLockingState?> tcs = new TaskCompletionSource<LockitLockingState?>();
|
||||
var cts = new CancellationTokenSource(OPEN_CLOSE_TIMEOUT_MS);
|
||||
cts.Token.Register(() => tcs.TrySetCanceled(), useSynchronizationContext: false);
|
||||
|
||||
EventHandler<CharacteristicUpdatedEventArgs> lockStateCharcteristicChanged = (sender, args) => GetStateValue(tcs, args.Characteristic.Value);
|
||||
EventHandler<CharacteristicUpdatedEventArgs> lockStateCharcteristicChanged = (sender, args) => GetStateValue(tcs, args.Characteristic.Value);
|
||||
|
||||
try
|
||||
{
|
||||
lockStateCharacteristic.ValueUpdated += lockStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Subscribing to events when closing lock failed.{Exception}", ex);
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StartUpdatesAsync();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Starting update when closing lock failed.{Exception}", ex);
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
try
|
||||
{
|
||||
lockStateCharacteristic.ValueUpdated += lockStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Subscribing to events when closing lock failed.{Exception}", ex);
|
||||
return await Task.FromResult((LockitLockingState?)null);
|
||||
}
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StartUpdatesAsync();
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
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}.");
|
||||
var result = await OpenCloseLock(false); // Close lock
|
||||
if (!result)
|
||||
{
|
||||
// State did not change. Return previous state.
|
||||
Log.ForContext<LockItEventBased>().Information($"Closing lock failed.");
|
||||
return await Task.FromResult(lockingState.Value);
|
||||
}
|
||||
try
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Debug($"Request to close lock. Current state is {lockingState}, counter value {ActivateLockWriteCounter}.");
|
||||
var result = await OpenCloseLockAsync(false); // Close lock
|
||||
if (!result)
|
||||
{
|
||||
// State did not change. Return previous state.
|
||||
Log.ForContext<LockItEventBased>().Information($"Closing lock failed.");
|
||||
return await Task.FromResult(lockingState.Value);
|
||||
}
|
||||
|
||||
// Wait until event has been received.
|
||||
lockingState = await tcs.Task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StopUpdatesAsync();
|
||||
// Wait until event has been received.
|
||||
lockingState = await tcs.Task;
|
||||
}
|
||||
finally
|
||||
{
|
||||
try
|
||||
{
|
||||
await lockStateCharacteristic.StopUpdatesAsync();
|
||||
|
||||
lockStateCharacteristic.ValueUpdated -= lockStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Sopping update/ unsubscribing from events when closing lock failed.{Exception}", ex);
|
||||
}
|
||||
}
|
||||
lockStateCharacteristic.ValueUpdated -= lockStateCharcteristicChanged;
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Sopping update/ unsubscribing from events when closing lock failed.{Exception}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
if (lockingState == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (lockingState == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (lockingState.Value)
|
||||
{
|
||||
case LockitLockingState.CouldntCloseBoldBlocked:
|
||||
// Expected error. ILockIt could not be closed (Spoke blocks lock, ....)
|
||||
throw new CouldntCloseBoldBlockedException();
|
||||
switch (lockingState.Value)
|
||||
{
|
||||
case LockitLockingState.CouldntCloseBoldBlocked:
|
||||
// Expected error. ILockIt could not be closed (Spoke blocks lock, ....)
|
||||
Log.ForContext<LockItEventBased>().Debug($"Closing lock failed. Bold is blocked.");
|
||||
throw new CouldntCloseBoldBlockedException();
|
||||
|
||||
case LockitLockingState.CouldntCloseMoving:
|
||||
// Expected error. ILockIt could not be closed (bike is moving)
|
||||
throw new CouldntCloseMovingException();
|
||||
case LockitLockingState.CouldntCloseMoving:
|
||||
// Expected error. ILockIt could not be closed (bike is moving)
|
||||
Log.ForContext<LockItEventBased>().Debug($"Closing lock failed. Bike is moving.");
|
||||
throw new CouldntCloseMovingException();
|
||||
|
||||
case LockitLockingState.Closed:
|
||||
return lockingState;
|
||||
case LockitLockingState.Closed:
|
||||
Log.ForContext<LockItEventBased>().Information($"Lock was closed successfully.");
|
||||
return lockingState;
|
||||
|
||||
default:
|
||||
// Comprises values
|
||||
// - LockitLockingState.Open
|
||||
// - LockitLockingState.Unknown
|
||||
// - LockitLockingState.CouldntOpenBoldBlocked
|
||||
// Internal error which sould never occurre. Lock refuses to close but connection is ok.
|
||||
throw new CouldntCloseInconsistentStateExecption(lockingState.Value.GetLockingState());
|
||||
}
|
||||
}
|
||||
default:
|
||||
// Comprises values
|
||||
// - LockitLockingState.Open
|
||||
// - LockitLockingState.Unknown
|
||||
// - LockitLockingState.CouldntOpenBoldBlocked
|
||||
// Internal error which sould never occurre. Lock refuses to close but connection is ok.
|
||||
Log.ForContext<LockItEventBased>().Debug($"Opening lock failed. Unexpected lock state {lockingState.Value.GetLockingState()} detected.");
|
||||
throw new CouldntCloseInconsistentStateExecption(lockingState.Value.GetLockingState());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary> Gets the locking state from event argument. </summary>
|
||||
Action<TaskCompletionSource<LockitLockingState?>, byte[]> GetStateValue = (tcs, value) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (value?.Length <= 0)
|
||||
{
|
||||
tcs.TrySetResult(null);
|
||||
return;
|
||||
}
|
||||
/// <summary> Gets the locking state from event argument. </summary>
|
||||
Action<TaskCompletionSource<LockitLockingState?>, byte[]> GetStateValue = (tcs, value) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (value?.Length <= 0)
|
||||
{
|
||||
tcs.TrySetResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
tcs.TrySetResult((LockitLockingState?)value[0]);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Error on sinking lock state characteristic on opening/closing .{Exception}", ex);
|
||||
tcs.TrySetResult(null);
|
||||
}
|
||||
};
|
||||
tcs.TrySetResult((LockitLockingState?)value[0]);
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Error on sinking lock state characteristic on opening/closing .{Exception}", ex);
|
||||
tcs.TrySetResult(null);
|
||||
}
|
||||
};
|
||||
|
||||
/// <summary> Gets the battery state from lock.</summary>
|
||||
Func<byte[], double> GetChargeValue = (value) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (value.Length <= 0)
|
||||
{
|
||||
return double.NaN;
|
||||
}
|
||||
/// <summary> Gets the battery state from lock.</summary>
|
||||
Func<byte[], double> GetChargeValue = (value) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (value.Length <= 0)
|
||||
{
|
||||
return double.NaN;
|
||||
}
|
||||
|
||||
return value[0];
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Error on sinking battery state characteristic on opening.{Exception}", ex);
|
||||
return double.NaN;
|
||||
}
|
||||
};
|
||||
}
|
||||
return value[0];
|
||||
}
|
||||
catch (System.Exception ex)
|
||||
{
|
||||
Log.ForContext<LockItEventBased>().Error("Error on sinking battery state characteristic on opening.{Exception}", ex);
|
||||
return double.NaN;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue