This commit is contained in:
Oliver Hauff 2022-01-22 18:28:01 +01:00
parent f38b516d25
commit 578fcee611
70 changed files with 6828 additions and 9625 deletions

View file

@ -74,6 +74,8 @@ namespace TINK.Services.BluetoothLock.BLE
private ICharacteristic AlarmCharacteristic { get; set; }
private ICharacteristic AlarmSettingsCharacteristic { get; set; }
private ICharacteristic AuthCharacteristic { get; set; }
private ICharacteristic StateCharacteristic { get; set; }
@ -165,6 +167,7 @@ namespace TINK.Services.BluetoothLock.BLE
return ActivateLockCharacteristic;
}
/// <summary> Gets the alarm characteristic.</summary>
private async Task<ICharacteristic> GetAlarmCharacteristicAsync()
{
@ -188,6 +191,29 @@ namespace TINK.Services.BluetoothLock.BLE
return AlarmCharacteristic;
}
/// <summary> Gets the alarm settings characteristic.</summary>
private async Task<ICharacteristic> GetAlarmSettingsCharacteristicAsync()
{
if (AlarmSettingsCharacteristic != null) return AlarmSettingsCharacteristic;
AlarmSettingsCharacteristic = null;
Log.ForContext<LockItBase>().Debug("Request to get alarm settings characteristic.");
try
{
AlarmSettingsCharacteristic = await (await GetLockControlService())?.GetCharacteristicAsync(new Guid("0000BFFE-1212-efde-1523-785fef13d123"));
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Getting alarm settings charcteristic failed. {Exception}", exception);
throw new System.Exception($"Can not get alarm settings characteristic. {exception.Message}", exception);
}
Log.ForContext<LockItBase>().Debug("Get alarm settings characteristic retrieved successfully.");
return AlarmSettingsCharacteristic;
}
/// <summary> Gets the auth characteristic.</summary>
private async Task<ICharacteristic> GetAuthCharacteristicAsync()
{
@ -404,6 +430,7 @@ namespace TINK.Services.BluetoothLock.BLE
LockControl = null;
ActivateLockCharacteristic = null;
AlarmCharacteristic = null;
AlarmSettingsCharacteristic = null;
AuthCharacteristic = null;
StateCharacteristic = null;
SoundCharacteristic = null;
@ -885,7 +912,7 @@ namespace TINK.Services.BluetoothLock.BLE
return success;
}
/// <summary> Turns off the alarm.</summary>
/// <summary> Gets a value indicating whether alarm is on or off.</summary>
public async Task<bool> GetIsAlarmOffAsync()
{
if (DeviceInfo.Platform != DevicePlatform.Unknown && MainThread.IsMainThread == false)
@ -897,7 +924,7 @@ namespace TINK.Services.BluetoothLock.BLE
if (alarmCharacteristic == null)
{
Log.ForContext<LockItBase>().Debug("Getting alarm characteristic failed.");
return false;
throw new System.Exception($"Can not get alarm whether alarm is on or off. Getting alarm characteristic returned null.");
}
byte[] alarmSettings;
@ -961,7 +988,7 @@ namespace TINK.Services.BluetoothLock.BLE
throw new System.Exception($"Can not set alarm settings. Writing settings did not succeed.");
}
Log.ForContext<LockItBase>().Debug("Sound settings written successfully.{AlarmCharacteristic}{CommandWritten}.", ToSerilogString(alarmCharacteristic), command[0]);
Log.ForContext<LockItBase>().Debug("Alarm settings written successfully.{AlarmCharacteristic}{CommandWritten}.", ToSerilogString(alarmCharacteristic), command[0]);
}
public async Task<bool> SetSoundAsync(SoundSettings settings)
@ -992,12 +1019,46 @@ namespace TINK.Services.BluetoothLock.BLE
Log.ForContext<LockItBase>().Debug(success
? "Writing sound settings failed.{SoundCharacteristic}{CommandWritten}"
: "Sound setting written successfully.{SoundCharacteristic}{CommandWritten}",
: "Sound settings written successfully.{SoundCharacteristic}{CommandWritten}",
ToSerilogString(soundCharacteristic), command);
return success;
}
public async Task<bool> SetAlarmSettingsAsync(AlarmSettings settings)
{
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");
}
ICharacteristic alarmSettingsCharacteristic = await GetAlarmSettingsCharacteristicAsync();
if (alarmSettingsCharacteristic == null)
{
Log.ForContext<LockItBase>().Debug("Getting alarm settings characteristic failed.");
return false;
}
bool success;
byte command = (byte)settings;
try
{
success = await alarmSettingsCharacteristic.WriteAsync(new byte[] { command });
}
catch (System.Exception exception)
{
Log.ForContext<LockItBase>().Error("Writing alarm settings failed.{SoundCharacteristic}{CommandWritten}{Exception}", ToSerilogString(alarmSettingsCharacteristic), command, exception);
throw new System.Exception($"Can not set alarm settings. {exception.Message}", exception);
}
Log.ForContext<LockItBase>().Debug(success
? "Writing alarm settings failed.{SoundCharacteristic}{CommandWritten}"
: "Alarm settings written successfully.{SoundCharacteristic}{CommandWritten}",
ToSerilogString(alarmSettingsCharacteristic), command);
return success;
}
private static byte[] bitShift(byte[] data, int counter)
{
int mask = 0x000000FF;