Example #1
0
 public TransactionsPollingWorkerBase(ILogger <PollingWorkerBase <Transaction> > logger,
                                      AdamantApi api,
                                      IPusher pusher,
                                      ANSContext context) : base(api, context, logger)
 {
     _pusher = pusher;
 }
        static async Task Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += Global_UnhandledException;

            #region Config

            var configuration = ConfigurationLoader.GetConfiguration();

            var provider         = configuration["Database:Provider"];
            var connectionName   = configuration["Database:ConnectionString"] ?? "devices";
            var connectionString = configuration.GetConnectionString(connectionName);

            if (!int.TryParse(configuration["SignalsRegistration:Delay"], out int delay))
            {
                delay = 2000;
            }

            if (!Enum.TryParse(configuration["SignalsRegistration:Startup"], out StartupMode startupMode))
            {
                startupMode = StartupMode.database;
            }

            #endregion

            #region Services

            // Data context
            var context = new ANSContext(connectionString, provider);
            context.Database.Migrate();

            // API
            var api = new AdamantApi(configuration);

            #endregion

            #region DI & NLog

            var nLogConfig = configuration["SignalsRegistration:NlogConfig"];
            if (String.IsNullOrEmpty(nLogConfig))
            {
                nLogConfig = "nlog.config";
            }

            else
            {
                nLogConfig = Utilities.HandleUnixHomeDirectory(nLogConfig);
            }

            _logger = NLog.LogManager.LoadConfiguration(nLogConfig).GetCurrentClassLogger();

            var services = new ServiceCollection();

            // Application services

            services.AddSingleton <IConfiguration>(configuration);
            services.AddSingleton <AdamantApi>();
            services.AddSingleton(context);

            services.AddSingleton <SignalsPoller>();

            // Other
            services.AddSingleton <ILoggerFactory, LoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(Logger <>));
            services.AddLogging(b => b.SetMinimumLevel(LogLevel.Trace));

            var serviceProvider = services.BuildServiceProvider();

            var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();

            loggerFactory.AddNLog(new NLogProviderOptions {
                CaptureMessageTemplates = true, CaptureMessageProperties = true
            });

            #endregion

            var totalDevices = context.Devices.Count();
            _logger.Info("Database initialized. Total devices in db: {0}", totalDevices);
            _logger.Info("Starting polling. Delay: {0}ms.", delay);

            var address = configuration["SignalsRegistration:Address"];
            if (string.IsNullOrEmpty(address))
            {
                throw new Exception("ANS account address is required");
            }

            var privateKey = configuration["SignalsRegistration:PrivateKey"];
            if (string.IsNullOrEmpty(privateKey))
            {
                throw new Exception("ANS account private key is required");
            }

            var worker = serviceProvider.GetRequiredService <SignalsPoller>();
            worker.Delay      = TimeSpan.FromMilliseconds(delay);
            worker.Address    = address;
            worker.PrivateKey = privateKey;
            worker.StartPolling(startupMode);

            if (worker.PollingTask != null)
            {
                await worker.PollingTask;
            }
            else
            {
                throw new Exception("Can't await worker");
            }
        }
Example #3
0
 protected PollingWorkerBase(AdamantApi api, ANSContext context, ILogger <PollingWorkerBase <T> > logger)
 {
     Api     = api;
     Context = context;
     Logger  = logger;
 }
 public ChatPollingWorker(ILogger <ChatPollingWorker> logger,
                          AdamantApi api,
                          IPusher pusher,
                          ANSContext context) : base(logger, api, pusher, context)
 {
 }
        static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += Global_UnhandledException;

            #region Config

            var configuration = ConfigurationLoader.GetConfiguration();

            var provider         = configuration["Database:Provider"];
            var connectionName   = configuration["Database:ConnectionString"] ?? "devices";
            var connectionString = configuration.GetConnectionString(connectionName);

            if (!int.TryParse(configuration["PollingWorker:Delay"], out int delay))
            {
                delay = 2000;
            }

            if (!Enum.TryParse(configuration["PollingWorker:Startup"], out StartupMode startupMode))
            {
                startupMode = StartupMode.database;
            }

            #endregion

            #region Services

            // Data context
            _context = new ANSContext(connectionString, provider);
            _context.Database.Migrate();

            // API
            var api = new AdamantApi(configuration);

            #endregion

            #region DI & NLog

            var nLogConfig = configuration["PollingWorker:NlogConfig"];
            if (String.IsNullOrEmpty(nLogConfig))
            {
                nLogConfig = "nlog.config";
            }
            else
            {
                nLogConfig = Utilities.HandleUnixHomeDirectory(nLogConfig);
            }

            _logger = NLog.LogManager.LoadConfiguration(nLogConfig).GetCurrentClassLogger();

            var services = new ServiceCollection();

            // Application services

            services.AddSingleton <IConfiguration>(configuration);
            services.AddSingleton <AdamantApi>();
            services.AddSingleton(typeof(IPusher), typeof(ApplePusher.ApplePusher));
            services.AddSingleton(_context);

            // Polling workers
            services.AddSingleton <ChatPollingWorker>();
            services.AddSingleton <TransferPollingWorker>();

            // Other
            services.AddSingleton <ILoggerFactory, LoggerFactory>();
            services.AddSingleton(typeof(ILogger <>), typeof(Logger <>));
            services.AddLogging(b => b.SetMinimumLevel(LogLevel.Trace));

            var serviceProvider = services.BuildServiceProvider();

            var loggerFactory = serviceProvider.GetRequiredService <ILoggerFactory>();

            loggerFactory.AddNLog(new NLogProviderOptions {
                CaptureMessageTemplates = true, CaptureMessageProperties = true
            });

            #endregion

            var totalDevices = _context.Devices.Count();
            _logger.Info("Database initialized. Total devices in db: {0}", totalDevices);
            _logger.Info("Starting polling. Delay: {0}ms.", delay);

            var applePusher = serviceProvider.GetRequiredService <IPusher>();
            applePusher.OnInvalidToken += ApplePusher_OnInvalidToken;
            applePusher.Start();

            var chatWorker = serviceProvider.GetRequiredService <ChatPollingWorker>();
            chatWorker.Delay = TimeSpan.FromMilliseconds(delay);
            chatWorker.StartPolling(startupMode);

            var transferWorker = serviceProvider.GetRequiredService <TransferPollingWorker>();
            transferWorker.Delay = TimeSpan.FromMilliseconds(delay);
            transferWorker.StartPolling(startupMode);

            Task.WaitAll(chatWorker.PollingTask, transferWorker.PollingTask);
        }
 public SignalsPoller(ILogger <PollingWorkerBase <Transaction> > logger,
                      AdamantApi api,
                      ANSContext context) : base(api, context, logger)
 {
 }