Version 3.0.338

This commit is contained in:
Anja Müller-Meißner 2022-09-06 16:08:19 +02:00 committed by Anja
parent 573fe77e12
commit 0468955d49
751 changed files with 62747 additions and 60672 deletions

View file

@ -3,80 +3,80 @@ using TINK.Model.Device;
namespace TINK.Services.BluetoothLock.Crypto
{
public class AuthCryptoHelper
{
private ICipher Cipher { get; }
public class AuthCryptoHelper
{
private ICipher Cipher { get; }
/// <summary> Encrypted seed (random number) created inside ILOCKIT and passd to app.</summary>
private byte[] SeedLockEncrypted { get; }
/// <summary> Encrypted seed (random number) created inside ILOCKIT and passd to app.</summary>
private byte[] SeedLockEncrypted { get; }
/// <summary> Contstructs a auth crypto helper object.</summary>
/// <param name="seedLockEncrypted">Encrypted seed to deocode using <see cref="KeyCopri"/>.</param>
/// <param name="keyCopri">Key used to to decrypt <see cref="SeedLockEncrypted"/>.</param>
public AuthCryptoHelper(
byte[] seedLockEncrypted,
byte[] keyCopri,
ICipher cipher)
{
KeyCopri = keyCopri;
SeedLockEncrypted = seedLockEncrypted;
Cipher = cipher ?? new Cipher();
}
/// <summary> Contstructs a auth crypto helper object.</summary>
/// <param name="seedLockEncrypted">Encrypted seed to deocode using <see cref="KeyCopri"/>.</param>
/// <param name="keyCopri">Key used to to decrypt <see cref="SeedLockEncrypted"/>.</param>
public AuthCryptoHelper(
byte[] seedLockEncrypted,
byte[] keyCopri,
ICipher cipher)
{
KeyCopri = keyCopri;
SeedLockEncrypted = seedLockEncrypted;
Cipher = cipher ?? new Cipher();
}
/// <summary> Public for testing purposes only.</summary>
public byte[] GetSeedLock()
{
byte[] seedLockDecrypted;
var seedLockEncrypted = SeedLockEncrypted;
var keyCopri = KeyCopri;
try
{
seedLockDecrypted = Cipher.Decrypt(
keyCopri,
seedLockEncrypted);
}
catch (System.Exception exception)
{
Log.ForContext<AuthCryptoHelper>().Error("Decrypting seed from lock failed. {Exception}", exception);
throw;
}
/// <summary> Public for testing purposes only.</summary>
public byte[] GetSeedLock()
{
byte[] seedLockDecrypted;
var seedLockEncrypted = SeedLockEncrypted;
var keyCopri = KeyCopri;
try
{
seedLockDecrypted = Cipher.Decrypt(
keyCopri,
seedLockEncrypted);
}
catch (System.Exception exception)
{
Log.ForContext<AuthCryptoHelper>().Error("Decrypting seed from lock failed. {Exception}", exception);
throw;
}
Log.ForContext<AuthCryptoHelper>().Verbose($"Lock random number decrypted from {string.Join(",", seedLockEncrypted)} to {string.Join(",", seedLockDecrypted)} using {string.Join(", ", keyCopri)}.");
return seedLockDecrypted;
}
Log.ForContext<AuthCryptoHelper>().Verbose($"Lock random number decrypted from {string.Join(",", seedLockEncrypted)} to {string.Join(",", seedLockDecrypted)} using {string.Join(", ", keyCopri)}.");
return seedLockDecrypted;
}
public byte[] GetAccessKeyEncrypted()
{
public byte[] GetAccessKeyEncrypted()
{
var accessKey = GetSeedLock();
var accessKey = GetSeedLock();
if (accessKey == null || accessKey.Length <= 0)
{
Log.ForContext<AuthCryptoHelper>().Error("Creating access key failed, Key must not be null or empty.");
throw new System.Exception();
}
if (accessKey == null || accessKey.Length <= 0)
{
Log.ForContext<AuthCryptoHelper>().Error("Creating access key failed, Key must not be null or empty.");
throw new System.Exception();
}
accessKey[accessKey.Length - 1] += 1;
accessKey[accessKey.Length - 1] += 1;
var keyCopri = KeyCopri;
byte[] acccessKeyEncrypted;
try
{
acccessKeyEncrypted = Cipher.Encrypt(
keyCopri,
accessKey);
}
catch (System.Exception exception)
{
Log.ForContext<AuthCryptoHelper>().Error("Encrypting access key failed. {Exception}", exception);
throw;
}
var keyCopri = KeyCopri;
byte[] acccessKeyEncrypted;
try
{
acccessKeyEncrypted = Cipher.Encrypt(
keyCopri,
accessKey);
}
catch (System.Exception exception)
{
Log.ForContext<AuthCryptoHelper>().Error("Encrypting access key failed. {Exception}", exception);
throw;
}
Log.ForContext<AuthCryptoHelper>().Verbose($"Access key encrypted from {string.Join(",", accessKey)} to {string.Join(",", acccessKeyEncrypted)} using {string.Join(", ", keyCopri)}.");
return acccessKeyEncrypted;
}
Log.ForContext<AuthCryptoHelper>().Verbose($"Access key encrypted from {string.Join(",", accessKey)} to {string.Join(",", acccessKeyEncrypted)} using {string.Join(", ", keyCopri)}.");
return acccessKeyEncrypted;
}
public byte[] KeyCopri { get; }
}
public byte[] KeyCopri { get; }
}
}

View file

