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
{
///
/// Facke locks service implementation which simulates locks which are in reach.
///
public class LocksServiceInReach : ILocksServiceFake
{
private IEnumerable LocksInfo { get; set; } = new List();
/// Holds timeout values for series of connecting attemps to a lock or multiple locks.
public ITimeOutProvider TimeOut { get; set; }
/// Connects to lock.
/// Info required to connect to lock.
/// Timeout for connect operation.
public async Task ConnectAsync(LockInfoAuthTdo authInfo, TimeSpan connectTimeout)
{
return await Task.FromResult(new LockInfoTdo.Builder { Id = authInfo.Id, Guid = authInfo.Guid, State = LockitLockingState.Closed }.Build());
}
/// Timeout for connect operation of a single lock.
public async Task> GetLocksStateAsync(IEnumerable locksInfo, TimeSpan connectTimeout)
{
return await Task.FromResult(LocksInfo);
}
public void UpdateSimulation(BikeCollection bikes)
{
var locksInfo = new List ();
// Add and process locks info object
foreach (var bikeInfo in bikes.OfType())
{
var lockInfo = bikeInfo.LockInfo;
switch (bikeInfo.State.Value)
{
case InUseStateEnum.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;
}
}
/// Opens a lock.
/// Id of lock to open.
public async Task OpenAsync(int bikeId, byte[] copriKey)
{
return await Task.FromResult(LockitLockingState.Open);
}
/// Closes a lock.
/// Id of lock to close.
public async Task CloseAsync(int bikeId, byte[] copriKey)
{
return await Task.FromResult(LockitLockingState.Closed);
}
/// Set sound settings.
/// Id of lock to set sound settings.
public async Task SetSoundAsync(int lockId, SoundSettings settings)
{
return await Task.FromResult(true);
}
/// Sets whether alarm is on or off.
/// Id of lock to get info from.
public async Task SetIsAlarmOffAsync(int lockId, bool activated)
{
await Task.FromResult(true);
}
/// Gets battery percentage.
/// Id of lock to get info for.
public Task GetBatteryPercentageAsync(int lockId)
{
throw new NotSupportedException();
}
/// Gets whether alarm is on or off.
/// Id of lock to get info for.
public async Task GetIsAlarmOffAsync(int lockId)
{
return await Task.FromResult(true);
}
/// Gets a lock by bike Id.
///
/// Lock object
public ILockService this[int bikeId]
{
get
{
return null;
}
}
/// Disconnects lock.
/// Id of lock to disconnect.
/// Guid of lock to disconnect.
public async Task DisconnectAsync(int bikeId, Guid bikeGuid) => await Task.FromResult(LockingState.UnknownDisconnected);
}
}