2022-08-30 15:42:25 +02:00
using System ;
using System.Collections.Generic ;
2021-05-13 20:03:07 +02:00
using System.Linq ;
using System.Threading.Tasks ;
2022-08-30 15:42:25 +02:00
using TINK.Model.Bikes ;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock ;
2021-05-13 20:03:07 +02:00
using TINK.Model.State ;
2022-08-30 15:42:25 +02:00
using TINK.Services.BluetoothLock.Tdo ;
2021-05-13 20:03:07 +02:00
namespace TINK.Services.BluetoothLock
{
2022-09-06 16:08:19 +02:00
/// <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 < Model . Bikes . BikeInfoNS . BluetoothLock . BikeInfo > ( ) )
{
var lockInfo = bikeInfo . LockInfo ;
switch ( bikeInfo . State . Value )
{
case InUseStateEnum . Disposable :
case InUseStateEnum . FeedbackPending : // State feedback pending does not exist for bluetooth locks but maches from bluetooth perspective state disposable.
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
}