2021-05-13 20:03:07 +02:00
|
|
|
|
using System;
|
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-05-13 20:03:07 +02:00
|
|
|
|
using TINK.Services.BluetoothLock;
|
2022-04-10 17:38:34 +02:00
|
|
|
|
using TINK.Services.Geolocation;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
using TINK.View;
|
|
|
|
|
using TINK.ViewModel.Bikes.Bike.BluetoothLock.RequestHandler;
|
|
|
|
|
|
|
|
|
|
namespace TINK.ViewModel.Bikes.Bike.BluetoothLock
|
|
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
|
public static class RequestHandlerFactory
|
|
|
|
|
{
|
|
|
|
|
/// <summary> Creates a request handler.</summary>
|
|
|
|
|
/// <param name="selectedBike"></param>
|
|
|
|
|
/// <param name="isConnectedDelegate"></param>
|
|
|
|
|
/// <param name="connectorFactory"></param>
|
|
|
|
|
/// <param name="bikeRemoveDelegate"></param>
|
|
|
|
|
/// <param name="viewUpdateManager"></param>
|
|
|
|
|
/// <param name="smartDevice">Provides info about the smart device (phone, tablet, ...)</param>
|
|
|
|
|
/// <param name="viewService"></param>
|
|
|
|
|
/// <param name="bikesViewModel">View model to be used for progress report and unlocking/ locking view.</param>
|
|
|
|
|
/// <returns>Request handler.</returns>
|
|
|
|
|
public static IRequestHandler Create(
|
|
|
|
|
Model.Bikes.BikeInfoNS.BC.IBikeInfoMutable selectedBike,
|
|
|
|
|
Func<bool> isConnectedDelegate,
|
|
|
|
|
Func<bool, IConnector> connectorFactory,
|
|
|
|
|
IGeolocation geolocation,
|
|
|
|
|
ILocksService lockService,
|
|
|
|
|
Func<IPollingUpdateTaskManager> viewUpdateManager,
|
|
|
|
|
ISmartDevice smartDevice,
|
|
|
|
|
IViewService viewService,
|
|
|
|
|
IBikesViewModel bikesViewModel,
|
|
|
|
|
IUser activeUser)
|
|
|
|
|
{
|
|
|
|
|
if (!(selectedBike is Model.Bikes.BikeInfoNS.BluetoothLock.IBikeInfoMutable selectedBluetoothLockBike))
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
switch (selectedBluetoothLockBike.State.Value)
|
|
|
|
|
{
|
|
|
|
|
case Model.State.InUseStateEnum.Disposable:
|
|
|
|
|
|
|
|
|
|
// Bike is reserved, selecte action depending on lock state.
|
|
|
|
|
switch (selectedBluetoothLockBike.LockInfo.State)
|
|
|
|
|
{
|
|
|
|
|
case LockingState.Closed:
|
|
|
|
|
// Unexepected state detected.
|
|
|
|
|
// This state is unexpected because connection is closed
|
|
|
|
|
// - when reservation is canceled or
|
|
|
|
|
// - when bike is returned.
|
|
|
|
|
Log.Error("Unexpected state {BookingState}/ {LockingState} detected.", selectedBluetoothLockBike.State.Value, selectedBluetoothLockBike.LockInfo.State);
|
|
|
|
|
return new InvalidState(
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
selectedBluetoothLockBike.State.Value,
|
|
|
|
|
selectedBluetoothLockBike.LockInfo.State,
|
|
|
|
|
string.Format(AppResources.MarkingBikeInfoErrorStateDisposableClosedDetected, selectedBluetoothLockBike.Description));
|
|
|
|
|
|
|
|
|
|
case LockingState.Open:
|
|
|
|
|
case LockingState.UnknownFromHardwareError:
|
|
|
|
|
// Unexepected state detected.
|
|
|
|
|
/// This state is unexpected because
|
|
|
|
|
/// - app does not allow to return bike/ cancel reservation when lock is closed
|
|
|
|
|
/// - as long as app is connected to lock
|
|
|
|
|
/// - lock can not be opened manually
|
|
|
|
|
/// - no other device can access lock
|
|
|
|
|
/// Nevetheless this state is not expected let user either
|
|
|
|
|
/// - close lock or
|
|
|
|
|
/// - rent bike
|
|
|
|
|
/// </remarks>
|
|
|
|
|
Log.Error("Unexpected state {BookingState}/ {LockingState} detected.", selectedBluetoothLockBike.State.Value, selectedBluetoothLockBike.LockInfo.State);
|
|
|
|
|
return new DisposableOpen(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// Do not allow interaction with lock before reserving bike.
|
|
|
|
|
return new DisposableDisconnected(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Model.State.InUseStateEnum.Reserved:
|
|
|
|
|
|
|
|
|
|
// Bike is reserved, selecte action depending on lock state.
|
|
|
|
|
switch (selectedBluetoothLockBike.LockInfo.State)
|
|
|
|
|
{
|
|
|
|
|
case LockingState.Closed:
|
|
|
|
|
// Lock could not be opened after reserving bike.
|
|
|
|
|
return new ReservedClosed(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
case LockingState.UnknownDisconnected:
|
|
|
|
|
return new ReservedDisconnected(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
case LockingState.Open:
|
|
|
|
|
// Unwanted state detected.
|
|
|
|
|
/// This state might occure when a ILOCKIT was manually opened (color code) and app connects afterwards.
|
|
|
|
|
Log.Error("Unwanted state {BookingState}/ {LockingState} detected.", selectedBluetoothLockBike.State.Value, selectedBluetoothLockBike.LockInfo.State);
|
|
|
|
|
return new ReservedOpen(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
case LockingState.UnknownFromHardwareError:
|
|
|
|
|
// User wants to return bike/ pause ride.
|
|
|
|
|
return new ReservedUnknown(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// Invalid state detected. Lock must never be open if bike is reserved.
|
|
|
|
|
throw new ArgumentException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case Model.State.InUseStateEnum.Booked:
|
|
|
|
|
|
|
|
|
|
// Bike is booked, selecte action depending on lock state.
|
|
|
|
|
switch (selectedBluetoothLockBike.LockInfo.State)
|
|
|
|
|
{
|
|
|
|
|
case LockingState.Closed:
|
|
|
|
|
// Ride was paused.
|
|
|
|
|
return new BookedClosed(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
case LockingState.Open:
|
|
|
|
|
// User wants to return bike/ pause ride.
|
|
|
|
|
return new BookedOpen(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
case LockingState.UnknownFromHardwareError:
|
|
|
|
|
// User wants to return bike/ pause ride.
|
|
|
|
|
return new BookedUnknown(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// Invalid state detected.
|
|
|
|
|
// If bike is booked lock state must be querried before creating view model.
|
|
|
|
|
return new BookedDisconnected(
|
|
|
|
|
selectedBluetoothLockBike,
|
|
|
|
|
isConnectedDelegate,
|
|
|
|
|
connectorFactory,
|
|
|
|
|
geolocation,
|
|
|
|
|
lockService,
|
|
|
|
|
viewUpdateManager,
|
|
|
|
|
smartDevice,
|
|
|
|
|
viewService,
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
activeUser);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// Unexpected copri state detected.
|
|
|
|
|
Log.Error("Unexpected locking {BookingState}/ {LockingState} detected.", selectedBluetoothLockBike.State.Value, selectedBluetoothLockBike.LockInfo.State);
|
|
|
|
|
return new InvalidState(
|
|
|
|
|
bikesViewModel,
|
|
|
|
|
selectedBluetoothLockBike.State.Value,
|
|
|
|
|
selectedBluetoothLockBike.LockInfo.State,
|
|
|
|
|
string.Format(AppResources.MarkingBikeInfoErrorStateUnknownDetected, selectedBluetoothLockBike.Description));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|