sharee.bike-App/TINKLib/Services/BluetoothLock/LocksServiceInReach.cs

158 lines
6.6 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TINK.Model.Bike;
using TINK.Model.Bike.BluetoothLock;
using TINK.Services.BluetoothLock.Tdo;
using TINK.Model.State;
using System;
namespace TINK.Services.BluetoothLock
{
/// <summary>
/// Facke locks service implementation which simulates locks which are in reach.
/// </summary>
public class LocksServiceInReach : ILocksServiceFake
{
private IEnumerable<LockInfoTdo> LocksInfo { get; set; } = new List<LockInfoTdo>();
/// <summary> Holds timeout values for series of connecting attemps to a lock or multiple locks. </summary>
public ITimeOutProvider TimeOut { get; set; }
/// <summary> Connects to lock.</summary>
/// <param name="authInfo"> Info required to connect to lock.</param>
/// <param name="connectTimeout">Timeout for connect operation.</param>
public async Task<LockInfoTdo> ConnectAsync(LockInfoAuthTdo authInfo, TimeSpan connectTimeout)
{
return await Task.FromResult(new LockInfoTdo.Builder { Id = authInfo.Id, Guid = authInfo.Guid, State = LockitLockingState.Closed }.Build());
}
/// <param name="connectTimeout">Timeout for connect operation of a single lock.</param>
public async Task<IEnumerable<LockInfoTdo>> GetLocksStateAsync(IEnumerable<LockInfoAuthTdo> locksInfo, TimeSpan connectTimeout)
{
return await Task.FromResult(LocksInfo);
}
public void UpdateSimulation(BikeCollection bikes)
{
var locksInfo = new List<LockInfoTdo> ();
// Add and process locks info object
foreach (var bikeInfo in bikes.OfType<BikeInfo>())
{
var lockInfo = bikeInfo.LockInfo;
switch (bikeInfo.State.Value)
{
case InUseStateEnum.Disposable:
switch (lockInfo.State )
{
case LockingState.Open:
2022-04-10 17:38:34 +02:00
case LockingState.UnknownDisconnected:
case LockingState.UnknownFromHardwareError:
2021-05-13 20:03:07 +02:00
// Open bikes are never disposable because as soon as a they are open they get booked.
if (LocksInfo.FirstOrDefault(x => x.Id == lockInfo.Id) != null)
{
continue; // Lock was already added.
}
locksInfo.Add(new LockInfoTdo.Builder { Id = lockInfo.Id, State = LockitLockingState.Closed }.Build());
break;
}
break;
case InUseStateEnum.Reserved:
switch (lockInfo.State)
{
case LockingState.Open:
2022-04-10 17:38:34 +02:00
case LockingState.UnknownDisconnected:
case LockingState.UnknownFromHardwareError:
2021-05-13 20:03:07 +02:00
// Closed bikes are never reserved because as soon as they are open they get booked.
if (LocksInfo.FirstOrDefault(x => x.Id == lockInfo.Id) != null)
{
continue; // Lock was already added.
}
locksInfo.Add(new LockInfoTdo.Builder { Id = lockInfo.Id, State = LockitLockingState.Closed }.Build());
break;
}
break;
case InUseStateEnum.Booked:
switch (lockInfo.State)
{
2022-04-10 17:38:34 +02:00
case LockingState.UnknownDisconnected:
case LockingState.UnknownFromHardwareError:
2021-05-13 20:03:07 +02:00
if (LocksInfo.FirstOrDefault(x => x.Id == lockInfo.Id) != null)
{
continue; // Lock was already added.
}
locksInfo.Add(new LockInfoTdo.Builder { Id = lockInfo.Id, State = LockitLockingState.Closed }.Build());
break;
}
break;
}
LocksInfo = locksInfo;
}
}
/// <summary> Opens a lock.</summary>
/// <param name="lockId">Id of lock to open.</param>
public async Task<LockitLockingState?> OpenAsync(int bikeId, byte[] copriKey)
{
return await Task.FromResult(LockitLockingState.Open);
}
/// <summary> Closes a lock.</summary>
/// <param name="lockId">Id of lock to close.</param>
public async Task<LockitLockingState?> CloseAsync(int bikeId, byte[] copriKey)
{
return await Task.FromResult(LockitLockingState.Closed);
}
/// <summary> Set sound settings.</summary>
/// <param name="lockId">Id of lock to set sound settings.</param>
public async Task<bool> SetSoundAsync(int lockId, SoundSettings settings)
{
return await Task.FromResult(true);
}
/// <summary> Sets whether alarm is on or off.</summary>
/// <param name="lockId">Id of lock to get info from.</param>
public async Task SetIsAlarmOffAsync(int lockId, bool activated)
{
await Task.FromResult(true);
}
/// <summary> Gets battery percentage.</summary>
/// <param name="lockId">Id of lock to get info for.</param>
public Task<double> GetBatteryPercentageAsync(int lockId)
{
throw new NotSupportedException();
}
/// <summary> Gets whether alarm is on or off.</summary>
/// <param name="lockId">Id of lock to get info for.</param>
public async Task<bool> GetIsAlarmOffAsync(int lockId)
{
return await Task.FromResult(true);
}
/// <summary>Gets a lock by bike Id.</summary>
/// <param name="bikeId"></param>
/// <returns>Lock object</returns>
public ILockService this[int bikeId]
{
get
{
return null;
}
}
/// <summary> Disconnects lock.</summary>
/// <param name="bikeId"> Id of lock to disconnect.</param>
/// <param name="bikeGuid"> Guid of lock to disconnect.</param>
2022-04-10 17:38:34 +02:00
public async Task<LockingState> DisconnectAsync(int bikeId, Guid bikeGuid) => await Task.FromResult(LockingState.UnknownDisconnected);
2021-05-13 20:03:07 +02:00
}
}