@ -5,84 +5,84 @@ using TINK.Model.Device;
namespace TINK.Services.BluetoothLock.Crypto
{
public class Cipher : ICipher
{
/// <summary> Decrypt data. </summary>
/// <remarks>
/// Further info see:
/// https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netcore-3.1 for further info
// https://docs.microsoft.com/en-us/dotnet/standard/security/cryptographic-services
// https://stackoverflow.com/questions/24903575/how-to-return-byte-when-decrypt-using-cryptostream-descryptoserviceprovider/24903689
public class Cipher : ICipher
{
/// <summary> Decrypt data. </summary>
/// <remarks>
/// Further info see:
/// https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.aes?view=netcore-3.1 for further info
// https://docs.microsoft.com/en-us/dotnet/standard/security/cryptographic-services
// https://stackoverflow.com/questions/24903575/how-to-return-byte-when-decrypt-using-cryptostream-descryptoserviceprovider/24903689
/// </remarks>
/// <param name="key">Key to decrypt data with.</param>
/// <param name="encrypted">Encrpyted data to decrypt.</param>
/// <returns>Decrypted data.</returns>
public byte[] Decrypt(byte[] key, byte[] encrypted)
{
// Check arguments.
if (encrypted == null || encrypted.Length <= 0)
throw new ArgumentNullException(nameof(encrypted));
/// </remarks>
/// <param name="key">Key to decrypt data with.</param>
/// <param name="encrypted">Encrpyted data to decrypt.</param>
/// <returns>Decrypted data.</returns>
public byte[] Decrypt(byte[] key, byte[] encrypted)
{
// Check arguments.
if (encrypted == null || encrypted.Length <= 0)
throw new ArgumentNullException(nameof(encrypted));
if (key == null || key.Length <= 0)
throw new ArgumentNullException(nameof(key));
if (key == null || key.Length <= 0)
throw new ArgumentNullException(nameof(key));
using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = 192;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = key;
using (Aes aesAlg = Aes.Create())
{
aesAlg.KeySize = 192;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = key;
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream())
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
csDecrypt.Write(encrypted, 0, encrypted.Length);
csDecrypt.FlushFinalBlock();
return msDecrypt.ToArray();
}
}
}
}
// Create the streams used for decryption.
using (var msDecrypt = new MemoryStream())
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
{
csDecrypt.Write(encrypted, 0, encrypted.Length);
csDecrypt.FlushFinalBlock();
return msDecrypt.ToArray();
}
}
}
}
public byte[] Encrypt(byte[] key, byte[] clear)
{
// Check arguments.
if (clear == null || clear.Length <= 0)
throw new ArgumentNullException("plainText");
public byte[] Encrypt(byte[] key, byte[] clear)
{
// Check arguments.
if (clear == null || clear.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
// Create an AesCryptoServiceProvider object
// with the specified key and IV.
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.KeySize = 192;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = key;
// Create an AesCryptoServiceProvider object
// with the specified key and IV.
using (AesCryptoServiceProvider aesAlg = new AesCryptoServiceProvider())
{
aesAlg.KeySize = 192;
aesAlg.Mode = CipherMode.ECB;
aesAlg.Padding = PaddingMode.None;
aesAlg.Key = key;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(clear, 0, clear.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
}
}
// Create the streams used for encryption.
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
csEncrypt.Write(clear, 0, clear.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
}
}
}
}
}
}
}

View file

@ -1,7 +1,7 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class AlreadyConnectedException : System.Exception
{
public AlreadyConnectedException() : base("Invalid reconnect call detected. Device is already connected.") { }
}
public class AlreadyConnectedException : System.Exception
{
public AlreadyConnectedException() : base("Invalid reconnect call detected. Device is already connected.") { }
}
}

View file

@ -1,8 +1,8 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class AuthKeyException : System.Exception
{
public AuthKeyException(string message) : base(message)
{ }
}
public class AuthKeyException : System.Exception
{
public AuthKeyException(string message) : base(message)
{ }
}
}

View file

@ -2,17 +2,17 @@
namespace TINK.Services.BluetoothLock.Exception
{
/// <summary>
/// If fired if app is disconnected from bluetooth.
/// </summary>
/// <remarks>
/// Member to determine if app is disconnected: IDevice.State from server Plugin.BLE. If DeviceState.Disconnected exception is fired.
/// All possible states: Disconnected, Connecting, Connected, Limited.
/// </remarks>
public class BluetoothDisconnectedException : StateAwareException
{
public BluetoothDisconnectedException() : base(LockingState.UnknownDisconnected, MultilingualResources.Resources.ErrorBluetoothDisconnectedException)
{
}
}
/// <summary>
/// If fired if app is disconnected from bluetooth.
/// </summary>
/// <remarks>
/// Member to determine if app is disconnected: IDevice.State from server Plugin.BLE. If DeviceState.Disconnected exception is fired.
/// All possible states: Disconnected, Connecting, Connected, Limited.
/// </remarks>
public class BluetoothDisconnectedException : StateAwareException
{
public BluetoothDisconnectedException() : base(LockingState.UnknownDisconnected, MultilingualResources.Resources.ErrorBluetoothDisconnectedException)
{
}
}
}

View file

@ -3,15 +3,15 @@ using TINK.MultilingualResources;
namespace TINK.Services.BluetoothLock.Exception
{
/// <summary> Exception which is fired when bluetooth is not on. </summary>
/// <remarks>
/// Member to determine if app is disconnected: IBluetoothLE.State from server Plugin.BLE. If current value is not BluetoothState.On exception is fired.
/// All possible states: Unknown, Unavailable, Unauthorized, TurningOn, On, TurningOff, Off.
/// </remarks>
public class ConnectBluetoothNotOnException : System.Exception
{
public ConnectBluetoothNotOnException() : base(Resources.ErrorConnectLockBluetoothOffException) { }
/// <summary> Exception which is fired when bluetooth is not on. </summary>
/// <remarks>
/// Member to determine if app is disconnected: IBluetoothLE.State from server Plugin.BLE. If current value is not BluetoothState.On exception is fired.
/// All possible states: Unknown, Unavailable, Unauthorized, TurningOn, On, TurningOff, Off.
/// </remarks>
public class ConnectBluetoothNotOnException : System.Exception
{
public ConnectBluetoothNotOnException() : base(Resources.ErrorConnectLockBluetoothOffException) { }
public ConnectBluetoothNotOnException(object state) : base(string.Format(Resources.ErrorConnectLockBluetoothNotOnException, state.ToString())) { }
}
public ConnectBluetoothNotOnException(object state) : base(string.Format(Resources.ErrorConnectLockBluetoothNotOnException, state.ToString())) { }
}
}

