internal PrintCommandHandler(IPrintCommand cmd, IPrinterNotifier notifier, params ISufficientLevelIndicator[] indicators)
        {
            _cmd = cmd;
            _n   = notifier ?? new EmptyPrinterNotifier();

            InjectSufficientLevelIndicators(indicators);

            ValidateHandler();
        }
Exemple #2
0
 public Printer(IPaperLevelIndicator paperLevelIndicator,
                IBlackLevelIndicator blackLevelIndicator,
                IColorLevelIndicator colorLevelIndicator,
                IPrinterNotifier notifier)
 {
     _pli = paperLevelIndicator;
     _bli = blackLevelIndicator;
     _cli = colorLevelIndicator;
     _n   = notifier;
 }
        public PrintBlackCommand(byte[] content,
                                 IPaperLevelIndicator paperLevelIndicator,
                                 IBlackLevelIndicator blackLevelIndicator,
                                 IPrinterNotifier notifier = null)
        {
            if (content is null || content.Length == 0)
            {
                throw new NoContentToPrintException();
            }

            _handler = new PrintCommandHandler(this, notifier, paperLevelIndicator, blackLevelIndicator);
            Content  = content;
            Id       = Guid.NewGuid();
        }
        private static async Task Print(string content)
        {
            byte[] c = System.Text.Encoding.UTF8.GetBytes(content);

            IPaperLevelIndicator pli = _printerType == "p" ?
                                       new ComplexPaperLevelIndicator((Int32)Math.Ceiling((double)(c.Length / 100))) as IPaperLevelIndicator :
                                       new SimplePaperLevelIndicator() as IPaperLevelIndicator;
            IBlackLevelIndicator bli = new SimpleBlackLevelIndicator();
            IPrinterNotifier     n   = null;
            IPrintCommand        cmd;

            if (_notifie == true)
            {
                n = new RabbitMq(_config);
            }

            try
            {
                if (_printType == "c")
                {
                    IColorLevelIndicator cli = _printerType == "p" ?
                                               new ComplexColorLevelIndicator() as IColorLevelIndicator :
                                               new SimpleColorLevelIndicator() as IColorLevelIndicator;
                    cmd = new PrintColorCommand(content: c,
                                                paperLevelIndicator: pli,
                                                blackLevelIndicator: bli,
                                                colorLevelIndicator: cli,
                                                notifier: n);
                }
                else
                {
                    cmd = new PrintBlackCommand(content: c,
                                                paperLevelIndicator: pli,
                                                blackLevelIndicator: bli,
                                                notifier: n);
                }
                if (!(n is null))
                {
                    await n.Subscribe((msg) =>
                    {
                        if (_printType == "c")
                        {
                            ForegroundColor = ConsoleColor.Cyan;
                        }

                        WriteLine(msg);
                        ResetColor();
                    }, PrinterNotificationTypes.Printed, cmd.Id);
                }

                await cmd.Execute();
            }
            catch (CannotConnectToNotificationServiceException)
            {
                Write("Can not connect to notification service. Do you want to proceed? [y/n]: ");
                var key = ReadKey();
                Write(NewLine);
                if (key.Key == ConsoleKey.Y)
                {
                    _notifie = false;
                    await Print(content);
                }
            }
            catch (ApplicationException ex)
            {
                WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                ForegroundColor = ConsoleColor.Magenta;
                WriteLine(ex.Message);
                ResetColor();

                ConsoleKeyInfo key;
                do
                {
                    key = ReadKey(intercept: false);
                }while (key.Key == ConsoleKey.Enter);

                await ProcessInput("exit");
            }
            finally
            {
                _processing  = false;
                _printerType = null;
                _printType   = null;
                _notifie     = null;
                n?.Dispose();
                await ProcessInput(ReadLine());
            }
        }