Version 3.0.370

This commit is contained in:
Anja 2023-08-31 12:20:06 +02:00
parent f5cf9bb22f
commit bdb2dec1c1
233 changed files with 10252 additions and 6779 deletions

View file

@ -20,7 +20,7 @@ namespace TINK.Services.BluetoothLock.BLE
/// <summary> Manages a single lock.</summary>
public abstract class LockItBase : ILockService
{
/// <summary> Lenght of seed in bytes.</summary>
/// <summary> Length of seed in bytes.</summary>
private const int SEEDLENGTH = 16;
/// <summary> Timeout for open/ close operations.</summary>
@ -160,7 +160,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting activate lock charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting activate lock characteristic failed. {Exception}", exception);
throw new System.Exception($"Can not get activate characteristic. {exception.Message}", exception);
}
@ -184,7 +184,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting alarm-charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting alarm-characteristic failed. {Exception}", exception);
throw new System.Exception($"Can not get alarm characteristic. {exception.Message}", exception);
}
@ -207,7 +207,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting alarm settings charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting alarm settings characteristic failed. {Exception}", exception);
throw new System.Exception($"Can not get alarm settings characteristic. {exception.Message}", exception);
}
@ -229,7 +229,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting auth-charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting auth-characteristic failed. {Exception}", exception);
throw new System.Exception(string.Format("Can not get auth characteristic. {0}", exception.Message), exception);
}
@ -251,7 +251,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting state charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting state characteristic failed. {Exception}", exception);
throw new System.Exception(string.Format("Can not get state characteristic. {0}", exception.Message), exception);
}
@ -273,7 +273,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting sound charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting sound characteristic failed. {Exception}", exception);
throw new System.Exception($"Can not get sound characteristic. {exception.Message}", exception);
}
@ -295,7 +295,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting battery charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting battery characteristic failed. {Exception}", exception);
throw new System.Exception($"Can not get battery characteristic. {exception.Message}", exception);
}
@ -317,7 +317,7 @@ namespace TINK.Services.BluetoothLock.BLE
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting versions info charcteristic failed. {Exception}", exception);
Log.ForContext<LockItBase>().Error("Getting versions info characteristic failed. {Exception}", exception);
throw new System.Exception(string.Format("Can not get versions info characteristic. {0}", exception.Message), exception);
}
@ -349,10 +349,10 @@ namespace TINK.Services.BluetoothLock.BLE
return;
}
/// <summary> Full idvertisement name.</summary>
/// <summary> Full advertisement name.</summary>
public string Name { get; private set; } = string.Empty;
/// <summary> Id part of idvertisement name.</summary>
/// <summary> Id part of advertisement name.</summary>
public int Id { get; private set; }
/// <summary> Query GUID of lock.</summary>
@ -603,12 +603,12 @@ namespace TINK.Services.BluetoothLock.BLE
LockInfoAuthTdo lockInfo,
ICipher cipher)
{
Log.ForContext<LockItBase>().Debug($"Request to autenticate for {lockIt.Name}.");
Log.ForContext<LockItBase>().Debug($"Request to authenticate for {lockIt.Name}.");
var authCharacteristic = await lockIt.GetAuthCharacteristicAsync();
if (authCharacteristic == null)
{
Log.ForContext<LockItBase>().Debug("Getting auth-charcteristic failed.");
Log.ForContext<LockItBase>().Debug("Getting auth-characteristic failed.");
throw new CoundntGetCharacteristicException("Authentication failed. Auth characteristic must not be null.");
}
@ -687,14 +687,14 @@ namespace TINK.Services.BluetoothLock.BLE
/// <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").
/// Lock state is first byte 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
/// CouldntOpenBoltBlocked = 0x04,
/// CouldntCloseBoltBlocked = 0x05
/// TINK.Services.BluetoothLock.Tdo.LockitLockingState.
/// </remarks>
/// <returns> Lock state.</returns>
@ -750,6 +750,8 @@ namespace TINK.Services.BluetoothLock.BLE
}
byte[] state;
// Reads the lock state from characteristic
async Task<byte[]> readAsyncDelegate()
{
var cts = new CancellationTokenSource();
@ -786,11 +788,12 @@ namespace TINK.Services.BluetoothLock.BLE
throw new System.Exception("Can not get lock state. No data read");
}
int lockingState = state[0];
var lockInfoTdo = new LockInfoTdo.Builder
{
Id = Id,
Guid = Guid,
State = (LockitLockingState?)state[0]
State = Enum.IsDefined(typeof(LockitLockingState), lockingState) ? (LockitLockingState?) lockingState : null
}.Build();
Log.ForContext<LockItBase>().Debug("Retrieving lock state (ReadAsync-call) succeeded. {@LockInfoTdo}{StateCharacteristic}{Reading}",
@ -1251,6 +1254,6 @@ namespace TINK.Services.BluetoothLock.BLE
=> charcteristic.Id.ToString();
private static string ToSerilogString(byte[] byteArray)
=> "***"; // For debugging purposes it might be required to return string.Join(",", byteArray); Do not log any confidental value in production context.
=> "***"; // For debugging purposes it might be required to return string.Join(",", byteArray); Do not log any confidential value in production context.
}
}

