2023-04-19 12:14:14 +02:00
using System ;
2022-08-30 15:42:25 +02:00
using System.Collections.Generic ;
2021-05-13 20:03:07 +02:00
using System.Linq ;
using System.Threading.Tasks ;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Bikes ;
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock ;
using ShareeBike.Model.State ;
using ShareeBike.Services.BluetoothLock.Tdo ;
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Services.BluetoothLock
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
/// <summary>
2023-04-19 12:14:14 +02:00
/// Fake locks service implementation which simulates locks which are in reach.
2022-09-06 16:08:19 +02:00
/// </summary>
public class LocksServiceInReach : ILocksServiceFake
{
private IEnumerable < LockInfoTdo > LocksInfo { get ; set ; } = new List < LockInfoTdo > ( ) ;
2023-04-19 12:14:14 +02:00
/// <summary> Holds timeout values for series of connecting attempts to a lock or multiple locks. </summary>
2022-09-06 16:08:19 +02:00
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 < Model . Bikes . BikeInfoNS . BluetoothLock . BikeInfo > ( ) )
{
var lockInfo = bikeInfo . LockInfo ;
switch ( bikeInfo . State . Value )
{
case InUseStateEnum . Disposable :
2023-04-19 12:14:14 +02:00
case InUseStateEnum . FeedbackPending : // State feedback pending does not exist for bluetooth locks but matches from bluetooth perspective state disposable.
2022-09-06 16:08:19 +02:00
switch ( lockInfo . State )
{
case LockingState . Open :
case LockingState . UnknownDisconnected :
case LockingState . UnknownFromHardwareError :
// 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 :
case LockingState . UnknownDisconnected :
case LockingState . UnknownFromHardwareError :
// 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 )
{
case LockingState . UnknownDisconnected :
case LockingState . UnknownFromHardwareError :
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>
public async Task < LockingState > DisconnectAsync ( int bikeId , Guid bikeGuid ) = > await Task . FromResult ( LockingState . UnknownDisconnected ) ;
}
2021-05-13 20:03:07 +02:00
}