2021-05-13 20:03:07 +02:00
|
|
|
|
using Serilog;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-06-26 20:57:55 +02:00
|
|
|
|
using TINK.Model.Device;
|
|
|
|
|
using TINK.Repository;
|
|
|
|
|
using TINK.Repository.Request;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
using TINK.Repository.Response;
|
|
|
|
|
|
|
|
|
|
namespace TINK.Model.Services.CopriApi
|
|
|
|
|
{
|
|
|
|
|
/// <summary> Object which manages calls to copri in a thread safe way inclding cache functionality. </summary>
|
|
|
|
|
public class CopriProviderHttps : ICachedCopriServer
|
|
|
|
|
{
|
|
|
|
|
/// <summary> Object which manages stored copri answers. </summary>
|
|
|
|
|
private ICopriCache CacheServer { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary> Communicates whith copri server. </summary>
|
|
|
|
|
private ICopriServer HttpsServer { get; }
|
|
|
|
|
|
|
|
|
|
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
|
|
|
|
|
public bool IsConnected => HttpsServer.IsConnected;
|
|
|
|
|
|
|
|
|
|
/// <summary> Gets the session cookie if user is logged in, an empty string otherwise. </summary>
|
|
|
|
|
public string SessionCookie => HttpsServer.SessionCookie;
|
|
|
|
|
|
|
|
|
|
/// <summary> Gets the merchant id.</summary>
|
|
|
|
|
public string MerchantId => HttpsServer.MerchantId;
|
|
|
|
|
|
|
|
|
|
/// <summary> Constructs copri provider object to connet to https using a cache objet. </summary>
|
|
|
|
|
/// <param name="copriHost"></param>
|
|
|
|
|
/// <param name="merchantId"></param>
|
|
|
|
|
/// <param name="userAgent">Holds the name and version of the TINKApp.</param>
|
|
|
|
|
/// <param name="sessionCookie">Cookie of user if a user is logged in, false otherwise.</param>
|
|
|
|
|
/// <param name="expiresAfter">Timespan which holds value after which cache expires.</param>
|
|
|
|
|
/// <param name="isExpired">Delegate which returns if cache conted is out of date or not.</param>
|
|
|
|
|
public CopriProviderHttps(
|
|
|
|
|
Uri copriHost,
|
|
|
|
|
string merchantId,
|
|
|
|
|
string userAgent,
|
|
|
|
|
string sessionCookie = null,
|
|
|
|
|
TimeSpan? expiresAfter = null,
|
|
|
|
|
ICopriCache cacheServer = null,
|
|
|
|
|
ICopriServer httpsServer = null)
|
|
|
|
|
{
|
|
|
|
|
CacheServer = cacheServer ?? new CopriCallsMonkeyStore(merchantId, sessionCookie, expiresAfter);
|
|
|
|
|
HttpsServer = httpsServer ?? new CopriCallsHttps(copriHost, merchantId, userAgent, sessionCookie);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Gets bikes available.</summary>
|
|
|
|
|
/// <param name="p_strMerchantId">Id of the merchant.</param>
|
|
|
|
|
/// <param name="sessionCookie">Auto cookie of user if user is logged in.</param>
|
|
|
|
|
/// <returns>Response holding list of bikes.</returns>
|
|
|
|
|
public async Task<Result<BikesAvailableResponse>> GetBikesAvailable(bool fromCache = false)
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Request to get bikes available{(fromCache ? " from cache" : "")}...");
|
|
|
|
|
if (!CacheServer.IsBikesAvailableExpired
|
|
|
|
|
|| fromCache)
|
|
|
|
|
{
|
|
|
|
|
// No need to query because previous answer is not yet outdated.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Returning bikes available from cache.");
|
|
|
|
|
return new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesAvailableAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Querrying bikes available from copri.");
|
|
|
|
|
return new Result<BikesAvailableResponse>(
|
|
|
|
|
typeof(CopriCallsHttps),
|
|
|
|
|
(await HttpsServer.GetBikesAvailableAsync()).GetIsResponseOk("Abfrage der verfügbaren Räder fehlgeschlagen."));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
// Return response from cache.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying bikes available. {Exception}.", exception);
|
|
|
|
|
return new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesAvailableAsync(), exception);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary> Gets a list of bikes reserved/ booked by acctive user. </summary>
|
|
|
|
|
/// <param name="sessionCookie">Cookie to authenticate user.</param>
|
|
|
|
|
/// <returns>Response holding list of bikes.</returns>
|
|
|
|
|
public async Task<Result<BikesReservedOccupiedResponse>> GetBikesOccupied(bool fromCache = false)
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Request to get bikes occupied{(fromCache ? " from cache" : "")}...");
|
|
|
|
|
if (!CacheServer.IsBikesOccupiedExpired
|
|
|
|
|
|| fromCache)
|
|
|
|
|
{
|
|
|
|
|
// No need to query because previous answer is not yet outdated.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Returning bikes occupied from cache.");
|
|
|
|
|
return new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesOccupiedAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Querrying bikes occupied from copri.");
|
|
|
|
|
return new Result<BikesReservedOccupiedResponse>(
|
|
|
|
|
typeof(CopriCallsHttps),
|
|
|
|
|
(await HttpsServer.GetBikesOccupiedAsync()).GetIsResponseOk("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen."));
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
// Return response from cache.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying bikes occupied. {Exception}.", exception);
|
|
|
|
|
return new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesOccupiedAsync(), exception);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary> Get list of stations. </summary>
|
|
|
|
|
/// <returns>List of files.</returns>
|
|
|
|
|
public async Task<Result<StationsAllResponse>> GetStations(bool fromCache = false)
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Request to get stations{(fromCache ? " from cache" : "")}...");
|
|
|
|
|
if (!CacheServer.IsStationsExpired
|
|
|
|
|
|| fromCache)
|
|
|
|
|
{
|
|
|
|
|
// No need to query because previous answer is not yet outdated.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Returning stations from cache.");
|
|
|
|
|
return new Result<StationsAllResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetStationsAsync());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Querrying stations from copri.");
|
2021-06-26 20:57:55 +02:00
|
|
|
|
|
|
|
|
|
var stations = await HttpsServer.GetStationsAsync();
|
|
|
|
|
|
2021-05-13 20:03:07 +02:00
|
|
|
|
return new Result<StationsAllResponse>(
|
|
|
|
|
typeof(CopriCallsHttps),
|
2021-06-26 20:57:55 +02:00
|
|
|
|
stations.GetIsResponseOk("Abfrage der Stationen fehlsgeschlagen."));
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
// Return response from cache.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying stations. {Exception}.", exception);
|
|
|
|
|
return new Result<StationsAllResponse>(typeof(CopriCallsMonkeyStore), await CacheServer.GetStationsAsync(), exception);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds https--response to cache if response is ok. </summary>
|
|
|
|
|
/// <param name="response">Response to add to cache.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public void AddToCache(Result<StationsAllResponse> result)
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Request to add stations all response to cache...");
|
|
|
|
|
if (result.Source == typeof(CopriCallsMonkeyStore)
|
|
|
|
|
|| result.Exception != null)
|
|
|
|
|
{
|
|
|
|
|
// Do not add responses form cache or invalid responses to cache.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Add bikes available response to cache.");
|
|
|
|
|
CacheServer.AddToCache(result.Response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds https--response to cache if response is ok. </summary>
|
|
|
|
|
/// <param name="response">Response to add to cache.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public void AddToCache(Result<BikesAvailableResponse> result)
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Request to add bikes available response to cache...");
|
|
|
|
|
if (result.Source == typeof(CopriCallsMonkeyStore)
|
|
|
|
|
|| result.Exception != null)
|
|
|
|
|
{
|
|
|
|
|
// Do not add responses form cache or invalid responses to cache.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Add bikes available response to cache.");
|
|
|
|
|
CacheServer.AddToCache(result.Response);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds https--response to cache if response is ok. </summary>
|
|
|
|
|
/// <param name="response">Response to add to cache.</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public void AddToCache(Result<BikesReservedOccupiedResponse> result)
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Request to add bikes occupied response to cache...");
|
|
|
|
|
if (result.Source == typeof(CopriCallsMonkeyStore)
|
|
|
|
|
|| result.Exception != null)
|
|
|
|
|
{
|
|
|
|
|
// Do not add responses form cache or invalid responses to cache.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Add bikes occupied response to cache.");
|
|
|
|
|
CacheServer.AddToCache(result.Response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AuthorizationResponse> DoAuthorizationAsync(string p_strMailAddress, string p_strPassword, string p_strDeviceId)
|
|
|
|
|
{
|
|
|
|
|
return await HttpsServer.DoAuthorizationAsync(p_strMailAddress, p_strPassword, p_strDeviceId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<AuthorizationoutResponse> DoAuthoutAsync()
|
|
|
|
|
{
|
|
|
|
|
return await HttpsServer.DoAuthoutAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 20:57:55 +02:00
|
|
|
|
public async Task<ReservationBookingResponse> DoReserveAsync(string bikeId, Uri operatorUri)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
2021-06-26 20:57:55 +02:00
|
|
|
|
return await HttpsServer.DoReserveAsync(bikeId, operatorUri);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 20:57:55 +02:00
|
|
|
|
public async Task<ReservationCancelReturnResponse> DoCancelReservationAsync(string bikeId, Uri operatorUri)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
2021-06-26 20:57:55 +02:00
|
|
|
|
return await HttpsServer.DoCancelReservationAsync(bikeId, operatorUri);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 20:57:55 +02:00
|
|
|
|
public async Task<ReservationBookingResponse> CalculateAuthKeysAsync(string bikeId, Uri operatorUri)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
|
|
|
|
return await HttpsServer.CalculateAuthKeysAsync(bikeId, operatorUri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<ReservationBookingResponse> UpdateLockingStateAsync(
|
2021-06-26 20:57:55 +02:00
|
|
|
|
string bikeId,
|
2021-05-13 20:03:07 +02:00
|
|
|
|
LocationDto location,
|
|
|
|
|
lock_state state,
|
|
|
|
|
double batteryLevel,
|
|
|
|
|
Uri operatorUri)
|
|
|
|
|
=> await HttpsServer.UpdateLockingStateAsync(bikeId, location, state, batteryLevel, operatorUri);
|
|
|
|
|
|
|
|
|
|
/// <summary> Books a bike. </summary>
|
|
|
|
|
/// <param name="bikeId">Id of the bike to book.</param>
|
|
|
|
|
/// <param name="guid">Used to publish GUID from app to copri. Used for initial setup of bike in copri.</param>
|
|
|
|
|
/// <param name="batteryPercentage">Holds the filling level percentage of the battery.</param>
|
|
|
|
|
/// <returns>Response on booking request.</returns>
|
2021-06-26 20:57:55 +02:00
|
|
|
|
public async Task<ReservationBookingResponse> DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
|
|
|
|
return await HttpsServer.DoBookAsync(bikeId, guid, batteryPercentage, operatorUri);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 20:57:55 +02:00
|
|
|
|
public async Task<ReservationCancelReturnResponse> DoReturn(
|
|
|
|
|
string bikeId,
|
|
|
|
|
LocationDto location,
|
|
|
|
|
ISmartDevice smartDevice,
|
|
|
|
|
Uri operatorUri)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
2021-06-26 20:57:55 +02:00
|
|
|
|
return await HttpsServer.DoReturn(bikeId, location, smartDevice, operatorUri);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Submits feedback to copri server.
|
|
|
|
|
/// </summary>
|
2021-06-26 20:57:55 +02:00
|
|
|
|
/// <param name="bikeId">Id of the bike to submit feedback for.</param>
|
2021-05-13 20:03:07 +02:00
|
|
|
|
/// <param name="message">General purpose message or error description.</param>
|
|
|
|
|
/// <param name="isBikeBroken">True if bike is broken.</param>
|
2021-06-26 20:57:55 +02:00
|
|
|
|
public async Task<SubmitFeedbackResponse> DoSubmitFeedback(string bikeId, string message, bool isBikeBroken, Uri opertorUri) =>
|
|
|
|
|
await HttpsServer.DoSubmitFeedback(bikeId, message, isBikeBroken, opertorUri);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|