2021-05-13 20:03:07 +02:00
using System ;
using System.Threading.Tasks ;
using TINK.Model.Connector ;
using TINK.Model.Bike.BluetoothLock ;
using TINK.Model.State ;
using TINK.View ;
using TINK.Model.Services.Geolocation ;
using TINK.Services.BluetoothLock ;
using Serilog ;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Exception ;
2021-05-13 20:03:07 +02:00
using TINK.Services.BluetoothLock.Exception ;
using Xamarin.Essentials ;
using TINK.MultilingualResources ;
using TINK.Model.Bikes.Bike.BluetoothLock ;
using TINK.Model.User ;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Request ;
using TINK.Model.Device ;
2021-05-13 20:03:07 +02:00
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
public class BookedOpen : Base , IRequestHandler
{
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
public BookedOpen (
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 . ActionCloseAndReturn , // Copri button text: "Schloss schließen & Miete beenden"
true , // Show button to allow user to return bike.
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 . ActionClose ; // BT button text "Schließen".
IsLockitButtonVisible = true ; // Show button to allow user to lock bike.
}
/// <summary> Gets the bike state. </summary>
public override InUseStateEnum State = > InUseStateEnum . Disposable ;
/// <summary> Close lock and return bike.</summary>
2021-06-26 20:57:55 +02:00
public async Task < IRequestHandler > HandleRequestOption1 ( ) = > await CloseLockAndReturnBike ( ) ;
/// <summary> Close lock in order to pause ride and update COPRI lock state.</summary>
public async Task < IRequestHandler > HandleRequestOption2 ( ) = > await CloseLock ( ) ;
/// <summary> Close lock and return bike.</summary>
public async Task < IRequestHandler > CloseLockAndReturnBike ( )
2021-05-13 20:03:07 +02:00
{
// Ask whether to really return bike?
BikesViewModel . IsIdle = false ;
var l_oResult = await ViewService . DisplayAlert (
string . Empty ,
2021-06-26 20:57:55 +02:00
$"Fahrrad {SelectedBike.GetFullDisplayName()} abschließen und zurückgeben?" ,
2021-05-13 20:03:07 +02:00
"Ja" ,
"Nein" ) ;
if ( l_oResult = = false )
{
// User aborted closing and returning bike process
Log . ForContext < BookedOpen > ( ) . Information ( "User selected booked bike {l_oId} in order to close and return but action was canceled." , SelectedBike . Id ) ;
BikesViewModel . IsIdle = true ;
return this ;
}
// Unlock bike.
Log . ForContext < BookedOpen > ( ) . Information ( "Request to return bike {bike} detected." , SelectedBike ) ;
// Stop polling before returning bike.
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
await ViewUpdateManager ( ) . StopUpdatePeridically ( ) ;
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 < BookedOpen > ( ) . Debug ( "Lock can not be closed. Lock is out of reach. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
AppResources . ErrorCloseLockOutOfReachMessage ,
"OK" ) ;
}
else if ( exception is CounldntCloseMovingException )
{
Log . ForContext < BookedOpen > ( ) . Debug ( "Lock can not be closed. Lock is out of reach. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
AppResources . ErrorCloseLockMovingMessage ,
"OK" ) ;
}
else if ( exception is CouldntCloseBoldBlockedException )
{
Log . ForContext < BookedOpen > ( ) . Debug ( "Lock can not be closed. Lock is out of reach. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
AppResources . ErrorCloseLockBoldBlockedMessage ,
"OK" ) ;
}
else
{
Log . ForContext < BookedOpen > ( ) . Error ( "Lock can not be closed. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
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 ; // 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
}
if ( SelectedBike . LockInfo . State ! = LockingState . Closed )
{
Log . ForContext < BookedOpen > ( ) . Error ( $"Lock can not be closed. Invalid locking state state {SelectedBike.LockInfo.State} detected." ) ;
BikesViewModel . ActionText = string . Empty ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
SelectedBike . LockInfo . State = = LockingState . Open
? AppResources . ErrorCloseLockStillOpenMessage
: string . Format ( AppResources . ErrorCloseLockUnexpectedStateMessage , SelectedBike . LockInfo . State ) ,
"OK" ) ;
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
}
// Get geoposition.
var timeStamp = DateTime . Now ;
BikesViewModel . ActionText = "Abfrage Standort..." ;
Location currentLocation ;
try
{
currentLocation = await Geolocation . GetAsync ( timeStamp ) ;
}
catch ( Exception ex )
{
// No location information available.
Log . ForContext < BookedOpen > ( ) . Information ( "Returning bike {Bike} is not possible. {Exception}" , SelectedBike , ex ) ;
BikesViewModel . ActionText = string . Empty ;
await ViewService . DisplayAlert (
"Fehler bei Standortabfrage!" ,
string . Format ( $"Schloss schließen und Miete beenden ist nicht möglich.\r\n{ex.Message}" ) ,
"OK" ) ;
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
}
// Lock list to avoid multiple taps while copri action is pending.
BikesViewModel . ActionText = "Gebe Rad zurück..." ;
IsConnected = IsConnectedDelegate ( ) ;
var feedBackUri = SelectedBike ? . OperatorUri ;
try
{
await ConnectorFactory ( IsConnected ) . Command . DoReturn (
SelectedBike ,
currentLocation ! = null
? new LocationDto . Builder
{
Latitude = currentLocation . Latitude ,
Longitude = currentLocation . Longitude ,
Accuracy = currentLocation . Accuracy ? ? double . NaN ,
Age = timeStamp . Subtract ( currentLocation . Timestamp . DateTime ) ,
} . Build ( )
2021-06-26 20:57:55 +02:00
: null ,
SmartDevice ) ;
2021-05-13 20:03:07 +02:00
// If canceling bike succedes remove bike because it is not ready to be booked again
IsRemoveBikeRequired = true ;
}
catch ( Exception exception )
{
BikesViewModel . ActionText = string . Empty ;
if ( exception is WebConnectFailureException )
{
// Copri server is not reachable.
Log . ForContext < BookedOpen > ( ) . Information ( "User selected booked bike {bike} but returing failed (Copri server not reachable)." , SelectedBike ) ;
await ViewService . DisplayAlert (
"Verbingungsfehler beim Zurückgeben des Rads!" ,
string . Format ( "{0}\r\n{1}\r\n{2}" , "Internet muss erreichbar sein beim Zurückgeben des Rads." , exception . Message , WebConnectFailureException . GetHintToPossibleExceptionsReasons ) ,
"OK" ) ;
}
else if ( exception is NotAtStationException notAtStationException )
{
// COPRI returned an error.
Log . ForContext < BookedOpen > ( ) . Information ( "User selected booked bike {bike} but returing failed. COPRI returned an error." , SelectedBike ) ;
await ViewService . DisplayAlert (
AppResources . ErrorReturnBikeNotAtStationTitle ,
string . Format ( AppResources . ErrorReturnBikeNotAtStationMessage , notAtStationException . StationNr , notAtStationException . Distance ) ,
"OK" ) ;
}
else if ( exception is NoGPSDataException )
{
// COPRI returned an error.
Log . ForContext < BookedOpen > ( ) . Information ( "User selected booked bike {bike} but returing failed. COPRI returned an no GPS- data error." , SelectedBike ) ;
await ViewService . DisplayAlert (
AppResources . ErrorReturnBikeNotAtStationTitle ,
string . Format ( AppResources . ErrorReturnBikeLockOpenNoGPSMessage ) ,
"OK" ) ;
}
else if ( exception is ResponseException copriException )
{
// Copri server is not reachable.
Log . ForContext < BookedOpen > ( ) . Information ( "User selected booked bike {bike} but returing failed. COPRI returned an error." , SelectedBike ) ;
await ViewService . DisplayAdvancedAlert (
"Statusfehler beim Zurückgeben des Rads!" ,
copriException . Message ,
copriException . Response ,
"OK" ) ;
}
else
{
Log . ForContext < BookedOpen > ( ) . Error ( "User selected availalbe bike {bike} but reserving failed. {@l_oException}" , SelectedBike . Id , exception ) ;
await ViewService . DisplayAlert ( "Fehler beim Zurückgeben des Rads!" , exception . Message , "OK" ) ;
}
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ;
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
}
Log . ForContext < BookedOpen > ( ) . Information ( "User returned bike {bike} successfully." , SelectedBike ) ;
// 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 ;
}
#if ! USERFEEDBACKDLG_OFF
// Do get Feedback
var feedback = await ViewService . DisplayUserFeedbackPopup ( ) ;
try
{
await ConnectorFactory ( IsConnected ) . Command . DoSubmitFeedback (
2021-06-26 20:57:55 +02:00
new UserFeedbackDto { BikeId = SelectedBike . Id , IsBikeBroken = feedback . IsBikeBroken , Message = feedback . Message } ,
2021-05-13 20:03:07 +02:00
feedBackUri ) ;
}
catch ( Exception exception )
{
BikesViewModel . ActionText = string . Empty ;
if ( exception is ResponseException copriException )
{
// Copri server is not reachable.
Log . ForContext < BookedOpen > ( ) . Information ( "User selected booked bike {bike} but returing failed. COPRI returned an error." , SelectedBike ) ;
}
else
{
Log . ForContext < BookedOpen > ( ) . Error ( "User selected availalbe bike {bike} but reserving failed. {@l_oException}" , SelectedBike . Id , exception ) ;
}
2021-06-26 20:57:55 +02:00
await ViewService . DisplayAlert (
AppResources . ErrorReturnSubmitFeedbackTitle ,
AppResources . ErrorReturnSubmitFeedbackMessage ,
AppResources . MessageAnswerOk ) ;
2021-05-13 20:03:07 +02:00
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ;
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
}
#endif
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ;
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
}
/// <summary> Close lock in order to pause ride and update COPRI lock state.</summary>
2021-06-26 20:57:55 +02:00
public async Task < IRequestHandler > CloseLock ( )
2021-05-13 20:03:07 +02:00
{
// Unlock bike.
BikesViewModel . IsIdle = false ;
Log . ForContext < BookedOpen > ( ) . Information ( "User request to lock bike {bike} in order to pause ride." , SelectedBike ) ;
// Stop polling before returning bike.
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
await ViewUpdateManager ( ) . StopUpdatePeridically ( ) ;
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 < BookedOpen > ( ) . Debug ( "Lock can not be closed. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
AppResources . ErrorCloseLockOutOfReachMessage ,
"OK" ) ;
}
else if ( exception is CounldntCloseMovingException )
{
Log . ForContext < BookedOpen > ( ) . Debug ( "Lock can not be closed. Lock is out of reach. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
AppResources . ErrorCloseLockMovingMessage ,
"OK" ) ;
}
else if ( exception is CouldntCloseBoldBlockedException )
{
Log . ForContext < BookedOpen > ( ) . Debug ( "Lock can not be closed. Lock is out of reach. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
AppResources . ErrorCloseLockBoldBlockedMessage ,
"OK" ) ;
}
else
{
Log . ForContext < BookedOpen > ( ) . Error ( "Lock can not be closed. {Exception}" , exception ) ;
await ViewService . DisplayAlert (
AppResources . ErrorCloseLockTitle ,
exception . Message ,
"OK" ) ;
}
SelectedBike . LockInfo . State = exception is StateAwareException stateAwareException
? stateAwareException . State
: LockingState . Disconnected ;
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ;
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
}
// Get geoposition.
var timeStamp = DateTime . Now ;
BikesViewModel . ActionText = "Abfrage Standort..." ;
Location currentLocation = null ;
try
{
currentLocation = await Geolocation . GetAsync ( timeStamp ) ;
}
catch ( Exception ex )
{
// No location information available.
Log . ForContext < BookedOpen > ( ) . Information ( "Returning bike {Bike} is not possible. {Exception}" , SelectedBike , ex ) ;
BikesViewModel . ActionText = "Keine Standortinformationen verfügbar." ;
}
// Lock list to avoid multiple taps while copri action is pending.
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdatingLockingState ;
IsConnected = IsConnectedDelegate ( ) ;
try
{
await ConnectorFactory ( IsConnected ) . Command . UpdateLockingStateAsync (
SelectedBike ,
currentLocation ! = null
? new LocationDto . Builder
{
Latitude = currentLocation . Latitude ,
Longitude = currentLocation . Longitude ,
Accuracy = currentLocation . Accuracy ? ? double . NaN ,
Age = timeStamp . Subtract ( currentLocation . Timestamp . DateTime ) ,
} . Build ( )
: null ) ;
}
catch ( Exception exception )
{
if ( exception is WebConnectFailureException )
{
// Copri server is not reachable.
Log . ForContext < BookedOpen > ( ) . Information ( "User locked bike {bike} in order to pause ride but updating failed (Copri server not reachable)." , SelectedBike ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorNoWebUpdateingLockstate ;
}
else if ( exception is ResponseException copriException )
{
// Copri server is not reachable.
Log . ForContext < BookedOpen > ( ) . Information ( "User locked bike {bike} in order to pause ride but updating failed. Message: {Message} Details: {Details}" , SelectedBike , copriException . Message , copriException . Response ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorStatusUpdateingLockstate ;
}
else
{
Log . ForContext < BookedOpen > ( ) . Error ( "User locked bike {bike} in order to pause ride but updating failed. {@l_oException}" , SelectedBike . Id , exception ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorConnectionUpdateingLockstate ;
}
}
Log . ForContext < BookedOpen > ( ) . Information ( "User paused ride using {bike} successfully." , SelectedBike ) ;
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdater ;
await ViewUpdateManager ( ) . StartUpdateAyncPeridically ( ) ;
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
}
}
}