View file

@ -2,8 +2,8 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class ConnectLocationOffException : System.Exception
{
public ConnectLocationOffException() : base(Resources.ErrorConnectLockLocationOffException) { }
}
public class ConnectLocationOffException : System.Exception
{
public ConnectLocationOffException() : base(Resources.ErrorConnectLockLocationOffException) { }
}
}

View file

@ -2,8 +2,8 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class ConnectLocationPermissionMissingException : System.Exception
{
public ConnectLocationPermissionMissingException() : base(Resources.ErrorConnectLockLocationPermissingMissingException) { }
}
public class ConnectLocationPermissionMissingException : System.Exception
{
public ConnectLocationPermissionMissingException() : base(Resources.ErrorConnectLockLocationPermissingMissingException) { }
}
}

View file

@ -2,12 +2,12 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class CouldntCloseBoldBlockedException : StateAwareException
{
public CouldntCloseBoldBlockedException() : base(
LockingState.UnknownFromHardwareError, // Lock is closed in most cases, but this is not guaranteed according to haveltec.
MultilingualResources.Resources.ErrorCloseLockBoldBlocked)
{
}
}
public class CouldntCloseBoldBlockedException : StateAwareException
{
public CouldntCloseBoldBlockedException() : base(
LockingState.UnknownFromHardwareError, // Lock is closed in most cases, but this is not guaranteed according to haveltec.
MultilingualResources.Resources.ErrorCloseLockBoldBlocked)
{
}
}
}

View file

@ -4,15 +4,15 @@ using TINK.MultilingualResources;
namespace TINK.Services.BluetoothLock.Exception
{
public class CouldntCloseInconsistentStateExecption : StateAwareException
{
public CouldntCloseInconsistentStateExecption(LockingState state) :
base(
state,
state != LockingState.UnknownFromHardwareError
? string.Format(Resources.ErrorCloseLockUnexpectedState, state)
: Resources.ErrorCloseLockUnknownPosition)
{
}
}
public class CouldntCloseInconsistentStateExecption : StateAwareException
{
public CouldntCloseInconsistentStateExecption(LockingState state) :
base(
state,
state != LockingState.UnknownFromHardwareError
? string.Format(Resources.ErrorCloseLockUnexpectedState, state)
: Resources.ErrorCloseLockUnknownPosition)
{
}
}
}

View file

@ -2,12 +2,12 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class CouldntCloseMovingException : StateAwareException
{
public CouldntCloseMovingException() : base(
LockingState.Open, // Locking bold is probable (according to haveltec) still open.
MultilingualResources.Resources.ErrorCloseLockBikeMoving)
{
}
}
public class CouldntCloseMovingException : StateAwareException
{
public CouldntCloseMovingException() : base(
LockingState.Open, // Locking bold is probable (according to haveltec) still open.
MultilingualResources.Resources.ErrorCloseLockBikeMoving)
{
}
}
}

View file

@ -2,15 +2,15 @@
namespace TINK.Services.BluetoothLock.Exception
{
/// <summary>
/// Lock can not be opened, because bold is blocked. Lock reports that obstacle is still blocking.
/// </summary>
public class CouldntOpenBoldIsBlockedException : StateAwareException
{
public CouldntOpenBoldIsBlockedException() : base(
LockingState.UnknownFromHardwareError,
MultilingualResources.Resources.ErrorOpenLockBoldBlocked)
{
}
}
/// <summary>
/// Lock can not be opened, because bold is blocked. Lock reports that obstacle is still blocking.
/// </summary>
public class CouldntOpenBoldIsBlockedException : StateAwareException
{
public CouldntOpenBoldIsBlockedException() : base(
LockingState.UnknownFromHardwareError,
MultilingualResources.Resources.ErrorOpenLockBoldBlocked)
{
}
}
}

View file

@ -2,15 +2,15 @@
namespace TINK.Services.BluetoothLock.Exception
{
/// <summary>
/// Lock can not be opened, because bold is/ was blocked. Obstacle might no more block but lock could not be opened completely to obstacle.
/// </summary>
public class CouldntOpenBoldWasBlockedException : StateAwareException
{
public CouldntOpenBoldWasBlockedException() : base(
LockingState.UnknownFromHardwareError,
MultilingualResources.Resources.ErrorOpenLockBoldWasBlocked)
{
}
}
/// <summary>
/// Lock can not be opened, because bold is/ was blocked. Obstacle might no more block but lock could not be opened completely to obstacle.
/// </summary>
public class CouldntOpenBoldWasBlockedException : StateAwareException
{
public CouldntOpenBoldWasBlockedException() : base(
LockingState.UnknownFromHardwareError,
MultilingualResources.Resources.ErrorOpenLockBoldWasBlocked)
{
}
}
}

View file

@ -4,13 +4,13 @@ using TINK.MultilingualResources;
namespace TINK.Services.BluetoothLock.Exception
{
public class CouldntOpenInconsistentStateExecption : StateAwareException
{
public CouldntOpenInconsistentStateExecption(LockingState state) :
base(
state,
string.Format(Resources.ErrorOpenLockUnexpectedState, state))
{
}
}
public class CouldntOpenInconsistentStateExecption : StateAwareException
{
public CouldntOpenInconsistentStateExecption(LockingState state) :
base(
state,
string.Format(Resources.ErrorOpenLockUnexpectedState, state))
{
}
}
}

