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

545 lines
21 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;
using TINK.Services.BluetoothLock.Tdo;
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;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
2022-09-06 16:08:19 +02:00
public class ReservedDisconnected : 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 ReservedDisconnected(
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,
true, // Show button "Cancel Reservation".
2022-09-06 16:08:19 +02:00
isConnectedDelegate,
connectorFactory,
geolocation,
lockService,
viewUpdateManager,
smartDevice,
viewService,
bikesViewModel,
activeUser)
{
LockitButtonText = AppResources.ActionSearchLock;
2023-11-06 12:23:09 +01:00
IsLockitButtonVisible = true; // Show button "Search Lock"
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> Connect to reserved bike ask whether to rent bike or not and if yes open lock. </summary>
2022-09-06 16:08:19 +02:00
/// <returns></returns>
2023-11-06 12:23:09 +01:00
public async Task<IRequestHandler> HandleRequestOption2() => await ConnectLockAndRentBike();
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<ReservedDisconnected>().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.
var alertResult = await ViewService.DisplayAlert(
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
if (alertResult == false)
{
// User aborted cancel process
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().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<ReservedDisconnected>().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
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Information("User selected reserved bike {bikeId} but cancel reservation failed.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = string.Empty;
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<ReservedDisconnected>().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<ReservedDisconnected>().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<ReservedDisconnected>().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,
AppResources.MessageAnswerOk);
2022-09-06 16:08:19 +02:00
}
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
}
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> Connect to reserved bike ask whether to rent bike or not and if yes open lock. </summary>
2022-09-06 16:08:19 +02:00
/// <returns></returns>
2023-11-06 12:23:09 +01:00
public async Task<IRequestHandler> ConnectLockAndRentBike()
2022-09-06 16:08:19 +02:00
{
BikesViewModel.IsIdle = false;
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Information("User request to connect to lock of bike {bikeId}.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
// Stop polling before getting new auth-values.
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.ActivityTextQuerryServer;
IsConnected = IsConnectedDelegate();
try
{
// Repeat reservation to get a new seed/ k_user value.
await ConnectorFactory(IsConnected).Command.CalculateAuthKeys(SelectedBike);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Information("Calculation of AuthKeys successfully.");
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
BikesViewModel.ActionText = string.Empty;
2022-10-26 20:53:18 +02:00
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<ReservedDisconnected>().Information("Calculation of AuthKeys failed (Copri server not reachable).");
2022-09-06 16:08:19 +02:00
await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorNoConnectionTitle,
AppResources.ErrorNoWeb,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Error("Calculation of AuthKeys failed. {@exception}", exception);
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAdvancedAlert(
AppResources.ErrorConnectLockTitle,
exception.Message,
AppResources.ErrorConnectLock,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
}
// Restart polling again.
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 this;
}
// Connect to lock.
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));
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Information("Connected to lock of bike {bikeId} successfully. Value is {lockState}.", SelectedBike.Id, SelectedBike.LockInfo.State);
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
BikesViewModel.ActionText = string.Empty;
if (exception is ConnectBluetoothNotOnException)
{
continueConnect = false;
await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorConnectLockTitle,
AppResources.ErrorLockBluetoothNotOn,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Debug("Connection to lock of bike {bikeId} failed, bluetooth is off.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
else if (exception is ConnectLocationPermissionMissingException)
{
continueConnect = false;
await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorConnectLockTitle,
AppResources.ErrorNoLocationPermission,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Debug("Connection to lock of bike {bikeId} failed, location permissions are missing.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
else if (exception is ConnectLocationOffException)
{
continueConnect = false;
await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorConnectLockTitle,
AppResources.ErrorLockLocationOff,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Debug("Connection to lock of bike {bikeId} failed, location services are off.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
else if (exception is OutOfReachException)
{
2023-08-31 12:20:06 +02:00
continueConnect = false;
2022-09-06 16:08:19 +02:00
2023-08-31 12:20:06 +02:00
await ViewService.DisplayAlert(
AppResources.ErrorConnectLockTitle,
AppResources.ErrorLockOutOfReach,
AppResources.MessageAnswerOk);
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Debug("Connection to lock of bike {bikeId} failed, lock is out of reach.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Error("Lock of bike {bikeId} can not be found. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
string message;
if (retryCount < 2)
{
2023-08-31 12:20:06 +02:00
message = AppResources.ErrorConnectLock;
2022-09-06 16:08:19 +02:00
}
else
{
2023-08-31 12:20:06 +02:00
message = AppResources.ErrorConnectLockEscalationLevel1;
2022-09-06 16:08:19 +02:00
}
Log.ForContext<ReservedDisconnected>().Error("Lock state can not be retrieved. {Exception}", exception);
continueConnect = await ViewService.DisplayAdvancedAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorConnectLockTitle,
2022-09-06 16:08:19 +02:00
message,
2023-08-31 12:20:06 +02:00
"", // Might show detailed info in future versions. Property used earlier: IsReportLevelVerbose. Maybe use ActiveUser.DebugLevel.HasFlag(Permissions.ReportLevel) instead.
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerRetry,
AppResources.MessageAnswerCancel);
}
if (continueConnect)
{
retryCount++;
continue;
}
// Restart polling again.
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 this;
}
}
2023-08-31 12:20:06 +02:00
if (!(result?.State is LockitLockingState lockingState))
2022-09-06 16:08:19 +02:00
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Information("Lock for bike {bikeId} not found.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = string.Empty;
await ViewService.DisplayAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorConnectLockTitle,
AppResources.ErrorLockNoStatus,
2022-09-06 16:08:19 +02:00
AppResources.MessageAnswerOk);
// Restart polling again.
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 this;
}
2023-11-06 12:23:09 +01:00
// get current locking state
var state = lockingState.GetLockingState();
SelectedBike.LockInfo.State = state;
2022-09-06 16:08:19 +02:00
SelectedBike.LockInfo.Guid = result?.Guid ?? new Guid();
// Ask whether to really book bike?
2023-11-06 12:23:09 +01:00
var alertResult
= SelectedBike.LockInfo.State != LockingState.Open
? await ViewService.DisplayAlert(
string.Empty,
string.Format(AppResources.QuestionOpenLockAndBookBike, SelectedBike.GetFullDisplayName()),
AppResources.MessageAnswerYes,
AppResources.MessageAnswerNo)
: await ViewService.DisplayAlert(
string.Empty,
string.Format(AppResources.QuestionBookBike, SelectedBike.GetFullDisplayName()),
AppResources.MessageAnswerYes,
AppResources.MessageAnswerNo);
2022-09-06 16:08:19 +02:00
if (alertResult == false)
{
// User aborted booking process
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Information("User denied to book reserved bike {bikeId}.", SelectedBike.Id);
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<ReservedDisconnected>().Information("Lock of bike {bikeId} disconnected successfully.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Error("Lock of bike {bikeId} can not be disconnected. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorDisconnect;
}
// Restart polling again.
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;
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
}
// Book bike prior to opening lock.
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Information("User request to book reserved bike {bikeId}.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextRentingBike;
IsConnected = IsConnectedDelegate();
try
{
2023-11-06 12:23:09 +01:00
if (SelectedBike.LockInfo.State != LockingState.Open)
{
await ConnectorFactory(IsConnected).Command.DoBookAsync(SelectedBike, LockingAction.Open);
}
else
{
await ConnectorFactory(IsConnected).Command.DoBookAsync(SelectedBike);
}
Log.ForContext<ReservedDisconnected>().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
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<ReservedDisconnected>().Information("Booking of bike {bikeId} failed (Copri server not reachable).", SelectedBike.Id);
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<ReservedDisconnected>().Error("Booking of bike {bikeId} failed. {@exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
await ViewService.DisplayAdvancedAlert(
2023-08-31 12:20:06 +02:00
AppResources.ErrorRentingBikeTitle,
2023-11-06 12:23:09 +01: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
}
// Unlock bike.
2023-11-06 12:23:09 +01:00
ILockService btLock = LockService[SelectedBike.LockInfo.Id];
if (SelectedBike.LockInfo.State != LockingState.Open)
2022-09-06 16:08:19 +02:00
{
2023-11-06 12:23:09 +01:00
BikesViewModel.ActionText = AppResources.ActivityTextOpeningLock;
try
2022-09-06 16:08:19 +02:00
{
2023-11-06 12:23:09 +01:00
SelectedBike.LockInfo.State = (await btLock.OpenAsync())?.GetLockingState() ?? LockingState.UnknownDisconnected;
Log.ForContext<ReservedDisconnected>().Debug("Lock from {bikeId} opened 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
{
2023-11-06 12:23:09 +01:00
BikesViewModel.ActionText = string.Empty;
if (exception is OutOfReachException)
{
Log.ForContext<ReservedDisconnected>().Debug("Lock from {bikeId} can not be opened. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
await ViewService.DisplayAlert(
AppResources.ErrorOpenLockTitle,
AppResources.ErrorLockOutOfReach,
AppResources.MessageAnswerOk);
}
else if (exception is CouldntOpenBoldIsBlockedException)
{
Log.ForContext<ReservedDisconnected>().Debug("Lock from {bikeId} can not be opened. Bold is blocked. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
await ViewService.DisplayAlert(
AppResources.ErrorOpenLockTitle,
AppResources.ErrorOpenLockBoldBlocked,
AppResources.MessageAnswerOk);
}
else if (exception is CouldntOpenBoldStatusIsUnknownException)
{
Log.ForContext<ReservedDisconnected>().Debug("Lock from {bikeId} can not be opened. Bold status is unknown. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
await ViewService.DisplayAlert(
AppResources.ErrorOpenLockTitle,
AppResources.ErrorOpenLockStatusUnknown,
AppResources.MessageAnswerOk);
}
else if (exception is CouldntOpenInconsistentStateExecption inconsistentState
&& inconsistentState.State == LockingState.Closed)
{
Log.ForContext<ReservedDisconnected>().Debug("Lock from {bikeId} can not be opened. lock reports state closed. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
await ViewService.DisplayAlert(
AppResources.ErrorOpenLockTitle,
AppResources.ErrorOpenLockStillClosed,
AppResources.MessageAnswerOk);
}
else
{
Log.ForContext<ReservedDisconnected>().Error("Lock from {bikeId} can not be opened. {Exception}", SelectedBike.Id, exception);
await ViewService.DisplayAdvancedAlert(
AppResources.ErrorOpenLockTitle,
exception.Message,
AppResources.ErrorTryAgain,
AppResources.MessageAnswerOk);
}
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
SelectedBike.LockInfo.State = exception is StateAwareException stateAwareException
? stateAwareException.State
: LockingState.UnknownDisconnected;
2022-09-06 16:08:19 +02:00
2023-11-06 12:23:09 +01:00
}
2022-09-06 16:08:19 +02:00
}
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<ReservedDisconnected>().Information("Battery state of lock from {bikeId} read successfully.", SelectedBike.Id);
2022-09-06 16:08:19 +02:00
}
catch (Exception exception)
{
if (exception is OutOfReachException)
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Debug("Battery state of lock from {bikeId} can not be read, bike out of range. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelOutOfReach;
}
else
{
2023-11-06 12:23:09 +01:00
Log.ForContext<ReservedDisconnected>().Error("Battery state of lock from {bikeId} can not be read. {Exception}", SelectedBike.Id, exception);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelGeneral;
}
}
// Lock list to avoid multiple taps while copri action is pending.
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<ReservedDisconnected>().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<ReservedDisconnected>().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<ReservedDisconnected>().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<ReservedDisconnected>().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<ReservedDisconnected>().Debug("{@exception}", exception);
2022-09-06 16:08:19 +02:00
BikesViewModel.ActionText = AppResources.ActivityTextErrorConnectionUpdateingLockstate;
}
}
// Restart polling again.
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; // 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
}