2023-04-19 12:14:14 +02:00
|
|
|
using System;
|
2022-04-25 22:15:15 +02:00
|
|
|
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
|
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
using IRequestHandler = BluetoothLock.IRequestHandler;
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public class BookedClosed : 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 BookedClosed(
|
|
|
|
IBikeInfoMutable selectedBike,
|
|
|
|
Func<bool> isConnectedDelegate,
|
|
|
|
Func<bool, IConnector> connectorFactory,
|
|
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
|
|
ISmartDevice smartDevice,
|
|
|
|
IViewService viewService,
|
|
|
|
IBikesViewModel bikesViewModel,
|
|
|
|
IUser activeUser) : base(
|
|
|
|
selectedBike,
|
|
|
|
nameof(BookedClosed),
|
|
|
|
false, // Lock can only be closed manually and returning is performed by placing bike into the station.
|
|
|
|
isConnectedDelegate,
|
|
|
|
connectorFactory,
|
|
|
|
viewUpdateManager,
|
|
|
|
smartDevice,
|
|
|
|
viewService,
|
|
|
|
bikesViewModel,
|
|
|
|
activeUser)
|
|
|
|
{
|
|
|
|
LockitButtonText = AppResources.ActionOpenAndPause;
|
|
|
|
IsLockitButtonVisible = true; // Show button to enable opening lock in case user took a pause and does not want to return the bike.
|
|
|
|
}
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Return bike. </summary>
|
|
|
|
public async Task<IRequestHandler> HandleRequestOption1() => await UnsupportedRequest();
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Open bike and update COPRI lock state. </summary>
|
|
|
|
public async Task<IRequestHandler> HandleRequestOption2() => await OpenLock();
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2023-04-19 12:14:14 +02:00
|
|
|
/// <summary> Request is not supported, button should be disabled. </summary>
|
2022-09-06 16:08:19 +02:00
|
|
|
public async Task<IRequestHandler> UnsupportedRequest()
|
|
|
|
{
|
|
|
|
Log.ForContext<BookedClosed>().Error("Click of unsupported button click detected.");
|
|
|
|
return await Task.FromResult<IRequestHandler>(this);
|
|
|
|
}
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Open bike. </summary>
|
|
|
|
public async Task<IRequestHandler> OpenLock()
|
|
|
|
{
|
|
|
|
// Unlock bike.
|
|
|
|
Log.ForContext<BookedClosed>().Information("User request to unlock bike {bike}.", SelectedBike);
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
// Stop polling before returning bike.
|
|
|
|
BikesViewModel.IsIdle = false;
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
2023-08-31 12:20:06 +02:00
|
|
|
await ViewUpdateManager().StopAsync();
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOpeningLock;
|
|
|
|
IsConnected = IsConnectedDelegate();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await ConnectorFactory(IsConnected).Command.OpenLockAsync(SelectedBike);
|
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
if (exception is WebConnectFailureException)
|
|
|
|
{
|
|
|
|
// Copri server is not reachable.
|
|
|
|
Log.ForContext<BookedClosed>().Information("User selected bike {id} but opening lock failed (Copri server not reachable).", SelectedBike.Id);
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2023-08-31 12:20:06 +02:00
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.ErrorNoConnectionTitle,
|
|
|
|
AppResources.ErrorNoWeb,
|
2022-09-06 16:08:19 +02:00
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.ForContext<BookedClosed>().Error("Lock can not be opened. {Exception}", exception);
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.ErrorOpenLockTitle,
|
|
|
|
exception.Message,
|
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
}
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
2023-08-31 12:20:06 +02:00
|
|
|
await ViewUpdateManager().StartAsync();
|
2022-09-06 16:08:19 +02:00
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
|
|
BikesViewModel.IsIdle = true;
|
|
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
|
|
}
|
2022-04-25 22:15:15 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
Log.ForContext<BookedClosed>().Information("User paused ride using {bike} successfully.", SelectedBike);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
2023-08-31 12:20:06 +02:00
|
|
|
await ViewUpdateManager().StartAsync();
|
2022-09-06 16:08:19 +02:00
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
|
|
BikesViewModel.IsIdle = true;
|
|
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
|
|
}
|
|
|
|
}
|
2022-04-25 22:15:15 +02:00
|
|
|
}
|