Exemple #1
0
        public static void SendToDb(List <UsbPrinter> printers, ApplicationSettings config, ILogger <Worker> logger)
        {
            try
            {
                using (ApplicationContext db = new ApplicationContext(config))
                {
                    foreach (UsbPrinter usbPrinter in printers)
                    {
                        UsbPrinter p = db.Printers.Include(x => x.Jobs)
                                       .SingleOrDefault(x => x.HardwareSerial.Equals(usbPrinter.HardwareSerial));
                        if (p == null)
                        {
                            db.Printers.Add(usbPrinter);
                            db.SaveChanges();
                            logger.LogInformation(
                                $"Printer {usbPrinter.HardwareSerial} added to DB with {usbPrinter.Jobs.Count} job(s) at {DateTimeOffset.Now}");
                        }
                        else
                        {
                            if (usbPrinter.Jobs.Count != 0)
                            {
                                p.Jobs.AddRange(usbPrinter.Jobs);
                                db.SaveChanges();
                                logger.LogInformation($"Sent {usbPrinter.Jobs.Count} job(s) at {DateTimeOffset.Now}");
                            }

                            if (!p.LastKnowIp.Equals(usbPrinter.LastKnowIp))
                            {
                                p.LastKnowIp = usbPrinter.LastKnowIp;
                                db.SaveChanges();
                                logger.LogInformation(
                                    $"IP of {usbPrinter.HardwareSerial} changed to {usbPrinter.LastKnowIp} at {DateTimeOffset.Now}");
                            }
                        }
                    }

                    if (config.IsWindows)
                    {
                        WindowsHelpers.ClearLog(logger);
                    }
                    else
                    {
                        LinuxHelpers.ClearLog(logger);
                    }
                }
            }
            catch (SqlException sqlException)
            {
                foreach (SqlError sqlError in sqlException.Errors)
                {
                    logger.LogError(sqlError.ToString());
                }
            }
            catch (InvalidOperationException ioException)
            {
                logger.LogError(ioException.Message);
                if (config.IsWindows)
                {
                    WindowsHelpers.ClearLog(logger);
                }
                else
                {
                    LinuxHelpers.ClearLog(logger);
                }
            }
        }
Exemple #2
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                if (config.IsWindows)
                {
                    while (!stoppingToken.IsCancellationRequested)
                    {
                        Windows();
                        await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
                    }
                }
                else
                {
                    while (!stoppingToken.IsCancellationRequested)
                    {
                        Linux();
                        await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
                    }
                }
            }
            catch (TaskCanceledException)
            {
            }
            catch (UnauthorizedAccessException e)
            {
                logger.LogError(e.Message);
            }
            catch (Exception e)
            {
                logger.LogCritical(e.ToString());
                throw;
            }

            void Windows()
            {
                WindowsHelpers.CheckAndEnableLogging(logger);
                List <UsbPrinter> printers = PrinterResolver.GetUsbPrintersData();

                if (printers.Count != 0)
                {
                    foreach (UsbPrinter usbPrinter in printers)
                    {
                        usbPrinter.Jobs = JobsParser.ParseJobsForPrinter(usbPrinter);
                        if (usbPrinter.Jobs.Count != 0)
                        {
                            DbSender.SendToDb(printers, config, logger);
                        }
                    }
                }
                else
                {
                    logger.LogWarning($"No USB printer found at:{DateTimeOffset.Now}");
                }
            }

            void Linux()
            {
                LinuxHelpers.CheckAndEnableLogging(logger);
                List <UsbPrinter> printers = PageCounter.Linux.PrinterResolver.GetUsbPrintersData();

                if (printers.Count != 0)
                {
                    foreach (UsbPrinter usbPrinter in printers)
                    {
                        usbPrinter.Jobs = PageCounter.Linux.JobsParser.ParseJobsForPrinter(usbPrinter);
                        if (usbPrinter.Jobs.Count != 0)
                        {
                            DbSender.SendToDb(printers, config, logger);
                        }
                    }
                }
                else
                {
                    logger.LogWarning($"No USB printer found at:{DateTimeOffset.Now}");
                }
            }
        }