View file

@ -1,8 +1,8 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class CoundntGetCharacteristicException : System.Exception
{
public CoundntGetCharacteristicException(string message) : base(message)
{ }
}
public class CoundntGetCharacteristicException : System.Exception
{
public CoundntGetCharacteristicException(string message) : base(message)
{ }
}
}

View file

@ -1,9 +1,9 @@
namespace TINK.Services.BluetoothLock.Exception
{
public class GuidUnknownException : System.Exception
{
public GuidUnknownException() : base("Can not connect to lock. No Guid available.")
{
}
}
public class GuidUnknownException : System.Exception
{
public GuidUnknownException() : base("Can not connect to lock. No Guid available.")
{
}
}
}

View file

@ -1,8 +1,8 @@
namespace TINK.Services.BluetoothLock.Exception
{
/// <summary> Thrown whenever lock is out of reach.</summary>
/// <remarks> If fired when scan for devices does not result in lock beeing found.</remarks>
public class OutOfReachException : System.Exception
{
}
/// <summary> Thrown whenever lock is out of reach.</summary>
/// <remarks> If fired when scan for devices does not result in lock beeing found.</remarks>
public class OutOfReachException : System.Exception
{
}
}

View file

@ -2,14 +2,14 @@
namespace TINK.Services.BluetoothLock.Exception
{
public abstract class StateAwareException : System.Exception
{
public StateAwareException(LockingState state, string description) : base(description)
{
State = state;
}
public abstract class StateAwareException : System.Exception
{
public StateAwareException(LockingState state, string description) : base(description)
{
State = state;
}
/// <summary> Holds the state reported by lock.</summary>
public LockingState State { get; }
}
/// <summary> Holds the state reported by lock.</summary>
public LockingState State { get; }
}
}

View file

@ -4,57 +4,57 @@ using TINK.Services.BluetoothLock.Tdo;
namespace TINK.Services.BluetoothLock
{
public enum DeviceState
{
Disconnected = 0,
Connecting = 1,
Connected = 2,
Limited = 3 /* Mactches Connecting? */
}
public enum DeviceState
{
Disconnected = 0,
Connecting = 1,
Connected = 2,
Limited = 3 /* Mactches Connecting? */
}
public interface ILockService
{
/// <summary> Reconnect to lock. </summary>
Task ReconnectAsync(
LockInfoAuthTdo authInfo,
TimeSpan connectTimeout);
public interface ILockService
{
/// <summary> Reconnect to lock. </summary>
Task ReconnectAsync(
LockInfoAuthTdo authInfo,
TimeSpan connectTimeout);
/// <summary> Opens lock. </summary>
/// <returns> State of lock after performing open operation. </returns>
Task<LockitLockingState?> OpenAsync();
/// <summary> Opens lock. </summary>
/// <returns> State of lock after performing open operation. </returns>
Task<LockitLockingState?> OpenAsync();
/// <summary> Closes lock. </summary>
/// <returns>State of lock after performing close operation.</returns>
Task<LockitLockingState?> CloseAsync();
/// <summary> Closes lock. </summary>
/// <returns>State of lock after performing close operation.</returns>
Task<LockitLockingState?> CloseAsync();
string Name { get; }
string Name { get; }
Guid Guid { get; }
Guid Guid { get; }
int Id { get; }
int Id { get; }
/// <summary> Gets the device state.</summary>
DeviceState? GetDeviceState();
/// <summary> Gets the device state.</summary>
DeviceState? GetDeviceState();
/// <summary>
/// Gets the locking state.
/// </summary>
/// <param name="doWaitRetry">True if to wait and retry in case of failures. </param>
/// <returns></returns>
Task<LockInfoTdo> GetLockStateAsync(bool doWaitRetry = false);
/// <summary>
/// Gets the locking state.
/// </summary>
/// <param name="doWaitRetry">True if to wait and retry in case of failures. </param>
/// <returns></returns>
Task<LockInfoTdo> GetLockStateAsync(bool doWaitRetry = false);
Task<bool> SetSoundAsync(SoundSettings settings);
Task<bool> SetSoundAsync(SoundSettings settings);
Task<bool> SetAlarmSettingsAsync(AlarmSettings settings);
Task<bool> SetAlarmSettingsAsync(AlarmSettings settings);
Task<bool> GetIsAlarmOffAsync();
Task<bool> GetIsAlarmOffAsync();
Task SetIsAlarmOffAsync(bool isActivated);
Task SetIsAlarmOffAsync(bool isActivated);
/// <summary>Gets the battery percentage.</summary>
Task<double> GetBatteryPercentageAsync();
/// <summary>Gets the battery percentage.</summary>
Task<double> GetBatteryPercentageAsync();
/// <summary> Disconnect from lock.</summary>
Task Disconnect();
}
/// <summary> Disconnect from lock.</summary>
Task Disconnect();
}
}

View file

