Version 3.0.294

This commit is contained in:
Oliver Hauff 2022-04-25 22:15:15 +02:00
parent d92fb4a40f
commit 8f40f2c208
133 changed files with 17890 additions and 14246 deletions

View file

@ -234,11 +234,16 @@ namespace TINK.Model.Services.CopriApi
public async Task<ReservationBookingResponse> UpdateLockingStateAsync(
string bikeId,
LocationDto location,
lock_state state,
double batteryLevel,
Uri operatorUri)
=> await HttpsServer.UpdateLockingStateAsync(bikeId, location, state, batteryLevel, operatorUri);
Uri operatorUri,
LocationDto location,
double batteryLevel)
=> await HttpsServer.UpdateLockingStateAsync(
bikeId,
state,
operatorUri,
location,
batteryLevel);
/// <summary> Books a bike. </summary>
/// <param name="bikeId">Id of the bike to book.</param>
@ -246,9 +251,17 @@ namespace TINK.Model.Services.CopriApi
/// <param name="batteryPercentage">Holds the filling level percentage of the battery.</param>
/// <returns>Response on booking request.</returns>
public async Task<ReservationBookingResponse> DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri)
{
return await HttpsServer.DoBookAsync(bikeId, guid, batteryPercentage, operatorUri);
}
=> await HttpsServer.DoBookAsync(bikeId, guid, batteryPercentage, operatorUri);
/// <summary> Books a bike and starts opening bike. </summary>
/// <param name="bikeId">Id of the bike to book.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response on booking request.</returns>
public async Task<ReservationBookingResponse> BookAndStartOpeningAsync(
string bikeId,
Uri operatorUri)
=> await HttpsServer.BookAndStartOpeningAsync(bikeId, operatorUri);
public async Task<DoReturnResponse> DoReturn(
string bikeId,
@ -257,6 +270,17 @@ namespace TINK.Model.Services.CopriApi
Uri operatorUri)
=> await HttpsServer.DoReturn(bikeId, location, smartDevice, operatorUri);
/// <summary> Returns a bike and starts closing. </summary>
/// <param name="bikeId">Id of the bike to return.</param>
/// <param name="smartDevice">Provides info about hard and software.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response on returning request.</returns>
public async Task<DoReturnResponse> ReturnAndStartClosingAsync(
string bikeId,
ISmartDevice smartDevice,
Uri operatorUri)
=> await HttpsServer.ReturnAndStartClosingAsync(bikeId, smartDevice, operatorUri);
/// <summary>
/// Submits feedback to copri server.
/// </summary>

View file

@ -48,17 +48,27 @@ namespace TINK.Model.Services.CopriApi
public async Task<ReservationBookingResponse> UpdateLockingStateAsync(
string bikeId,
LocationDto geolocation,
lock_state state,
double batteryLevel,
Uri operatorUri)
=> await monkeyStore.UpdateLockingStateAsync(bikeId, geolocation, state, batteryLevel, operatorUri);
Uri operatorUri,
LocationDto geolocation,
double batteryLevel)
=> await monkeyStore.UpdateLockingStateAsync(
bikeId,
state,
operatorUri,
geolocation,
batteryLevel);
public async Task<ReservationBookingResponse> DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri)
{
return await monkeyStore.DoBookAsync(bikeId, guid, batteryPercentage, operatorUri);
}
public async Task<ReservationBookingResponse> BookAndStartOpeningAsync(
string bikeId,
Uri operatorUri)
=> await monkeyStore.BookAndStartOpeningAsync(bikeId, operatorUri);
public async Task<DoReturnResponse> DoReturn(
string bikeId,
LocationDto geolocation,
@ -66,6 +76,12 @@ namespace TINK.Model.Services.CopriApi
Uri operatorUri)
=> await monkeyStore.DoReturn(bikeId, geolocation, smartDevice, operatorUri);
public async Task<DoReturnResponse> ReturnAndStartClosingAsync(
string bikeId,
ISmartDevice smartDevice,
Uri operatorUri)
=> await monkeyStore.ReturnAndStartClosingAsync(bikeId, smartDevice, operatorUri);
public Task<SubmitFeedbackResponse> DoSubmitFeedback(string bikeId, string messge, bool bIsBikeBroke, Uri operatorUri)
=> throw new NotImplementedException();

View file

@ -0,0 +1,209 @@
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();
}
}
}