sharee.bike-App/SharedBusinessLogic/Model/Connector/Connector.cs
2024-04-09 12:53:23 +02:00

72 lines
2.7 KiB
C#

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