@ -6,62 +6,62 @@ using TINK.Services.BluetoothLock.Tdo;
namespace TINK.Services.BluetoothLock
{
public interface ILocksService
{
/// <summary> Holds timeout values for series of connecting attemps to a lock or multiple locks. </summary>
ITimeOutProvider TimeOut { get; set; }
public interface ILocksService
{
/// <summary> Holds timeout values for series of connecting attemps to a lock or multiple locks. </summary>
ITimeOutProvider TimeOut { get; set; }
/// <summary> Gets lock info for all lock in reach./// </summary>
/// <param name="connectTimeout">Timeout for connect operation of a single lock.</param>
Task<IEnumerable<LockInfoTdo>> GetLocksStateAsync(
IEnumerable<LockInfoAuthTdo> locksInfo,
TimeSpan connectTimeout);
/// <summary> Gets lock info for all lock in reach./// </summary>
/// <param name="connectTimeout">Timeout for connect operation of a single lock.</param>
Task<IEnumerable<LockInfoTdo>> GetLocksStateAsync(
IEnumerable<LockInfoAuthTdo> locksInfo,
TimeSpan connectTimeout);
/// <summary> Connects to lock.</summary>
/// <param name="authInfo"> Info required to connect to lock.</param>
/// <param name="connectTimeout">Timeout for connect operation.</param>
Task<LockInfoTdo> ConnectAsync(
LockInfoAuthTdo authInfo,
TimeSpan connectTimeout);
/// <summary> Connects to lock.</summary>
/// <param name="authInfo"> Info required to connect to lock.</param>
/// <param name="connectTimeout">Timeout for connect operation.</param>
Task<LockInfoTdo> ConnectAsync(
LockInfoAuthTdo authInfo,
TimeSpan connectTimeout);
/// <summary>Gets a lock by bike Id.</summary>
/// <param name="bikeId"></param>
/// <returns>Lock object</returns>
ILockService this[int bikeId] { get; }
/// <summary>Gets a lock by bike Id.</summary>
/// <param name="bikeId"></param>
/// <returns>Lock object</returns>
ILockService this[int bikeId] { get; }
/// <summary> Disconnects lock.</summary>
/// <param name="bikeId"> Id of lock to disconnect.</param>
/// <param name="bikeGuid"> Guid of lock to disconnect.</param>
/// <returns> State disconnected it lock is already disconneced or after successfully disconnecting.</returns>
Task<LockingState> DisconnectAsync(int bikeId, Guid bikeGuid);
}
/// <summary> Disconnects lock.</summary>
/// <param name="bikeId"> Id of lock to disconnect.</param>
/// <param name="bikeGuid"> Guid of lock to disconnect.</param>
/// <returns> State disconnected it lock is already disconneced or after successfully disconnecting.</returns>
Task<LockingState> DisconnectAsync(int bikeId, Guid bikeGuid);
}
/// <summary> Alarm types types. </summary>
/// <remarks> Api-doc v29.0 </remarks>
public enum AlarmSettings
{
SmallSensivity = 2,
SmallSensivitySilent = 3,
SmallSensitivityPrealarm = 22,
MediumSensivity = 4,
MediumSensivitySilent = 0x05,
MediumSensivityPrealarm = 0x24,
HightestSensivity = 0x10,
HightestSensivitySilent = 0x11,
HightestSensivityPrealarm = 0x30,
}
/// <summary> Alarm types types. </summary>
/// <remarks> Api-doc v29.0 </remarks>
public enum AlarmSettings
{
SmallSensivity = 2,
SmallSensivitySilent = 3,
SmallSensitivityPrealarm = 22,
MediumSensivity = 4,
MediumSensivitySilent = 0x05,
MediumSensivityPrealarm = 0x24,
HightestSensivity = 0x10,
HightestSensivitySilent = 0x11,
HightestSensivityPrealarm = 0x30,
}
/// <summary> Sound types. </summary>
/// <remarks> Api-doc v29.0 </remarks>
public enum SoundSettings
{
OnLockingStartedWarn = 0x00,
Warn = 0x01,
LockingStarted = 0x02,
AllOff = 0x03, // Mute
OpenedSuccessfully = 0x04,
LockingStartedOpenedSuccessfully = 0x05,
AllOn = 0x06, // All sounds on
OpenedSuccessfullyWarn = 0x07,
}
/// <summary> Sound types. </summary>
/// <remarks> Api-doc v29.0 </remarks>
public enum SoundSettings
{
OnLockingStartedWarn = 0x00,
Warn = 0x01,
LockingStarted = 0x02,
AllOff = 0x03, // Mute
OpenedSuccessfully = 0x04,
LockingStartedOpenedSuccessfully = 0x05,
AllOn = 0x06, // All sounds on
OpenedSuccessfullyWarn = 0x07,
}
}

View file

@ -2,10 +2,10 @@
namespace TINK.Services.BluetoothLock
{
public interface ITimeOutProvider
{
TimeSpan MultiConnect { get; }
public interface ITimeOutProvider
{
TimeSpan MultiConnect { get; }
TimeSpan GetSingleConnect(int countOfTry);
}
TimeSpan GetSingleConnect(int countOfTry);
}
}

View file

