2022-09-20 13:51:55 +02:00
|
|
|
using System;
|
2022-08-30 15:42:25 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
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-08-30 15:42:25 +02:00
|
|
|
using TINK.Services.Geolocation;
|
|
|
|
using TINK.View;
|
2023-08-31 12:20:06 +02:00
|
|
|
using static TINK.Model.Bikes.BikeInfoNS.BluetoothLock.Command.CloseCommand;
|
|
|
|
using static TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandlerFactory;
|
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 BookedUnknown : 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 BookedUnknown(
|
|
|
|
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.ActionOpenLock, // Open Lock
|
|
|
|
true, // Show button "Open Lock"
|
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.ActionCloseLock; // Close Lock
|
|
|
|
IsLockitButtonVisible = true; // Show button "Close Lock"
|
2023-08-31 12:20:06 +02:00
|
|
|
|
|
|
|
_closeLockActionViewModel = new CloseLockActionViewModel<BookedOpen>(
|
|
|
|
selectedBike,
|
|
|
|
viewUpdateManager,
|
|
|
|
viewService,
|
|
|
|
bikesViewModel);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
2023-08-31 12:20:06 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Holds the view model for close action.
|
|
|
|
/// </summary>
|
|
|
|
private CloseLockActionViewModel<BookedOpen> _closeLockActionViewModel;
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Open bike and update COPRI lock state. </summary>
|
|
|
|
public async Task<IRequestHandler> HandleRequestOption1() => await OpenLock();
|
|
|
|
|
|
|
|
/// <summary> Close lock in order to pause ride and update COPRI lock state.</summary>
|
2023-08-31 12:20:06 +02:00
|
|
|
public async Task<IRequestHandler> HandleRequestOption2()
|
|
|
|
{
|
|
|
|
await _closeLockActionViewModel.CloseLockAsync();
|
2023-11-06 12:23:09 +01:00
|
|
|
if (_closeLockActionViewModel.IsEndRentalRequested == false)
|
|
|
|
{
|
|
|
|
return Create(
|
|
|
|
SelectedBike,
|
|
|
|
IsConnectedDelegate,
|
|
|
|
ConnectorFactory,
|
|
|
|
GeolocationService,
|
|
|
|
LockService,
|
|
|
|
ViewUpdateManager,
|
|
|
|
SmartDevice,
|
|
|
|
ViewService,
|
|
|
|
BikesViewModel,
|
|
|
|
ActiveUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
var _endRentalActionViewModel = new EndRentalActionViewModel<BookedClosed>(
|
|
|
|
SelectedBike,
|
|
|
|
IsConnectedDelegate,
|
|
|
|
ConnectorFactory,
|
|
|
|
LockService,
|
|
|
|
ViewUpdateManager,
|
|
|
|
ViewService,
|
|
|
|
BikesViewModel);
|
|
|
|
|
|
|
|
await _endRentalActionViewModel.EndRentalAsync();
|
|
|
|
|
|
|
|
return Create(
|
|
|
|
SelectedBike,
|
|
|
|
IsConnectedDelegate,
|
|
|
|
ConnectorFactory,
|
|
|
|
GeolocationService,
|
|
|
|
LockService,
|
|
|
|
ViewUpdateManager,
|
|
|
|
SmartDevice,
|
|
|
|
ViewService,
|
|
|
|
BikesViewModel,
|
|
|
|
ActiveUser);
|
2023-08-31 12:20:06 +02:00
|
|
|
}
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <summary> Open bike and update COPRI lock state. </summary>
|
|
|
|
public async Task<IRequestHandler> OpenLock()
|
|
|
|
{
|
2023-11-06 12:23:09 +01:00
|
|
|
Log.ForContext<BookedUnknown>().Information("User request to unlock bike {bikeId}.", SelectedBike.Id);
|
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-09-06 16:08:19 +02:00
|
|
|
|
2023-11-06 12:23:09 +01:00
|
|
|
// open lock
|
2022-09-06 16:08:19 +02:00
|
|
|
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
|
|
|
|
{
|
|
|
|
SelectedBike.LockInfo.State = (await LockService[SelectedBike.LockInfo.Id].OpenAsync())?.GetLockingState() ?? LockingState.UnknownDisconnected;
|
2023-11-06 12:23:09 +01:00
|
|
|
Log.ForContext<BookedUnknown>().Information("Lock from {bikeId} opened successfully.", SelectedBike.Id);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
catch (Exception exception)
|
|
|
|
{
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
2023-11-06 12:23:09 +01:00
|
|
|
Log.ForContext<BookedUnknown>().Information("Lock from {bikeId} can not be opened.", SelectedBike.Id);
|
2022-09-06 16:08:19 +02:00
|
|
|
if (exception is OutOfReachException)
|
|
|
|
{
|
2023-11-06 12:23:09 +01:00
|
|
|
Log.ForContext<BookedUnknown>().Debug("Lock is out of reach");
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.ErrorOpenLockTitle,
|
2023-08-31 12:20:06 +02:00
|
|
|
AppResources.ErrorLockOutOfReach,
|
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<BookedUnknown>().Debug("Bold is blocked.");
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.ErrorOpenLockTitle,
|
2023-08-31 12:20:06 +02:00
|
|
|
AppResources.ErrorOpenLockBoldBlocked,
|
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<BookedUnknown>().Debug("Bold status is unknown.");
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
await ViewService.DisplayAlert(
|
2023-08-31 12:20:06 +02:00
|
|
|
AppResources.ErrorOpenLockTitle,
|
|
|
|
AppResources.ErrorOpenLockStatusUnknown,
|
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<BookedUnknown>().Debug("Lock reports that it is still closed.");
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
await ViewService.DisplayAlert(
|
|
|
|
AppResources.ErrorOpenLockTitle,
|
2023-08-31 12:20:06 +02:00
|
|
|
AppResources.ErrorOpenLockStillClosed,
|
2022-09-06 16:08:19 +02:00
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-11-06 12:23:09 +01:00
|
|
|
Log.ForContext<BookedUnknown>().Error("{@exception}", exception);
|
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,
|
|
|
|
exception.Message,
|
2023-08-31 12:20:06 +02:00
|
|
|
AppResources.ErrorTryAgain,
|
2022-09-06 16:08:19 +02:00
|
|
|
AppResources.MessageAnswerOk);
|
|
|
|
}
|
|
|
|
|
2023-04-19 12:14:14 +02:00
|
|
|
// When bold is blocked lock is still closed even if exception occurs.
|
2022-09-06 16:08:19 +02:00
|
|
|
// In all other cases state is supposed to be unknown. Example: Lock is out of reach and no more bluetooth connected.
|
|
|
|
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<BookedUnknown>().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<BookedUnknown>().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<BookedUnknown>().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<BookedUnknown>().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<BookedUnknown>().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<BookedUnknown>().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<BookedUnknown>().Information("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<BookedUnknown>().Information("{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<BookedUnknown>().Error("{@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();
|
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
|
|
|
}
|
|
|
|
|
2023-08-31 12:20:06 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Processes the close lock progress.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Only used for testing.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="step">Current step to process.</param>
|
|
|
|
public void ReportStep(Step step) => _closeLockActionViewModel?.ReportStep(step);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Processes the close lock state.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Only used for testing.
|
|
|
|
/// </remarks>
|
|
|
|
/// <param name="state">State to process.</param>
|
|
|
|
/// <param name="details">Textual details describing current state.</param>
|
|
|
|
public async Task ReportStateAsync(State state, string details) => await _closeLockActionViewModel.ReportStateAsync(state, details);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
}
|