using System; using TINK.Model.Device; using TINK.Model.Services.CopriApi; using TINK.Repository; namespace TINK.Model.Connector { /// /// Connects app to copri data by getting data from copri. /// public class Connector : IConnector { /// Constructs a copri connector object to connect to copri by https with cache fallback. /// Uri to connect to. /// Provides app related info (app name and version, merchantid) to pass to COPRI. /// Two letter ISO language name. /// Holds the session cookie. /// Mail of user. /// Holds info about smart device. /// Timespan which holds value after which cache expires. /// Is null in production and migh be a mock in testing context. public Connector( Uri activeUri, AppContextInfo appContextInfo, string uiIsoLangugageName, string sessionCookie, string mail, ISmartDevice smartDevice = null, TimeSpan? expiresAfter = null, ICachedCopriServer server = null) { Command = CreateCommand( server ?? new CopriProviderHttps( activeUri, appContextInfo.MerchantId, appContextInfo, uiIsoLangugageName, smartDevice, sessionCookie), sessionCookie, mail); Query = CreateQuery( server ?? new CopriProviderHttps( activeUri, appContextInfo.MerchantId, appContextInfo, uiIsoLangugageName, smartDevice, sessionCookie, expiresAfter), sessionCookie, mail); } /// Object for queriying stations and bikes. public ICommand Command { get; private set; } /// Object for queriying stations and bikes. public IQuery Query { get; private set; } /// True if connector has access to copri server, false if cached values are used. public bool IsConnected => Command.IsConnected; /// Creates a command object to perform copri commands. 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; /// Creates a command object to perform copri queries. 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); } }