@ -5,66 +5,66 @@ using TINK.Services.BluetoothLock.Tdo;
namespace TINK.Model.Bikes.BikeInfoNS.BluetoothLock
{
public static class LockInfoHelper
{
/// <summary> Update by id.</summary>
/// <param name="locksInfo"> Lock info objects to update by id.</param>
/// <param name="locksInfoTdo">Tdos holding data to updat from</param>
/// <returns></returns>
public static IEnumerable<LockInfo> UpdateById(
this IEnumerable<LockInfo> locksInfo,
IEnumerable<LockInfoTdo> locksInfoTdo)
{
var locksInfoUpdated = new List<LockInfo>();
public static class LockInfoHelper
{
/// <summary> Update by id.</summary>
/// <param name="locksInfo"> Lock info objects to update by id.</param>
/// <param name="locksInfoTdo">Tdos holding data to updat from</param>
/// <returns></returns>
public static IEnumerable<LockInfo> UpdateById(
this IEnumerable<LockInfo> locksInfo,
IEnumerable<LockInfoTdo> locksInfoTdo)
{
var locksInfoUpdated = new List<LockInfo>();
foreach (var lockInfo in locksInfo)
{
var lockInfoTdo = locksInfoTdo.FirstOrDefault(x => x.Id == lockInfo.Id);
if (lockInfoTdo == null)
{
// No object to update from found.
locksInfoUpdated.Add(lockInfo);
continue;
}
foreach (var lockInfo in locksInfo)
{
var lockInfoTdo = locksInfoTdo.FirstOrDefault(x => x.Id == lockInfo.Id);
if (lockInfoTdo == null)
{
// No object to update from found.
locksInfoUpdated.Add(lockInfo);
continue;
}
var state = lockInfoTdo.State.HasValue ? lockInfoTdo.State.Value.GetLockingState() : LockingState.UnknownDisconnected;
var state = lockInfoTdo.State.HasValue ? lockInfoTdo.State.Value.GetLockingState() : LockingState.UnknownDisconnected;
locksInfoUpdated.Add(state != lockInfo.State || lockInfoTdo.Guid != lockInfo.Guid
? new LockInfo.Builder(lockInfo) { Guid = lockInfoTdo.Guid, State = state }.Build() // State has changed, update required.
: lockInfo);
}
locksInfoUpdated.Add(state != lockInfo.State || lockInfoTdo.Guid != lockInfo.Guid
? new LockInfo.Builder(lockInfo) { Guid = lockInfoTdo.Guid, State = state }.Build() // State has changed, update required.
: lockInfo);
}
return locksInfoUpdated;
}
return locksInfoUpdated;
}
public static LockingState GetLockingState(this LockitLockingState lockingState)
{
switch (lockingState)
{
case LockitLockingState.Unknown:
case LockitLockingState.CouldntOpenBoldBlocked: // Lock is closed in most cases, but this is not guarnteed according to haveltec.
return LockingState.UnknownFromHardwareError;
public static LockingState GetLockingState(this LockitLockingState lockingState)
{
switch (lockingState)
{
case LockitLockingState.Unknown:
case LockitLockingState.CouldntOpenBoldBlocked: // Lock is closed in most cases, but this is not guarnteed according to haveltec.
return LockingState.UnknownFromHardwareError;
case LockitLockingState.Open:
case LockitLockingState.CouldntCloseMoving:
case LockitLockingState.CouldntCloseBoldBlocked:
return LockingState.Open;
case LockitLockingState.Open:
case LockitLockingState.CouldntCloseMoving:
case LockitLockingState.CouldntCloseBoldBlocked:
return LockingState.Open;
case LockitLockingState.Closed:
return LockingState.Closed;
}
case LockitLockingState.Closed:
return LockingState.Closed;
}
throw new ArgumentException($"Can not convert LockIt specific locking state to logical locking state. Unknown state {lockingState} detected.");
}
throw new ArgumentException($"Can not convert LockIt specific locking state to logical locking state. Unknown state {lockingState} detected.");
}
/// <summary> Gets one type from another.</summary>
/// <param name="lockInfo"></param>
/// <returns></returns>
public static LockInfoAuthTdo ToLockInfoTdo(this LockInfo lockInfo)
{
return new LockInfoAuthTdo.Builder() { Id = lockInfo.Id, Guid = lockInfo.Guid, K_u = lockInfo.UserKey, K_a = lockInfo.AdminKey, K_seed = lockInfo.Seed }.Build();
}
/// <summary> Gets one type from another.</summary>
/// <param name="lockInfo"></param>
/// <returns></returns>
public static LockInfoAuthTdo ToLockInfoTdo(this LockInfo lockInfo)
{
return new LockInfoAuthTdo.Builder() { Id = lockInfo.Id, Guid = lockInfo.Guid, K_u = lockInfo.UserKey, K_a = lockInfo.AdminKey, K_seed = lockInfo.Seed }.Build();
}
}
}
}

View file

@ -3,18 +3,18 @@ using System.Collections.Generic;
namespace LockItShared.Services.BluetoothLock
{
public static class LockItByGuidServiceHelper
{
/// <summary>
/// Holds guids of developent locks to ensure that no wrong guid are provided by COPRI
/// </summary>
public static Dictionary<int, Guid> DevelGuids = new Dictionary<int, Guid>
{
{ 2200537, new Guid("00000000-0000-0000-0000-d589a8023487") },
{ 2200543, new Guid("00000000-0000-0000-0000-cc141a6f68bb") },
{ 2200544, new Guid("00000000-0000-0000-0000-dc969f648732") },
{ 2200545, new Guid("00000000-0000-0000-0000-e38bf9d32234") }
};
public static class LockItByGuidServiceHelper
{
/// <summary>
/// Holds guids of developent locks to ensure that no wrong guid are provided by COPRI
/// </summary>
public static Dictionary<int, Guid> DevelGuids = new Dictionary<int, Guid>
{
{ 2200537, new Guid("00000000-0000-0000-0000-d589a8023487") },
{ 2200543, new Guid("00000000-0000-0000-0000-cc141a6f68bb") },
{ 2200544, new Guid("00000000-0000-0000-0000-dc969f648732") },
{ 2200545, new Guid("00000000-0000-0000-0000-e38bf9d32234") }
};
}
}
}

View file

