using System; using System.Collections.Generic; using System.Threading.Tasks; using MonkeyCache.FileStore; using TINK.Model.Bikes.BikeInfoNS.BluetoothLock; using TINK.Model.Connector; using TINK.Model.Device; using TINK.Model.Services.CopriApi; using TINK.MultilingualResources; using TINK.Repository.Request; using TINK.Repository.Response; using TINK.Repository.Response.Stations; namespace TINK.Repository { public class CopriCallsMonkeyStore : ICopriCache { /// Prevents concurrent communication. private object monkeyLock = new object(); /// Builds requests. private IRequestBuilder requestBuilder; public const string BIKESAVAILABLE = @"{ ""copri_version"" : ""4.1.0.0"", ""bikes"" : {}, ""response_state"" : ""OK"", ""apiserver"" : ""https://app.tink-konstanz.de"", ""authcookie"" : """", ""response"" : ""bikes_available"" }"; /// Gets an empty response. /// Version of empty response. /// Response. public static BikesAvailableResponse GetEmptyBikesAvailableResponse(string copriVersion) => JsonConvertRethrow.DeserializeObject(BIKESAVAILABLE.Replace("4.1.0.0", copriVersion)); #if !COPRIVERSION41 public const string BIKESOCCUPIED = @"{ ""debuglevel"" : ""1"", ""user_id"" : """", ""response"" : ""user_bikes_occupied"", ""user_group"" : [ ""Konrad"", ""TINK"" ], ""authcookie"" : """", ""response_state"" : ""OK"", ""bikes_occupied"" : {}, ""copri_version"" : ""4.1.0.0"", ""apiserver"" : ""https://app.tink-konstanz.de"" }"; #endif /// Gets an empty response. /// Version of empty response. /// Response. public static BikesReservedOccupiedResponse GetEmptyBikesReservedOccupiedResponse(string copriVersion) => JsonConvertRethrow.DeserializeObject(BIKESOCCUPIED.Replace("4.1.0.0", copriVersion)); #if !COPRIVERSION41 /// Version COPRI 4.0. or earlier public const string STATIONSALL = @"{ ""apiserver"" : """", ""authcookie"" : """", ""response"" : ""stations_all"", ""copri_version"" : ""4.1.0.0"", ""stations"" : {}, ""response_state"" : ""OK"" }"; #else /// Version COPRI 4.1 or later. public const string STATIONSALL = @"{ ""uri_primary"": """" ""uri_operator_array"": [ ] ""authcookie"" : """", ""response"" : ""stations_all"", ""copri_version"" : ""4.1.0.0"", ""stations"" : {}, ""response_state"" : ""OK"" }"; #endif #if !COPRIVERSION41 /// Version COPRI 4.0. or earlier public const string AUTHORIZATION = @"{ ""user_group"" : [ """" ], ""response"" : ""authorization"", ""response_state"" : ""OK"", ""copri_version"" : ""4.1.0.0"" }"; #else /// Version COPRI 4.0. or earlier public const string AUTHORIZATION = @"{ ""user_group"" : [], ""response"" : ""authorization"", ""response_state"" : ""OK"", ""copri_version"" : ""4.1.0.0"" }"; #endif /// Gets an empty response. /// Version of empty response. /// Response. public static StationsAvailableResponse GetEmptyStationsAllResponse(string copriVersion) => JsonConvertRethrow.DeserializeObject(STATIONSALL.Replace("4.1.0.0", copriVersion)); /// /// Holds the seconds after which station and bikes info is considered to be invalid. /// Default value 1s. /// private TimeSpan ExpiresAfter { get; } /// Returns false because cached values are returned. public bool IsConnected => false; /// Gets the merchant id. public string MerchantId => requestBuilder.MerchantId; /// Gets the merchant id. public string SessionCookie => requestBuilder.SessionCookie; /// Initializes a instance of the copri monkey store object. /// Id of the merchant. /// Two letter ISO language name. /// Session cookie if user is logged in, null otherwise. /// Holds info about smart device. public CopriCallsMonkeyStore( string merchantId, string uiIsoLangugageName, string sessionCookie = null, ISmartDevice smartDevice = null, TimeSpan? expiresAfter = null) { ExpiresAfter = expiresAfter ?? TimeSpan.FromSeconds(1); requestBuilder = string.IsNullOrEmpty(sessionCookie) ? new RequestBuilder(merchantId, uiIsoLangugageName, smartDevice) as IRequestBuilder : new RequestBuilderLoggedIn(merchantId, uiIsoLangugageName, sessionCookie, smartDevice); // Ensure that store holds valid entries. if (!Barrel.Current.Exists(requestBuilder.GetBikesAvailable())) { AddToCache(JsonConvertRethrow.DeserializeObject(BIKESAVAILABLE), new TimeSpan(0)); } // 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())) { AddToCache(JsonConvertRethrow.DeserializeObject(BIKESOCCUPIED), new TimeSpan(0)); } if (!Barrel.Current.Exists(requestBuilder.GetStations())) { AddToCache(JsonConvertRethrow.DeserializeObject(STATIONSALL), new TimeSpan(0)); } } public Task DoReserveAsync(string bikeId, Uri operatorUri) { throw new System.Exception(AppResources.ErrorNoWeb); } public Task DoCancelReservationAsync(string bikeId, Uri operatorUri) { throw new System.Exception(AppResources.ErrorNoWeb); } public Task CalculateAuthKeysAsync(string bikeId, Uri operatorUri) => throw new System.Exception(AppResources.ErrorNoWeb); public Task StartReturningBike( string bikeId, Uri operatorUri) => throw new System.Exception(AppResources.ErrorNoWeb); public Task UpdateLockingStateAsync( string bikeId, lock_state state, Uri operatorUri, LocationDto geolocation, double batteryLevel, IVersionInfo versionInfo) => throw new System.Exception(AppResources.ErrorNoWeb); public Task DoBookAsync(Uri operatorUri, string bikeId, Guid guid, double batteryPercentage, LockingAction? nextAction = null) => throw new System.Exception(AppResources.ErrorNoWeb); /// Books a bike and starts opening bike. /// Id of the bike to book. /// Holds the uri of the operator or null, in case of single operator setup. /// Response on booking request. public Task BookAvailableAndStartOpeningAsync( string bikeId, Uri operatorUri) => throw new System.Exception(AppResources.ErrorNoWeb); /// Books a bike and starts opening bike. /// Id of the bike to book. /// Holds the uri of the operator or null, in case of single operator setup. /// Response on booking request. public Task BookReservedAndStartOpeningAsync( string bikeId, Uri operatorUri) => throw new System.Exception(AppResources.ErrorNoWeb); public Task DoReturn( string bikeId, LocationDto geolocation, Uri operatorUri) => throw new System.Exception(AppResources.ErrorNoWeb); /// Returns a bike and starts closing. /// Id of the bike to return. /// Holds the uri of the operator or null, in case of single operator setup. /// Response on returning request. public Task ReturnAndStartClosingAsync( string bikeId, Uri operatorUri) => throw new System.Exception(AppResources.ErrorNoWeb); public Task DoSubmitFeedback(string bikeId, int? currentChargeBars, string message, bool isBikeBroken, Uri operatorUri) => throw new System.Exception(AppResources.ErrorNoWeb); /// Submits mini survey to copri server. /// Collection of answers. public Task DoSubmitMiniSurvey(IDictionary answers) => throw new System.Exception(AppResources.ErrorNoWeb); public Task DoAuthorizationAsync(string p_strMailAddress, string p_strPassword, string p_strDeviceId) { throw new System.Exception(AppResources.ErrorNoWeb); } public Task DoAuthoutAsync() { throw new System.Exception(AppResources.ErrorNoWeb); } /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. public async Task GetBikesAvailableAsync(Uri operatorUri = null) { var bikesAvailableTask = new TaskCompletionSource(); lock (monkeyLock) { bikesAvailableTask.SetResult(Barrel.Current.Get($"{operatorUri?.AbsoluteUri ?? string.Empty}{requestBuilder.GetBikesAvailable()}")); } return await bikesAvailableTask.Task; } public async Task GetBikesOccupiedAsync() { try { var l_oBikesOccupiedTask = new TaskCompletionSource(); lock (monkeyLock) { l_oBikesOccupiedTask.SetResult(Barrel.Current.Get(requestBuilder.GetBikesOccupied())); } return await l_oBikesOccupiedTask.Task; } catch (NotSupportedException) { // No user logged in. await Task.CompletedTask; return ResponseHelper.GetBikesOccupiedNone(); } } public async Task GetStationsAsync() { var l_oStationsAllTask = new TaskCompletionSource(); lock (monkeyLock) { l_oStationsAllTask.SetResult(Barrel.Current.Get(requestBuilder.GetStations())); } return await l_oStationsAllTask.Task; } /// Gets a value indicating whether stations are expired or not. public bool IsStationsExpired { get { lock (monkeyLock) { return Barrel.Current.IsExpired(requestBuilder.GetStations()); } } } /// Adds a stations all response to cache. /// Stations to add. public void AddToCache(StationsAvailableResponse stations) { AddToCache(stations, ExpiresAfter); } /// Adds a stations all response to cache. /// Stations to add. /// Time after which anser is considered to be expired. private void AddToCache(StationsAvailableResponse stations, TimeSpan expiresAfter) { lock (monkeyLock) { Barrel.Current.Add( requestBuilder.GetStations(), JsonConvertRethrow.SerializeObject(stations), expiresAfter); } } /// Gets a value indicating whether stations are expired or not. public bool IsBikesAvailableExpired { get { lock (monkeyLock) { return Barrel.Current.IsExpired(requestBuilder.GetBikesAvailable()); } } } /// Adds a bikes response to cache. /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. /// Bikes to add. public void AddToCache(BikesAvailableResponse bikes, Uri operatorUri = null) => AddToCache(bikes, ExpiresAfter, operatorUri); /// Adds a bikes response to cache. /// Bikes to add. /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. /// Time after which answer is considered to be expired. private void AddToCache(BikesAvailableResponse bikes, TimeSpan expiresAfter, Uri operatorUri = null) { lock (monkeyLock) { Barrel.Current.Add( $"{operatorUri?.AbsoluteUri ?? string.Empty}{requestBuilder.GetBikesAvailable()}", JsonConvertRethrow.SerializeObject(bikes), expiresAfter); } } /// Gets a value indicating whether stations are expired or not. public bool IsBikesOccupiedExpired { get { lock (monkeyLock) { return Barrel.Current.IsExpired(requestBuilder.GetBikesOccupied()); } } } /// Adds a bikes response to cache. /// Bikes to add. public void AddToCache(BikesReservedOccupiedResponse bikes) { AddToCache(bikes, ExpiresAfter); } /// Adds a bikes response to cache. /// Bikes to add. /// Time after which anser is considered to be expired. private void AddToCache(BikesReservedOccupiedResponse bikes, TimeSpan expiresAfter) { lock (monkeyLock) { Barrel.Current.Add( requestBuilder.GetBikesOccupied(), JsonConvertRethrow.SerializeObject(bikes), expiresAfter); } } } }