sharee.bike-App/TINKLib/ViewModel/Bikes/Bike/CopriLock/RequestHandler/BookedOpen.cs

116 lines
5.2 KiB
C#
Raw Normal View History

2022-04-25 22:15:15 +02:00
using System;
using System.Threading.Tasks;
using Serilog;
2022-08-30 15:42:25 +02:00
using TINK.Model.Bikes.BikeInfoNS.CopriLock;
using TINK.Model.Connector;
2022-04-25 22:15:15 +02:00
using TINK.Model.Device;
2022-08-30 15:42:25 +02:00
using TINK.Model.User;
using TINK.MultilingualResources;
using TINK.Repository.Exception;
using TINK.View;
2022-04-25 22:15:15 +02:00
namespace TINK.ViewModel.Bikes.Bike.CopriLock.RequestHandler
{
using IRequestHandler = BluetoothLock.IRequestHandler;
public class BookedOpen : 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 BookedOpen(
IBikeInfoMutable selectedBike,
Func<bool> isConnectedDelegate,
Func<bool, IConnector> connectorFactory,
Func<IPollingUpdateTaskManager> viewUpdateManager,
ISmartDevice smartDevice,
IViewService viewService,
IBikesViewModel bikesViewModel,
IUser activeUser) : base(
selectedBike,
2022-08-30 15:42:25 +02:00
nameof(BookedOpen),
false, // Lock can only be closed manually and returning is performed by placing bike into the station.
2022-04-25 22:15:15 +02:00
isConnectedDelegate,
connectorFactory,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser)
{
2022-08-30 15:42:25 +02:00
LockitButtonText = AppResources.ActionOpenAndPause; // Lock is open but show button anyway to be less prone to errors.
IsLockitButtonVisible = true;
2022-04-25 22:15:15 +02:00
}
/// <summary> Close lock and return bike.</summary>
2022-08-30 15:42:25 +02:00
public async Task<IRequestHandler> HandleRequestOption1() => await UnsupportedRequest();
2022-04-25 22:15:15 +02:00
/// <summary> Close lock in order to pause ride and update COPRI lock state.</summary>
2022-08-30 15:42:25 +02:00
public async Task<IRequestHandler> HandleRequestOption2() => await OpenLock();
2022-04-25 22:15:15 +02:00
2022-08-30 15:42:25 +02:00
/// <summary> Requst is not supported, button should be disabled. </summary>
public async Task<IRequestHandler> UnsupportedRequest()
2022-04-25 22:15:15 +02:00
{
2022-08-30 15:42:25 +02:00
Log.ForContext<BookedOpen>().Error("Click of unsupported button click detected.");
return await Task.FromResult<IRequestHandler>(this);
}
2022-04-25 22:15:15 +02:00
2022-08-30 15:42:25 +02:00
/// <summary> Open bike. </summary>
public async Task<IRequestHandler> OpenLock()
{
2022-04-25 22:15:15 +02:00
// Unlock bike.
2022-08-30 15:42:25 +02:00
Log.ForContext<BookedOpen>().Information("User request to unlock bike {bike}. For locking state {state} this request is unexpected.", SelectedBike, SelectedBike?.LockInfo?.State);
2022-04-25 22:15:15 +02:00
// Stop polling before returning bike.
2022-08-30 15:42:25 +02:00
BikesViewModel.IsIdle = false;
2022-04-25 22:15:15 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
await ViewUpdateManager().StopUpdatePeridically();
2022-08-30 15:42:25 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextOpeningLock;
2022-04-25 22:15:15 +02:00
IsConnected = IsConnectedDelegate();
try
{
2022-08-30 15:42:25 +02:00
await ConnectorFactory(IsConnected).Command.OpenLockAsync(SelectedBike);
2022-04-25 22:15:15 +02:00
}
catch (Exception exception)
{
BikesViewModel.ActionText = string.Empty;
2022-08-30 15:42:25 +02:00
if (exception is WebConnectFailureException)
2022-04-25 22:15:15 +02:00
{
// Copri server is not reachable.
2022-08-30 15:42:25 +02:00
Log.ForContext<BookedOpen>().Information("User selected bike {id} but opening lock failed (Copri server not reachable).", SelectedBike.Id);
2022-04-25 22:15:15 +02:00
await ViewService.DisplayAdvancedAlert(
2022-08-30 15:42:25 +02:00
AppResources.MessageOpeningLockErrorConnectionTitle,
WebConnectFailureException.GetHintToPossibleExceptionsReasons,
2022-04-25 22:15:15 +02:00
exception.Message,
AppResources.MessageAnswerOk);
}
else
{
2022-08-30 15:42:25 +02:00
Log.ForContext<BookedOpen>().Error("Lock can not be opened. {Exception}", exception);
2022-04-25 22:15:15 +02:00
await ViewService.DisplayAlert(
2022-08-30 15:42:25 +02:00
AppResources.ErrorOpenLockTitle,
2022-04-25 22:15:15 +02:00
exception.Message,
AppResources.MessageAnswerOk);
}
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
await ViewUpdateManager().StartUpdateAyncPeridically();
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true;
2022-08-30 15:42:25 +02:00
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
2022-04-25 22:15:15 +02:00
}
Log.ForContext<BookedOpen>().Information("User paused ride using {bike} successfully.", SelectedBike);
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
await ViewUpdateManager().StartUpdateAyncPeridically();
BikesViewModel.ActionText = string.Empty;
BikesViewModel.IsIdle = true;
2022-08-30 15:42:25 +02:00
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
2022-04-25 22:15:15 +02:00
}
}
}