mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-14 14:26:28 +01:00
491 lines
23 KiB
C#
491 lines
23 KiB
C#
using Serilog;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model.Connector;
|
|
using TINK.Repository.Exception;
|
|
using TINK.Model.Bike.BluetoothLock;
|
|
using TINK.Model.State;
|
|
using TINK.View;
|
|
using TINK.Model.Services.Geolocation;
|
|
using TINK.Services.BluetoothLock;
|
|
using TINK.Services.BluetoothLock.Exception;
|
|
using TINK.Model.Bikes.Bike.BluetoothLock;
|
|
using TINK.MultilingualResources;
|
|
using TINK.Model.User;
|
|
using TINK.Model.Device;
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
|
|
{
|
|
/// <summary> Bike is reserved, lock is open and connected to app. </summary>
|
|
/// <remarks>
|
|
/// This state might occure 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 conect 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,
|
|
IGeolocation geolocation,
|
|
ILocksService lockService,
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
ISmartDevice smartDevice,
|
|
IViewService viewService,
|
|
IBikesViewModel bikesViewModel,
|
|
IUser activeUser) : base(
|
|
selectedBike,
|
|
"Rad zurückgeben oder mieten",
|
|
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> Gets the bike state. </summary>
|
|
public override InUseStateEnum State => InUseStateEnum.Reserved;
|
|
|
|
/// <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("Rad {0} abschließen und zurückgeben oder Rad mieten?", SelectedBike.GetFullDisplayName()),
|
|
"Zurückgeben",
|
|
"Mieten");
|
|
|
|
// Stop polling before cancel request.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextOneMomentPlease;
|
|
await ViewUpdateManager().StopUpdatePeridically();
|
|
|
|
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("Akkustate can not be read, bike out of range. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelOutOfReach;
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Akkustate can not be read. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextErrorReadingChargingLevelGeneral;
|
|
}
|
|
}
|
|
|
|
// Notify corpi about unlock action in order to start booking.
|
|
BikesViewModel.ActionText = AppResources.ActivityTextRentingBike;
|
|
IsConnected = IsConnectedDelegate();
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.DoBook(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.MessageRentingBikeErrorConnectionTitle,
|
|
string.Format(AppResources.MessageErrorLockIsClosedThreeLines, l_oException.Message, WebConnectFailureException.GetHintToPossibleExceptionsReasons),
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("User selected requested bike {l_oId} but reserving failed. {@l_oException}", SelectedBike.Id, l_oException);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.MessageRentingBikeErrorGeneralTitle,
|
|
string.Format(AppResources.MessageErrorLockIsClosedTwoLines, l_oException.Message),
|
|
AppResources.MessageAnswerOk);
|
|
}
|
|
|
|
// If booking failed lock bike again because bike is only reserved.
|
|
BikesViewModel.ActionText = "Wiederverschließe Schloss...";
|
|
try
|
|
{
|
|
SelectedBike.LockInfo.State = (await LockService[SelectedBike.LockInfo.Id].CloseAsync())?.GetLockingState() ?? LockingState.Disconnected;
|
|
}
|
|
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.Disconnected;
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
Log.ForContext<ReservedOpen>().Information("User booked bike {bike} successfully.", SelectedBike);
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, 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.Disconnected;
|
|
}
|
|
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.ErrorCloseLockOutOfReachStateReservedMessage,
|
|
"OK");
|
|
}
|
|
else if (exception is CounldntCloseMovingException)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Lock can not be closed. Lock bike is moving. {Exception}", exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCloseLockTitle,
|
|
AppResources.ErrorCloseLockMovingMessage,
|
|
"OK");
|
|
}
|
|
else if (exception is CouldntCloseBoldBlockedException)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Lock can not be closed. Lock is out of reach. {Exception}", exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCloseLockTitle,
|
|
AppResources.ErrorCloseLockBoldBlockedMessage,
|
|
"OK");
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Lock can not be closed. {Exception}", exception);
|
|
|
|
await ViewService.DisplayAlert(
|
|
AppResources.ErrorCloseLockTitle,
|
|
string.Format(AppResources.ErrorCloseLockUnkErrorMessage, exception.Message),
|
|
"OK");
|
|
}
|
|
|
|
SelectedBike.LockInfo.State = exception is StateAwareException stateAwareException
|
|
? stateAwareException.State
|
|
: LockingState.Disconnected;
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
BikesViewModel.ActionText = "";
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextCancelingReservation;
|
|
IsConnected = IsConnectedDelegate();
|
|
try
|
|
{
|
|
await ConnectorFactory(IsConnected).Command.DoCancelReservation(SelectedBike);
|
|
|
|
// If canceling bike succedes remove bike because it is not ready to be booked again
|
|
IsRemoveBikeRequired = true;
|
|
}
|
|
catch (Exception l_oException)
|
|
{
|
|
BikesViewModel.ActionText = String.Empty;
|
|
|
|
if (l_oException 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("Fehler beim Aufheben der Reservierung!", l_oException.Message, "OK");
|
|
}
|
|
else if (l_oException is WebConnectFailureException)
|
|
{
|
|
// 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(
|
|
"Verbingungsfehler beim Aufheben der Reservierung!",
|
|
string.Format("{0}\r\n{1}", l_oException.Message, WebConnectFailureException.GetHintToPossibleExceptionsReasons),
|
|
"OK");
|
|
}
|
|
else
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("User selected reserved bike {l_oId} but cancel reservation failed. {@l_oException}.", SelectedBike.Id, l_oException);
|
|
await ViewService.DisplayAlert("Fehler beim Aufheben der Reservierung!", l_oException.Message, "OK");
|
|
}
|
|
|
|
BikesViewModel.ActionText = AppResources.ActivityTextStartingUpdater;
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
BikesViewModel.ActionText = "";
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, 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().StartUpdateAyncPeridically(); // Restart polling again.
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, 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().StopUpdatePeridically();
|
|
|
|
// Close lock
|
|
Log.ForContext<ReservedOpen>().Information("User selected disposable bike {bike} in order to manage sound/ alarm settings.", SelectedBike);
|
|
|
|
// Check current state.
|
|
BikesViewModel.ActionText = "Schlosseinstellung abfragen...";
|
|
bool isAlarmOff;
|
|
try
|
|
{
|
|
isAlarmOff = await LockService[SelectedBike.LockInfo.Id].GetIsAlarmOffAsync();
|
|
}
|
|
catch (OutOfReachException exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Can not get lock alarm settings. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Abfragen der Alarmeinstellungen!",
|
|
"Schloss kann erst geschlossen werden, wenn Rad in der Nähe ist.",
|
|
"OK");
|
|
|
|
return this;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Can not get lock alarm settings. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Abfragen der Alarmeinstellungen!",
|
|
exception.Message,
|
|
"OK");
|
|
|
|
return this;
|
|
}
|
|
|
|
if (isAlarmOff)
|
|
{
|
|
// Switch on sound.
|
|
BikesViewModel.ActionText = "Anschalten von Sounds...";
|
|
try
|
|
{
|
|
await LockService[SelectedBike.LockInfo.Id].SetSoundAsync(SoundSettings.AllOn);
|
|
}
|
|
catch (OutOfReachException exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Can not turn on sounds. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Anschalten der Sounds!",
|
|
"Sounds können erst angeschalten werden, wenn Rad in der Nähe ist.",
|
|
"OK");
|
|
|
|
return this;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Can not turn on sounds. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Anschalten der Sounds!",
|
|
exception.Message,
|
|
"OK");
|
|
|
|
return this;
|
|
}
|
|
|
|
// Switch off alarm.
|
|
BikesViewModel.ActionText = "Anschalten von Alarm...";
|
|
try
|
|
{
|
|
await LockService[SelectedBike.LockInfo.Id].SetIsAlarmOffAsync(true);
|
|
}
|
|
catch (OutOfReachException exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Debug("Can not turn on alarm settings. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Anschalten des Alarms!",
|
|
"Alarm kann erst angeschalten werden, wenn Rad in der Nähe ist.",
|
|
"OK");
|
|
|
|
return this;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.ForContext<ReservedOpen>().Error("Can not turn on alarm. {Exception}", exception);
|
|
|
|
BikesViewModel.ActionText = string.Empty;
|
|
await ViewService.DisplayAlert(
|
|
"Fehler beim Anschalten des Alarms!",
|
|
exception.Message,
|
|
"OK");
|
|
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
finally
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
}
|
|
|
|
await ViewService.DisplayAlert(
|
|
"Hinweis",
|
|
"Alarm und Sounds erfolgreich aktiviert",
|
|
"OK");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
// Switch off sound.
|
|
BikesViewModel.ActionText = "Abschalten der Sounds...";
|
|
try
|
|
{
|
|
await LockService[SelectedBike.LockInfo.Id].SetSoundAsync(SoundSettings.AllOff);
|
|
}
|
|
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.",
|
|
"OK");
|
|
|
|
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,
|
|
"OK");
|
|
|
|
return this;
|
|
}
|
|
|
|
// Switch off alarm.
|
|
BikesViewModel.ActionText = "Abschalten von Alarm...";
|
|
try
|
|
{
|
|
await LockService[SelectedBike.LockInfo.Id].SetIsAlarmOffAsync(false);
|
|
}
|
|
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.",
|
|
"OK");
|
|
|
|
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,
|
|
"OK");
|
|
|
|
return RequestHandlerFactory.Create(SelectedBike, IsConnectedDelegate, ConnectorFactory, Geolocation, LockService, ViewUpdateManager, SmartDevice, ViewService, BikesViewModel, ActiveUser);
|
|
}
|
|
finally
|
|
{
|
|
BikesViewModel.ActionText = string.Empty;
|
|
BikesViewModel.IsIdle = true; // Unlock GUI
|
|
await ViewUpdateManager().StartUpdateAyncPeridically(); // Restart polling again.
|
|
}
|
|
|
|
await ViewService.DisplayAlert(
|
|
"Hinweis",
|
|
"Alarm und Sounds erfolgreich abgeschalten.",
|
|
"OK");
|
|
|
|
return this;
|
|
}
|
|
}
|
|
}
|