2022-09-20 13:51:55 +02:00
|
|
|
using System;
|
2022-08-30 15:42:25 +02:00
|
|
|
using System.Collections.Generic;
|
2021-05-13 20:03:07 +02:00
|
|
|
using System.Threading.Tasks;
|
2022-08-30 15:42:25 +02:00
|
|
|
using MonkeyCache.FileStore;
|
2024-04-09 12:53:23 +02:00
|
|
|
using SharedBusinessLogic.Tests.Framework.Repository;
|
|
|
|
using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock;
|
|
|
|
using ShareeBike.Model.Connector;
|
|
|
|
using ShareeBike.Model.Device;
|
|
|
|
using ShareeBike.Model.Services.CopriApi;
|
|
|
|
using ShareeBike.MultilingualResources;
|
|
|
|
using ShareeBike.Repository.Request;
|
|
|
|
using ShareeBike.Repository.Response;
|
|
|
|
using ShareeBike.Repository.Response.Stations;
|
|
|
|
|
|
|
|
namespace ShareeBike.Repository
|
2021-05-13 20:03:07 +02:00
|
|
|
{
|
2022-09-06 16:08:19 +02:00
|
|
|
public class CopriCallsMonkeyStore : ICopriCache
|
|
|
|
{
|
2023-11-06 12:23:09 +01:00
|
|
|
/// <summary> Prevents concurrent communication. </summary>
|
2022-09-06 16:08:19 +02:00
|
|
|
private object monkeyLock = new object();
|
2021-05-13 20:03:07 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Builds requests.</summary>
|
|
|
|
private IRequestBuilder requestBuilder;
|
2021-05-13 20:03:07 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public const string BIKESAVAILABLE = @"{
|
2024-04-09 12:53:23 +02:00
|
|
|
""copri_version"" : ""4.1.0.0"",
|
|
|
|
""bikes"" : {},
|
|
|
|
""response_state"" : ""OK"",
|
|
|
|
""apiserver"" : ""https://app.tink-konstanz.de"",
|
|
|
|
""authcookie"" : """",
|
|
|
|
""response"" : ""bikes_available""
|
|
|
|
}";
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
public const string BIKESOCCUPIED = @"{
|
2024-04-09 12:53:23 +02:00
|
|
|
""debuglevel"" : ""1"",
|
|
|
|
""user_id"" : """",
|
|
|
|
""response"" : ""user_bikes_occupied"",
|
|
|
|
""user_group"" : [ ""Citybike"", ""ShareeBike"" ],
|
|
|
|
""authcookie"" : """",
|
|
|
|
""response_state"" : ""OK"",
|
|
|
|
""bikes_occupied"" : {},
|
|
|
|
""copri_version"" : ""4.1.0.0"",
|
|
|
|
""apiserver"" : ""https://app.tink-konstanz.de""
|
|
|
|
}";
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Version COPRI 4.0. or earlier</summary>
|
|
|
|
public const string STATIONSALL = @"{
|
2024-04-09 12:53:23 +02:00
|
|
|
""apiserver"" : """",
|
|
|
|
""authcookie"" : """",
|
|
|
|
""response"" : ""stations_all"",
|
|
|
|
""copri_version"" : ""4.1.0.0"",
|
|
|
|
""stations"" : {},
|
|
|
|
""response_state"" : ""OK"",
|
|
|
|
""bikes_occupied"" : {}
|
|
|
|
}";
|
2022-09-06 16:08:19 +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;
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Holds a cache of copri, i.e. stations and bikes.
|
|
|
|
/// </summary>
|
|
|
|
private readonly CopriResponseModel copriModel;
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Initializes a instance of the copri monkey store object. </summary>
|
2023-11-21 15:26:57 +01:00
|
|
|
/// <param name="merchantId">Id of the merchant. Used to access </param>
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
|
|
|
|
/// <param name="sessionCookie">Session cookie if user is logged in, null otherwise.</param>
|
2023-02-22 14:03:35 +01:00
|
|
|
/// <param name="smartDevice">Holds info about smart device.</param>
|
2022-09-06 16:08:19 +02:00
|
|
|
public CopriCallsMonkeyStore(
|
|
|
|
string merchantId,
|
|
|
|
string uiIsoLangugageName,
|
|
|
|
string sessionCookie = null,
|
2023-02-22 14:03:35 +01:00
|
|
|
ISmartDevice smartDevice = null,
|
2022-09-06 16:08:19 +02:00
|
|
|
TimeSpan? expiresAfter = null)
|
|
|
|
{
|
|
|
|
ExpiresAfter = expiresAfter ?? TimeSpan.FromSeconds(1);
|
|
|
|
|
|
|
|
requestBuilder = string.IsNullOrEmpty(sessionCookie)
|
2023-02-22 14:03:35 +01:00
|
|
|
? new RequestBuilder(merchantId, uiIsoLangugageName, smartDevice) as IRequestBuilder
|
|
|
|
: new RequestBuilderLoggedIn(merchantId, uiIsoLangugageName, sessionCookie, smartDevice);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
var bikesAvailableEntryExists = Barrel.Current.Exists(requestBuilder.GetBikesAvailable());
|
|
|
|
var bikesAvailable = bikesAvailableEntryExists
|
|
|
|
? Barrel.Current.Get<BikesAvailableResponse>(requestBuilder.GetBikesAvailable())
|
|
|
|
: JsonConvertRethrow.DeserializeObject<BikesAvailableResponse>(BIKESAVAILABLE);
|
2022-09-06 16:08:19 +02:00
|
|
|
// Ensure that store holds valid entries.
|
2024-04-09 12:53:23 +02:00
|
|
|
if (!bikesAvailableEntryExists)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
AddToCache(bikesAvailable, new TimeSpan(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do not query bikes occupied if no user is logged in (leads to not implemented exception)
|
2024-04-09 12:53:23 +02:00
|
|
|
var isLoggedIn = !string.IsNullOrEmpty(sessionCookie);
|
|
|
|
var readBikesOccupiedFromCache = isLoggedIn && Barrel.Current.Exists(requestBuilder.GetBikesOccupied());
|
|
|
|
var bikesOccupied = readBikesOccupiedFromCache
|
|
|
|
? Barrel.Current.Get<BikesReservedOccupiedResponse>(requestBuilder.GetBikesOccupied())
|
|
|
|
: JsonConvertRethrow.DeserializeObject<BikesReservedOccupiedResponse>(BIKESOCCUPIED);
|
|
|
|
|
|
|
|
if (isLoggedIn && !readBikesOccupiedFromCache)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
AddToCache(bikesOccupied, new TimeSpan(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
var stationsEntryExists = Barrel.Current.Exists(requestBuilder.GetStations());
|
|
|
|
var stations = stationsEntryExists
|
|
|
|
? Barrel.Current.Get<StationsAvailableResponse>(requestBuilder.GetStations())
|
|
|
|
: JsonConvertRethrow.DeserializeObject<StationsAvailableResponse>(STATIONSALL);
|
|
|
|
|
|
|
|
if (!stationsEntryExists)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
AddToCache(stations, new TimeSpan(0));
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
2024-04-09 12:53:23 +02:00
|
|
|
|
|
|
|
copriModel = new CopriResponseModel(bikesAvailable, bikesOccupied, stations);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<ReservationBookingResponse> DoReserveAsync(string bikeId, Uri operatorUri)
|
|
|
|
{
|
2023-08-31 12:20:06 +02:00
|
|
|
throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
public Task<BookingActionResponse> DoCancelReservationAsync(string bikeId, Uri operatorUri)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2023-08-31 12:20:06 +02:00
|
|
|
throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<ReservationBookingResponse> CalculateAuthKeysAsync(string bikeId, Uri operatorUri)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
public Task<ResponseBase> StartReturningBike(
|
|
|
|
string bikeId,
|
|
|
|
Uri operatorUri)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
public Task<ReservationBookingResponse> UpdateLockingStateAsync(
|
|
|
|
string bikeId,
|
|
|
|
lock_state state,
|
|
|
|
Uri operatorUri,
|
|
|
|
LocationDto geolocation,
|
2022-09-20 13:51:55 +02:00
|
|
|
double batteryLevel,
|
|
|
|
IVersionInfo versionInfo)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
2022-12-27 21:08:09 +01:00
|
|
|
public Task<ReservationBookingResponse> DoBookAsync(Uri operatorUri, string bikeId, Guid guid, double batteryPercentage, LockingAction? nextAction = null)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <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 Task<ReservationBookingResponse> BookAvailableAndStartOpeningAsync(
|
|
|
|
string bikeId,
|
|
|
|
Uri operatorUri)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <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 Task<ReservationBookingResponse> BookReservedAndStartOpeningAsync(
|
|
|
|
string bikeId,
|
|
|
|
Uri operatorUri)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
public Task<DoReturnResponse> DoReturn(
|
|
|
|
string bikeId,
|
|
|
|
LocationDto geolocation,
|
|
|
|
Uri operatorUri)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <summary> Returns a bike and starts closing. </summary>
|
|
|
|
/// <param name="bikeId">Id of the bike to return.</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 Task<DoReturnResponse> ReturnAndStartClosingAsync(
|
|
|
|
string bikeId,
|
|
|
|
Uri operatorUri)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
2023-08-31 12:20:06 +02:00
|
|
|
public Task<SubmitFeedbackResponse> DoSubmitFeedback(string bikeId, int? currentChargeBars, string message, bool isBikeBroken, Uri operatorUri)
|
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +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)
|
2023-08-31 12:20:06 +02:00
|
|
|
=> throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
public Task<AuthorizationResponse> DoAuthorizationAsync(string p_strMailAddress, string p_strPassword, string p_strDeviceId)
|
|
|
|
{
|
2023-08-31 12:20:06 +02:00
|
|
|
throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<AuthorizationoutResponse> DoAuthoutAsync()
|
|
|
|
{
|
2023-08-31 12:20:06 +02:00
|
|
|
throw new System.Exception(AppResources.ErrorNoWeb);
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
2023-11-21 15:26:57 +01:00
|
|
|
/// <summary>Gets bikes available.</summary>
|
2023-11-06 12:23:09 +01:00
|
|
|
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
|
2023-11-21 15:26:57 +01:00
|
|
|
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
|
|
|
|
/// <param name="bikeId"> Id of bike which is used for filtering bikes. Null if no filtering should be applied.</param>
|
|
|
|
public async Task<BikesAvailableResponse> GetBikesAvailableAsync(
|
|
|
|
Uri operatorUri = null,
|
|
|
|
string stationId = null,
|
|
|
|
string bikeId = null)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
2023-11-06 12:23:09 +01:00
|
|
|
var bikesAvailableTask = new TaskCompletionSource<BikesAvailableResponse>();
|
2024-04-09 12:53:23 +02:00
|
|
|
bikesAvailableTask.SetResult(copriModel.BikesAll
|
|
|
|
.FilterByStation(stationId)
|
|
|
|
.FilterByBike(bikeId));
|
|
|
|
|
2023-11-06 12:23:09 +01:00
|
|
|
return await bikesAvailableTask.Task;
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<BikesReservedOccupiedResponse> GetBikesOccupiedAsync()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
var bikesOccupiedTask = new TaskCompletionSource<BikesReservedOccupiedResponse>();
|
|
|
|
bikesOccupiedTask.SetResult(copriModel.BikesReservedOccupied);
|
|
|
|
return await bikesOccupiedTask.Task;
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
catch (NotSupportedException)
|
|
|
|
{
|
|
|
|
// No user logged in.
|
|
|
|
await Task.CompletedTask;
|
|
|
|
return ResponseHelper.GetBikesOccupiedNone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task<StationsAvailableResponse> GetStationsAsync()
|
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
var stationsAllTask = new TaskCompletionSource<StationsAvailableResponse>();
|
|
|
|
stationsAllTask.SetResult(copriModel.Stations);
|
|
|
|
return await stationsAllTask.Task;
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
var updateTarget = copriModel.Update(stations);
|
|
|
|
|
|
|
|
AddToCache(copriModel.Stations, ExpiresAfter);
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesAll, ExpiresAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesReservedOccupiedResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesReservedOccupied, ExpiresAfter);
|
|
|
|
}
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary> Adds a stations all response to cache.</summary>
|
|
|
|
/// <param name="stations">Stations to add.</param>
|
2023-11-21 15:26:57 +01:00
|
|
|
/// <param name="expiresAfter">Time after which answer is considered to be expired.</param>
|
2022-09-06 16:08:19 +02:00
|
|
|
private void AddToCache(StationsAvailableResponse stations, TimeSpan expiresAfter)
|
|
|
|
{
|
|
|
|
lock (monkeyLock)
|
|
|
|
{
|
|
|
|
Barrel.Current.Add(
|
|
|
|
requestBuilder.GetStations(),
|
|
|
|
JsonConvertRethrow.SerializeObject(stations),
|
|
|
|
expiresAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
/// <summary>
|
|
|
|
/// Updates cache from bike which changed rental state.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="response">Response to update from.</param>
|
|
|
|
public void Update(BikeInfoReservedOrBooked response)
|
|
|
|
{
|
|
|
|
var updateTarget = copriModel.Update(response);
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.StationsAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.Stations, ExpiresAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesAll, ExpiresAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesReservedOccupiedResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesReservedOccupied, ExpiresAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary> Updates cache from bike which changed rental state (reservation/ booking canceled). </summary>
|
|
|
|
public void Update(BookingActionResponse response)
|
|
|
|
{
|
|
|
|
var updateTarget = copriModel.Update(response);
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.StationsAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.Stations, ExpiresAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesAll, ExpiresAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesReservedOccupiedResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesReservedOccupied, ExpiresAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <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>
|
2023-11-21 15:26:57 +01:00
|
|
|
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
|
|
|
|
/// <param name="stationId"> Id of station which was used for filtering bikes. Null if no filtering was applied.</param>
|
|
|
|
/// <param name="bikeId"> Id of bike which was used for filtering bikes. Null if no filtering was applied.</param>
|
2024-04-09 12:53:23 +02:00
|
|
|
public void AddToCache(
|
|
|
|
BikesAvailableResponse bikes,
|
|
|
|
Uri operatorUri = null,
|
|
|
|
string stationId = null,
|
|
|
|
string bikeId = null)
|
|
|
|
{
|
|
|
|
var updateTarget = copriModel.Update(bikes, stationId, bikeId);
|
|
|
|
|
|
|
|
AddToCache(copriModel.BikesAll, ExpiresAfter);
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesReservedOccupiedResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesReservedOccupied, ExpiresAfter);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.StationsAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.Stations, ExpiresAfter);
|
|
|
|
}
|
|
|
|
}
|
2022-09-06 16:08:19 +02:00
|
|
|
|
|
|
|
/// <summary> Adds a bikes response to cache.</summary>
|
|
|
|
/// <param name="bikes">Bikes to add.</param>
|
2023-11-06 12:23:09 +01:00
|
|
|
/// <param name="expiresAfter">Time after which answer is considered to be expired.</param>
|
2023-11-21 15:26:57 +01:00
|
|
|
/// <param name="operatorUri">Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host.</param>
|
|
|
|
/// <param name="stationId"> Id of station which is used for filtering bikes. Null if no filtering should be applied.</param>
|
|
|
|
private void AddToCache(
|
|
|
|
BikesAvailableResponse bikes,
|
2024-04-09 12:53:23 +02:00
|
|
|
TimeSpan expiresAfter)
|
2022-09-06 16:08:19 +02:00
|
|
|
{
|
|
|
|
lock (monkeyLock)
|
|
|
|
{
|
|
|
|
Barrel.Current.Add(
|
2024-04-09 12:53:23 +02:00
|
|
|
$"{requestBuilder.GetBikesAvailable()}",
|
2022-09-06 16:08:19 +02:00
|
|
|
JsonConvertRethrow.SerializeObject(bikes),
|
|
|
|
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)
|
|
|
|
{
|
2024-04-09 12:53:23 +02:00
|
|
|
// Update model in order to ensure a consistent state.
|
|
|
|
var updateTarget = copriModel.Update(bikes);
|
2022-09-06 16:08:19 +02:00
|
|
|
|
2024-04-09 12:53:23 +02:00
|
|
|
// Update cache.
|
|
|
|
AddToCache(copriModel.BikesReservedOccupied, ExpiresAfter);
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.BikesAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.BikesAll, ExpiresAfter);
|
|
|
|
}
|
|
|
|
if (updateTarget.HasFlag(UpdateTarget.StationsAvailableResponse))
|
|
|
|
{
|
|
|
|
AddToCache(copriModel.Stations, ExpiresAfter);
|
|
|
|
}
|
2022-09-06 16:08:19 +02:00
|
|
|
}
|
2024-04-09 12:53:23 +02:00
|
|
|
|
2022-09-06 16:08:19 +02:00
|
|
|
/// <summary> Adds a bikes response to cache.</summary>
|
|
|
|
/// <param name="bikes">Bikes to add.</param>
|
2023-11-21 15:26:57 +01:00
|
|
|
/// <param name="expiresAfter">Time after which answer is considered to be expired.</param>
|
2022-09-06 16:08:19 +02:00
|
|
|
private void AddToCache(BikesReservedOccupiedResponse bikes, TimeSpan expiresAfter)
|
|
|
|
{
|
|
|
|
lock (monkeyLock)
|
|
|
|
{
|
|
|
|
Barrel.Current.Add(
|
|
|
|
requestBuilder.GetBikesOccupied(),
|
|
|
|
JsonConvertRethrow.SerializeObject(bikes),
|
|
|
|
expiresAfter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-13 20:03:07 +02:00
|
|
|
}
|