using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Serilog; using ShareeBike.Model.Bikes.BikeInfoNS.BluetoothLock; using ShareeBike.Model.Connector; using ShareeBike.Model.Connector.Updater; using ShareeBike.Model.Device; using ShareeBike.Repository; using ShareeBike.Repository.Request; using ShareeBike.Repository.Response; using ShareeBike.Repository.Response.Stations; namespace ShareeBike.Model.Services.CopriApi { /// Object which manages calls to copri in a thread safe way including cache functionality. public class CopriProviderHttps : ICachedCopriServer { /// Object which manages stored copri answers. private ICopriCache CacheServer { get; } /// Communicates with 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 connect to https using a cache object. /// /// Provides app related info (app name and version, merchant id) to pass to COPRI. /// Two letter ISO language name. /// Holds info about smart device. /// Cookie of user if a user is logged in, false otherwise. /// Timespan which holds value after which cache expires. public CopriProviderHttps( Uri copriHost, string merchantId, AppContextInfo appContextInfo, string uiIsoLangugageName, ISmartDevice smartDevice = null, string sessionCookie = null, TimeSpan? expiresAfter = null, ICopriCache cacheServer = null, ICopriServer httpsServer = null) { CacheServer = cacheServer ?? new CopriCallsMonkeyStore(merchantId, uiIsoLangugageName, sessionCookie, smartDevice, expiresAfter); HttpsServer = httpsServer ?? new CopriCallsHttps(copriHost, appContextInfo, uiIsoLangugageName, sessionCookie, smartDevice); } /// Gets bikes available. /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. /// Id of station which is used for filtering bikes. Null if no filtering should be applied. /// Id of bike which is used for filtering bikes. Null if no filtering should be applied. /// Response holding list of bikes. public async Task> GetBikesAvailable( bool fromCache = false, Uri operatorUri = null, string stationId = null, string bikeId = null) { 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."); var bikesAvailableResponse = await CacheServer.GetBikesAvailableAsync(operatorUri, stationId, bikeId); return new Result(typeof(CopriCallsMonkeyStore), bikesAvailableResponse, bikesAvailableResponse.GetGeneralData()); } try { Log.ForContext().Debug($"Querying bikes available from copri."); var bikesAvailableResponse = await HttpsServer.GetBikesAvailableAsync(operatorUri, stationId, bikeId); return new Result( typeof(CopriCallsHttps), bikesAvailableResponse.GetIsResponseOk(MultilingualResources.AppResources.ErrorBikesAvailableResponseNotOk), bikesAvailableResponse.GetGeneralData()); } catch (Exception exception) { // Return response from cache. Log.ForContext().Debug("An error occurred querying bikes available. {Exception}.", exception); var bikesAvailableResponse = await CacheServer.GetBikesAvailableAsync(operatorUri, stationId, bikeId); return new Result(typeof(CopriCallsMonkeyStore), bikesAvailableResponse, bikesAvailableResponse.GetGeneralData(), exception); } } /// Gets a list of bikes reserved/ booked by active 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. var bikesOccupiedResponse = await CacheServer.GetBikesOccupiedAsync(); Log.ForContext().Debug($"Returning bikes occupied from cache."); return new Result(typeof(CopriCallsMonkeyStore), bikesOccupiedResponse, bikesOccupiedResponse.GetGeneralData()); } try { Log.ForContext().Debug($"Querying bikes occupied from copri."); var bikesOccupiedResponse = await HttpsServer.GetBikesOccupiedAsync(); return new Result( typeof(CopriCallsHttps), bikesOccupiedResponse.GetIsResponseOk("Abfrage der reservierten/ gebuchten Räder fehlgeschlagen."), bikesOccupiedResponse.GetGeneralData()); } catch (Exception exception) { // Return response from cache. Log.ForContext().Debug("An error occurred querying bikes occupied. {Exception}.", exception); var bikesOccupiedResponse = await CacheServer.GetBikesOccupiedAsync(); return new Result(typeof(CopriCallsMonkeyStore), bikesOccupiedResponse, bikesOccupiedResponse.GetGeneralData(), 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."); var stationsResponse = await CacheServer.GetStationsAsync(); return new Result(typeof(CopriCallsMonkeyStore), stationsResponse, stationsResponse.GetGeneralData()); } try { Log.ForContext().Debug($"Querying stations from copri."); var stations = await HttpsServer.GetStationsAsync(); return new Result( typeof(CopriCallsHttps), stations.GetIsResponseOk("Abfrage der Stationen fehlsgeschlagen."), stations.GetGeneralData()); } catch (Exception exception) { // Return response from cache. Log.ForContext().Debug("An error occurred querying stations. {Exception}.", exception); var stationsResponse = await CacheServer.GetStationsAsync(); return new Result(typeof(CopriCallsMonkeyStore), stationsResponse, stationsResponse.GetGeneralData(), 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. /// Uri of the operator host to get bikes from or null if bikes have to be gotten form primary host. /// Id of station which is used for filtering bikes. Null if no filtering should be applied. public void AddToCache( Result result, Uri operatorUri = null, string stationId = null, string bikeId = null) { 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, operatorUri, stationId, bikeId); } /// 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) { var response = await HttpsServer.DoReserveAsync(bikeId, operatorUri); try { response.GetIsResponseOk(string.Empty); } catch { // No need to update cache because reservation failed. return response; } CacheServer.Update(response.bikes_occupied?.GetByBikeId(response.bike)); return response; } public async Task DoCancelReservationAsync(string bikeId, Uri operatorUri) { var response = await HttpsServer.DoCancelReservationAsync(bikeId, operatorUri); try { response.GetIsResponseOk(string.Empty); } catch { // No need to update cache because cancel reservation failed. return response; } CacheServer.Update(response); return response; } public async Task CalculateAuthKeysAsync(string bikeId, Uri operatorUri) { return await HttpsServer.CalculateAuthKeysAsync(bikeId, operatorUri); } public async Task StartReturningBike( string bikeId, Uri operatorUri) => await HttpsServer.StartReturningBike(bikeId, operatorUri); public async Task UpdateLockingStateAsync( string bikeId, lock_state state, Uri operatorUri, LocationDto location, double batteryLevel, IVersionInfo versionInfo) => await HttpsServer.UpdateLockingStateAsync( bikeId, state, operatorUri, location, batteryLevel, versionInfo); /// Books a bike. /// Holds the uri of the operator or null, in case of single operator setup. /// 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. /// If not null next locking action which is performed after booking. /// Response on booking request. public async Task DoBookAsync( Uri operatorUri, string bikeId, Guid guid, double batteryPercentage, LockingAction? nextAction = null) { var response = await HttpsServer.DoBookAsync(operatorUri, bikeId, guid, batteryPercentage, nextAction); try { response.GetIsResponseOk(string.Empty); } catch { // No need to update cache because cancel booking failed. return response; } CacheServer.Update(response.bikes_occupied?.GetByBikeId(response.bike)); return response; } /// 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 async Task BookAvailableAndStartOpeningAsync( string bikeId, Uri operatorUri) => await HttpsServer.BookAvailableAndStartOpeningAsync(bikeId, operatorUri); /// 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 async Task BookReservedAndStartOpeningAsync( string bikeId, Uri operatorUri) => await HttpsServer.BookReservedAndStartOpeningAsync(bikeId, operatorUri); public async Task DoReturn( string bikeId, LocationDto location, Uri operatorUri) { var response = await HttpsServer.DoReturn(bikeId, location, operatorUri); try { response.GetIsResponseOk(string.Empty); } catch { // No need to update cache because returning bike failed. return response; } CacheServer.Update(response); return response; } /// 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 async Task ReturnAndStartClosingAsync( string bikeId, Uri operatorUri) => await HttpsServer.ReturnAndStartClosingAsync(bikeId, 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, int? currentChargeBars, string message, bool isBikeBroken, Uri opertorUri) => await HttpsServer.DoSubmitFeedback(bikeId, currentChargeBars, message, isBikeBroken, opertorUri); /// Submits mini survey to copri server. /// Collection of answers. public async Task DoSubmitMiniSurvey(IDictionary answers) => await HttpsServer.DoSubmitMiniSurvey(answers); } }