sharee.bike-App/TINKLib/ViewModel/Bikes/Bike/BluetoothLock/RequestHandler/ReservedClosed.cs

361 lines
14 KiB
C#
Raw Normal View History

2022-09-20 13:51:55 +02:00
using System;
2021-05-13 20:03:07 +02:00
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using Serilog;
using TINK.Model.Bikes.BikeInfoNS.BluetoothLock;
2021-05-13 20:03:07 +02:00
using TINK.Model.Connector;
2022-08-30 15:42:25 +02:00
using TINK.Model.Device;
using TINK.Model.User;
using TINK.MultilingualResources;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Exception;
2021-05-13 20:03:07 +02:00
using TINK.Services.BluetoothLock;
using TINK.Services.BluetoothLock.Exception;
2022-10-26 20:53:18 +02:00
using TINK.Services.CopriApi.Exception;
2022-08-30 15:42:25 +02:00
using TINK.Services.Geolocation;
using TINK.View;
using IBikeInfoMutable = TINK.Model.Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
2023-04-19 12:14:14 +02:00
/// <summary> Bike is reserved, lock is closed and connected to app. </summary>
2022-09-06 16:08:19 +02:00
/// <remarks>
2023-04-19 12:14:14 +02:00
/// Occurs when
/// - bike was reserved out of reach and is in reach now
/// - bike is reserved while in reach
2022-09-06 16:08:19 +02:00
/// </remarks>
public class ReservedClosed : Base, IRequestHandler
{
/// <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 ReservedClosed(
IBikeInfoMutable selectedBike,
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
2023-04-05 15:02:10 +02:00
IGeolocationService geolocation,
2022-09-06 16:08:19 +02:00
ILocksService lockService,
Func<IPollingUpdateTaskManager> viewUpdateManager,
ISmartDevice smartDevice,
IViewService viewService,
IBikesViewModel bikesViewModel,
IUser activeUser) : base(
selectedBike,
2023-11-06 12:23:09 +01:00
AppResources.ActionCancelReservation, // Cancel Reservation
true, // Show button "Cancel Reservation"
2022-09-06 16:08:19 +02:00
isConnectedDelegate,
connectorFactory,
geolocation,
lockService,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser)
{
2023-11-06 12:23:09 +01:00
LockitButtonText = AppResources.ActionOpenLockAndRentBike; // Open Lock & Rent Bike
IsLockitButtonVisible = true; // Show button "Open Lock & Rent Bike"
2022-09-06 16:08:19 +02:00
}
/// <summary> Cancel reservation. </summary>
public async Task<IRequestHandler> HandleRequestOption1() => await CancelReservation();
2023-11-06 12:23:09 +01:00
/// <summary> Open lock and rent bike. </summary>
public async Task<IRequestHandler> HandleRequestOption2() => await OpenLockAndRentBike();
2022-09-06 16:08:19 +02:00
/// <summary> Cancel reservation. </summary>
public async Task<IRequestHandler> CancelReservation()
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("User request to cancel reservation of bike {bikeId}", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.IsIdle = false; // Lock list to avoid multiple taps while copri action is pending.
2023-11-06 12:23:09 +01:00
var result = await ViewService.DisplayAlert(
2022-09-06 16:08:19 +02:00
string.Empty,
string.Format(AppResources.QuestionCancelReservation, SelectedBike.GetFullDisplayName()),
2023-08-31 12:20:06 +02:00
AppResources.MessageAnswerYes,
AppResources.MessageAnswerNo);
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
if (result == false)
2022-09-06 16:08:19 +02:00
{
// User aborted cancel process
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("User canceled request to cancel reservation of bike {bikeId}.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.IsIdle = true;
return this;
}
// Stop polling before cancel request.
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
2023-08-31 12:20:06 +02:00
await ViewUpdateManager().StopAsync();
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextCancelingReservation;
IsConnected = IsConnectedDelegate();
try
{
await ConnectorFactory(IsConnected).Command.DoCancelReservation(SelectedBike);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("User canceled reservation of bike {bikeId} successfully.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
2022-10-26 20:53:18 +02:00
catch (Exception exception)
2022-09-06 16:08:19 +02:00
{
BikesViewModel.ActionText = string.Empty;
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Canceling reservation of bike {bikeId} failed.", SelectedBike.Id);
2022-10-26 20:53:18 +02:00
if (exception is InvalidAuthorizationResponseException)
2022-09-06 16:08:19 +02:00
{
// Copri response is invalid.
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Error("Invalid auth. response.");
2022-09-06 16:08:19 +02:00
await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorCancelReservationTitle,
AppResources.ErrorAccountInvalidAuthorization,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
2022-10-26 20:53:18 +02:00
else if (exception is WebConnectFailureException
|| exception is RequestNotCachableException)
2022-09-06 16:08:19 +02:00
{
// Copri server is not reachable.
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Error("Copri server not reachable.");
2022-09-06 16:08:19 +02:00
await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorCancelReservationTitle,
AppResources.ErrorNoWeb,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Error("{@exception}", exception);
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAdvancedAlert(
AppResources.ErrorCancelReservationTitle,
2022-10-26 20:53:18 +02:00
exception.Message,
2023-08-31 12:20:06 +02:00
AppResources.ErrorTryAgain,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
2023-08-31 12:20:06 +02:00
await ViewUpdateManager().StartAsync(); // Restart polling again.
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true; // Unlock GUI
2023-04-05 15:02:10 +02:00
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
2022-09-06 16:08:19 +02:00
}
// Disconnect lock.
BikesViewModel.ActionText = AppResources.ActivityTextDisconnectingLock;
try
{
SelectedBike.LockInfo.State = await LockService.DisconnectAsync(SelectedBike.LockInfo.Id, SelectedBike.LockInfo.Guid);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Lock from bike {bikeId} disconnected successfully.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
2023-11-06 12:23:09 +01:00
catch (Exception disconnectException)
2022-09-06 16:08:19 +02:00
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Lock from bike {bikeId} can not be disconnected. {@exception}", SelectedBike.Id, disconnectException);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorDisconnect;
}
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
2023-08-31 12:20:06 +02:00
await ViewUpdateManager().StartAsync(); // Restart polling again.
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true; // Unlock GUI
2023-04-05 15:02:10 +02:00
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
2022-09-06 16:08:19 +02:00
}
2023-11-06 12:23:09 +01:00
/// <summary> Open lock and rent bike. </summary>
public async Task<IRequestHandler> OpenLockAndRentBike()
2022-09-06 16:08:19 +02:00
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("User request to book and open bike {bikeId}.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.IsIdle = false;
// Ask whether to really book bike?
var alertResult = await ViewService.DisplayAlert(
string.Empty,
string.Format(AppResources.QuestionOpenLockAndBookBike, SelectedBike.GetFullDisplayName()),
AppResources.MessageAnswerYes,
AppResources.MessageAnswerNo);
if (alertResult == false)
{
// User aborted booking process
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("User canceled request to book bike {bikeId}.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.IsIdle = true;
return this;
}
// Stop polling before cancel request.
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
2023-08-31 12:20:06 +02:00
await ViewUpdateManager().StopAsync();
2022-09-06 16:08:19 +02:00
// Book bike prior to opening lock.
BikesViewModel.ActionText = AppResources.ActivityTextRentingBike;
IsConnected = IsConnectedDelegate();
try
{
2022-12-27 21:08:09 +01:00
await ConnectorFactory(IsConnected).Command.DoBookAsync(SelectedBike, LockingAction.Open);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("User booked bike {bikeId} successfully.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
2023-11-06 12:23:09 +01:00
catch (Exception exception)
2022-09-06 16:08:19 +02:00
{
BikesViewModel.ActionText = string.Empty;
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Booking of bike {bikeId} failed.", SelectedBike.Id);
if (exception is WebConnectFailureException)
2022-09-06 16:08:19 +02:00
{
// Copri server is not reachable.
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Error("Copri server not reachable.");
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAlert(
AppResources.ErrorRentingBikeTitle,
AppResources.ErrorNoWeb,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Error("{@exception}", exception);
2022-09-06 16:08:19 +02:00
await ViewService.DisplayAdvancedAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorRentingBikeTitle,
AppResources.ErrorTryAgain,
2023-11-06 12:23:09 +01:00
exception.Message,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
2023-08-31 12:20:06 +02:00
await ViewUpdateManager().StartAsync(); // Restart polling again.
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true; // Unlock GUI
2023-04-05 15:02:10 +02:00
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
2022-09-06 16:08:19 +02:00
}
// Unlock bike.
BikesViewModel.ActionText = AppResources.ActivityTextOpeningLock;
2023-11-06 12:23:09 +01:00
ILockService btLock = LockService[SelectedBike.LockInfo.Id];
2022-09-06 16:08:19 +02:00
try
{
2022-09-20 13:51:55 +02:00
SelectedBike.LockInfo.State = (await btLock.OpenAsync())?.GetLockingState() ?? LockingState.UnknownDisconnected;
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Lock from {bikeId} opened successfully.",SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Lock from {bikeId} can not be opened.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = string.Empty;
if (exception is OutOfReachException)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("Lock is out of reach.");
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAdvancedAlert(
2022-09-06 16:08:19 +02:00
AppResources.ErrorOpenLockTitle,
2023-08-31 12:20:06 +02:00
AppResources.ErrorLockOutOfReach,
AppResources.ErrorOpenLockBikeAlreadyBooked,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
else if (exception is CouldntOpenBoldIsBlockedException)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("Bold is blocked.");
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAdvancedAlert(
2022-09-06 16:08:19 +02:00
AppResources.ErrorOpenLockTitle,
2023-08-31 12:20:06 +02:00
AppResources.ErrorOpenLockBoldBlocked,
AppResources.ErrorOpenLockBikeAlreadyBooked,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
2023-02-22 14:03:35 +01:00
else if (exception is CouldntOpenBoldStatusIsUnknownException)
2022-09-06 16:08:19 +02:00
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("Bold status is unknown.");
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAdvancedAlert(
AppResources.ErrorOpenLockTitle,
AppResources.ErrorOpenLockStatusUnknown,
AppResources.ErrorOpenLockBikeAlreadyBooked,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
else if (exception is CouldntOpenInconsistentStateExecption inconsistentState
&& inconsistentState.State == LockingState.Closed)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("Lock reports that it is still closed.");
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAdvancedAlert(
2022-09-06 16:08:19 +02:00
AppResources.ErrorOpenLockTitle,
2023-08-31 12:20:06 +02:00
AppResources.ErrorOpenLockStillClosed,
AppResources.ErrorOpenLockBikeAlreadyBooked,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("{@exception}", exception);
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAdvancedAlert(
2022-09-06 16:08:19 +02:00
AppResources.ErrorOpenLockTitle,
2023-08-31 12:20:06 +02:00
AppResources.ErrorTryAgain,
2022-09-06 16:08:19 +02:00
exception.Message,
AppResources.MessageAnswerOk);
}
SelectedBike.LockInfo.State = exception is StateAwareException stateAwareException
? stateAwareException.State
: LockingState.UnknownDisconnected;
}
2023-11-06 12:23:09 +01:00
// get current charging level
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextReadingChargingLevel;
try
{
2022-09-20 13:51:55 +02:00
SelectedBike.LockInfo.BatteryPercentage = await btLock.GetBatteryPercentageAsync();
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Battery state of lock from {bikeId} read successfully.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Battery state of lock from {bikeId} can not be read.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
if (exception is OutOfReachException)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("Lock is out of reach.");
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelOutOfReach;
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("{@exception}", exception);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelGeneral;
}
}
2023-11-06 12:23:09 +01:00
// get lock infos.
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdatingLockingState;
2022-09-20 13:51:55 +02:00
var versionTdo = btLock.VersionInfo;
if (versionTdo != null)
{
SelectedBike.LockInfo.VersionInfo = new VersionInfo.Builder
{
FirmwareVersion = versionTdo.FirmwareVersion,
HardwareVersion = versionTdo.HardwareVersion,
LockVersion = versionTdo.LockVersion,
}.Build();
}
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
// update backend
IsConnected = IsConnectedDelegate();
2022-09-06 16:08:19 +02:00
try
{
await ConnectorFactory(IsConnected).Command.UpdateLockingStateAsync(SelectedBike);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Backend updated for bike {bikeId} successfully.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Information("Updating backend for bike {bikeId} failed.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
if (exception is WebConnectFailureException)
{
2023-11-06 12:23:09 +01:00
// No web.
Log.ForContext<ReservedClosed>().Debug("Copri server not reachable. No web.");
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorNoWebUpdateingLockstate;
}
else if (exception is ResponseException copriException)
{
2023-11-06 12:23:09 +01:00
// Copri exception.
Log.ForContext<ReservedClosed>().Debug("{response}", copriException.Response);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorStatusUpdateingLockstate;
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedClosed>().Debug("{@exception}", exception);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorConnectionUpdateingLockstate;
}
}
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
2023-08-31 12:20:06 +02:00
await ViewUpdateManager().StartAsync(); // Restart polling again.
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true; // Unlock GUI
2023-04-05 15:02:10 +02:00
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 20:03:07 +02:00
}