2021-05-13 20:03:07 +02:00
|
|
|
|
using Serilog;
|
|
|
|
|
using System;
|
2021-08-01 17:24:15 +02:00
|
|
|
|
using System.Collections.Generic;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
using System.Threading.Tasks;
|
2022-01-04 18:54:03 +01:00
|
|
|
|
using TINK.Model.Connector;
|
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>
|
2022-01-04 18:54:03 +01:00
|
|
|
|
/// <param name="appContextInfo">Provides app related info (app name and version, merchantid) to pass to COPRI.</param>
|
2021-05-13 20:03:07 +02:00
|
|
|
|
/// <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,
|
2022-01-04 18:54:03 +01:00
|
|
|
|
AppContextInfo appContextInfo,
|
2021-05-13 20:03:07 +02:00
|
|
|
|
string sessionCookie = null,
|
|
|
|
|
TimeSpan? expiresAfter = null,
|
|
|
|
|
ICopriCache cacheServer = null,
|
|
|
|
|
ICopriServer httpsServer = null)
|
|
|
|
|
{
|
|
|
|
|
CacheServer = cacheServer ?? new CopriCallsMonkeyStore(merchantId, sessionCookie, expiresAfter);
|
2022-01-04 18:54:03 +01:00
|
|
|
|
HttpsServer = httpsServer ?? new CopriCallsHttps(copriHost, appContextInfo, sessionCookie);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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.");
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var bikesAvailableResponse = await CacheServer.GetBikesAvailableAsync();
|
|
|
|
|
return new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), bikesAvailableResponse, bikesAvailableResponse.GetGeneralData());
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Querrying bikes available from copri.");
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var bikesAvailableResponse = await HttpsServer.GetBikesAvailableAsync();
|
2021-05-13 20:03:07 +02:00
|
|
|
|
return new Result<BikesAvailableResponse>(
|
|
|
|
|
typeof(CopriCallsHttps),
|
2022-01-04 18:54:03 +01:00
|
|
|
|
bikesAvailableResponse.GetIsResponseOk("Abfrage der verfügbaren Räder fehlgeschlagen."),
|
|
|
|
|
bikesAvailableResponse.GetGeneralData());
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
// Return response from cache.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying bikes available. {Exception}.", exception);
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var bikesAvailableResponse = await CacheServer.GetBikesAvailableAsync();
|
|
|
|
|
return new Result<BikesAvailableResponse>(typeof(CopriCallsMonkeyStore), bikesAvailableResponse, bikesAvailableResponse.GetGeneralData(), exception);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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.
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var bikesOccupiedResponse = await CacheServer.GetBikesOccupiedAsync();
|
2021-05-13 20:03:07 +02:00
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Returning bikes occupied from cache.");
|
2022-01-04 18:54:03 +01:00
|
|
|
|
return new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), bikesOccupiedResponse, bikesOccupiedResponse.GetGeneralData());
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Querrying bikes occupied from copri.");
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var bikesOccupiedResponse = await HttpsServer.GetBikesOccupiedAsync();
|
2021-05-13 20:03:07 +02:00
|
|
|
|
return new Result<BikesReservedOccupiedResponse>(
|
|
|
|
|
typeof(CopriCallsHttps),
|
2022-01-04 18:54:03 +01:00
|
|
|
|
bikesOccupiedResponse.GetIsResponseOk("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen."),
|
|
|
|
|
bikesOccupiedResponse.GetGeneralData());
|
2021-05-13 20:03:07 +02:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exception)
|
|
|
|
|
{
|
|
|
|
|
// Return response from cache.
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug("An error occurred querrying bikes occupied. {Exception}.", exception);
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var bikesOccupiedResponse = await CacheServer.GetBikesOccupiedAsync();
|
|
|
|
|
return new Result<BikesReservedOccupiedResponse>(typeof(CopriCallsMonkeyStore), bikesOccupiedResponse, bikesOccupiedResponse.GetGeneralData(), exception);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary> Get list of stations. </summary>
|
|
|
|
|
/// <returns>List of files.</returns>
|
2021-07-20 23:06:09 +02:00
|
|
|
|
public async Task<Result<StationsAvailableResponse>> GetStations(bool fromCache = false)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
|
|
|
|
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.");
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var stationsResponse = await CacheServer.GetStationsAsync();
|
|
|
|
|
return new Result<StationsAvailableResponse>(typeof(CopriCallsMonkeyStore), stationsResponse, stationsResponse.GetGeneralData());
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Log.ForContext<CopriProviderHttps>().Debug($"Querrying stations from copri.");
|
2021-06-26 20:57:55 +02:00
|
|
|
|
|
|
|
|
|
var stations = await HttpsServer.GetStationsAsync();
|
|
|
|
|
|
2021-07-20 23:06:09 +02:00
|
|
|
|
return new Result<StationsAvailableResponse>(
|
2021-05-13 20:03:07 +02:00
|
|
|
|
typeof(CopriCallsHttps),
|
2022-01-04 18:54:03 +01:00
|
|
|
|
stations.GetIsResponseOk("Abfrage der Stationen fehlsgeschlagen."),
|
|
|
|
|
stations.GetGeneralData());
|
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);
|
2022-01-04 18:54:03 +01:00
|
|
|
|
var stationsResponse = await CacheServer.GetStationsAsync();
|
|
|
|
|
return new Result<StationsAvailableResponse>(typeof(CopriCallsMonkeyStore), stationsResponse, stationsResponse.GetGeneralData(), exception);
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Adds https--response to cache if response is ok. </summary>
|
|
|
|
|
/// <param name="response">Response to add to cache.</param>
|
|
|
|
|
/// <returns></returns>
|
2021-07-20 23:06:09 +02:00
|
|
|
|
public void AddToCache(Result<StationsAvailableResponse> result)
|
2021-05-13 20:03:07 +02:00
|
|
|
|
{
|
|
|
|
|
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-12-08 17:57:30 +01:00
|
|
|
|
public async Task<DoReturnResponse> DoReturn(
|
2021-06-26 20:57:55 +02:00
|
|
|
|
string bikeId,
|
|
|
|
|
LocationDto location,
|
|
|
|
|
ISmartDevice smartDevice,
|
|
|
|
|
Uri operatorUri)
|
2021-12-08 17:57:30 +01:00
|
|
|
|
=> 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-08-01 17:24:15 +02:00
|
|
|
|
|
|
|
|
|
/// <summary> Submits mini survey to copri server. </summary>
|
|
|
|
|
/// <param name="answers">Collection of answers.</param>
|
|
|
|
|
public async Task<ResponseBase> DoSubmitMiniSurvey(IDictionary<string, string> answers)
|
|
|
|
|
=> await HttpsServer.DoSubmitMiniSurvey(answers);
|
|
|
|
|
|
2021-05-13 20:03:07 +02:00
|
|
|
|
}
|
|
|
|
|
}
|