using Serilog;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TINK.Model.Device;
using TINK.Repository;
using TINK.Repository.Request;
using TINK.Repository.Response;
namespace TINK.Model.Services.CopriApi
{
/// Object which manages calls to copri in a thread safe way inclding cache functionality.
public class CopriProviderHttps : ICachedCopriServer
{
/// Object which manages stored copri answers.
private ICopriCache CacheServer { get; }
/// Communicates whith copri server.
private ICopriServer HttpsServer { get; }
/// True if connector has access to copri server, false if cached values are used.
public bool IsConnected => HttpsServer.IsConnected;
/// Gets the session cookie if user is logged in, an empty string otherwise.
public string SessionCookie => HttpsServer.SessionCookie;
/// Gets the merchant id.
public string MerchantId => HttpsServer.MerchantId;
/// Constructs copri provider object to connet to https using a cache objet.
///
///
/// Holds the name and version of the TINKApp.
/// Cookie of user if a user is logged in, false otherwise.
/// Timespan which holds value after which cache expires.
/// Delegate which returns if cache conted is out of date or not.
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);
}
/// Gets bikes available.
/// Id of the merchant.
/// Auto cookie of user if user is logged in.
/// Response holding list of bikes.
public async Task> GetBikesAvailable(bool fromCache = false)
{
Log.ForContext().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().Debug($"Returning bikes available from cache.");
return new Result(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesAvailableAsync());
}
try
{
Log.ForContext().Debug($"Querrying bikes available from copri.");
return new Result(
typeof(CopriCallsHttps),
(await HttpsServer.GetBikesAvailableAsync()).GetIsResponseOk("Abfrage der verfügbaren Räder fehlgeschlagen."));
}
catch (Exception exception)
{
// Return response from cache.
Log.ForContext().Debug("An error occurred querrying bikes available. {Exception}.", exception);
return new Result(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesAvailableAsync(), exception);
}
}
/// Gets a list of bikes reserved/ booked by acctive user.
/// Cookie to authenticate user.
/// Response holding list of bikes.
public async Task> GetBikesOccupied(bool fromCache = false)
{
Log.ForContext().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().Debug($"Returning bikes occupied from cache.");
return new Result(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesOccupiedAsync());
}
try
{
Log.ForContext().Debug($"Querrying bikes occupied from copri.");
return new Result(
typeof(CopriCallsHttps),
(await HttpsServer.GetBikesOccupiedAsync()).GetIsResponseOk("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen."));
}
catch (Exception exception)
{
// Return response from cache.
Log.ForContext().Debug("An error occurred querrying bikes occupied. {Exception}.", exception);
return new Result(typeof(CopriCallsMonkeyStore), await CacheServer.GetBikesOccupiedAsync(), exception);
}
}
/// Get list of stations.
/// List of files.
public async Task> GetStations(bool fromCache = false)
{
Log.ForContext().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().Debug($"Returning stations from cache.");
return new Result(typeof(CopriCallsMonkeyStore), await CacheServer.GetStationsAsync());
}
try
{
Log.ForContext().Debug($"Querrying stations from copri.");
var stations = await HttpsServer.GetStationsAsync();
return new Result(
typeof(CopriCallsHttps),
stations.GetIsResponseOk("Abfrage der Stationen fehlsgeschlagen."));
}
catch (Exception exception)
{
// Return response from cache.
Log.ForContext().Debug("An error occurred querrying stations. {Exception}.", exception);
return new Result(typeof(CopriCallsMonkeyStore), await CacheServer.GetStationsAsync(), exception);
}
}
/// Adds https--response to cache if response is ok.
/// Response to add to cache.
///
public void AddToCache(Result result)
{
Log.ForContext().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().Debug($"Add bikes available response to cache.");
CacheServer.AddToCache(result.Response);
}
/// Adds https--response to cache if response is ok.
/// Response to add to cache.
///
public void AddToCache(Result result)
{
Log.ForContext().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().Debug($"Add bikes available response to cache.");
CacheServer.AddToCache(result.Response);
}
/// Adds https--response to cache if response is ok.
/// Response to add to cache.
///
public void AddToCache(Result result)
{
Log.ForContext().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().Debug($"Add bikes occupied response to cache.");
CacheServer.AddToCache(result.Response);
}
public async Task DoAuthorizationAsync(string p_strMailAddress, string p_strPassword, string p_strDeviceId)
{
return await HttpsServer.DoAuthorizationAsync(p_strMailAddress, p_strPassword, p_strDeviceId);
}
public async Task DoAuthoutAsync()
{
return await HttpsServer.DoAuthoutAsync();
}
public async Task DoReserveAsync(string bikeId, Uri operatorUri)
{
return await HttpsServer.DoReserveAsync(bikeId, operatorUri);
}
public async Task DoCancelReservationAsync(string bikeId, Uri operatorUri)
{
return await HttpsServer.DoCancelReservationAsync(bikeId, operatorUri);
}
public async Task CalculateAuthKeysAsync(string bikeId, Uri operatorUri)
{
return await HttpsServer.CalculateAuthKeysAsync(bikeId, operatorUri);
}
public async Task UpdateLockingStateAsync(
string bikeId,
LocationDto location,
lock_state state,
double batteryLevel,
Uri operatorUri)
=> await HttpsServer.UpdateLockingStateAsync(bikeId, location, state, batteryLevel, operatorUri);
/// Books a bike.
/// Id of the bike to book.
/// Used to publish GUID from app to copri. Used for initial setup of bike in copri.
/// Holds the filling level percentage of the battery.
/// Response on booking request.
public async Task DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri)
{
return await HttpsServer.DoBookAsync(bikeId, guid, batteryPercentage, operatorUri);
}
public async Task DoReturn(
string bikeId,
LocationDto location,
ISmartDevice smartDevice,
Uri operatorUri)
{
return await HttpsServer.DoReturn(bikeId, location, smartDevice, operatorUri);
}
///
/// Submits feedback to copri server.
///
/// Id of the bike to submit feedback for.
/// General purpose message or error description.
/// True if bike is broken.
public async Task DoSubmitFeedback(string bikeId, string message, bool isBikeBroken, Uri opertorUri) =>
await HttpsServer.DoSubmitFeedback(bikeId, message, isBikeBroken, opertorUri);
/// Submits mini survey to copri server.
/// Collection of answers.
public async Task DoSubmitMiniSurvey(IDictionary answers)
=> await HttpsServer.DoSubmitMiniSurvey(answers);
}
}