@ -4,60 +4,60 @@ using TINK.Services.BluetoothLock.Tdo;
namespace TINK.Services.BluetoothLock
{
/// <summary>
/// Represents a not exisitng lock.
/// </summary>
public class NullLock : ILockService
{
private int BikeId { get; }
/// <summary>
/// Represents a not exisitng lock.
/// </summary>
public class NullLock : ILockService
{
private int BikeId { get; }
public NullLock(int bikeId)
{
BikeId = bikeId;
}
public NullLock(int bikeId)
{
BikeId = bikeId;
}
public Task ReconnectAsync(LockInfoAuthTdo authInfo, TimeSpan connectTimeout) =>
throw new NotImplementedException();
public Task ReconnectAsync(LockInfoAuthTdo authInfo, TimeSpan connectTimeout) =>
throw new NotImplementedException();
public string Name =>
throw new NotImplementedException();
public string Name =>
throw new NotImplementedException();
public Guid Guid =>
throw new NotImplementedException();
public Guid Guid =>
throw new NotImplementedException();
public int Id =>
throw new NotImplementedException();
public int Id =>
throw new NotImplementedException();
public async Task<LockitLockingState?> OpenAsync() =>
await Task.FromResult((LockitLockingState?)null);
public async Task<LockitLockingState?> OpenAsync() =>
await Task.FromResult((LockitLockingState?)null);
public async Task<LockitLockingState?> CloseAsync() =>
await Task.FromResult((LockitLockingState?)null);
public async Task<LockitLockingState?> CloseAsync() =>
await Task.FromResult((LockitLockingState?)null);
public Task<double> GetBatteryPercentageAsync() =>
throw new System.Exception($"Can not get battery percentage. Lock {BikeId} not found.");
public Task<double> GetBatteryPercentageAsync() =>
throw new System.Exception($"Can not get battery percentage. Lock {BikeId} not found.");
public DeviceState? GetDeviceState() =>
throw new NotImplementedException();
public DeviceState? GetDeviceState() =>
throw new NotImplementedException();
public Task<bool> GetIsAlarmOffAsync() =>
throw new System.Exception($"Can not get whether alarm is on or off. Lock {BikeId} not found.");
public Task<bool> GetIsAlarmOffAsync() =>
throw new System.Exception($"Can not get whether alarm is on or off. Lock {BikeId} not found.");
public Task<LockInfoTdo> GetLockStateAsync(bool doWaitRetry = false) =>
throw new NotImplementedException();
public Task<LockInfoTdo> GetLockStateAsync(bool doWaitRetry = false) =>
throw new NotImplementedException();
public Task SetIsAlarmOffAsync(bool isActivated) =>
throw new System.Exception($"Can not set alarm {isActivated}. Lock {BikeId} not found.");
public Task SetIsAlarmOffAsync(bool isActivated) =>
throw new System.Exception($"Can not set alarm {isActivated}. Lock {BikeId} not found.");
public async Task<bool> SetSoundAsync(SoundSettings settings) =>
await Task.FromResult(false);
public async Task<bool> SetSoundAsync(SoundSettings settings) =>
await Task.FromResult(false);
public async Task<bool> SetAlarmSettingsAsync(AlarmSettings settings) =>
await Task.FromResult(false);
public async Task<bool> SetAlarmSettingsAsync(AlarmSettings settings) =>
await Task.FromResult(false);
/// <summary> Disconnect from bluetooth lock. </summary>
public Task Disconnect() =>
throw new NotImplementedException();
/// <summary> Disconnect from bluetooth lock. </summary>
public Task Disconnect() =>
throw new NotImplementedException();
}
}
}

View file

@ -3,59 +3,59 @@ using TINK.Model.Connector;
namespace TINK.Services.BluetoothLock.Tdo
{
/// <summary> Data required to connect to a blutooth lock.</summary>
public class LockInfoAuthTdo
{
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get; private set; }
/// <summary> Data required to connect to a blutooth lock.</summary>
public class LockInfoAuthTdo
{
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get; private set; }
/// <summary> GUID to the device. </summary>
public Guid Guid { get; private set; }
/// <summary> GUID to the device. </summary>
public Guid Guid { get; private set; }
/// <summary> Seed used to generate key for connecting to bluetooth lock.</summary>
public byte[] K_seed { get; private set; }
/// <summary> Seed used to generate key for connecting to bluetooth lock.</summary>
public byte[] K_seed { get; private set; }
/// <summary> Key for connect to bluetooth lock as user.</summary>
public byte[] K_u { get; private set; }
/// <summary> Key for connect to bluetooth lock as user.</summary>
public byte[] K_u { get; private set; }
/// <summary> Key for connect to bluetooth lock as admin.</summary>
public byte[] K_a { get; private set; }
/// <summary> Key for connect to bluetooth lock as admin.</summary>
public byte[] K_a { get; private set; }
public bool IsIdValid => Id != TextToLockItTypeHelper.INVALIDLOCKID;
public bool IsIdValid => Id != TextToLockItTypeHelper.INVALIDLOCKID;
public bool IsGuidValid => Guid != TextToLockItTypeHelper.INVALIDLOCKGUID;
public bool IsGuidValid => Guid != TextToLockItTypeHelper.INVALIDLOCKGUID;
public class Builder
{
private LockInfoAuthTdo lockInfoTdo = new LockInfoAuthTdo();
public class Builder
{
private LockInfoAuthTdo lockInfoTdo = new LockInfoAuthTdo();
public Builder() { }
public Builder() { }
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get => lockInfoTdo.Id; set { lockInfoTdo.Id = value; } }
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get => lockInfoTdo.Id; set { lockInfoTdo.Id = value; } }
/// <summary> GUID to the device. </summary>
public Guid Guid { get => lockInfoTdo.Guid; set { lockInfoTdo.Guid = value; } }
/// <summary> GUID to the device. </summary>
public Guid Guid { get => lockInfoTdo.Guid; set { lockInfoTdo.Guid = value; } }
/// <summary> Seed used to generate key for connecting to bluetooth lock.</summary>
public byte[] K_seed { get => lockInfoTdo.K_seed; set { lockInfoTdo.K_seed = value; } }
/// <summary> Seed used to generate key for connecting to bluetooth lock.</summary>
public byte[] K_seed { get => lockInfoTdo.K_seed; set { lockInfoTdo.K_seed = value; } }
/// <summary> Key for connect to bluetooth lock as user.</summary>
public byte[] K_u { get => lockInfoTdo.K_u; set { lockInfoTdo.K_u = value; } }
/// <summary> Key for connect to bluetooth lock as user.</summary>
public byte[] K_u { get => lockInfoTdo.K_u; set { lockInfoTdo.K_u = value; } }
/// <summary> Key for connect to bluetooth lock as admin.</summary>
public byte[] K_a { get => lockInfoTdo.K_a; set { lockInfoTdo.K_a = value; } }
/// <summary> Key for connect to bluetooth lock as admin.</summary>
public byte[] K_a { get => lockInfoTdo.K_a; set { lockInfoTdo.K_a = value; } }
public LockInfoAuthTdo Build()
{
if (K_seed == null) K_seed = new byte[0];
public LockInfoAuthTdo Build()
{
if (K_seed == null) K_seed = new byte[0];
if (K_u == null) K_u = new byte[0];
if (K_u == null) K_u = new byte[0];
if (K_a == null) K_a = new byte[0];
if (K_a == null) K_a = new byte[0];
return lockInfoTdo;
}
}
}
return lockInfoTdo;
}
}
}
}

