mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-04-23 13:26:29 +02:00
Version 3.0.381
This commit is contained in:
parent
f963c0a219
commit
3a363acf3a
1525 changed files with 60589 additions and 125098 deletions
|
@ -0,0 +1,82 @@
|
|||
using Serilog;
|
||||
using ShareeBike.Model.Device;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Crypto
|
||||
{
|
||||
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> 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;
|
||||
}
|
||||
|
||||
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()
|
||||
{
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
}
|
88
LockIt.BusinessLogic/Services/BluetoothLock/Crypto/Cipher.cs
Normal file
88
LockIt.BusinessLogic/Services/BluetoothLock/Crypto/Cipher.cs
Normal file
|
@ -0,0 +1,88 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Security.Cryptography;
|
||||
using ShareeBike.Model.Device;
|
||||
|
||||
namespace ShareeBike.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
|
||||
|
||||
/// </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));
|
||||
|
||||
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 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");
|
||||
|
||||
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 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class AlreadyConnectedException : System.Exception
|
||||
{
|
||||
public AlreadyConnectedException() : base("Invalid reconnect call detected. Device is already connected.") { }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class AuthKeyException : System.Exception
|
||||
{
|
||||
public AuthKeyException(string message) : base(message)
|
||||
{ }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
|
||||
namespace ShareeBike.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.LockItExceptionBluetoothDisconnected)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
|
||||
using ShareeBike.MultilingualResources;
|
||||
|
||||
namespace ShareeBike.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.LockItExceptionConnectLockBluetoothOffException) { }
|
||||
|
||||
public ConnectBluetoothNotOnException(object state) : base(string.Format(Resources.LockItExceptionConnectLockBluetoothNotOnException, state.ToString())) { }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
using ShareeBike.MultilingualResources;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class ConnectLocationOffException : System.Exception
|
||||
{
|
||||
public ConnectLocationOffException() : base(Resources.LockItExceptionConnectLockLocationOffException) { }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
using ShareeBike.MultilingualResources;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class ConnectLocationPermissionMissingException : System.Exception
|
||||
{
|
||||
public ConnectLocationPermissionMissingException() : base(Resources.LockItExceptionConnectLockLocationPermissingMissingException) { }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,22 @@
|
|||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class CouldntCloseBoltBlockedException : StateAwareException
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructs a bold blocked exception.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Lock is closed in most cases, but this is not guaranteed according to haveltec.
|
||||
/// </remarks>
|
||||
/// <param name="state">State of the lock while/ after bold blocked event.</param>
|
||||
/// <remarks>
|
||||
/// </remarks>
|
||||
public CouldntCloseBoltBlockedException(LockingState state = LockingState.UnknownFromHardwareError) : base(
|
||||
state,
|
||||
MultilingualResources.Resources.LockItExceptionCloseLockBoltBlocked)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
|
||||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using ShareeBike.MultilingualResources;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class CouldntCloseInconsistentStateExecption : StateAwareException
|
||||
{
|
||||
public CouldntCloseInconsistentStateExecption(LockingState state) :
|
||||
base(
|
||||
state,
|
||||
state != LockingState.UnknownFromHardwareError
|
||||
? string.Format(Resources.LockItExceptionCloseLockUnexpectedLockState, state)
|
||||
: Resources.LockItExceptionCloseLockUnknownPosition)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class CouldntCloseMovingException : StateAwareException
|
||||
{
|
||||
public CouldntCloseMovingException() : base(
|
||||
LockingState.Open, // Locking bold is probable (according to haveltec) still open.
|
||||
MultilingualResources.Resources.LockItExceptionCloseLockMoving)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
|
||||
namespace ShareeBike.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.LockItExceptionOpenLockBoltBlocked)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
|
||||
namespace ShareeBike.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 CouldntOpenBoldStatusIsUnknownException : StateAwareException
|
||||
{
|
||||
public CouldntOpenBoldStatusIsUnknownException() : base(
|
||||
LockingState.UnknownFromHardwareError,
|
||||
MultilingualResources.Resources.LockItExceptionOpenLockBoldStatusIsUnknown)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
|
||||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using ShareeBike.MultilingualResources;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class CouldntOpenInconsistentStateExecption : StateAwareException
|
||||
{
|
||||
public CouldntOpenInconsistentStateExecption(LockingState state) :
|
||||
base(
|
||||
state,
|
||||
string.Format(Resources.LockItExceptionOpenLockUnexpectedState, state))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class CoundntGetCharacteristicException : System.Exception
|
||||
{
|
||||
public CoundntGetCharacteristicException(string message) : base(message)
|
||||
{ }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
public class GuidUnknownException : System.Exception
|
||||
{
|
||||
public GuidUnknownException() : base("Can not connect to lock. No Guid available.")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
/// <summary> Thrown whenever lock is out of reach.</summary>
|
||||
/// <remarks> If fired when scan for devices does not result in lock being found.</remarks>
|
||||
public class OutOfReachException : System.Exception
|
||||
{
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Exception
|
||||
{
|
||||
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; }
|
||||
}
|
||||
}
|
71
LockIt.BusinessLogic/Services/BluetoothLock/ILockService.cs
Normal file
71
LockIt.BusinessLogic/Services/BluetoothLock/ILockService.cs
Normal file
|
@ -0,0 +1,71 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ShareeBike.Services.BluetoothLock.Tdo;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock
|
||||
{
|
||||
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);
|
||||
|
||||
/// <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();
|
||||
|
||||
string Name { get; }
|
||||
|
||||
Guid Guid { get; }
|
||||
|
||||
int Id { get; }
|
||||
|
||||
/// <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);
|
||||
|
||||
Task<bool> SetSoundAsync(SoundSettings settings);
|
||||
|
||||
Task<bool> SetAlarmSettingsAsync(AlarmSettings settings);
|
||||
|
||||
Task<bool> GetIsAlarmOffAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Get info about lock firmware- hardware- and lock-version.
|
||||
/// </summary>
|
||||
/// <returns>Lock versions info.</returns>
|
||||
Task<VersionInfoTdo> GetVersionInfoAsync();
|
||||
|
||||
/// <summary>
|
||||
/// Holds info about lock firmware- hardware- and lock-version, null if info has not yet been read.
|
||||
/// </summary>
|
||||
VersionInfoTdo VersionInfo { get; }
|
||||
|
||||
Task SetIsAlarmOffAsync(bool isActivated);
|
||||
|
||||
/// <summary>Gets the battery percentage.</summary>
|
||||
Task<double> GetBatteryPercentageAsync();
|
||||
|
||||
/// <summary> Disconnect from lock.</summary>
|
||||
Task Disconnect();
|
||||
}
|
||||
}
|
67
LockIt.BusinessLogic/Services/BluetoothLock/ILocksService.cs
Normal file
67
LockIt.BusinessLogic/Services/BluetoothLock/ILocksService.cs
Normal file
|
@ -0,0 +1,67 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
||||
using ShareeBike.Services.BluetoothLock.Tdo;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock
|
||||
{
|
||||
public interface ILocksService
|
||||
{
|
||||
/// <summary> Holds timeout values for series of connecting attempts 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> 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> 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> 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,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock
|
||||
{
|
||||
public interface ITimeOutProvider
|
||||
{
|
||||
TimeSpan MultiConnect { get; }
|
||||
|
||||
TimeSpan GetSingleConnect(int countOfTry);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ShareeBike.Services.BluetoothLock.Tdo;
|
||||
|
||||
namespace ShareeBike.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>();
|
||||
|
||||
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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public static LockingState GetLockingState(this LockitLockingState lockingState)
|
||||
{
|
||||
switch (lockingState)
|
||||
{
|
||||
case LockitLockingState.Unknown:
|
||||
case LockitLockingState.CouldntOpenBoltBlocked: // Lock is closed in most cases, but this is not guaranteed according to haveltec.
|
||||
return LockingState.UnknownFromHardwareError;
|
||||
|
||||
case LockitLockingState.Open:
|
||||
case LockitLockingState.CouldntCloseMoving:
|
||||
case LockitLockingState.CouldntCloseBoltBlocked:
|
||||
return LockingState.Open;
|
||||
|
||||
case LockitLockingState.Closed:
|
||||
return LockingState.Closed;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LockIt.BusinessLogic.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") }
|
||||
};
|
||||
|
||||
}
|
||||
}
|
70
LockIt.BusinessLogic/Services/BluetoothLock/NullLock.cs
Normal file
70
LockIt.BusinessLogic/Services/BluetoothLock/NullLock.cs
Normal file
|
@ -0,0 +1,70 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using ShareeBike.Services.BluetoothLock.Tdo;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a not exisitng lock.
|
||||
/// </summary>
|
||||
public class NullLock : ILockService
|
||||
{
|
||||
private int BikeId { get; }
|
||||
|
||||
public NullLock(int bikeId)
|
||||
{
|
||||
BikeId = bikeId;
|
||||
}
|
||||
|
||||
public Task ReconnectAsync(LockInfoAuthTdo authInfo, TimeSpan connectTimeout) =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public string Name =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public Guid Guid =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public int Id =>
|
||||
throw new NotImplementedException();
|
||||
|
||||
public async Task<LockitLockingState?> OpenAsync() =>
|
||||
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<VersionInfoTdo> GetVersionInfoAsync() =>
|
||||
throw new System.Exception($"Can not get versions. Lock {BikeId} not found.");
|
||||
|
||||
public VersionInfoTdo VersionInfo
|
||||
{
|
||||
get => throw new System.Exception($"Can not get versions. Lock {BikeId} not found.");
|
||||
}
|
||||
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<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 async Task<bool> SetSoundAsync(SoundSettings 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();
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
using System;
|
||||
using ShareeBike.Model.Connector;
|
||||
|
||||
namespace ShareeBike.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> 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> 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; }
|
||||
|
||||
public bool IsIdValid => Id != TextToLockItTypeHelper.INVALIDLOCKID;
|
||||
|
||||
public bool IsGuidValid => Guid != TextToLockItTypeHelper.INVALIDLOCKGUID;
|
||||
|
||||
public class Builder
|
||||
{
|
||||
private LockInfoAuthTdo lockInfoTdo = new LockInfoAuthTdo();
|
||||
|
||||
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> 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> 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; } }
|
||||
|
||||
public LockInfoAuthTdo Build()
|
||||
{
|
||||
if (K_seed == null) K_seed = new byte[0];
|
||||
|
||||
if (K_u == null) K_u = new byte[0];
|
||||
|
||||
if (K_a == null) K_a = new byte[0];
|
||||
|
||||
return lockInfoTdo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
using System;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock.Tdo
|
||||
{
|
||||
/// <summary> Genuine ILOCKIT state.</summary>
|
||||
public enum LockitLockingState
|
||||
{
|
||||
Open = 0x00,
|
||||
|
||||
Closed = 0x01,
|
||||
|
||||
Unknown = 0x02,
|
||||
|
||||
CouldntCloseMoving = 0x03,
|
||||
|
||||
CouldntOpenBoltBlocked = 0x04,
|
||||
|
||||
CouldntCloseBoltBlocked = 0x05
|
||||
}
|
||||
|
||||
/// <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> 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; }
|
||||
|
||||
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> 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; }
|
||||
|
||||
public LockInfoTdo Build()
|
||||
{
|
||||
return lockInfoTdo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
namespace ShareeBike.Services.BluetoothLock.Tdo
|
||||
{
|
||||
public class VersionInfoTdo
|
||||
{
|
||||
/// <summary>
|
||||
/// Holds info about firmware- and hardware version of a lock and the type of lock (lock version).
|
||||
/// </summary>
|
||||
private VersionInfoTdo() { }
|
||||
|
||||
/// <summary>
|
||||
/// Holds the firmware version of the lock.
|
||||
/// </summary>
|
||||
public int FirmwareVersion { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Holds the hardware version (revision) of the lock.
|
||||
/// </summary>
|
||||
public int HardwareVersion { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Holds lock version (2 – classic, 3 – plus, 4 – GPS).
|
||||
/// </summary>
|
||||
public int LockVersion { get; private set; }
|
||||
|
||||
public class Builder
|
||||
{
|
||||
private VersionInfoTdo lockVersionTdo = new VersionInfoTdo();
|
||||
|
||||
public int FirmwareVersion { get => lockVersionTdo.FirmwareVersion; set => lockVersionTdo.FirmwareVersion = value; }
|
||||
|
||||
public int HardwareVersion { get => lockVersionTdo.HardwareVersion; set => lockVersionTdo.HardwareVersion = value; }
|
||||
|
||||
public int LockVersion { get => lockVersionTdo.LockVersion; set => lockVersionTdo.LockVersion = value; }
|
||||
|
||||
public VersionInfoTdo Build()
|
||||
{
|
||||
return lockVersionTdo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace ShareeBike.Services.BluetoothLock
|
||||
{
|
||||
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;
|
||||
|
||||
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; }
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue