mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-05 02:26:29 +01:00
209 lines
7.7 KiB
C#
209 lines
7.7 KiB
C#
using Serilog;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using TINK.Model;
|
|
using TINK.Model.Bikes.Bike.CopriLock;
|
|
using TINK.Model.Connector;
|
|
using TINK.Model.Device;
|
|
using TINK.Model.Services.CopriApi;
|
|
using TINK.Repository;
|
|
using TINK.Repository.Response;
|
|
|
|
namespace TINK.Services.CopriApi
|
|
{
|
|
public static class Polling
|
|
{
|
|
/// <summary> Timeout for open/ close operations.</summary>
|
|
private const int OPEN_CLOSE_TIMEOUT_MS = 50000;
|
|
|
|
/// <summary>
|
|
/// Returns a bike and closes the lock.
|
|
/// </summary>
|
|
/// <param name="corpiServer"> Instance to communicate with backend.</param>
|
|
/// <param name="bike">Bike to open.</param>
|
|
public static async Task OpenAync(
|
|
this ICopriServerBase corpiServer,
|
|
IBikeInfoMutable bike)
|
|
{
|
|
if (!(corpiServer is ICachedCopriServer cachedServer))
|
|
throw new ArgumentNullException(nameof(corpiServer));
|
|
|
|
// Send command to close lock
|
|
await corpiServer.UpdateLockingStateAsync(
|
|
bike.Id,
|
|
Repository.Request.lock_state.unlocking,
|
|
bike.OperatorUri);
|
|
|
|
var lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
|
|
var watch = new Stopwatch();
|
|
watch.Start();
|
|
|
|
while (lockingState != LockingState.Open
|
|
&& lockingState != LockingState.UnknownDisconnected
|
|
&& watch.Elapsed < TimeSpan.FromMilliseconds(OPEN_CLOSE_TIMEOUT_MS))
|
|
{
|
|
// Delay a litte to reduce load on backend.
|
|
await Task.Delay(3000);
|
|
|
|
lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
Log.Information($"Current lock state is {lockingState}.");
|
|
}
|
|
|
|
// Update locking state.
|
|
bike.LockInfo.State = lockingState;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Books a bike and opens the lock.
|
|
/// </summary>
|
|
/// <param name="corpiServer"> Instance to communicate with backend.</param>
|
|
/// <param name="bike">Bike to book and open.</param>
|
|
/// <param name="mailAddress">Mail address of user which books bike.</param>
|
|
public static async Task BookAndOpenAync(
|
|
this ICopriServerBase corpiServer,
|
|
IBikeInfoMutable bike,
|
|
string mailAddress)
|
|
{
|
|
if (bike == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(bike), "Can not book bike and open lock. No bike object available.");
|
|
}
|
|
|
|
if (!(corpiServer is ICachedCopriServer cachedServer))
|
|
throw new ArgumentNullException(nameof(corpiServer));
|
|
|
|
// Send command to open lock
|
|
var response = (await corpiServer.BookAndStartOpeningAsync(bike.Id, bike.OperatorUri)).GetIsBookingResponseOk(bike.Id);
|
|
|
|
// Upate booking state
|
|
bike.Load(
|
|
response,
|
|
mailAddress,
|
|
Model.Bikes.Bike.BC.NotifyPropertyChangedLevel.None);
|
|
|
|
// Upated locking state.
|
|
var lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
|
|
var watch = new Stopwatch();
|
|
watch.Start();
|
|
|
|
|
|
while (lockingState!= LockingState.Open
|
|
&& lockingState != LockingState.UnknownDisconnected
|
|
&& watch.Elapsed < TimeSpan.FromMilliseconds(OPEN_CLOSE_TIMEOUT_MS))
|
|
{
|
|
// Delay a litte to reduce load on backend.
|
|
await Task.Delay(3000);
|
|
|
|
lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
Log.Information($"Current lock state is {lockingState}.");
|
|
}
|
|
|
|
// Update locking state.
|
|
bike.LockInfo.State = lockingState;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a bike and closes the lock.
|
|
/// </summary>
|
|
/// <param name="corpiServer"> Instance to communicate with backend.</param>
|
|
/// <param name="bike">Bike to close.</param>
|
|
public static async Task CloseAync(
|
|
this ICopriServerBase corpiServer,
|
|
IBikeInfoMutable bike)
|
|
{
|
|
if (!(corpiServer is ICachedCopriServer cachedServer))
|
|
throw new ArgumentNullException(nameof(corpiServer));
|
|
|
|
// Send command to close lock
|
|
await corpiServer.UpdateLockingStateAsync(
|
|
bike.Id,
|
|
Repository.Request.lock_state.locking,
|
|
bike.OperatorUri);
|
|
|
|
var lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
|
|
var watch = new Stopwatch();
|
|
watch.Start();
|
|
|
|
while (lockingState != LockingState.Closed
|
|
&& lockingState != LockingState.UnknownDisconnected
|
|
&& watch.Elapsed < TimeSpan.FromMilliseconds(OPEN_CLOSE_TIMEOUT_MS))
|
|
{
|
|
// Delay a litte to reduce load on backend.
|
|
await Task.Delay(3000);
|
|
|
|
lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
Log.Information($"Current lock state is {lockingState}.");
|
|
}
|
|
|
|
// Update locking state.
|
|
bike.LockInfo.State = lockingState;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a bike and closes the lock.
|
|
/// </summary>
|
|
/// <param name="corpiServer"> Instance to communicate with backend.</param>
|
|
/// <param name="smartDevice">Smart device on which app runs on.</param>
|
|
/// <param name="mailAddress">Mail address of user which books bike.</param>
|
|
public static async Task<BookingFinishedModel> ReturnAndCloseAync(
|
|
this ICopriServerBase corpiServer,
|
|
ISmartDevice smartDevice,
|
|
IBikeInfoMutable bike)
|
|
{
|
|
if (!(corpiServer is ICachedCopriServer cachedServer))
|
|
throw new ArgumentNullException(nameof(corpiServer));
|
|
|
|
// Send command to open lock
|
|
DoReturnResponse response =
|
|
await corpiServer.ReturnAndStartClosingAsync(bike.Id, smartDevice, bike.OperatorUri);
|
|
|
|
// Upate booking state
|
|
bike.Load(Model.Bikes.Bike.BC.NotifyPropertyChangedLevel.None);
|
|
|
|
var lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
|
|
var watch = new Stopwatch();
|
|
watch.Start();
|
|
|
|
while (lockingState != LockingState.Closed
|
|
&& lockingState != LockingState.UnknownDisconnected
|
|
&& watch.Elapsed < TimeSpan.FromMilliseconds(OPEN_CLOSE_TIMEOUT_MS))
|
|
{
|
|
// Delay a litte to reduce load on backend.
|
|
await Task.Delay(3000);
|
|
|
|
lockingState = await cachedServer.GetLockStateAsync(bike.Id);
|
|
Log.Information($"Current lock state is {lockingState}.");
|
|
}
|
|
|
|
// Update locking state.
|
|
bike.LockInfo.State = lockingState;
|
|
|
|
return response?.Create() ?? new BookingFinishedModel();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Queries the locking state from copri.
|
|
/// </summary>
|
|
/// <param name="corpiServer">Service to use.</param>
|
|
/// <param name="bikeId">Bike id to query lock state for.</param>
|
|
/// <returns>Locking state</returns>
|
|
private static async Task<LockingState> GetLockStateAsync(
|
|
this ICachedCopriServer corpiServer,
|
|
string bikeId)
|
|
{
|
|
var bike = (await corpiServer.GetBikesOccupied(false))?.Response.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
|
|
|
|
if (bike == null)
|
|
return LockingState.UnknownDisconnected;
|
|
|
|
return bike.GetCopriLockingState();
|
|
}
|
|
}
|
|
}
|