mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-06 11:06:26 +01:00
248 lines
8.8 KiB
C#
248 lines
8.8 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using Serilog;
|
|
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
|
|
using TINK.Model.Connector;
|
|
using TINK.Model.Device;
|
|
using TINK.Model.User;
|
|
using TINK.MultilingualResources;
|
|
using TINK.Repository.Exception;
|
|
using TINK.Services.BluetoothLock;
|
|
using TINK.Services.BluetoothLock.Exception;
|
|
using TINK.Services.Geolocation;
|
|
using TINK.View;
|
|
using static TINK.Model.Bikes.BikeInfoNS.BluetoothLock.Command.GetLockedLocationCommand;
|
|
using static TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandlerFactory;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
|
|
{
|
|
public class BookedClosed : Base, IRequestHandler, IGetLockedLocationCommandListener
|
|
{
|
|
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
|
|
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
|
public BookedClosed(
|
|
IBikeInfoMutable selectedBike,
|
|
Func<bool> isConnectedDelegate,
|
|
Func<bool, IConnector> connectorFactory,
|
|
IGeolocationService geolocation,
|
|
ILocksService lockService,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
ISmartDevice smartDevice,
|
|
IViewService viewService,
|
|
IBikesViewModel bikesViewModel,
|
|
IUser activeUser) : base(
|
|
selectedBike,
|
|
AppResources.ActionEndRental, // End Rental
|
|
true, // Show button "End Rental"
|
|
isConnectedDelegate,
|
|
connectorFactory,
|
|
geolocation,
|
|
lockService,
|
|
viewUpdateManager,
|
|
smartDevice,
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser)
|
|
{
|
|
LockitButtonText = AppResources.ActionOpenLock; // Open Lock
|
|
IsLockitButtonVisible = true; // Show button "Open Lock"
|
|
|
|
_endRentalActionViewModel = new EndRentalActionViewModel<BookedClosed>(
|
|
selectedBike,
|
|
isConnectedDelegate,
|
|
connectorFactory,
|
|
lockService,
|
|
viewUpdateManager,
|
|
viewService,
|
|
bikesViewModel);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Holds the view model for end rental action.
|
|
/// </summary>
|
|
private EndRentalActionViewModel<BookedClosed> _endRentalActionViewModel;
|
|
|
|
/// <summary> Return bike. </summary>
|
|
public async Task<IRequestHandler> HandleRequestOption1()
|
|
{
|
|
await _endRentalActionViewModel.EndRentalAsync();
|
|
return Create(
|
|
SelectedBike,
|
|
IsConnectedDelegate,
|
|
ConnectorFactory,
|
|
GeolocationService,
|
|
LockService,
|
|
ViewUpdateManager,
|
|
SmartDevice,
|
|
ViewService,
|
|
BikesViewModel,
|
|
ActiveUser);
|
|
}
|
|
|
|
/// <summary> Open bike and update COPRI lock state. </summary>
|
|
public async Task<IRequestHandler> HandleRequestOption2() => await OpenLock();
|
|
|
|
/// <summary>
|
|
/// Processes the get lock location progress.
|
|
/// </summary>
|
|
/// <param name="step">Current step to process.</param>
|
|
public void ReportStep(Step step) => _endRentalActionViewModel.ReportStep(step);
|
|
|
|
/// <summary>
|
|
/// Processes the get lock location state.
|
|
/// </summary>
|
|
/// <param name="state">State to process.</param>
|
|
/// <param name="details">Textual details describing current state.</param>
|
|
public async Task ReportStateAsync(State state, string details) => await _endRentalActionViewModel.ReportStateAsync(state, details);
|
|
|
|
/// <summary> Open bike and update COPRI lock state. </summary>
|
|
public async Task<IRequestHandler> OpenLock()
|
|
{
|
|
Log.ForContext<BookedClosed>().Information("User request to unlock booked bike {bikeId}.", SelectedBike.Id);
|
|
|
|
// Stop polling before returning bike.
|
|
BikesViewModel.IsIdle = false;
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StopAsync();
|
|
|
|
// open lock
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOpeningLock;
|
|
ILockService btLock = LockService[SelectedBike.LockInfo.Id];
|
|
try
|
|
{
|
|
SelectedBike.LockInfo.State = (await btLock.OpenAsync())?.GetLockingState() ?? LockingState.UnknownDisconnected;
|
|
Log.ForContext<BookedClosed>().Information("Lock of bike {bikeId} opened successfully.", SelectedBike.Id);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
Log.ForContext<BookedClosed>().Information("Lock of bike {bikeId} can not be opened.", SelectedBike.Id);
|
|
if (exception is OutOfReachException)
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("Lock is out of reach.");
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorLockOutOfReach,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is CouldntOpenBoldIsBlockedException)
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("Bold is blocked.");
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorOpenLockBoldBlocked,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is CouldntOpenBoldStatusIsUnknownException)
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("Bold status is unknown.");
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorOpenLockStatusUnknown,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is CouldntOpenInconsistentStateExecption inconsistentState
|
|
&& inconsistentState.State == LockingState.Closed)
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("Lock reports that it is still closed.");
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
AppResources.ErrorOpenLockStillClosed,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("{@exception}", exception);
|
|
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.ErrorOpenLockTitle,
|
|
exception.Message,
|
|
AppResources.ErrorTryAgain,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
// When bold is blocked lock is still closed even if exception occurs.
|
|
// In all other cases state is supposed to be unknown. Example: Lock is out of reach and no more bluetooth connected.
|
|
SelectedBike.LockInfo.State = exception is StateAwareException stateAwareException
|
|
? stateAwareException.State
|
|
: LockingState.UnknownDisconnected;
|
|
|
|
}
|
|
|
|
// get current charging level
|
|
BikesViewModel.ActionText = AppResources.ActivityTextReadingChargingLevel;
|
|
try
|
|
{
|
|
SelectedBike.LockInfo.BatteryPercentage = await btLock.GetBatteryPercentageAsync();
|
|
Log.ForContext<ReservedClosed>().Information("Battery state of lock from {bikeId} read successfully.", SelectedBike.Id);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<BookedClosed>().Information("Battery state of lock from {bikeId} can not be read. ", SelectedBike.Id);
|
|
if (exception is OutOfReachException)
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("Lock is out of reach.");
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelOutOfReach;
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("{@exception}", exception);
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelGeneral;
|
|
}
|
|
}
|
|
|
|
// get lock infos.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdatingLockingState;
|
|
var versionTdo = btLock.VersionInfo;
|
|
if (versionTdo != null)
|
|
{
|
|
SelectedBike.LockInfo.VersionInfo = new VersionInfo.Builder
|
|
{
|
|
FirmwareVersion = versionTdo.FirmwareVersion,
|
|
HardwareVersion = versionTdo.HardwareVersion,
|
|
LockVersion = versionTdo.LockVersion,
|
|
}.Build();
|
|
}
|
|
|
|
// update backend
|
|
IsConnected = IsConnectedDelegate();
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.UpdateLockingStateAsync(SelectedBike);
|
|
Log.ForContext<ReservedClosed>().Information("Backend updated for bike {bikeId} successfully.", SelectedBike.Id);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<BookedClosed>().Information("Updating backend for bike {bikeId} failed.", SelectedBike.Id);
|
|
if (exception is WebConnectFailureException)
|
|
{
|
|
// No web.
|
|
Log.ForContext<BookedClosed>().Debug("Copri server not reachable. No web.");
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorNoWebUpdateingLockstate;
|
|
}
|
|
else if (exception is ResponseException copriException)
|
|
{
|
|
// Copri exception.
|
|
Log.ForContext<BookedClosed>().Debug("{response}.", copriException.Response);
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorStatusUpdateingLockstate;
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<BookedClosed>().Debug("{@exception}", exception);
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorConnectionUpdateingLockstate;
|
|
}
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartAsync();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
BikesViewModel.RentalProcess.State = CurrentRentalProcess.None;
|
|
return Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
}
|
|
}
|