mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2025-01-03 20:26:26 +01:00
240 lines
7.2 KiB
C#
240 lines
7.2 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using ShareeBike.Model.Connector;
|
|
using ShareeBike.Repository.Request;
|
|
using ShareeBike.Services.BluetoothLock;
|
|
using ShareeBike.Services.Geolocation;
|
|
using ShareeBike.ViewModel;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.ConnectAndGetStateCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.OpenCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.CloseCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock.Command.DisconnectCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.StartReservationCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.CancelReservationCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.AuthCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.StartRentalCommand;
|
|
using static ShareeBike.Model.Bikes.BikeInfoNS.BikeNS.Command.EndRentalCommand;
|
|
|
|
namespace ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock
|
|
{
|
|
public class BikeInfoMutable : BC.BikeInfoMutable, IBikeInfoMutable
|
|
{
|
|
/// <summary> Provides a connector object.</summary>
|
|
private Func<bool, IConnector> ConnectorFactory { get; }
|
|
|
|
/// <summary> Provides geolocation information.</summary>
|
|
private IGeolocationService GeolocationService { get; }
|
|
|
|
/// <summary> Provides geolocation for end rental.</summary>
|
|
private LocationDto EndRentalLocation { get; }
|
|
|
|
/// <summary> Provides a connector object.</summary>
|
|
private ILocksService LockService { get; }
|
|
|
|
/// <summary> Delegate to retrieve connected state. </summary>
|
|
private Func<bool> IsConnectedDelegate { get; }
|
|
|
|
/// <summary>Object to manage update of view model objects from Copri.</summary>
|
|
protected Func<IPollingUpdateTaskManager> ViewUpdateManager { get; }
|
|
|
|
/// <summary> Constructs a bike object from source. </summary>
|
|
/// <param name="viewUpdateManager">Manages update of view model objects from Copri.</param>
|
|
public BikeInfoMutable(
|
|
IGeolocationService geolocation,
|
|
ILocksService lockService,
|
|
Func<bool> isConnectedDelegate,
|
|
Func<bool, IConnector> connectorFactory,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
BikeInfo bike,
|
|
string stationName) : base(
|
|
bike != null
|
|
? bike.Bike
|
|
: throw new ArgumentNullException(nameof(bike)),
|
|
bike.Drive,
|
|
bike.DataSource,
|
|
bike.IsDemo,
|
|
bike.Group,
|
|
bike.StationId,
|
|
stationName,
|
|
bike.OperatorUri,
|
|
bike.TariffDescription,
|
|
() => DateTime.Now,
|
|
bike.State)
|
|
{
|
|
LockInfo = new LockInfoMutable(
|
|
bike.LockInfo.Id,
|
|
bike.LockInfo.Guid,
|
|
bike.LockInfo.UserKey,
|
|
bike.LockInfo.AdminKey,
|
|
bike.LockInfo.Seed,
|
|
bike.LockInfo.State);
|
|
|
|
GeolocationService = geolocation
|
|
?? throw new ArgumentException($"Can not instantiate {nameof(BikeInfoMutable)}- object. No geolocation object available.");
|
|
|
|
LockService = lockService
|
|
?? throw new ArgumentException($"Can not instantiate {nameof(BikeInfoMutable)}- object. No lock service object available.");
|
|
|
|
IsConnectedDelegate = isConnectedDelegate
|
|
?? throw new ArgumentException($"Can not instantiate {nameof(BikeInfoMutable)}- object. No is connected delegate available.");
|
|
|
|
ConnectorFactory = connectorFactory
|
|
?? throw new ArgumentException($"Can not instantiate {nameof(BikeInfoMutable)}- object. No connector available.");
|
|
|
|
ViewUpdateManager = viewUpdateManager
|
|
?? throw new ArgumentException($"Can not instantiate {nameof(BikeInfoMutable)}- object. No update manger available.");
|
|
}
|
|
|
|
public LockInfoMutable LockInfo { get; }
|
|
|
|
ILockInfoMutable IBikeInfoMutable.LockInfo => LockInfo;
|
|
|
|
/// <summary>
|
|
/// Connects the lock.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process closing notifications.</param>
|
|
public async Task ConnectAsync(
|
|
IConnectAndGetStateCommandListener listener)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
LockService,
|
|
listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Opens the lock and updates copri.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process closing notifications.</param>
|
|
/// <param name="stopPollingTask">Task which stops polling.</param>
|
|
public async Task OpenLockAsync(
|
|
IOpenCommandListener listener,
|
|
Task stopPollingTask)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
LockService,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
listener,
|
|
stopPollingTask);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Closes the lock and updates copri.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process closing notifications.</param>
|
|
/// <param name="stopPollingTask">Task which stops polling.</param>
|
|
public async Task CloseLockAsync(
|
|
ICloseCommandListener listener,
|
|
Task stopPollingTask)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
GeolocationService,
|
|
LockService,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
listener,
|
|
stopPollingTask);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Disconnects the lock.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process closing notifications.</param>
|
|
public async Task DisconnectAsync(
|
|
IDisconnectCommandListener listener)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
LockService,
|
|
listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reserves the bike.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process notifications.</param>
|
|
/// <returns></returns>
|
|
public async Task ReserveBikeAsync(
|
|
IStartReservationCommandListener listener)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Cancels the reservation.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process notifications.</param>
|
|
/// <returns></returns>
|
|
public async Task CancelReservationAsync(
|
|
ICancelReservationCommandListener listener)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Authenticates the user.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process notifications.</param>
|
|
/// <returns></returns>
|
|
public async Task AuthAsync(
|
|
IAuthCommandListener listener)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Rents the bike.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process notifications.</param>
|
|
/// <returns></returns>
|
|
public async Task RentBikeAsync(
|
|
IStartRentalCommandListener listener)
|
|
{
|
|
await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
listener);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the bike.
|
|
/// </summary>
|
|
/// <param name="listener">View model to process notifications.</param>
|
|
/// <returns></returns>
|
|
public async Task<BookingFinishedModel> ReturnBikeAsync(
|
|
IEndRentalCommandListener listener)
|
|
{
|
|
var bookingFinished = await InvokeAsync<BikeInfoMutable>(
|
|
this,
|
|
GeolocationService,
|
|
LockService,
|
|
() => DateTime.Now,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
listener);
|
|
|
|
return bookingFinished;
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"Id={Id}{(TypeOfBike != null ? $";type={TypeOfBike}" : "")};state={State.ToString()};Lock id={LockInfo.Id}";
|
|
}
|
|
}
|
|
}
|