sharee.bike-App/LockIt.BusinessLogic/Services/BluetoothLock/TimeOutProvider.cs

31 lines
987 B
C#
Raw Normal View History

2021-05-13 17:07:16 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Services.BluetoothLock
2021-05-13 17:07:16 +02:00
{
2022-09-06 16:08:19 +02:00
public class TimeOutProvider : ITimeOutProvider
{
/// <summary> Maximum factor applied on timeout factor. </summary>
private readonly int MAXIMUMFACTORTIMEOUT = 4;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Default timeout to connect to bluetooth lock. </summary>
public static int DEFAULT_BLUETOOTHCONNECT_TIMEOUTSECONDS = 5;
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public TimeOutProvider(IEnumerable<TimeSpan> timeOuts = null)
{
TimeOuts = timeOuts != null && timeOuts.Count() > 0
? timeOuts.ToList()
: new List<TimeSpan> { new TimeSpan(0, 0, DEFAULT_BLUETOOTHCONNECT_TIMEOUTSECONDS) };
}
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
private List<TimeSpan> TimeOuts { get; }
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public TimeSpan MultiConnect => TimeOuts.ToArray()[0];
2021-05-13 17:07:16 +02:00
2022-09-06 16:08:19 +02:00
public TimeSpan GetSingleConnect(int countOfTry) => countOfTry < TimeOuts.Count
? TimeOuts.ToArray()[countOfTry]
: new TimeSpan(TimeOuts.ToArray()[0].Ticks * Math.Min(countOfTry, MAXIMUMFACTORTIMEOUT));
}
2021-05-13 17:07:16 +02:00
}