2021-05-13 20:03:07 +02:00
using Serilog ;
using System ;
using System.Threading.Tasks ;
using TINK.Model.Connector ;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Exception ;
2021-05-13 20:03:07 +02:00
using TINK.Model.Bike.BluetoothLock ;
using TINK.Model.State ;
using TINK.View ;
using IBikeInfoMutable = TINK . Model . Bikes . Bike . BluetoothLock . IBikeInfoMutable ;
using TINK.Model.Services.Geolocation ;
using TINK.Services.BluetoothLock ;
using TINK.Services.BluetoothLock.Exception ;
using TINK.MultilingualResources ;
using TINK.Model.User ;
2021-06-26 20:57:55 +02:00
using TINK.Model.Device ;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
/// <summary> Bike is reserved, lock is closed and and connected to app. </summary>
/// <remarks>
/// Occures when
/// - biks was reserved out of reach and is in reach now
/// - bike is is reserved while in reach
/// </remarks>
public class ReservedClosed : Base , IRequestHandler
{
2021-06-26 20:57:55 +02:00
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
2021-05-13 20:03:07 +02:00
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
public ReservedClosed (
IBikeInfoMutable selectedBike ,
Func < bool > isConnectedDelegate ,
Func < bool , IConnector > connectorFactory ,
IGeolocation geolocation ,
ILocksService lockService ,
Func < IPollingUpdateTaskManager > viewUpdateManager ,
2021-06-26 20:57:55 +02:00
ISmartDevice smartDevice ,
2021-05-13 20:03:07 +02:00
IViewService viewService ,
IBikesViewModel bikesViewModel ,
IUser activeUser ) : base (
selectedBike ,
AppResources . ActionCancelRequest , // Copri button text: "Reservierung abbrechen"
true , // Show button to enable canceling reservation.
isConnectedDelegate ,
connectorFactory ,
geolocation ,
lockService ,
2021-06-26 20:57:55 +02:00
viewUpdateManager ,
smartDevice ,
2021-05-13 20:03:07 +02:00
viewService ,
bikesViewModel ,
activeUser )
{
LockitButtonText = AppResources . ActionOpenAndBook ; // Button text: "Schloss öffnen & Rad mieten"
IsLockitButtonVisible = true ; // Show "Öffnen" button to enable unlocking
}
/// <summary> Gets the bike state. </summary>
public override InUseStateEnum State = > InUseStateEnum . Reserved ;
/// <summary> Cancel reservation. </summary>
2021-06-26 20:57:55 +02:00
public async Task < IRequestHandler > HandleRequestOption1 ( ) = > await CancelReservation ( ) ;
/// <summary> Open lock and book bike. </summary>
public async Task < IRequestHandler > HandleRequestOption2 ( ) = > await OpenLockAndDooBook ( ) ;
/// <summary> Cancel reservation. </summary>
public async Task < IRequestHandler > CancelReservation ( )
2021-05-13 20:03:07 +02:00
{
BikesViewModel . IsIdle = false ; // Lock list to avoid multiple taps while copri action is pending.
var l_oResult = await ViewService . DisplayAlert (
string . Empty ,
2021-06-26 20:57:55 +02:00
string . Format ( AppResources . QuestionCancelReservation , SelectedBike . GetFullDisplayName ( ) ) ,
2021-05-13 20:03:07 +02:00
AppResources . QuestionAnswerYes ,
AppResources . QuestionAnswerNo ) ;
if ( l_oResult = = false )
{
// User aborted cancel process
Log . ForContext < ReservedClosed > ( ) . Information ( "User selected reserved bike {l_oId} in order to cancel reservation but action was canceled." , SelectedBike . Id ) ;
BikesViewModel . IsIdle = true ;
return this ;
}
Log . ForContext < ReservedClosed > ( ) . Information ( "User selected reserved bike {l_oId} in order to cancel reservation." , SelectedBike . Id ) ;
// Stop polling before cancel request.
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
await ViewUpdateManager ( ) . StopUpdatePeridically ( ) ;
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 < BikesViewModel > ( ) . 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 < BikesViewModel > ( ) . 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 < BikesViewModel > ( ) . 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
2021-06-26 20:57:55 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , Geolocation , LockService , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2021-05-13 20:03:07 +02:00
}
Log . ForContext < BikesViewModel > ( ) . 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
2021-06-26 20:57:55 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , Geolocation , LockService , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2021-05-13 20:03:07 +02:00
}
/// <summary> Open lock and book bike. </summary>
2021-06-26 20:57:55 +02:00
public async Task < IRequestHandler > OpenLockAndDooBook ( )
2021-05-13 20:03:07 +02:00
{
BikesViewModel . IsIdle = false ;
// Ask whether to really book bike?
var l_oResult = await ViewService . DisplayAlert (
string . Empty ,
2021-08-28 10:04:10 +02:00
string . Format ( AppResources . QuestionOpenLockAndBookBike , SelectedBike . GetFullDisplayName ( ) ) ,
2021-05-13 20:03:07 +02:00
AppResources . MessageAnswerYes ,
AppResources . MessageAnswerNo ) ;
if ( l_oResult = = false )
{
// User aborted booking process
Log . ForContext < ReservedClosed > ( ) . Information ( "User selected requested bike {bike} in order to book but action was canceled." , SelectedBike ) ;
BikesViewModel . IsIdle = true ;
return this ;
}
Log . ForContext < ReservedClosed > ( ) . Information ( "User selected requested bike {bike} in order to book but action was canceled." , SelectedBike ) ;
// Stop polling before cancel request.
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
await ViewUpdateManager ( ) . StopUpdatePeridically ( ) ;
// Book bike prior to opening lock.
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 < ReservedClosed > ( ) . Information ( "User selected requested bike {l_oId} but booking failed (Copri server not reachable)." , SelectedBike . Id ) ;
await ViewService . DisplayAdvancedAlert (
AppResources . MessageRentingBikeErrorConnectionTitle ,
WebConnectFailureException . GetHintToPossibleExceptionsReasons ,
l_oException . Message ,
AppResources . MessageAnswerOk ) ;
}
else
{
Log . ForContext < ReservedClosed > ( ) . Error ( "User selected requested bike {l_oId} but reserving failed. {@l_oException}" , SelectedBike . Id , l_oException ) ;
await ViewService . DisplayAdvancedAlert (
AppResources . MessageRentingBikeErrorGeneralTitle ,
string . Empty ,
l_oException . Message ,
AppResources . MessageAnswerOk ) ;
}
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ; // Restart polling again.
BikesViewModel . IsIdle = true ; // Unlock GUI
2021-06-26 20:57:55 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , Geolocation , LockService , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2021-05-13 20:03:07 +02:00
}
// Unlock bike.
BikesViewModel . ActionText = AppResources . ActivityTextOpeningLock ;
try
{
SelectedBike . LockInfo . State = ( await LockService [ SelectedBike . LockInfo . Id ] . OpenAsync ( ) ) ? . GetLockingState ( ) ? ? LockingState . Disconnected ;
}
catch ( Exception exception )
{
BikesViewModel . ActionText = string . Empty ;
if ( exception is OutOfReachException )
{
Log . ForContext < ReservedClosed > ( ) . Debug ( "Lock can not be opened. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorOpenLockTitle ,
AppResources . ErrorOpenLockOutOfReadMessage ,
"OK" ) ;
}
2021-06-26 20:57:55 +02:00
else if ( exception is CouldntOpenBoldIsBlockedException )
2021-05-13 20:03:07 +02:00
{
2021-08-28 10:04:10 +02:00
Log . ForContext < ReservedClosed > ( ) . Debug ( "Lock can not be opened. Bold is blocked. {Exception}" , exception ) ;
2021-05-13 20:03:07 +02:00
await ViewService . DisplayAlert (
AppResources . ErrorOpenLockTitle ,
2021-06-26 20:57:55 +02:00
AppResources . ErrorOpenLockBoldBlockedMessage ,
"OK" ) ;
}
else if ( exception is CouldntOpenBoldWasBlockedException )
{
2021-08-28 10:04:10 +02:00
Log . ForContext < ReservedClosed > ( ) . Debug ( "Lock can not be opened. Bold was or is blocked. {Exception}" , exception ) ;
2021-06-26 20:57:55 +02:00
await ViewService . DisplayAlert (
AppResources . ErrorOpenLockStillOpenTitle ,
AppResources . ErrorOpenLockBoldWasBlockedMessage ,
2021-05-13 20:03:07 +02:00
"OK" ) ;
}
else if ( exception is CouldntOpenInconsistentStateExecption inconsistentState
& & inconsistentState . State = = LockingState . Closed )
{
2021-08-28 10:04:10 +02:00
Log . ForContext < ReservedClosed > ( ) . Debug ( "Lock can not be opened. lock reports state closed. {Exception}" , exception ) ;
2021-05-13 20:03:07 +02:00
await ViewService . DisplayAlert (
AppResources . ErrorOpenLockTitle ,
AppResources . ErrorOpenLockStillClosedMessage ,
"OK" ) ;
}
else
{
Log . ForContext < ReservedClosed > ( ) . Error ( "Lock can not be opened. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorOpenLockTitle ,
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 = string . Empty ;
BikesViewModel . IsIdle = true ;
2021-06-26 20:57:55 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , Geolocation , LockService , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2021-05-13 20:03:07 +02:00
}
if ( SelectedBike . LockInfo . State ! = LockingState . Open )
{
// Opening lock failed.
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ; // Restart polling again.
BikesViewModel . ActionText = "" ;
BikesViewModel . IsIdle = true ; // Unlock GUI
2021-06-26 20:57:55 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , Geolocation , LockService , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2021-05-13 20:03:07 +02:00
}
BikesViewModel . ActionText = AppResources . ActivityTextReadingChargingLevel ;
try
{
2021-06-26 20:57:55 +02:00
SelectedBike . LockInfo . BatteryPercentage = await LockService [ SelectedBike . LockInfo . Id ] . GetBatteryPercentageAsync ( ) ;
2021-05-13 20:03:07 +02:00
}
catch ( Exception exception )
{
if ( exception is OutOfReachException )
{
Log . ForContext < ReservedClosed > ( ) . Debug ( "Akkustate can not be read, bike out of range. {Exception}" , exception ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorReadingChargingLevelOutOfReach ;
}
else
{
Log . ForContext < ReservedClosed > ( ) . Error ( "Akkustate can not be read. {Exception}" , exception ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorReadingChargingLevelGeneral ;
}
}
// Lock list to avoid multiple taps while copri action is pending.
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdatingLockingState ;
IsConnected = IsConnectedDelegate ( ) ;
try
{
await ConnectorFactory ( IsConnected ) . Command . UpdateLockingStateAsync ( SelectedBike ) ;
}
catch ( Exception exception )
{
if ( exception is WebConnectFailureException )
{
// Copri server is not reachable.
2021-08-28 10:04:10 +02:00
Log . ForContext < ReservedClosed > ( ) . Information ( "User locked bike {bike} in order to pause ride but updating failed (Copri server not reachable)." , SelectedBike ) ;
2021-05-13 20:03:07 +02:00
BikesViewModel . ActionText = AppResources . ActivityTextErrorNoWebUpdateingLockstate ;
}
else if ( exception is ResponseException copriException )
{
// Copri server is not reachable.
2021-08-28 10:04:10 +02:00
Log . ForContext < ReservedClosed > ( ) . Information ( "User locked bike {bike} in order to pause ride but updating failed. {response}." , SelectedBike , copriException . Response ) ;
2021-05-13 20:03:07 +02:00
BikesViewModel . ActionText = AppResources . ActivityTextErrorStatusUpdateingLockstate ;
}
else
{
2021-08-28 10:04:10 +02:00
Log . ForContext < ReservedClosed > ( ) . Error ( "User locked bike {bike} in order to pause ride but updating failed . {@l_oException}" , SelectedBike . Id , exception ) ;
2021-05-13 20:03:07 +02:00
BikesViewModel . ActionText = AppResources . ActivityTextErrorConnectionUpdateingLockstate ;
}
}
Log . ForContext < ReservedClosed > ( ) . Information ( "User reserved bike {bike} successfully." , SelectedBike ) ;
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ; // Restart polling again.
BikesViewModel . ActionText = string . Empty ;
BikesViewModel . IsIdle = true ; // Unlock GUI
2021-06-26 20:57:55 +02:00
return RequestHandlerFactory . Create ( SelectedBike , IsConnectedDelegate , ConnectorFactory , Geolocation , LockService , ViewUpdateManager , SmartDevice , ViewService , BikesViewModel , ActiveUser ) ;
2021-05-13 20:03:07 +02:00
}
}
}