sharee.bike-App/SharedBusinessLogic/Model/Connector/Connector.cs

72 lines
2.7 KiB
C#
Raw Normal View History

2023-02-22 14:03:35 +01:00
using System;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Device;
using ShareeBike.Model.Services.CopriApi;
using ShareeBike.Repository;
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.Model.Connector
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
/// <summary>
/// Connects app to copri data by getting data from copri.
/// </summary>
public class Connector : IConnector
{
2024-04-09 12:53:23 +02:00
/// <summary>Constructs a copri connector object to connect to copri by https with cache fall back.</summary>
2022-09-06 16:08:19 +02:00
/// <param name="activeUri"> Uri to connect to.</param>
2024-04-09 12:53:23 +02:00
/// <param name="appContextInfo">Provides app related info (app name and version, merchant id) to pass to COPRI.</param>
2022-09-06 16:08:19 +02:00
/// <param name="uiIsoLangugageName">Two letter ISO language name.</param>
/// <param name="sessionCookie"> Holds the session cookie.</param>
/// <param name="mail">Mail of user.</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
/// <param name="expiresAfter">Timespan which holds value after which cache expires.</param>
2024-04-09 12:53:23 +02:00
/// <param name="server"> Is null in production and might be a mock in testing context.</param>
2022-09-06 16:08:19 +02:00
public Connector(
Uri activeUri,
AppContextInfo appContextInfo,
string uiIsoLangugageName,
string sessionCookie,
string mail,
2023-02-22 14:03:35 +01:00
ISmartDevice smartDevice = null,
2022-09-06 16:08:19 +02:00
TimeSpan? expiresAfter = null,
ICachedCopriServer server = null)
{
2024-04-09 12:53:23 +02:00
var cachedServer = server ?? new CopriProviderHttps(
2022-09-06 16:08:19 +02:00
activeUri,
appContextInfo.MerchantId,
appContextInfo,
uiIsoLangugageName,
2023-02-22 14:03:35 +01:00
smartDevice,
2024-04-09 12:53:23 +02:00
sessionCookie,
expiresAfter);
Command = CreateCommand(
cachedServer,
2022-09-06 16:08:19 +02:00
sessionCookie,
mail);
2021-05-13 20:03:07 +02:00
2023-02-22 14:03:35 +01:00
Query = CreateQuery(
2024-04-09 12:53:23 +02:00
cachedServer,
2022-09-06 16:08:19 +02:00
sessionCookie,
mail);
}
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
/// <summary> Object for querying stations and bikes.</summary>
2022-09-06 16:08:19 +02:00
public ICommand Command { get; private set; }
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
/// <summary> Object for querying stations and bikes.</summary>
2022-09-06 16:08:19 +02:00
public IQuery Query { get; private set; }
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> True if connector has access to copri server, false if cached values are used. </summary>
public bool IsConnected => Command.IsConnected;
2021-05-13 20:03:07 +02:00
2023-02-22 14:03:35 +01:00
/// <summary> Creates a command object to perform copri commands. </summary>
public static ICommand CreateCommand(ICopriServerBase copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
2022-09-06 16:08:19 +02:00
? new Command(copri)
: new CommandLoggedIn(copri, sessioncookie, mail, () => DateTime.Now) as ICommand;
2021-05-13 20:03:07 +02:00
2023-02-22 14:03:35 +01:00
/// <summary> Creates a command object to perform copri queries. </summary>
private static IQuery CreateQuery(ICachedCopriServer copri, string sessioncookie, string mail) => string.IsNullOrEmpty(sessioncookie)
2022-09-06 16:08:19 +02:00
? new CachedQuery(copri) as IQuery
: new CachedQueryLoggedIn(copri, sessioncookie, mail, () => DateTime.Now);
}
2021-05-13 20:03:07 +02:00
}