sharee.bike-App/TINKLib/Model/Connector/Query/CachedQuery.cs

107 lines
3.7 KiB
C#
Raw Normal View History

2023-01-18 14:22:51 +01:00
using System;
2021-05-13 20:03:07 +02:00
using System.Collections.Generic;
using System.Threading.Tasks;
2022-08-30 15:42:25 +02:00
using Serilog;
using TINK.Model.Bikes;
using TINK.Model.Connector.Updater;
2021-05-13 20:03:07 +02:00
using TINK.Model.Services.CopriApi;
2021-06-26 20:57:55 +02:00
using TINK.Repository;
2022-01-04 18:54:03 +01:00
using TINK.Services.CopriApi;
2022-08-30 15:42:25 +02:00
using BikeInfo = TINK.Model.Bikes.BikeInfoNS.BC.BikeInfo;
2021-05-13 20:03:07 +02:00
namespace TINK.Model.Connector
{
2023-01-18 14:22:51 +01:00
/// <summary> Provides query functionality for use without log in. </summary>
2022-09-06 16:08:19 +02:00
public class CachedQuery : Base, IQuery
{
2023-01-18 14:22:51 +01:00
/// <summary> Cached copri server (connection to copri backed up by cache). </summary>
2022-09-06 16:08:19 +02:00
private readonly ICachedCopriServer server;
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary>Constructs a copri query object.</summary>
/// <param name="copriServer">Server which implements communication.</param>
public CachedQuery(
ICopriServerBase copriServer) : base(copriServer)
{
server = copriServer as ICachedCopriServer;
if (server == null)
{
throw new ArgumentException($"Copri server is not of expected typ. Type detected is {copriServer.GetType()}.");
}
}
2021-05-13 20:03:07 +02:00
2023-04-19 12:14:14 +02:00
/// <summary> Gets all stations including positions and bikes.</summary>
2022-09-06 16:08:19 +02:00
public async Task<Result<StationsAndBikesContainer>> GetBikesAndStationsAsync()
{
var resultStations = await server.GetStations();
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
if (resultStations.Source == typeof(CopriCallsMonkeyStore))
{
// Communication with copri in order to get stations failed.
return new Result<StationsAndBikesContainer>(
resultStations.Source,
new StationsAndBikesContainer(
resultStations.Response.GetStationsAllMutable(),
2023-01-18 14:22:51 +01:00
(await server.GetBikesAvailable(true)).Response.GetBikesAvailable(Bikes.BikeInfoNS.BC.DataSource.Cache)),
2022-09-06 16:08:19 +02:00
resultStations.GeneralData,
resultStations.Exception);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
var resultBikes = await server.GetBikesAvailable();
if (resultBikes.Source == typeof(CopriCallsMonkeyStore))
{
// Communication with copri in order to get bikes failed.
return new Result<StationsAndBikesContainer>(
resultBikes.Source,
new StationsAndBikesContainer(
(await server.GetStations(true)).Response.GetStationsAllMutable(),
2023-01-18 14:22:51 +01:00
resultBikes.Response.GetBikesAvailable(Bikes.BikeInfoNS.BC.DataSource.Cache)),
2022-09-06 16:08:19 +02:00
resultBikes.GeneralData,
resultBikes.Exception);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
// Communicatin with copri succeeded.
server.AddToCache(resultStations);
server.AddToCache(resultBikes);
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
return new Result<StationsAndBikesContainer>(
resultStations.Source,
2023-01-18 14:22:51 +01:00
new StationsAndBikesContainer(resultStations.Response.GetStationsAllMutable(), resultBikes.Response.GetBikesAvailable(Bikes.BikeInfoNS.BC.DataSource.Copri)),
2022-09-06 16:08:19 +02:00
resultStations.GeneralData);
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Gets bikes occupied. </summary>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesOccupiedAsync()
{
2023-04-19 12:14:14 +02:00
Log.ForContext<CachedQuery>().Error("Unexpected call to get be bikes occupied detected. No user is logged in.");
2023-01-18 14:22:51 +01:00
2022-09-06 16:08:19 +02:00
return new Result<BikeCollection>(
typeof(CopriCallsMonkeyStore),
await Task.FromResult(new BikeCollection(new Dictionary<string, BikeInfo>())),
new GeneralData(),
new Exception("Abfrage der reservierten/ gebuchten Räder nicht möglich. Kein Benutzer angemeldet."));
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Gets bikes available. </summary>
/// <returns>Collection of bikes.</returns>
public async Task<Result<BikeCollection>> GetBikesAsync()
{
var result = await server.GetBikesAvailable();
2023-01-18 14:22:51 +01:00
if (result.Source != typeof(CopriCallsMonkeyStore))
{
server.AddToCache(result);
}
return new Result<BikeCollection>(
result.Source,
result.Response.GetBikesAvailable(result.Source == typeof(CopriCallsMonkeyStore)
? Bikes.BikeInfoNS.BC.DataSource.Cache
: Bikes.BikeInfoNS.BC.DataSource.Copri),
result.GeneralData,
result.Exception);
2022-09-06 16:08:19 +02:00
}
}
2021-05-13 20:03:07 +02:00
}