View file

@ -2,53 +2,53 @@
namespace TINK.Services.BluetoothLock.Tdo
{
/// <summary> Genuine ILOCKIT state.</summary>
public enum LockitLockingState
{
Open = 0x00,
/// <summary> Genuine ILOCKIT state.</summary>
public enum LockitLockingState
{
Open = 0x00,
Closed = 0x01,
Closed = 0x01,
Unknown = 0x02,
Unknown = 0x02,
CouldntCloseMoving = 0x03,
CouldntCloseMoving = 0x03,
CouldntOpenBoldBlocked = 0x04,
CouldntOpenBoldBlocked = 0x04,
CouldntCloseBoldBlocked = 0x05
}
CouldntCloseBoldBlocked = 0x05
}
/// <summary> Object holding info about bluetooth lock. </summary>
public class LockInfoTdo
{
private LockInfoTdo() { }
/// <summary> Object holding info about bluetooth lock. </summary>
public class LockInfoTdo
{
private LockInfoTdo() { }
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get; private set; }
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get; private set; }
/// <summary> Guid for direct connect.</summary>
public Guid Guid { get; private set; }
/// <summary> Guid for direct connect.</summary>
public Guid Guid { get; private set; }
/// <summary> Gets the current state of the lock.</summary>
public LockitLockingState? State { get; private set; }
/// <summary> Gets the current state of the lock.</summary>
public LockitLockingState? State { get; private set; }
public class Builder
{
private LockInfoTdo lockInfoTdo = new LockInfoTdo();
public class Builder
{
private LockInfoTdo lockInfoTdo = new LockInfoTdo();
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get => lockInfoTdo.Id; set => lockInfoTdo.Id = value; }
/// <summary> Identification number of bluetooth lock, 6-digits, second part of advertisement name.</summary>
public int Id { get => lockInfoTdo.Id; set => lockInfoTdo.Id = value; }
/// <summary> Guid for direct connect.</summary>
public Guid Guid { get => lockInfoTdo.Guid; set => lockInfoTdo.Guid = value; }
/// <summary> Guid for direct connect.</summary>
public Guid Guid { get => lockInfoTdo.Guid; set => lockInfoTdo.Guid = value; }
/// <summary> Gets the current state of the lock.</summary>
public LockitLockingState? State { get => lockInfoTdo.State; set => lockInfoTdo.State = value; }
/// <summary> Gets the current state of the lock.</summary>
public LockitLockingState? State { get => lockInfoTdo.State; set => lockInfoTdo.State = value; }
public LockInfoTdo Build()
{
return lockInfoTdo;
}
}
}
public LockInfoTdo Build()
{
return lockInfoTdo;
}
}
}
}

View file

@ -4,27 +4,27 @@ using System.Linq;
namespace TINK.Services.BluetoothLock
{
public class TimeOutProvider : ITimeOutProvider
{
/// <summary> Maximum factor applied on timeout factor. </summary>
private readonly int MAXIMUMFACTORTIMEOUT = 4;
public class TimeOutProvider : ITimeOutProvider
{
/// <summary> Maximum factor applied on timeout factor. </summary>
private readonly int MAXIMUMFACTORTIMEOUT = 4;
/// <summary> Default timeout to connect to bluetooth lock. </summary>
public static int DEFAULT_BLUETOOTHCONNECT_TIMEOUTSECONDS = 5;
/// <summary> Default timeout to connect to bluetooth lock. </summary>
public static int DEFAULT_BLUETOOTHCONNECT_TIMEOUTSECONDS = 5;
public TimeOutProvider(IEnumerable<TimeSpan> timeOuts = null)
{
TimeOuts = timeOuts != null && timeOuts.Count() > 0
? timeOuts.ToList()
: new List<TimeSpan> { new TimeSpan(0, 0, DEFAULT_BLUETOOTHCONNECT_TIMEOUTSECONDS) };
}
public TimeOutProvider(IEnumerable<TimeSpan> timeOuts = null)
{
TimeOuts = timeOuts != null && timeOuts.Count() > 0
? timeOuts.ToList()
: new List<TimeSpan> { new TimeSpan(0, 0, DEFAULT_BLUETOOTHCONNECT_TIMEOUTSECONDS) };
}
private List<TimeSpan> TimeOuts { get; }
private List<TimeSpan> TimeOuts { get; }
public TimeSpan MultiConnect => TimeOuts.ToArray()[0];
public TimeSpan MultiConnect => TimeOuts.ToArray()[0];
public TimeSpan GetSingleConnect(int countOfTry) => countOfTry < TimeOuts.Count
? TimeOuts.ToArray()[countOfTry]
: new TimeSpan(TimeOuts.ToArray()[0].Ticks * Math.Min(countOfTry, MAXIMUMFACTORTIMEOUT));
}
public TimeSpan GetSingleConnect(int countOfTry) => countOfTry < TimeOuts.Count
? TimeOuts.ToArray()[countOfTry]
: new TimeSpan(TimeOuts.ToArray()[0].Ticks * Math.Min(countOfTry, MAXIMUMFACTORTIMEOUT));
}
}