Version 3.0.340

This commit is contained in:
Anja 2022-09-20 13:51:55 +02:00
parent 52c9f6f1d9
commit bad07e1ec9
62 changed files with 1401 additions and 1000 deletions

View file

@ -83,6 +83,8 @@ namespace TINK.Services.BluetoothLock.BLE
private ICharacteristic BatteryCharacteristic { get; set; }
private ICharacteristic FirmwareVersionCharacteristic { get; set; }
private readonly AsyncRetryPolicy<byte[]> _retryPollicy;
/// <summary> Gets the lock control service.</summary>
@ -301,6 +303,27 @@ namespace TINK.Services.BluetoothLock.BLE
return BatteryCharacteristic;
}
/// <summary> Gets the versions info characteristic.</summary>
protected async Task<ICharacteristic> GetVersionsCharacteristicAsync()
{
if (FirmwareVersionCharacteristic != null) return FirmwareVersionCharacteristic ;
FirmwareVersionCharacteristic = null;
Log.ForContext<LockItBase>().Debug("Request to get versions info characteristic.");
try
{
FirmwareVersionCharacteristic = await (await GetLockControlService())?.GetCharacteristicAsync(new Guid("0000baad-1212-efde-1523-785fef13d123"));
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting versions info charcteristic failed. {Exception}", exception);
throw new System.Exception(string.Format("Can not get versions info characteristic. {0}", exception.Message), exception);
}
Log.ForContext<LockItBase>().Debug("Get versions info characteristic retrieved successfully.");
return FirmwareVersionCharacteristic ;
}
/// <summary> Query name of lock.</summary>
private void GetName()
{
@ -822,12 +845,12 @@ namespace TINK.Services.BluetoothLock.BLE
throw new CoundntGetCharacteristicException("Can not get battery percentage. State characteristic must not be null.");
}
byte[] state;
byte[] percentage;
var cts = new CancellationTokenSource();
cts.CancelAfter(READ_TIMEOUT_MS);
try
{
state = await batteryCharacteristic.ReadAsync(cts.Token);
percentage = await batteryCharacteristic.ReadAsync(cts.Token);
}
catch (System.Exception exception)
{
@ -839,17 +862,109 @@ namespace TINK.Services.BluetoothLock.BLE
cts.Dispose();
}
if (state == null || state.Length <= 0)
if (percentage == null || percentage.Length <= 0)
{
Log.ForContext<LockItBase>().Debug("Retrieving charging level (ReadAsync-call) failed. Data read is null or empty.{BatteryCharacteristic}", ToSerilogString(batteryCharacteristic));
throw new System.Exception("Can not get battery percentage. No data read.");
}
Log.ForContext<LockItBase>().Debug("Retrieving charging level (ReadAsync-call) succeeded.{Level}{BatteryCharacteristic}{Reading}", state[0], ToSerilogString(batteryCharacteristic), state);
Log.ForContext<LockItBase>().Debug("Retrieving charging level (ReadAsync-call) succeeded.{Level}{BatteryCharacteristic}{Reading}", percentage[0], ToSerilogString(batteryCharacteristic), percentage);
return state[0];
return percentage[0];
}
/// <summary> Gets version info about the lock. </summary>
/// <remarks>
/// Lock state is first byte of of value read from state characteristic ("0000baaa-1212-efde-1523-785fef13d123").
/// Values are as follows
/// Byte number 0: firmware version,
/// Byte number 1: lock version (2 classic, 3 plus, 4 GPS)
/// Byte number 2: hardware version,
/// </remarks>
/// <returns> .</returns>
public async Task<VersionInfoTdo> GetVersionInfoAsync()
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
{
throw new System.Exception("Can not get versions info. 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 versions info.");
try
{
deviceState = Device?.State.GetDeviceState()
?? throw new System.Exception("Can not get bluetooth device state. State must not be null.");
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Can not get versions info. Retrieving bluetooth state failed. {Exception}", exception);
throw new System.Exception(string.Format("Can not get versions info. Getting bluetooth state failed. {0}", exception.Message), exception);
}
switch (deviceState)
{
case DeviceState.Disconnected:
throw new BluetoothDisconnectedException();
case DeviceState.Connected:
break;
default:
// Can not get state if device is not connected.
Log.ForContext<LockItBase>().Error($"Getting versions info failed. Unexpected versions info {deviceState} detected.");
throw new System.Exception(string.Format("Can not get versions info. Unexpected bluetooth state {0} detected.", deviceState));
}
Log.ForContext<LockItBase>().Debug($"Connection state is {deviceState}.");
var firmwareVersionCharacteristic = await GetVersionsCharacteristicAsync();
if (firmwareVersionCharacteristic == null)
{
Log.ForContext<LockItBase>().Error($"Can not get versions info. versions info characteristic is not available.");
throw new CoundntGetCharacteristicException("Can not get versions info. versions info characteristic must not be null.");
}
byte[] version;
var cts = new CancellationTokenSource();
cts.CancelAfter(READ_TIMEOUT_MS);
try
{
version = await firmwareVersionCharacteristic.ReadAsync(cts.Token);
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Retrieving versions info (ReadAsync-call) failed inside delegate.{StateCharacteristic}{Exception}", ToSerilogString(firmwareVersionCharacteristic), exception);
throw;
}
finally
{
cts.Dispose();
}
if (version == null || version.Length <= 0)
{
Log.ForContext<LockItBase>().Debug("Retrieving versions info (ReadAsync-call) failed. Data read is null or empty.{StateCharacteristic}", ToSerilogString(firmwareVersionCharacteristic));
throw new System.Exception("Can not get versions info. No data read");
}
VersionInfo = new VersionInfoTdo.Builder
{
FirmwareVersion = version[0],
LockVersion = version[1],
HardwareVersion = version[2]
}.Build();
Log.ForContext<LockItBase>().Debug("Retrieving versions info (ReadAsync-call) succeeded. {@LockInfoTdo}{StateCharacteristic}{Reading}",
VersionInfo,
ToSerilogString(firmwareVersionCharacteristic),
version);
return VersionInfo;
}
public VersionInfoTdo VersionInfo { get; private set; }
/// <summary> Opens lock. </summary>
/// <returns> Locking state.</returns>
public abstract Task<LockitLockingState?> OpenAsync();

View file

@ -151,6 +151,8 @@ namespace TINK.Services.BluetoothLock.BLE
DeviceList.Add(lockIt);
await lockIt.GetVersionInfoAsync();
return await lockIt.GetLockStateAsync();
}
@ -230,6 +232,8 @@ namespace TINK.Services.BluetoothLock.BLE
Log.ForContext<LockItByScanServiceBase>().Debug($"Auth succeeded for device {device}.");
locksList.Add(lockIt);
await lockIt.GetVersionInfoAsync();
}
return locksList;