예제 #1
0
        private static void onNewBatchObserved(List <Data.IPollerUnit> polled)
        {
            Console.WriteLine(
                $"\nЗагрузка...\n{polled.Aggregate(new StringBuilder(), (sb, item) => sb.Append($"{item.Timestamp.ToString("HH:mm:ss:ffff")} {item.Content} \n"))}");

            var _moment = polled.Aggregate(new StringBuilder(),
                                           (sb, item) => sb.Append($"{item.Timestamp.ToString("HH:mm:ss:ffff")}\n"));
            var _keystroke = polled.Aggregate(new StringBuilder(), (sb, item) => sb.Append($"{item.Content}\n"));

            using (dbService db = new dbService())
            {
                table_csharp_test group = new table_csharp_test
                {
                    moment_presses = _moment.ToString(), keystroke = _keystroke.ToString(), duration_sec = period
                };
                db.csharp_test.Add(group);
                db.SaveChanges();
            }
        }
예제 #2
0
        public static string period = null; //this shit

        static async Task Main(string[] args)
        {
            using (dbService db = new dbService())
            {
                var dt = db.csharp_test
                         .FromSqlRaw($"SELECT * FROM `csharp_test` WHERE `id`=(SELECT MAX(`id`)FROM `csharp_test`)")
                         .ToList();
                Console.WriteLine("Последняя сохраненная группа:");

                foreach (table_csharp_test u in dt)
                {
                    Console.Write(
                        $"момент нажатия:\n{u.moment_presses}нажатая клавиша:\n{u.keystroke}интервал опроса:{u.duration_sec} сек.");
                }
            }

            var pollingPeriod = TimeSpan.FromSeconds(1);

            var greetings = Task.Run(async() =>
            {
                Console.WriteLine(
                    "\n\nДобро пожаловать в бесполезное приложение для опроса!"); //Welcome to useless polling application!

                // display latest previous batch of polled units here (loaded from database)

                Console.WriteLine(
                    "Выберите интервал опроса измеряемый в секундах и нажмите клавишу Enter"); //Select pollling interval measured in seconds and press Enter

                pollingPeriod = await SelectPollingPeriod(TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(10));
                Console.WriteLine(
                    "Нажмите ESC для выхода или любую другую клавишу для входа в систему\n"); //Press ESC to exit or any other key to log it
            });
            await greetings;

            var cancelSource = new CancellationTokenSource();
            var transmitter  = new Subject <Data.PollerUnit>();

            var poller = new PolledConsumerWrapper(transmitter, pollingPeriod, onNewBatchObserved, cancelSource);

            period = pollingPeriod.Seconds.ToString();
            var polleach = Task.Run(() =>
            {
                while (!cancelSource.IsCancellationRequested)
                {
                    var key = Console.ReadKey();

                    if (key.Key == ConsoleKey.Escape)
                    {
                        cancelSource.Cancel();
                        break;
                    }

                    transmitter.OnNext(new Data.PollerUnit()
                    {
                        Timestamp = DateTimeOffset.Now,
                        Content   = key.KeyChar.ToString()
                    });
                }
            });
            await polleach;

            cancelSource.Cancel();
        }