예제 #1
0
        public ServiceFactory(IStorage storage, ITelegramBotClient client, ILogger logger)
        {
            this.storage = storage;
            this.logger  = logger;

            userServiceFactory = new UserServiceFactory(storage, logger);
            var metricFactory = new MetricsFactory(Settings.Instance.Env, Settings.Logger);
            var metricService = metricFactory.Create();

            senderFactory = new SenderFactory(client, metricService);
            parserService = new CommandHandlerFactory();

            stateFactory   = new StateFactory(logger);
            contextFactory = new ContextFactory(storage, userServiceFactory, stateFactory, this);
        }
예제 #2
0
        private static async Task <int> Main(string[] args)
        {
            Console.WriteLine("App starting, press Ctrl+C to cancel.");
            Console.CancelKeyPress += Console_CancelKeyPress;

            Log.Logger = new LoggerConfiguration()
                         .WriteTo.Console()
                         .CreateLogger();

            DeviceConfiguration config = DeviceConfigReader.GetDeviceConfig();

            if (!config.Validate(out string error))
            {
                Log.Error("Device config could not be loaded; {error}", error);
                return(-1);
            }

            Console.WriteLine();

            Console.Write("Renew IoT hub connection periodically (y/n)? ");
            ConsoleKeyInfo key = Console.ReadKey();

            Console.WriteLine();
            Console.WriteLine();

            bool renewHubConnection = key.Key == ConsoleKey.Y;

            Console.WriteLine();

            CancellationToken cancelToken = cancellationTokenSource.Token;

            IMetricsRoot metrics = MetricsFactory.Create();

            var deviceHubClient = new DeviceHubClient(config, metrics);
            await deviceHubClient.EnsureHubConnectionAsync(DispatchDirectCall, cancelToken);

            var         tasksModule = new TasksModule(deviceHubClient, metrics);
            List <Task> tasks       = tasksModule.GetTasks(cancelToken).ToList();

            if (renewHubConnection)
            {
                const int intervalMinutes = 5;
                Log.Information(
                    "IoT hub connection will be recreated every {interval} minutes regardless of status",
                    intervalMinutes);
                tasks.Add(deviceHubClient.RenewClientAsync(cancelToken, intervalMinutes));
            }
            else
            {
                Log.Information("IoT hub connection will not be periodically recreated");
            }

            try
            {
                await Task.WhenAll(tasks);
            }
            catch (TaskCanceledException)
            {
                Log.Information("App run cancelled");
            }
            catch (AggregateException)
            {
                Log.Information("Error running tasks");
            }

            return(0);
        }