sharee.bike-App/TINKLib/Repository/CopriCallsMonkeyStore.cs

343 lines
14 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00
using MonkeyCache.FileStore;
using System;
using System.Threading.Tasks;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Request;
2021-05-13 20:03:07 +02:00
using TINK.Repository.Response;
2021-06-26 20:57:55 +02:00
using TINK.Model.Services.CopriApi;
using TINK.Model.Device;
2021-08-01 17:24:15 +02:00
using System.Collections.Generic;
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
namespace TINK.Repository
2021-05-13 20:03:07 +02:00
{
public class CopriCallsMonkeyStore : ICopriCache
{
/// <summary> Prevents concurrent communictation. </summary>
private object monkeyLock = new object();
/// <summary> Builds requests.</summary>
private IRequestBuilder requestBuilder;
public const string BIKESAVAILABLE = @"{
2021-06-26 20:57:55 +02:00
""copri_version"" : ""4.1.0.0"",
2021-05-13 20:03:07 +02:00
""bikes"" : {},
""response_state"" : ""OK"",
""apiserver"" : ""https://app.tink-konstanz.de"",
""authcookie"" : """",
""response"" : ""bikes_available""
}";
2021-06-26 20:57:55 +02:00
/// <summary> Gets an empty response. </summary>
/// <param name="copriVersion">Version of empty response.</param>
/// <returns>Response.</returns>
public static BikesAvailableResponse GetEmptyBikesAvailableResponse(string copriVersion)
=> JsonConvertRethrow.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE.Replace("4.1.0.0", copriVersion));
#if !COPRIVERSION41
2021-05-13 20:03:07 +02:00
public const string BIKESOCCUPIED = @"{
""debuglevel"" : ""1"",
""user_id"" : """",
""response"" : ""user_bikes_occupied"",
2021-06-26 20:57:55 +02:00
""user_group"" : [ ""Konrad"", ""TINK"" ],
2021-05-13 20:03:07 +02:00
""authcookie"" : """",
""response_state"" : ""OK"",
""bikes_occupied"" : {},
2021-06-26 20:57:55 +02:00
""copri_version"" : ""4.1.0.0"",
2021-05-13 20:03:07 +02:00
""apiserver"" : ""https://app.tink-konstanz.de""
}";
2021-06-26 20:57:55 +02:00
#endif
/// <summary> Gets an empty response. </summary>
/// <param name="copriVersion">Version of empty response.</param>
/// <returns>Response.</returns>
public static BikesReservedOccupiedResponse GetEmptyBikesReservedOccupiedResponse(string copriVersion)
=> JsonConvertRethrow.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED.Replace("4.1.0.0", copriVersion));
#if !COPRIVERSION41
/// <summary> Version COPRI 4.0. or earlier</summary>
public const string STATIONSALL = @"{
""apiserver"" : """",
""authcookie"" : """",
""response"" : ""stations_all"",
""copri_version"" : ""4.1.0.0"",
""stations"" : {},
""response_state"" : ""OK""
}";
#else
/// <summary> Version COPRI 4.1 or later. </summary>
public const string STATIONSALL = @"{
""uri_primary"": """"
""uri_operator_array"": [ ]
2021-05-13 20:03:07 +02:00
""authcookie"" : """",
""response"" : ""stations_all"",
2021-06-26 20:57:55 +02:00
""copri_version"" : ""4.1.0.0"",
2021-05-13 20:03:07 +02:00
""stations"" : {},
""response_state"" : ""OK""
}";
2021-06-26 20:57:55 +02:00
#endif
#if !COPRIVERSION41
/// <summary> Version COPRI 4.0. or earlier</summary>
public const string AUTHORIZATION = @"{
""user_group"" : [ """" ],
""response"" : ""authorization"",
""response_state"" : ""OK"",
""copri_version"" : ""4.1.0.0""
}";
#else
/// <summary> Version COPRI 4.0. or earlier</summary>
public const string AUTHORIZATION = @"{
""user_group"" : [],
""response"" : ""authorization"",
""response_state"" : ""OK"",
""copri_version"" : ""4.1.0.0""
}";
#endif
/// <summary> Gets an empty response. </summary>
/// <param name="copriVersion">Version of empty response.</param>
/// <returns>Response.</returns>
public static StationsAvailableResponse GetEmptyStationsAllResponse(string copriVersion)
=> JsonConvertRethrow.DeserializeObject<StationsAvailableResponse>(STATIONSALL.Replace("4.1.0.0", copriVersion));
2021-05-13 20:03:07 +02:00
/// <summary>
/// Holds the seconds after which station and bikes info is considered to be invalid.
/// Default value 1s.
/// </summary>
private TimeSpan ExpiresAfter { get; }
/// <summary> Returns false because cached values are returned. </summary>
public bool IsConnected => false;
/// <summary> Gets the merchant id.</summary>
public string MerchantId => requestBuilder.MerchantId;
/// <summary> Gets the merchant id.</summary>
public string SessionCookie => requestBuilder.SessionCookie;
/// <summary> Initializes a instance of the copri monkey store object. </summary>
/// <param name="p_strMerchantId">Id of the merchant.</param>
/// <param name="sessionCookie">Session cookie if user is logged in, null otherwise.</param>
public CopriCallsMonkeyStore(
string merchantId,
string sessionCookie = null,
TimeSpan? expiresAfter = null)
{
ExpiresAfter = expiresAfter ?? TimeSpan.FromSeconds(1);
requestBuilder = string.IsNullOrEmpty(sessionCookie)
? new RequestBuilder(merchantId) as IRequestBuilder
: new RequestBuilderLoggedIn(merchantId, sessionCookie);
// Ensure that store holds valid entries.
if (!Barrel.Current.Exists(requestBuilder.GetBikesAvailable()))
{
2021-06-26 20:57:55 +02:00
AddToCache(JsonConvertRethrow.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE), new TimeSpan(0));
2021-05-13 20:03:07 +02:00
}
// Do not query bikes occupied if no user is logged in (leads to not implemented exception)
if (!string.IsNullOrEmpty(sessionCookie) && !Barrel.Current.Exists(requestBuilder.GetBikesOccupied()))
{
2021-06-26 20:57:55 +02:00
AddToCache(JsonConvertRethrow.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED), new TimeSpan(0));
2021-05-13 20:03:07 +02:00
}
if (!Barrel.Current.Exists(requestBuilder.GetStations()))
{
AddToCache(JsonConvertRethrow.DeserializeObject<StationsAvailableResponse>(STATIONSALL), new TimeSpan(0));
2021-05-13 20:03:07 +02:00
}
}
2021-06-26 20:57:55 +02:00
public Task<ReservationBookingResponse> DoReserveAsync(string bikeId, Uri operatorUri)
2021-05-13 20:03:07 +02:00
{
throw new System.Exception("Reservierung im Offlinemodus nicht möglich!");
}
2021-06-26 20:57:55 +02:00
public Task<ReservationCancelReturnResponse> DoCancelReservationAsync(string bikeId, Uri operatorUri)
2021-05-13 20:03:07 +02:00
{
throw new System.Exception("Abbrechen einer Reservierung im Offlinemodus nicht möglich!");
}
2021-06-26 20:57:55 +02:00
public Task<ReservationBookingResponse> CalculateAuthKeysAsync(string bikeId, Uri operatorUri)
2021-05-13 20:03:07 +02:00
=> throw new System.Exception("Schlosssuche im Offlinemodus nicht möglich!");
public Task<ReservationBookingResponse> UpdateLockingStateAsync(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
LocationDto geolocation,
lock_state state,
double batteryLevel,
Uri operatorUri)
=> throw new System.Exception("Aktualisierung des Schlossstatuses im Offlinemodus nicht möglich!");
2021-06-26 20:57:55 +02:00
public Task<ReservationBookingResponse> DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri)
2021-05-13 20:03:07 +02:00
{
throw new System.Exception("Buchung im Offlinemodus nicht möglich!");
}
2021-06-26 20:57:55 +02:00
public Task<ReservationCancelReturnResponse> DoReturn(
string bikeId,
LocationDto geolocation,
ISmartDevice smartDevice,
Uri operatorUri)
2021-05-13 20:03:07 +02:00
{
throw new System.Exception("Rückgabe im Offlinemodus nicht möglich!");
}
2021-06-26 20:57:55 +02:00
public Task<SubmitFeedbackResponse> DoSubmitFeedback(string bikeId, string message, bool isBikeBroken, Uri operatorUri) =>
2021-05-13 20:03:07 +02:00
throw new System.Exception("Übermittlung von Feedback im Offlinemodus nicht möglich!");
2021-08-01 17:24:15 +02:00
/// <summary> Submits mini survey to copri server. </summary>
/// <param name="answers">Collection of answers.</param>
public Task<ResponseBase> DoSubmitMiniSurvey(IDictionary<string, string> answers)
=> throw new System.Exception("Übermittlung von der Miniumfrage im Offlinemodus nicht möglich!");
2021-05-13 20:03:07 +02:00
public Task<AuthorizationResponse> DoAuthorizationAsync(string p_strMailAddress, string p_strPassword, string p_strDeviceId)
{
throw new System.Exception("Anmelden im Offlinemodus nicht möglich!");
}
public Task<AuthorizationoutResponse> DoAuthoutAsync()
{
throw new System.Exception("Abmelden im Offlinemodus nicht möglich!");
}
public async Task<BikesAvailableResponse> GetBikesAvailableAsync()
{
var l_oBikesAvailableTask = new TaskCompletionSource<BikesAvailableResponse>();
lock (monkeyLock)
{
l_oBikesAvailableTask.SetResult(Barrel.Current.Get<BikesAvailableResponse>(requestBuilder.GetBikesAvailable()));
}
return await l_oBikesAvailableTask.Task;
}
public async Task<BikesReservedOccupiedResponse> GetBikesOccupiedAsync()
{
try
{
var l_oBikesOccupiedTask = new TaskCompletionSource<BikesReservedOccupiedResponse>();
lock (monkeyLock)
{
l_oBikesOccupiedTask.SetResult(Barrel.Current.Get<BikesReservedOccupiedResponse>(requestBuilder.GetBikesOccupied()));
}
return await l_oBikesOccupiedTask.Task;
}
catch (NotSupportedException)
{
// No user logged in.
await Task.CompletedTask;
return ResponseHelper.GetBikesOccupiedNone();
}
}
public async Task<StationsAvailableResponse> GetStationsAsync()
2021-05-13 20:03:07 +02:00
{
var l_oStationsAllTask = new TaskCompletionSource<StationsAvailableResponse>();
2021-05-13 20:03:07 +02:00
lock (monkeyLock)
{
l_oStationsAllTask.SetResult(Barrel.Current.Get<StationsAvailableResponse>(requestBuilder.GetStations()));
2021-05-13 20:03:07 +02:00
}
return await l_oStationsAllTask.Task;
}
/// <summary> Gets a value indicating whether stations are expired or not.</summary>
public bool IsStationsExpired
{
get
{
lock (monkeyLock)
{
return Barrel.Current.IsExpired(requestBuilder.GetStations());
}
}
}
/// <summary> Adds a stations all response to cache.</summary>
/// <param name="stations">Stations to add.</param>
public void AddToCache(StationsAvailableResponse stations)
2021-05-13 20:03:07 +02:00
{
AddToCache(stations, ExpiresAfter);
}
/// <summary> Adds a stations all response to cache.</summary>
/// <param name="stations">Stations to add.</param>
/// <param name="expiresAfter">Time after which anser is considered to be expired.</param>
private void AddToCache(StationsAvailableResponse stations, TimeSpan expiresAfter)
2021-05-13 20:03:07 +02:00
{
lock (monkeyLock)
{
Barrel.Current.Add(
requestBuilder.GetStations(),
2021-06-26 20:57:55 +02:00
JsonConvertRethrow.SerializeObject(stations),
2021-05-13 20:03:07 +02:00
expiresAfter);
}
}
/// <summary> Gets a value indicating whether stations are expired or not.</summary>
public bool IsBikesAvailableExpired
{
get
{
lock (monkeyLock)
{
return Barrel.Current.IsExpired(requestBuilder.GetBikesAvailable());
}
}
}
/// <summary> Adds a bikes response to cache.</summary>
/// <param name="bikes">Bikes to add.</param>
public void AddToCache(BikesAvailableResponse bikes)
{
AddToCache(bikes, ExpiresAfter);
}
/// <summary> Adds a bikes response to cache.</summary>
/// <param name="bikes">Bikes to add.</param>
/// <param name="expiresAfter">Time after which anser is considered to be expired.</param>
private void AddToCache(BikesAvailableResponse bikes, TimeSpan expiresAfter)
{
lock (monkeyLock)
{
Barrel.Current.Add(
2021-06-26 20:57:55 +02:00
requestBuilder.GetBikesAvailable(),
JsonConvertRethrow.SerializeObject(bikes),
2021-05-13 20:03:07 +02:00
expiresAfter);
}
}
/// <summary> Gets a value indicating whether stations are expired or not.</summary>
public bool IsBikesOccupiedExpired
{
get
{
lock (monkeyLock)
{
return Barrel.Current.IsExpired(requestBuilder.GetBikesOccupied());
}
}
}
/// <summary> Adds a bikes response to cache.</summary>
/// <param name="bikes">Bikes to add.</param>
public void AddToCache(BikesReservedOccupiedResponse bikes)
{
AddToCache(bikes, ExpiresAfter);
}
/// <summary> Adds a bikes response to cache.</summary>
/// <param name="bikes">Bikes to add.</param>
/// <param name="expiresAfter">Time after which anser is considered to be expired.</param>
private void AddToCache(BikesReservedOccupiedResponse bikes, TimeSpan expiresAfter)
{
lock (monkeyLock)
{
Barrel.Current.Add(
requestBuilder.GetBikesOccupied(),
2021-06-26 20:57:55 +02:00
JsonConvertRethrow.SerializeObject(bikes),
2021-05-13 20:03:07 +02:00
expiresAfter);
}
}
}
}