2022-10-26 20:53:18 +02:00
using System ;
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 ;
using TINK.Services.BluetoothLock.Tdo ;
2022-10-26 20:53:18 +02:00
using TINK.Services.CopriApi.Exception ;
2022-04-10 17:38:34 +02:00
using TINK.Services.Geolocation ;
2021-05-13 20:03:07 +02:00
using TINK.View ;
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler
{
2022-09-06 16:08:19 +02:00
public class BookedDisconnected : 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 BookedDisconnected (
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 ,
nameof ( BookedDisconnected ) ,
false ,
isConnectedDelegate ,
connectorFactory ,
geolocation ,
lockService ,
viewUpdateManager ,
smartDevice ,
viewService ,
bikesViewModel ,
activeUser )
{
2023-11-06 12:23:09 +01:00
LockitButtonText = AppResources . ActionSearchLock ; // Search Lock
IsLockitButtonVisible = true ; // show button "Search Lock"
2022-09-06 16:08:19 +02:00
}
public async Task < IRequestHandler > HandleRequestOption1 ( ) = > await UnsupportedRequest ( ) ;
/// <summary> Scan for lock.</summary>
/// <returns></returns>
public async Task < IRequestHandler > HandleRequestOption2 ( ) = > await ConnectLock ( ) ;
2023-04-19 12:14:14 +02:00
/// <summary> Request is not supported, button should be disabled. </summary>
2022-09-06 16:08:19 +02:00
/// <returns></returns>
public async Task < IRequestHandler > UnsupportedRequest ( )
{
Log . ForContext < BookedDisconnected > ( ) . Error ( "Click of unsupported button click detected." ) ;
return await Task . FromResult < IRequestHandler > ( this ) ;
}
/// <summary> Scan for lock.</summary>
/// <returns></returns>
public async Task < IRequestHandler > ConnectLock ( )
{
// Lock list to avoid multiple taps while copri action is pending.
BikesViewModel . IsIdle = false ;
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Information ( "User request to connect to lock of bike {bikeId}." , SelectedBike . Id ) ;
2022-09-06 16:08:19 +02:00
// Stop polling before getting new auth-values.
BikesViewModel . ActionText = AppResources . ActivityTextOneMomentPlease ;
2023-08-31 12:20:06 +02:00
await ViewUpdateManager ( ) . StopAsync ( ) ;
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = AppResources . ActivityTextQuerryServer ;
IsConnected = IsConnectedDelegate ( ) ;
try
{
// Repeat booking to get a new seed/ k_user value.
await ConnectorFactory ( IsConnected ) . Command . CalculateAuthKeys ( SelectedBike ) ;
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Information ( "Calculation of AuthKeys successfully." ) ;
2022-09-06 16:08:19 +02:00
}
catch ( Exception exception )
{
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Information ( "Calculation of AuthKeys failed." ) ;
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = string . Empty ;
2022-10-26 20:53:18 +02:00
if ( exception is WebConnectFailureException
| | exception is RequestNotCachableException )
2022-09-06 16:08:19 +02:00
{
// Copri server is not reachable.
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "Copri server not reachable." ) ;
2022-09-06 16:08:19 +02:00
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorNoConnectionTitle ,
AppResources . ErrorNoWeb ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
}
else
{
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "{@exception}" , exception ) ;
2022-09-06 16:08:19 +02:00
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorConnectLockTitle ,
exception . Message ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
}
// Restart polling again.
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 ;
return this ;
}
2023-11-06 12:23:09 +01:00
// Reconnect to lock
2022-09-06 16:08:19 +02:00
LockInfoTdo result = null ;
var continueConnect = true ;
var retryCount = 1 ;
while ( continueConnect & & result = = null )
{
BikesViewModel . ActionText = AppResources . ActivityTextSearchingLock ;
try
{
result = await LockService . ConnectAsync (
new LockInfoAuthTdo . Builder { Id = SelectedBike . LockInfo . Id , Guid = SelectedBike . LockInfo . Guid , K_seed = SelectedBike . LockInfo . Seed , K_u = SelectedBike . LockInfo . UserKey } . Build ( ) ,
LockService . TimeOut . GetSingleConnect ( retryCount ) ) ;
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Information ( "Connected to lock of bike {bikeId} successfully. Value is {lockState}." , SelectedBike . Id , SelectedBike . LockInfo . State ) ;
2022-09-06 16:08:19 +02:00
}
catch ( Exception exception )
{
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Information ( "Connection to lock of bike {bikeId} failed. " , SelectedBike . Id ) ;
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = string . Empty ;
if ( exception is ConnectBluetoothNotOnException )
{
continueConnect = false ;
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "Bluetooth not on." ) ;
2022-09-06 16:08:19 +02:00
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorConnectLockTitle ,
AppResources . ErrorLockBluetoothNotOn ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
}
else if ( exception is ConnectLocationPermissionMissingException )
{
continueConnect = false ;
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "Location permission missing." ) ;
2022-09-06 16:08:19 +02:00
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorConnectLockTitle ,
AppResources . ErrorNoLocationPermission ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
}
else if ( exception is ConnectLocationOffException )
{
continueConnect = false ;
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "Location services not on." ) ;
2022-09-06 16:08:19 +02:00
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorConnectLockTitle ,
AppResources . ErrorLockLocationOff ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
}
else if ( exception is OutOfReachException )
{
2023-08-31 12:20:06 +02:00
continueConnect = false ;
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "Lock is out of reach." ) ;
2023-08-31 12:20:06 +02:00
await ViewService . DisplayAlert (
AppResources . ErrorConnectLockTitle ,
AppResources . ErrorLockOutOfReach ,
AppResources . MessageAnswerOk ) ;
2022-09-06 16:08:19 +02:00
}
else
{
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "{@exception}" , exception ) ;
2022-09-06 16:08:19 +02:00
string message ;
if ( retryCount < 2 )
{
2023-08-31 12:20:06 +02:00
message = AppResources . ErrorConnectLock ;
2022-09-06 16:08:19 +02:00
}
else if ( retryCount < 3 )
{
2023-08-31 12:20:06 +02:00
message = AppResources . ErrorConnectLockEscalationLevel1 ;
2022-09-06 16:08:19 +02:00
}
else
{
2023-08-31 12:20:06 +02:00
message = AppResources . ErrorConnectLockEscalationLevel2 ;
2022-09-06 16:08:19 +02:00
}
continueConnect = await ViewService . DisplayAdvancedAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorConnectLockTitle ,
2022-09-06 16:08:19 +02:00
message ,
2023-11-06 12:23:09 +01:00
string . Empty ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerRetry ,
AppResources . MessageAnswerCancel ) ;
}
if ( continueConnect )
{
retryCount + + ;
continue ;
}
// Quit and restart polling again.
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 ;
return this ;
}
}
2023-08-31 12:20:06 +02:00
if ( ! ( result ? . State is LockitLockingState lockingState ) )
2022-09-06 16:08:19 +02:00
{
2023-11-06 12:23:09 +01:00
Log . ForContext < BookedDisconnected > ( ) . Error ( "Locking state of bike {bikeId} not found." , SelectedBike . Id ) ;
2022-09-06 16:08:19 +02:00
BikesViewModel . ActionText = string . Empty ;
await ViewService . DisplayAlert (
2023-08-31 12:20:06 +02:00
AppResources . ErrorConnectLockTitle ,
AppResources . ErrorLockNoStatus ,
2022-09-06 16:08:19 +02:00
AppResources . MessageAnswerOk ) ;
// Restart polling again.
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 ;
return this ;
}
2023-11-06 12:23:09 +01:00
// get current locking state
2023-08-31 12:20:06 +02:00
var state = lockingState . GetLockingState ( ) ;
2022-09-06 16:08:19 +02:00
SelectedBike . LockInfo . State = state ;
SelectedBike . LockInfo . Guid = result ? . Guid ? ? new Guid ( ) ;
2023-11-06 12:23:09 +01:00
// get current lock charging level
ILockService btLock = LockService [ SelectedBike . LockInfo . Id ] ;
BikesViewModel . ActionText = AppResources . ActivityTextReadingChargingLevel ;
try
{
SelectedBike . LockInfo . BatteryPercentage = await btLock . GetBatteryPercentageAsync ( ) ;
Log . ForContext < BookedDisconnected > ( ) . Information ( "Battery state of lock from {bikeId} read successfully." , SelectedBike . Id ) ;
}
catch ( Exception exception )
{
Log . ForContext < BookedDisconnected > ( ) . Information ( "Battery state of lock from {bikeId} can not be read." , SelectedBike . Id ) ;
if ( exception is OutOfReachException )
{
Log . ForContext < BookedDisconnected > ( ) . Debug ( "Lock is out of reach." ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorReadingChargingLevelOutOfReach ;
}
else
{
Log . ForContext < BookedDisconnected > ( ) . Debug ( "{@exception}" , exception ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorReadingChargingLevelGeneral ;
}
}
// get lock infos
BikesViewModel . ActionText = AppResources . ActivityTextStartingUpdatingLockingState ;
var versionTdo = btLock . VersionInfo ;
if ( versionTdo ! = null )
{
SelectedBike . LockInfo . VersionInfo = new VersionInfo . Builder
{
FirmwareVersion = versionTdo . FirmwareVersion ,
HardwareVersion = versionTdo . HardwareVersion ,
LockVersion = versionTdo . LockVersion ,
} . Build ( ) ;
}
// update backend
IsConnected = IsConnectedDelegate ( ) ;
try
{
await ConnectorFactory ( IsConnected ) . Command . UpdateLockingStateAsync ( SelectedBike ) ;
Log . ForContext < BookedDisconnected > ( ) . Information ( "Backend updated for bike {bikeId} successfully." , SelectedBike . Id ) ;
}
catch ( Exception exception )
{
Log . ForContext < BookedDisconnected > ( ) . Information ( "Updating backend for bike {bikeId} failed." , SelectedBike . Id ) ;
if ( exception is WebConnectFailureException )
{
// No web.
Log . ForContext < BookedDisconnected > ( ) . Debug ( "Copri server not reachable, no web." ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorNoWebUpdateingLockstate ;
}
else if ( exception is ResponseException copriException )
{
// Copri exception.
Log . ForContext < BookedDisconnected > ( ) . Debug ( "{response}" , copriException . Response ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorStatusUpdateingLockstate ;
}
else
{
Log . ForContext < BookedDisconnected > ( ) . Debug ( "{@exception}" , exception ) ;
BikesViewModel . ActionText = AppResources . ActivityTextErrorConnectionUpdateingLockstate ;
}
}
2022-09-06 16:08:19 +02:00
// Restart polling again.
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
}
}
2021-05-13 20:03:07 +02:00
}