mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 07:06:33 +01:00
415 lines
16 KiB
C#
415 lines
16 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.CopriApi.Exception;
|
|
using TINK.Services.Geolocation;
|
|
using TINK.View;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
|
|
{
|
|
/// <summary> Bike is reserved, lock is open and connected to app. </summary>
|
|
/// <remarks>
|
|
/// This state might occur when a ILOCKIT was manually opened (color code) and app connects afterwards.
|
|
/// This should never during ILOCKIT is connected to app because
|
|
/// - manually opening lock is not possible when lock is connected
|
|
/// - two devices can not simultaneously connect to same lock.
|
|
public class ReservedOpen : 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 ReservedOpen(
|
|
IBikeInfoMutable selectedBike,
|
|
Func<bool> isConnectedDelegate,
|
|
Func<bool, IConnector> connectorFactory,
|
|
IGeolocationService geolocation,
|
|
ILocksService lockService,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
ISmartDevice smartDevice,
|
|
IViewService viewService,
|
|
IBikesViewModel bikesViewModel,
|
|
IUser activeUser) : base(
|
|
selectedBike,
|
|
AppResources.ActionCloseOrBook,
|
|
true, // Show button to enable canceling reservation.
|
|
isConnectedDelegate,
|
|
connectorFactory,
|
|
geolocation,
|
|
lockService,
|
|
viewUpdateManager,
|
|
smartDevice,
|
|
viewService,
|
|
bikesViewModel,
|
|
activeUser)
|
|
{
|
|
LockitButtonText = "Alarm/ Sounds verwalten";
|
|
IsLockitButtonVisible = activeUser.DebugLevel > 0; // Will be visible in future version of user with leveraged privileges.
|
|
}
|
|
|
|
/// <summary> Cancel reservation. </summary>
|
|
public async Task<IRequestHandler> HandleRequestOption1() => await CloseLockOrDoBook();
|
|
|
|
/// <summary> Manage sound/ alarm settings. </summary>
|
|
/// <returns></returns>
|
|
public async Task<IRequestHandler> HandleRequestOption2() => await ManageLockSettings();
|
|
|
|
/// <summary> Cancel reservation. </summary>
|
|
public async Task<IRequestHandler> CloseLockOrDoBook()
|
|
{
|
|
BikesViewModel.IsIdle = false; // Lock list to avoid multiple taps while copri action is pending.
|
|
|
|
var l_oResult = await ViewService.DisplayAlert(
|
|
string.Empty,
|
|
string.Format(AppResources.QuestionCloseOrBook, SelectedBike.GetFullDisplayName()),
|
|
AppResources.ActionClose,
|
|
AppResources.ActionBook);
|
|
|
|
// Stop polling before cancel request.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StopAsync();
|
|
|
|
if (l_oResult == false)
|
|
{
|
|
// User decided to book
|
|
Log.ForContext<ReservedOpen>().Information("User selected requested bike {bike} in order to book.", SelectedBike);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextReadingChargingLevel;
|
|
try
|
|
{
|
|
SelectedBike.LockInfo.BatteryPercentage = await LockService[SelectedBike.LockInfo.Id].GetBatteryPercentageAsync();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
if (exception is OutOfReachException)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Battery state can not be read, bike out of range. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelOutOfReach;
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Battery state can not be read. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelGeneral;
|
|
}
|
|
}
|
|
|
|
// Notify copri about unlock action in order to start booking.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextRentingBike;
|
|
IsConnected = IsConnectedDelegate();
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.DoBookAsync(SelectedBike);
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
|
|
if (l_oException is WebConnectFailureException)
|
|
{
|
|
// Copri server is not reachable.
|
|
Log.ForContext<ReservedOpen>().Information("User selected requested bike {l_oId} but booking failed (Copri server not reachable).", SelectedBike.Id);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorRentingBikeTitle,
|
|
AppResources.ErrorNoWeb,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("User selected requested bike {l_oId} but reserving failed. {@l_oException}", SelectedBike.Id, l_oException);
|
|
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.ErrorRentingBikeTitle,
|
|
l_oException.Message,
|
|
AppResources.ErrorTryAgain,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
// If booking failed lock bike again because bike is only reserved.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextClosingLock;
|
|
try
|
|
{
|
|
SelectedBike.LockInfo.State = (await LockService[SelectedBike.LockInfo.Id].CloseAsync())?.GetLockingState() ?? LockingState.UnknownDisconnected;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Locking bike after booking failure failed. {Exception}", exception);
|
|
|
|
SelectedBike.LockInfo.State = exception is StateAwareException stateAwareException
|
|
? stateAwareException.State
|
|
: LockingState.UnknownDisconnected;
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartAsync(); // Restart polling again.
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
Log.ForContext<ReservedOpen>().Information("User booked bike {bike} successfully.", SelectedBike);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartAsync(); // Restart polling again.
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
// Close lock and cancel reservation.
|
|
Log.ForContext<ReservedClosed>().Information("User selected reserved bike {l_oId} in order to cancel reservation.", SelectedBike.Id);
|
|
BikesViewModel.ActionText = AppResources.ActivityTextClosingLock;
|
|
try
|
|
{
|
|
SelectedBike.LockInfo.State = (await LockService[SelectedBike.LockInfo.Id].CloseAsync())?.GetLockingState() ?? LockingState.UnknownDisconnected;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
if (exception is OutOfReachException)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Lock can not be closed. {Exception}", exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCloseLockTitle,
|
|
AppResources.ErrorLockOutOfReach,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is CouldntCloseMovingException)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Lock can not be closed. Lock bike is moving. {Exception}", exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCloseLockTitle,
|
|
AppResources.ErrorLockMoving,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is CouldntCloseBoltBlockedException)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Lock can not be closed. Lock is out of reach. {Exception}", exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCloseLockTitle,
|
|
AppResources.ErrorCloseLockBoltBlocked,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Lock can not be closed. {Exception}", exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCloseLockTitle,
|
|
string.Format(AppResources.ErrorCloseLock, exception.Message),
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
SelectedBike.LockInfo.State = exception is StateAwareException stateAwareException
|
|
? stateAwareException.State
|
|
: LockingState.UnknownDisconnected;
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartAsync(); // Restart polling again.
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextCancelingReservation;
|
|
IsConnected = IsConnectedDelegate();
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.DoCancelReservation(SelectedBike);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
BikesViewModel.ActionText = String.Empty;
|
|
|
|
if (exception is InvalidAuthorizationResponseException)
|
|
{
|
|
// Copri response is invalid.
|
|
Log.ForContext<ReservedOpen>().Error("User selected reserved bike {l_oId} but canceling reservation failed (Invalid auth. response).", SelectedBike.Id);
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCancelReservationTitle,
|
|
AppResources.ErrorAccountInvalidAuthorization,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else if (exception is WebConnectFailureException
|
|
|| exception is RequestNotCachableException)
|
|
{
|
|
// Copri server is not reachable.
|
|
Log.ForContext<ReservedOpen>().Information("User selected reserved bike {l_oId} but cancel reservation failed (Copri server not reachable).", SelectedBike.Id);
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCancelReservationTitle,
|
|
AppResources.ErrorNoWeb,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("User selected reserved bike {l_oId} but cancel reservation failed. {@l_oException}.", SelectedBike.Id, exception);
|
|
await ViewService.DisplayAdvancedAlert(
|
|
AppResources.ErrorCancelReservationTitle,
|
|
exception.Message,
|
|
AppResources.ErrorTryAgain,
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartAsync(); // Restart polling again.
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
Log.ForContext<ReservedOpen>().Information("User canceled reservation of bike {l_oId} successfully.", SelectedBike.Id);
|
|
|
|
// Disconnect lock.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextDisconnectingLock;
|
|
try
|
|
{
|
|
SelectedBike.LockInfo.State = await LockService.DisconnectAsync(SelectedBike.LockInfo.Id, SelectedBike.LockInfo.Guid);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedClosed>().Error("Lock can not be disconnected. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorDisconnect;
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartAsync(); // Restart polling again.
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
/// <summary> Manage sound/ alarm settings. </summary>
|
|
/// <returns></returns>
|
|
public async Task<IRequestHandler> ManageLockSettings()
|
|
{
|
|
// Stop polling before requesting bike.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StopAsync();
|
|
|
|
// Close lock
|
|
Log.ForContext<ReservedOpen>().Information("User selected disposable bike {bike} in order to manage sound/ alarm settings.", SelectedBike);
|
|
|
|
// Alarm and sounds are on, toggle to off.
|
|
// Switch off sound.
|
|
BikesViewModel.ActionText = "Abschalten der Sounds...";
|
|
try
|
|
{
|
|
await LockService[SelectedBike.LockInfo.Id].SetSoundAsync(SoundSettings.Warn);
|
|
}
|
|
catch (OutOfReachException exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Can not turn off sounds. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Abschalten der Sounds!",
|
|
"Sounds können erst abgeschalten werden, wenn Rad in der Nähe ist.",
|
|
AppResources.MessageAnswerOk);
|
|
|
|
return this;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Can not turn off sounds. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Abschalten der Sounds!",
|
|
exception.Message,
|
|
AppResources.MessageAnswerOk);
|
|
|
|
return this;
|
|
}
|
|
|
|
// Lower alarm sensivity.
|
|
BikesViewModel.ActionText = "Setzen Alarm-Einstellungen...";
|
|
try
|
|
{
|
|
await LockService[SelectedBike.LockInfo.Id].SetAlarmSettingsAsync(AlarmSettings.SmallSensivitySilent);
|
|
}
|
|
catch (OutOfReachException exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Can not set alarm settings. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Setzen der Alarm-Einstellungen!",
|
|
"Alarm kann erst eingestellt werden, wenn Rad in der Nähe ist.",
|
|
AppResources.MessageAnswerOk);
|
|
|
|
return this;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Can not set alarm settings. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Setzen der Alarms-Einstellungen!",
|
|
exception.Message,
|
|
AppResources.MessageAnswerOk);
|
|
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
// Switch off alarm.
|
|
BikesViewModel.ActionText = "Abschalten von Alarm...";
|
|
try
|
|
{
|
|
await LockService[SelectedBike.LockInfo.Id].SetIsAlarmOffAsync(true);
|
|
}
|
|
catch (OutOfReachException exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Can not turn off alarm settings. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Abschalten des Alarms!",
|
|
"Alarm kann erst abgeschalten werden, wenn Rad in der Nähe ist.",
|
|
AppResources.MessageAnswerOk);
|
|
|
|
return this;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Can not turn off alarm. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Abschalten des Alarms!",
|
|
exception.Message,
|
|
AppResources.MessageAnswerOk);
|
|
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, GeolocationService, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
finally
|
|
{
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartAsync(); // Restart polling again.
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
}
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageHintTitle,
|
|
"Alarm und Sounds erfolgreich abgeschalten.",
|
|
AppResources.MessageAnswerOk);
|
|
|
|
return this;
|
|
}
|
|
}
|
|
}
|