mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-12-24 08:06:28 +01:00
246 lines
8.5 KiB
C#
246 lines
8.5 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.BluetoothLock.Tdo;
|
|
using TINK.Services.CopriApi.Exception;
|
|
using TINK.Services.Geolocation;
|
|
using TINK.View;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
|
|
{
|
|
public class BookedDisconnected : 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 BookedDisconnected(
|
|
IBikeInfoMutable selectedBike,
|
|
Func<bool> isConnectedDelegate,
|
|
Func<bool, IConnector> connectorFactory,
|
|
IGeolocation geolocation,
|
|
ILocksService lockService,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
ISmartDevice smartDevice,
|
|
IViewService viewService,
|
|
IBikesViewModel bikesViewModel,
|
|
IUser activeUser) :
|
|
base(
|
|
selectedBike,
|
|
nameof(BookedDisconnected),
|
|
false,
|
|
isConnectedDelegate,
|
|
connectorFactory,
|
|
geolocation,
|
|
lockService,
|
|
viewUpdateManager,
|
|
smartDevice,
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser)
|
|
{
|
|
LockitButtonText = AppResources.ActionSearchLock;
|
|
IsLockitButtonVisible = true;
|
|
}
|
|
|
|
public async Task<IRequestHandler> HandleRequestOption1() => await UnsupportedRequest();
|
|
|
|
/// <summary> Scan for lock.</summary>
|
|
/// <returns></returns>
|
|
public async Task<IRequestHandler> HandleRequestOption2() => await ConnectLock();
|
|
|
|
/// <summary> Requst is not supported, button should be disabled. </summary>
|
|
/// <returns></returns>
|
|
public async Task<IRequestHandler> UnsupportedRequest()
|
|
{
|
|
Log.ForContext<BookedDisconnected>().Error("Click of unsupported button click detected.");
|
|
return await Task.FromResult<IRequestHandler>(this);
|
|
}
|
|
|
|
/// <summary> Scan for lock.</summary>
|
|
/// <returns></returns>
|
|
public async Task<IRequestHandler> ConnectLock()
|
|
{
|
|
// Lock list to avoid multiple taps while copri action is pending.
|
|
BikesViewModel.IsIdle = false;
|
|
Log.ForContext<BookedDisconnected>().Information("Request to search {bike} detected.", SelectedBike);
|
|
|
|
// Stop polling before getting new auth-values.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StopUpdatePeridically();
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextQuerryServer;
|
|
IsConnected = IsConnectedDelegate();
|
|
try
|
|
{
|
|
// Repeat booking to get a new seed/ k_user value.
|
|
await ConnectorFactory(IsConnected).Command.CalculateAuthKeys(SelectedBike);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
|
|
if (exception is WebConnectFailureException
|
|
|| exception is RequestNotCachableException)
|
|
{
|
|
// Copri server is not reachable.
|
|
Log.ForContext<BookedDisconnected>().Information("User selected booked bike {l_oId} to connect to lock. (Copri server not reachable).", SelectedBike.Id);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
$"{AppResources.ErrorConnectLockRentedBikeNoWebMessage}\r\n{exception.Message}\r\n{WebConnectFailureException.GetHintToPossibleExceptionsReasons}",
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<BookedDisconnected>().Error("User selected booked bike {l_oId} to connect to lock. {@l_oException}", SelectedBike.Id, exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
$"{AppResources.ErrorConnectLockGeneralErrorMessage}\r\n{exception.Message}",
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
// Restart polling again.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
return this;
|
|
}
|
|
|
|
LockInfoTdo result = null;
|
|
var continueConnect = true;
|
|
var retryCount = 1;
|
|
while (continueConnect && result == null)
|
|
{
|
|
BikesViewModel.ActionText = AppResources.ActivityTextSearchingLock;
|
|
|
|
try
|
|
{
|
|
result = await LockService.ConnectAsync(
|
|
new LockInfoAuthTdo.Builder { Id = SelectedBike.LockInfo.Id, Guid = SelectedBike.LockInfo.Guid, K_seed = SelectedBike.LockInfo.Seed, K_u = SelectedBike.LockInfo.UserKey }.Build(),
|
|
LockService.TimeOut.GetSingleConnect(retryCount));
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
if (exception is ConnectBluetoothNotOnException)
|
|
{
|
|
continueConnect = false;
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
AppResources.ErrorFindLockBluetoothNotOn,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is ConnectLocationPermissionMissingException)
|
|
{
|
|
continueConnect = false;
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
AppResources.ErrorFindLockLocationPermissionMissing,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is ConnectLocationOffException)
|
|
{
|
|
continueConnect = false;
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
AppResources.ErrorFindLockLocationOff,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is OutOfReachException)
|
|
{
|
|
Log.ForContext<BookedDisconnected>().Debug("Lock can not be found because out of reach. {Exception}", exception);
|
|
|
|
continueConnect = await ViewService.DisplayAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
AppResources.ErrorFindLockRentedBikeOutOfReachMessage,
|
|
AppResources.MessageAnswerRetry,
|
|
AppResources.MessageAnswerCancel);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<BookedDisconnected>().Error("Lock can not be found. {Exception}", exception);
|
|
|
|
string message;
|
|
if (retryCount < 2)
|
|
{
|
|
message = AppResources.ErrorBookedSearchMessage;
|
|
}
|
|
else if (retryCount < 3)
|
|
{
|
|
message = AppResources.ErrorBookedSearchMessageEscalationLevel1;
|
|
}
|
|
else
|
|
{
|
|
message = AppResources.ErrorBookedSearchMessageEscalationLevel2;
|
|
}
|
|
|
|
|
|
continueConnect = await ViewService.DisplayAdvancedAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
message,
|
|
"", // bool IsReportLevelVerbose ? exception.Message : string.Empty, // or use ActiveUser.DebugLevel.HasFlag(Permissions.ReportLevel) instead?
|
|
AppResources.MessageAnswerRetry,
|
|
AppResources.MessageAnswerCancel);
|
|
}
|
|
|
|
if (continueConnect)
|
|
{
|
|
retryCount++;
|
|
continue;
|
|
}
|
|
|
|
// Quit and restart polling again.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
if (result?.State == null)
|
|
{
|
|
Log.ForContext<BookedDisconnected>().Information("Lock for bike {bike} not found.", SelectedBike);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageConnectLockErrorTitle,
|
|
$"Schlossstatus des gemieteten Rads konnte nicht ermittelt werden.",
|
|
AppResources.MessageAnswerOk);
|
|
|
|
// Restart polling again.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
return this;
|
|
}
|
|
|
|
var state = result.State.Value.GetLockingState();
|
|
SelectedBike.LockInfo.State = state;
|
|
SelectedBike.LockInfo.Guid = result?.Guid ?? new Guid();
|
|
|
|
Log.ForContext<BookedDisconnected>().Information($"State for bike {SelectedBike.Id} updated successfully. Value is {SelectedBike.LockInfo.State}.");
|
|
|
|
// Restart polling again.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically();
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true;
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
}
|
|
}
|