using Serilog; using System; using System.Collections.Generic; using System.Threading.Tasks; using TINK.Model.Bike; using TINK.Model.Services.CopriApi; using TINK.Repository; using BikeInfo = TINK.Model.Bike.BC.BikeInfo; namespace TINK.Model.Connector { public class CachedQuery : Base, IQuery { /// Cached copri server. private readonly ICachedCopriServer server; /// Constructs a copri query object. /// Server which implements communication. public CachedQuery( ICopriServerBase p_oCopriServer) : base(p_oCopriServer) { server = p_oCopriServer as ICachedCopriServer; if (server == null) { throw new ArgumentException($"Copri server is not of expected typ. Type detected is {p_oCopriServer.GetType()}."); } } /// Gets all stations including postions and bikes. public async Task> GetBikesAndStationsAsync() { var resultStations = await server.GetStations(); if (resultStations.Source == typeof(CopriCallsMonkeyStore)) { // Communication with copri in order to get stations failed. return new Result( resultStations.Source, new StationsAndBikesContainer( resultStations.Response.GetStationsAllMutable(), (await server.GetBikesAvailable(true)).Response.GetBikesAvailable()), resultStations.Exception); } var resultBikes = await server.GetBikesAvailable(); if (resultBikes.Source == typeof(CopriCallsMonkeyStore)) { // Communication with copri in order to get bikes failed. return new Result( resultBikes.Source, new StationsAndBikesContainer( (await server.GetStations(true)).Response.GetStationsAllMutable(), resultBikes.Response.GetBikesAvailable()), resultBikes.Exception); } // Communicatin with copri succeeded. server.AddToCache(resultStations); server.AddToCache(resultBikes); return new Result( resultStations.Source, new StationsAndBikesContainer(resultStations.Response.GetStationsAllMutable(), resultBikes.Response.GetBikesAvailable())); } /// Gets bikes occupied. /// Collection of bikes. public async Task> GetBikesOccupiedAsync() { Log.ForContext().Error("Unexpected call to get be bikes occpied detected. No user is logged in."); return new Result( typeof(CopriCallsMonkeyStore), await Task.Run(() => new BikeCollection(new Dictionary())), new System.Exception("Abfrage der reservierten/ gebuchten Räder nicht möglich. Kein Benutzer angemeldet.")); } /// Gets bikes available. /// Collection of bikes. public async Task> GetBikesAsync() { var result = await server.GetBikesAvailable(); server.AddToCache(result); return new Result(result.Source, result.Response.GetBikesAvailable(), result.Exception); } } }