View file

@ -147,7 +147,7 @@ namespace TINK.Services.BluetoothLock.BLE
Log.ForContext<LockItEventBased>().Information($"Lock was opened successfully.");
return lockingState.Value;
case LockitLockingState.CouldntOpenBoldBlocked:
case LockitLockingState.CouldntOpenBoltBlocked:
// Expected error. ILockIt count not be opened (Spoke blocks lock, ....)
Log.ForContext<LockItEventBased>().Debug($"Opening lock failed. Bold is blocked.");
throw new CouldntOpenBoldIsBlockedException();
@ -161,7 +161,7 @@ namespace TINK.Services.BluetoothLock.BLE
// Comprises values
// - LockitLockingState.Closed
// - LockitLockingState.Unknown
// - LockitLockingState.CouldntOpenBoldBlocked
// - LockitLockingState.CouldntOpenBoltBlocked
// Internal error which should never occur. 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());
@ -256,10 +256,10 @@ namespace TINK.Services.BluetoothLock.BLE
switch (lockingState.Value)
{
case LockitLockingState.CouldntCloseBoldBlocked:
case LockitLockingState.CouldntCloseBoltBlocked:
// Expected error. ILockIt could not be closed (Spoke blocks lock, ....)
Log.ForContext<LockItEventBased>().Debug($"Closing lock failed. Bold is blocked.");
throw new CouldntCloseBoldBlockedException();
throw new CouldntCloseBoltBlockedException();
case LockitLockingState.CouldntCloseMoving:
// Expected error. ILockIt could not be closed (bike is moving)
@ -273,13 +273,13 @@ namespace TINK.Services.BluetoothLock.BLE
case LockitLockingState.Open:
// Expected error. ILockIt could not be closed. Bolt was blocked but was opened again.
Log.ForContext<LockItPolling>().Debug($"Closing lock failed. Bold is blocked but was reopened again.");
throw new CouldntCloseBoldBlockedException(LockingState.Open);
throw new CouldntCloseBoltBlockedException(LockingState.Open);
default:
// Comprises values
// - LockitLockingState.Open
// - LockitLockingState.Unknown
// - LockitLockingState.CouldntOpenBoldBlocked
// - LockitLockingState.CouldntOpenBoltBlocked
// Internal error which should never occur. 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());

View file

@ -55,34 +55,35 @@ namespace TINK.Services.BluetoothLock.BLE
}
var info = await GetLockStateAsync();
if (info?.State == null)
if (!(info?.State is LockitLockingState initialLockingState))
{
// Device not reachable.
Log.ForContext<LockItPolling>().Information("Can not open lock. Device is not reachable (get state).");
return await Task.FromResult((LockitLockingState?)null);
}
if (info.State.Value.GetLockingState() == LockingState.Open)
if (initialLockingState.GetLockingState() == LockingState.Open)
{
// Lock is already open.
Log.ForContext<LockItPolling>().Information("No need to open lock. Lock is already open.");
return await Task.FromResult((LockitLockingState?)null);
}
Log.ForContext<LockItPolling>().Debug($"Request to open lock. Current locking state is {info}, counter value {ActivateLockWriteCounter}.");
// Send command to open to lock.
var result = await OpenCloseLockAsync(
true); // Close lock);
true /* Close lock*/ );
if (!result)
{
// State did not change. Return previous state.
Log.ForContext<LockItPolling>().Information($"Opening lock failed.");
return await Task.FromResult(info.State.Value);
return await Task.FromResult(initialLockingState);
}
info = await GetLockStateAsync();
if (info?.State == null)
var subsequentLockingStateNullable = (await GetLockStateAsync()).State;
if (subsequentLockingStateNullable == null)
{
// Device not reachable.
Log.ForContext<LockItPolling>().Information($"State after open command unknown. Device is not reachable (get state).");
@ -92,28 +93,28 @@ namespace TINK.Services.BluetoothLock.BLE
var watch = new Stopwatch();
watch.Start();
while (info?.State != null
&& info.State.Value != LockitLockingState.CouldntOpenBoldBlocked
&& info.State.Value != LockitLockingState.Open
while (subsequentLockingStateNullable != null
&& subsequentLockingStateNullable != LockitLockingState.CouldntOpenBoltBlocked
&& subsequentLockingStateNullable != LockitLockingState.Open
&& watch.Elapsed < TimeSpan.FromMilliseconds(OPEN_CLOSE_TIMEOUT_MS))
{
info = await GetLockStateAsync(true); // While opening lock seems not always respond to reading operations.
Log.ForContext<LockItPolling>().Debug($"Waiting for lock to open. Current lock state is {info?.State.Value}.");
subsequentLockingStateNullable = (await GetLockStateAsync(true)).State; // While opening lock seems not always respond to reading operations.
Log.ForContext<LockItPolling>().Debug($"Waiting for lock to open. Current lock state is {subsequentLockingStateNullable}.");
}
if (info == null)
if (!(subsequentLockingStateNullable is LockitLockingState subsequentLockingState))
{
Log.ForContext<LockItPolling>().Fatal($"Opening lock failed. State object is null.");
return null;
}
switch (info.State.Value)
switch (subsequentLockingState)
{
case LockitLockingState.Open:
Log.ForContext<LockItPolling>().Information($"Lock was opened successfully.");
return info.State.Value;
return subsequentLockingState;
case LockitLockingState.CouldntOpenBoldBlocked:
case LockitLockingState.CouldntOpenBoltBlocked:
// Expected error. ILockIt count not be opened (Spoke blocks lock, ....)
Log.ForContext<LockItPolling>().Debug($"Opening lock failed. Bold is blocked.");
throw new CouldntOpenBoldIsBlockedException();
@ -127,10 +128,10 @@ namespace TINK.Services.BluetoothLock.BLE
// Comprises values
// - LockitLockingState.Closed
// - LockitLockingState.CouldntCloseMoving (should never happen because command open was send)
// - LockitLockingState.CouldntCloseBoldBlocked (should never happen because command open was send)
// - LockitLockingState.CouldntCloseBoltBlocked (should never happen because command open was send)
// Internal error which should never occur. Lock refuses to open but connection is ok.
Log.ForContext<LockItPolling>().Debug($"Opening lock failed. Unexpected lock state {info.State.Value.GetLockingState()} detected.");
throw new CouldntOpenInconsistentStateExecption(info.State.Value.GetLockingState());
Log.ForContext<LockItPolling>().Debug($"Opening lock failed. Unexpected lock state {subsequentLockingState.GetLockingState()} detected.");
throw new CouldntOpenInconsistentStateExecption(subsequentLockingState.GetLockingState());
}
}
@ -145,33 +146,35 @@ namespace TINK.Services.BluetoothLock.BLE
// Get current state
var info = await GetLockStateAsync();
if (info?.State == null)
if (!(info?.State is LockitLockingState initialLockingState))
{
// Device not reachable.
Log.ForContext<LockItPolling>().Error("Can not close lock. Device is not reachable (get state).");
return await Task.FromResult((LockitLockingState?)null);
}
if (info.State.Value.GetLockingState() == LockingState.Closed)
if (initialLockingState.GetLockingState() == LockingState.Closed)
{
// Lock is already closed.
Log.ForContext<LockItPolling>().Error("No need to close lock. Lock is already closed.");
return await Task.FromResult(info.State.Value);
return await Task.FromResult(initialLockingState);
}
Log.ForContext<LockItPolling>().Debug($"Request to close lock. Current locking state is {info}, counter value {ActivateLockWriteCounter}.");
var result = await OpenCloseLockAsync(false); // Close lock
// Send command to close to lock.
var result = await OpenCloseLockAsync(
false /*Close lock*/);
if (!result)
{
// State did not change. Return previous state.
Log.ForContext<LockItPolling>().Information($"Closing lock failed.");
return await Task.FromResult(info.State.Value);
return await Task.FromResult(initialLockingState);
}
// Get lock state until either lock state chaneges or until log gets unreachable.
info = await GetLockStateAsync();
if (info?.State == null)
// Get lock state until either lock state changes or until log gets unreachable.
var subsequentLockingStateNullable = (await GetLockStateAsync()).State;
if (subsequentLockingStateNullable == null)
{
// Device not reachable.
Log.ForContext<LockItPolling>().Information($"Lock state after close command unknown.");
@ -182,14 +185,14 @@ namespace TINK.Services.BluetoothLock.BLE
watch.Start();
var hasBeenLocked = false;
while (info.State != null
&& info.State.Value != LockitLockingState.CouldntCloseMoving
&& info.State.Value != LockitLockingState.Closed
while (subsequentLockingStateNullable != null
&& subsequentLockingStateNullable != LockitLockingState.CouldntCloseMoving
&& subsequentLockingStateNullable != LockitLockingState.Closed
&& watch.Elapsed < TimeSpan.FromMilliseconds(OPEN_CLOSE_TIMEOUT_MS))
{
info = await GetLockStateAsync(true); ; // While closing lock seems not always respond to reading operations.
subsequentLockingStateNullable = (await GetLockStateAsync(true)).State; ; // While closing lock seems not always respond to reading operations.
if (info.State.Value == LockitLockingState.CouldntCloseBoldBlocked)
if (subsequentLockingStateNullable == LockitLockingState.CouldntCloseBoltBlocked)
{
// Lock reported a blocked bold.
hasBeenLocked = true;
@ -197,14 +200,14 @@ namespace TINK.Services.BluetoothLock.BLE
continue;
}
if (hasBeenLocked && info.State.Value == LockitLockingState.Open)
if (hasBeenLocked && subsequentLockingStateNullable == LockitLockingState.Open)
{
// ILockIt could not be closed. Bolt was blocked but was opened again.
Log.ForContext<LockItPolling>().Debug($"Closing lock failed. Bold was blocked and lock was reopened.");
throw new CouldntCloseBoldBlockedException(LockingState.Open);
throw new CouldntCloseBoltBlockedException(LockingState.Open);
}
Log.ForContext<LockItPolling>().Debug($"Waiting for lock to close. Current lock state is {info?.State.Value}.");
Log.ForContext<LockItPolling>().Debug($"Waiting for lock to close. Current lock state is {subsequentLockingStateNullable}.");
}
if (info == null)
@ -213,12 +216,19 @@ namespace TINK.Services.BluetoothLock.BLE
return null;
}
switch (info.State.Value)
if (!(subsequentLockingStateNullable is LockitLockingState subsequentLockingState))
{
case LockitLockingState.CouldntCloseBoldBlocked:
// Device not reachable.
Log.ForContext<LockItPolling>().Information($"Lock state after close command unknown.");
return await Task.FromResult((LockitLockingState?)null);
}
switch (subsequentLockingState)
{
case LockitLockingState.CouldntCloseBoltBlocked:
// Expected error. ILockIt could not be closed (Spoke blocks lock, ....)
Log.ForContext<LockItPolling>().Debug($"Closing lock failed. Bold is blocked.");
throw new CouldntCloseBoldBlockedException();
throw new CouldntCloseBoltBlockedException();
case LockitLockingState.CouldntCloseMoving:
// Expected error. ILockIt could not be closed (bike is moving)
@ -228,16 +238,16 @@ namespace TINK.Services.BluetoothLock.BLE
case LockitLockingState.Closed:
// Everything is ok.
Log.ForContext<LockItPolling>().Information($"Lock was closed successfully.");
return info.State.Value;
return subsequentLockingState;
default:
// Comprises values
// - LockitLockingState.Open
// - LockitLockingState.Unknown
// - LockitLockingState.CouldntOpenBoldBlocked
// - LockitLockingState.CouldntOpenBoltBlocked
// Internal error which should never occur. Lock refuses to close but connection is ok.
Log.ForContext<LockItPolling>().Debug($"Closing lock failed. Unexpected lock state {info.State.Value.GetLockingState()} detected.");
throw new CouldntCloseInconsistentStateExecption(info.State.Value.GetLockingState());
Log.ForContext<LockItPolling>().Debug($"Closing lock failed. Unexpected lock state {subsequentLockingState.GetLockingState()} detected.");
throw new CouldntCloseInconsistentStateExecption(subsequentLockingState.GetLockingState());
}
}
}