using System; using ShareeBike.Model.Device; using ShareeBike.Model.Services.CopriApi; using ShareeBike.Repository; namespace ShareeBike.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 fall back. /// Uri to connect to. /// Provides app related info (app name and version, merchant id) 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 might 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) { var cachedServer = server ?? new CopriProviderHttps( activeUri, appContextInfo.MerchantId, appContextInfo, uiIsoLangugageName, smartDevice, sessionCookie, expiresAfter); Command = CreateCommand( cachedServer, sessionCookie, mail); Query = CreateQuery( cachedServer, sessionCookie, mail); } /// Object for querying stations and bikes. public ICommand Command { get; private set; } /// Object for querying 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); } }