private void Save_Click(object sender, RoutedEventArgs e)
        {
            printerInfo = new PrinterInfo {
                Address = PrinterAddress_TextBox.Text
            };

            Connection       connection       = null;
            ZebraCardPrinter zebraCardPrinter = null;

            try {
                connection = new TcpConnection(printerInfo.Address, 9100);
                connection.Open();

                zebraCardPrinter  = ZebraCardPrinterFactory.GetInstance(connection);
                printerInfo.Model = zebraCardPrinter.GetPrinterInformation().Model;
                if (printerInfo.Model.ToLower().Contains("zxp1") || printerInfo.Model.ToLower().Contains("zxp3"))
                {
                    throw new ConnectionException("Printer model not supported");
                }
            } catch (Exception error) {
                printerInfo = null;
                MessageBoxHelper.ShowError(error.Message);
            } finally {
                ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);
                Close();
            }
        }
Esempio n. 2
0
        public void PrinterOnline(ZebraCardPrinter zebraCardPrinter, string firmwareVersion)
        {
            Connection newConnection = null;

            try {
                DiscoveredPrinter newPrinter = null;
                newConnection = zebraCardPrinter.Connection;
                newConnection.Open();

                Dictionary <string, string> discoveryData = DiscoveryUtilCard.GetDiscoveryDataMap(newConnection);

                if (oldPrinter is DiscoveredUsbPrinter)
                {
                    newPrinter = new DiscoveredUsbPrinter((newConnection as UsbConnection).SimpleConnectionName, discoveryData);
                }
                else if (oldPrinter is DiscoveredCardPrinterNetwork)
                {
                    newPrinter = new DiscoveredCardPrinterNetwork(discoveryData);
                }
                else
                {
                    throw new ArgumentException("Not a reconnectable printer type");
                }

                ReconnectionFinished(newPrinter);
            } catch (Exception e) {
                MessageBoxHelper.ShowError("Could not reconnect to printer: " + e.Message);
            } finally {
                ConnectionHelper.CleanUpQuietly(zebraCardPrinter, newConnection);
            }
        }
Esempio n. 3
0
        public GraphicsInfo CreateBackGraphics(ZebraCardPrinter zebraCardPrinter, ZebraCardPrintDetails printDetails)
        {
            using (ZebraCardGraphics graphics = new ZebraCardGraphics(zebraCardPrinter))
            {
                int?fillColour = null;
                graphics.Initialize(0, 0, OrientationType.Landscape, PrintType.MonoK, fillColour);

                // Loop through back text, if any
                if (printDetails.BackPanelText != null & printDetails.BackPanelText.Length > 0)
                {
                    foreach (var text in printDetails.BackPanelZebraText)
                    {
                        text.Add(graphics);
                    }
                }

                // Loop through back images, if any
                if (printDetails.BackPanelImages != null & printDetails.BackPanelImages.Length > 0)
                {
                    foreach (var image in printDetails.BackPanelZebraImages)
                    {
                        image.Add(graphics);
                    }
                }

                ZebraCardImageI zebraCardImage = graphics.CreateImage();
                return(AddImage(CardSide.Back, PrintType.MonoK, 0, 0, -1, zebraCardImage));
            }
        }
Esempio n. 4
0
        private int PrintAndMagEncode(ZebraCardPrinter zebraCardPrinter, MultiJobControlViewModel jobViewModel)
        {
            using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter)) {
                List <GraphicsInfo> graphicsInfoList = new List <GraphicsInfo>();

                if (jobViewModel.HasValidFrontSide)
                {
                    foreach (GraphicsInfo graphicsInfo in CreateGraphicsInfo(graphics, jobViewModel.FrontSideGraphicFilenames, CardSide.Front))
                    {
                        graphicsInfoList.Add(graphicsInfo);
                    }
                }

                if (jobViewModel.HasValidBackSide)
                {
                    foreach (GraphicsInfo graphicsInfo in CreateGraphicsInfo(graphics, jobViewModel.BackSideGraphicFilenames, CardSide.Back))
                    {
                        graphicsInfoList.Add(graphicsInfo);
                    }
                }

                int jobId;
                if (jobViewModel.HasValidMagEncodeJob)
                {
                    jobId = zebraCardPrinter.PrintAndMagEncode(jobViewModel.SelectedQuantity, graphicsInfoList, jobViewModel.Track1Data, jobViewModel.Track2Data, jobViewModel.Track3Data);
                }
                else
                {
                    jobId = zebraCardPrinter.Print(jobViewModel.SelectedQuantity, graphicsInfoList);
                }
                return(jobId);
            }
        }
Esempio n. 5
0
        public short Connect()
        {
            try
            {
                _connection = _discoveredPrinter.GetConnection();
                _connection.Open();

                if (_connection.Connected)
                {
                    _zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(_connection);

                    List <JobStatus> _joblist = _zebraCardPrinter.GetJobList();

                    if (_joblist != null && _joblist.Count > 0)
                    {
                        _currentPrintJob = _joblist.Max(i => i.ID);
                    }
                    else
                    {
                        _currentPrintJob = 0;
                    }

                    _zebraCardPrinter.RegisterAlarmHandler(this);

                    return(PrinterCodes.Success);
                }

                return(PrinterCodes.ConnectFailed);
            }
            catch
            {
                CloseQuietly(_connection, _zebraCardPrinter);
                throw;
            }
        }
Esempio n. 6
0
        public ZebraZC3(Connection connection, ZebraCardPrinter zebraCardPrinter, Error.IZebraErrorFactory zebraErrorFactory) : this()
        {
            _connection       = connection ?? throw new ArgumentNullException(nameof(connection));
            _zebraCardPrinter = zebraCardPrinter ?? throw new ArgumentNullException(nameof(zebraCardPrinter));
            _errorFactory     = zebraErrorFactory ?? throw new ArgumentNullException(nameof(zebraErrorFactory));

            // TODO: See how we can set properties from connection or zebraCardPrinter
        }
Esempio n. 7
0
        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            string ipAddressText = IpAddressInput.Text;

            IndeterminateProgressDialog indeterminateProgressDialog = new IndeterminateProgressDialog($"Connecting to {ipAddressText}...");

            Task.Run(() => {
                Application.Current.Dispatcher.Invoke(() => {
                    indeterminateProgressDialog.ShowDialog();
                });
            });

            Task.Run(() => {
                TcpConnection connection          = null;
                ZebraCardPrinter zebraCardPrinter = null;

                try {
                    connection = GetTcpConnection(ipAddressText);
                    connection.Open();

                    zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

                    string model = zebraCardPrinter.GetPrinterInformation().Model;
                    if (model != null)
                    {
                        if (!model.ToLower().Contains("zxp1") && !model.ToLower().Contains("zxp3"))
                        {
                            printerManager.Printer = new DiscoveredCardPrinterNetwork(DiscoveryUtilCard.GetDiscoveryDataMap(connection));

                            Application.Current.Dispatcher.Invoke(() => {
                                Close();
                            });
                        }
                        else
                        {
                            throw new ConnectionException("Printer model not supported");
                        }
                    }
                    else
                    {
                        throw new SettingsException("No printer model found");
                    }
                } catch (Exception exception) {
                    MessageBoxHelper.ShowError($"Error connecting to printer {ipAddressText}: {exception.Message}");
                } finally {
                    ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);

                    Application.Current.Dispatcher.Invoke(() => {
                        indeterminateProgressDialog.Close();
                    });
                }
            });
        }
        /// <exception cref="ArgumentException"></exception>
        /// <exception cref="ConnectionException"></exception>
        /// <exception cref="IOException"></exception>
        /// <exception cref="OverflowException"></exception>
        /// <exception cref="Zebra.Sdk.Settings.SettingsException"></exception>
        /// <exception cref="Zebra.Sdk.Card.Exceptions.ZebraCardException"></exception>
        private static JobStatusInfo PollJobStatus(int jobId, ZebraCardPrinter zebraCardPrinter)
        {
            JobStatusInfo jobStatusInfo = new JobStatusInfo();
            bool          isFeeding     = false;

            long start = Math.Abs(Environment.TickCount);

            while (true)
            {
                jobStatusInfo = zebraCardPrinter.GetJobStatus(jobId);

                if (!isFeeding)
                {
                    start = Math.Abs(Environment.TickCount);
                }

                isFeeding = jobStatusInfo.CardPosition.Contains("feeding");

                string alarmDesc = jobStatusInfo.AlarmInfo.Value > 0 ? $" ({jobStatusInfo.AlarmInfo.Description})" : "";
                string errorDesc = jobStatusInfo.ErrorInfo.Value > 0 ? $" ({jobStatusInfo.ErrorInfo.Description})" : "";

                Console.WriteLine($"Job {jobId}: status:{jobStatusInfo.PrintStatus}, position:{jobStatusInfo.CardPosition}, alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}");

                if (jobStatusInfo.PrintStatus.Contains("done_ok"))
                {
                    break;
                }
                else if (jobStatusInfo.PrintStatus.Contains("error") || jobStatusInfo.PrintStatus.Contains("cancelled"))
                {
                    Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
                    break;
                }
                else if (jobStatusInfo.ErrorInfo.Value > 0)
                {
                    Console.WriteLine($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.");
                    zebraCardPrinter.Cancel(jobId);
                }
                else if (jobStatusInfo.PrintStatus.Contains("in_progress") && isFeeding)
                {
                    if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT)
                    {
                        Console.WriteLine("The job timed out waiting for a card and was cancelled.");
                        zebraCardPrinter.Cancel(jobId);
                    }
                }

                Thread.Sleep(1000);
            }
            return(jobStatusInfo);
        }
Esempio n. 9
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBoxId.Text != "")
            {
                Value1 = textBoxId.Text;
                ZebraCardPrinter zebraCardPrinter = null;
                Connection       connection       = null;
                String           usbAdress        = null;
                try
                {
                    foreach (DiscoveredPrinter usbPrinter in UsbDiscoverer.GetZebraUsbPrinters(new ZebraCardPrinterFilter()))
                    {
                        usbAdress = usbPrinter.Address;
                    }
                    connection = new UsbConnection(usbAdress);
                    connection.Open();

                    zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);
                    ZebraTemplate zebraCardTemplate = new ZebraCardTemplate(zebraCardPrinter);
                    //string templateData = GetTemplateData();
                    List <string> templateFields          = zebraCardTemplate.GetTemplateDataFields(Template);
                    Dictionary <string, string> fieldData = PopulateTemplateFieldData(templateFields);

                    // Generate template job
                    TemplateJob templateJob = zebraCardTemplate.GenerateTemplateDataJob(Template, fieldData);

                    // Send job
                    int jobId = zebraCardPrinter.PrintTemplate(1, templateJob);

                    // Poll job status
                    JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter);
                    //labelStatus.Text = "Impression OK";
                    //Console.WriteLine($"Job {jobId} completed with status '{jobStatus.PrintStatus}'.");
                }
                catch (Exception ev)
                {
                    labelStatus.Text = "Erreur d'impression : " + ev.Message;
                    //Console.WriteLine($"Error printing template: {ev.Message}");
                }
                finally
                {
                    CloseQuietly(connection, zebraCardPrinter);
                }
            }
            else
            {
                MessageBox.Show("Pas de valeur");
            }
        }
Esempio n. 10
0
        private void SetJobSettings(ZebraCardPrinter zebraCardPrinter, MultiJobControlViewModel jobViewModel)
        {
            zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, jobViewModel.SelectedSource.ToString());
            zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, jobViewModel.SelectedDestination.ToString());

            if (viewModel.IsPrintOptimizationAvailable)
            {
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.PRINT_OPTIMIZATION, jobViewModel.SelectedPrintOptimization.ToString());
            }

            if (viewModel.HasMagneticEncoder)
            {
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_ENCODING_TYPE, MagEncodingType.ISO.ToString());

                if (jobViewModel.MagneticEncode)
                {
                    zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.MAG_COERCIVITY, jobViewModel.SelectedCoercivityType);
                }
            }
        }
Esempio n. 11
0
        public static void CleanUpQuietly(ZebraCardPrinter zebraCardPrinter, Connection connection)
        {
            try {
                if (zebraCardPrinter != null)
                {
                    zebraCardPrinter.Destroy();
                }
            } catch (ZebraCardException) {
                // Do nothing
            }

            try {
                if (connection != null)
                {
                    connection.Close();
                }
            } catch (ConnectionException) {
                // Do nothing
            }
        }
        private List <String> GetTemplateVariables(string selectedTemplate)
        {
            List <string>    templateVariables = null;
            ZebraCardPrinter zebraCardPrinter  = null;

            try {
                connection.Open();
                zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

                ZebraCardTemplate zebraCardTemplate = new ZebraCardTemplate(zebraCardPrinter);
                zebraCardTemplate.SetTemplateFileDirectory(Properties.Settings.Default.TemplateFileDirectory);

                templateVariables = zebraCardTemplate.GetTemplateFields(selectedTemplate + ".xml");
            } catch (Exception error) {
                errorMessage = $"Connection Error: {error.Message}";
            } finally {
                ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);
            }
            return(templateVariables);
        }
Esempio n. 13
0
        private void SetUpAndSendJob(ZebraCardPrinter zebraCardPrinter, MultiJobControlViewModel jobViewModel)
        {
            SetJobSettings(zebraCardPrinter, jobViewModel);

            int?jobId = null;

            if (jobViewModel.HasValidFrontSide || jobViewModel.HasValidBackSide)
            {
                jobId = PrintAndMagEncode(zebraCardPrinter, jobViewModel);
            }
            else if (jobViewModel.HasValidMagEncodeJob)
            {
                jobId = MagEncode(zebraCardPrinter, jobViewModel);
            }

            if (jobId.HasValue)
            {
                jobViewModel.JobId = jobId.Value;
            }
        }
Esempio n. 14
0
        private static void CloseQuietly(Connection connection, ZebraCardPrinter zebraCardPrinter)
        {
            try
            {
                if (zebraCardPrinter != null)
                {
                    zebraCardPrinter.Destroy();
                }
            }
            catch { }

            try
            {
                if (connection != null)
                {
                    connection.Close();
                }
            }
            catch { }
        }
        private void ConnectButton_Click(object sender, RoutedEventArgs e)
        {
            DiscoveredPrinter printer = viewModel.SelectedPrinter;
            string            address = printer.Address;

            IndeterminateProgressDialog indeterminateProgressDialog = new IndeterminateProgressDialog($"Connecting to {address}...");

            Task.Run(() => {
                Application.Current.Dispatcher.Invoke(() => {
                    indeterminateProgressDialog.ShowDialog();
                });
            });

            Task.Run(() => {
                Connection connection             = null;
                ZebraCardPrinter zebraCardPrinter = null;

                try {
                    connection = printer.GetConnection();
                    connection.Open();

                    zebraCardPrinter       = ZebraCardPrinterFactory.GetInstance(connection);
                    printerManager.Printer = printer;

                    Application.Current.Dispatcher.Invoke(() => {
                        Close();
                    });
                } catch (Exception exception) {
                    Application.Current.Dispatcher.Invoke(() => {
                        MessageBoxHelper.ShowError($"Error connecting to printer {address}: {exception.Message}");
                    });
                } finally {
                    ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);

                    Application.Current.Dispatcher.Invoke(() => {
                        indeterminateProgressDialog.Close();
                    });
                }
            });
        }
        private void PrintTemplate(string selectedTemplate)
        {
            ZebraCardPrinter zebraCardPrinter = null;

            try {
                int quantity = 0;
                Application.Current.Dispatcher.Invoke(() => {
                    quantity = int.Parse(Quantity_ComboBox.Text);
                });

                connection.Open();
                zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

                PrinterStatusInfo statusInfo = zebraCardPrinter.GetPrinterStatus();
                if (statusInfo.ErrorInfo.Value > 0)
                {
                    throw new ZebraCardException($"{statusInfo.Status} ({statusInfo.ErrorInfo.Description}). Please correct the issue and try again.");
                }
                else if (statusInfo.AlarmInfo.Value > 0)
                {
                    throw new ZebraCardException($"{statusInfo.Status} ({statusInfo.AlarmInfo.Description}). Please correct the issue and try again.");
                }
                else
                {
                    ZebraCardTemplate zebraCardTemplate = new ZebraCardTemplate(zebraCardPrinter);
                    zebraCardTemplate.SetTemplateFileDirectory(Properties.Settings.Default.TemplateFileDirectory);
                    zebraCardTemplate.SetTemplateImageDirectory(Properties.Settings.Default.TemplateImageDirectory);

                    Dictionary <string, string> templateData = GetTemplateData();
                    TemplateJob templateJob = zebraCardTemplate.GenerateTemplateJob(selectedTemplate + ".xml", templateData);
                    int         jobId       = zebraCardPrinter.PrintTemplate(quantity, templateJob);
                }
            } catch (Exception error) {
                MessageBoxHelper.ShowError(error.Message);
            } finally {
                ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);
            }
        }
Esempio n. 17
0
        public void   Print(string Printer)
        {
            Connection       connection       = null;
            ZebraCardPrinter zebraCardPrinter = null;

            ZebraCardPrint.DLL.DatosCarnet datosCarnet = new ZebraCardPrint.DLL.DatosCarnet();

            try
            {
                //connection = new TcpConnection("1.2.3.4", 9100);
                connection = new UsbConnection(Printer);

                connection.Open();

                zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

                List <GraphicsInfo> graphicsData = DrawGraphics(zebraCardPrinter, dataTable);


                // Set the card source

                //Descomentar
                zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, "Feeder"); // Feeder=default

                // Set the card destination - If the destination value is not specifically set, it will be auto set to the most appropriate value

                if (checkBox1.CheckState == CheckState.Unchecked)
                {
                    // Set the card source
                    zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_SOURCE, "Feeder"); // Feeder=default

                    // Set the card destination - If the destination value is not specifically set, it will be auto set to the most appropriate value
                    if (zebraCardPrinter.HasLaminator())
                    {
                        zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, "LaminatorAny");
                    }
                    else
                    {
                        zebraCardPrinter.SetJobSetting(ZebraCardJobSettingNames.CARD_DESTINATION, "Eject");
                    }

                    // Send job
                    int jobId = zebraCardPrinter.Print(1, graphicsData);

                    // Poll job status
                    JobStatusInfo jobStatus = PollJobStatus(jobId, zebraCardPrinter);
                    MessageBox.Show($"Impresion Id: {jobId} completada con estado: '{jobStatus.PrintStatus}'.");
                    if (jobStatus.PrintStatus.ToString().ToUpper() == "DONE_OK")
                    {
                        datosCarnet.SDInsertaImpresionCarnet(txtNumeroDeEmpleado.Text);
                        rdoAmbasCaras.Checked = true;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show($"Error printing image: {e.Message}");
            }
            finally
            {
                CloseQuietly(connection, zebraCardPrinter);
            }
        }
Esempio n. 18
0
        private List <GraphicsInfo> DrawGraphics(ZebraCardPrinter zebraCardPrinter, DataTable data)
        {
            // Generate image data
            ZebraCardImageI zebraCardImage = null;

            //byte[] fontFields = System.Text.Encoding.ASCII.GetBytes("Arial");

            List <GraphicsInfo> graphicsData = new List <GraphicsInfo>();

            using (ZebraGraphics graphics = new ZebraCardGraphics(zebraCardPrinter))
            {
                if (rdoAmbasCaras.Checked)
                {
                    // Front side color
                    zebraCardImage    = DrawCarnetFront(graphics, PrintType.Color, data);
                    pictureBox1.Image = ByteToImage(zebraCardImage.ImageData);

                    //zebraCardImage = DrawImage(graphics, PrintType.Color, imageData, 0, 0, 450, 150);
                    //graphics.DrawImage(imageData, 0, 450, 150, 0, RotationType.RotateNoneFlipXY);
                    //zebraCardImage = graphics.CreateImage();
                    graphicsData.Add(AddImage(CardSide.Front, PrintType.Color, 0, 0, -1, zebraCardImage));
                    graphics.Clear();
                    //// Front side full overlay
                    graphicsData.Add(AddImage(CardSide.Front, PrintType.Overlay, 0, 0, 1, null));

                    // Back side mono
                    zebraCardImage    = null;
                    zebraCardImage    = DrawCarnetBack(graphics, PrintType.MonoK, data);
                    pictureBox2.Image = ByteToImage(zebraCardImage.ImageData);
                    //zebraCardImage = DrawImage(graphics, PrintType.Color, imageData, 0, 0, 450, 150);
                    //graphics.DrawImage(imageData, 0, 450, 150, 0, RotationType.RotateNoneFlipXY);
                    //zebraCardImage = graphics.CreateImage();
                    graphicsData.Add(AddImage(CardSide.Back, PrintType.MonoK, 0, 0, -1, zebraCardImage));
                    graphics.Clear();
                    //// Back side full overlay
                    graphicsData.Add(AddImage(CardSide.Back, PrintType.Overlay, 0, 0, 1, null));
                }

                if (rdoFrontal.Checked)
                {
                    // Front side color
                    zebraCardImage    = DrawCarnetFront(graphics, PrintType.Color, data);
                    pictureBox1.Image = ByteToImage(zebraCardImage.ImageData);

                    //zebraCardImage = DrawImage(graphics, PrintType.Color, imageData, 0, 0, 450, 150);
                    //graphics.DrawImage(imageData, 0, 450, 150, 0, RotationType.RotateNoneFlipXY);
                    //zebraCardImage = graphics.CreateImage();
                    graphicsData.Add(AddImage(CardSide.Front, PrintType.Color, 0, 0, -1, zebraCardImage));
                    graphics.Clear();
                    //// Front side full overlay
                    graphicsData.Add(AddImage(CardSide.Front, PrintType.Overlay, 0, 0, 1, null));
                }

                if (rdoReverso.Checked)
                {
                    // Back side mono
                    zebraCardImage    = null;
                    zebraCardImage    = DrawCarnetBack(graphics, PrintType.MonoK, data);
                    pictureBox2.Image = ByteToImage(zebraCardImage.ImageData);
                    //zebraCardImage = DrawImage(graphics, PrintType.Color, imageData, 0, 0, 450, 150);
                    //graphics.DrawImage(imageData, 0, 450, 150, 0, RotationType.RotateNoneFlipXY);
                    //zebraCardImage = graphics.CreateImage();
                    graphicsData.Add(AddImage(CardSide.Back, PrintType.MonoK, 0, 0, -1, zebraCardImage));
                    graphics.Clear();
                    //// Back side full overlay
                    graphicsData.Add(AddImage(CardSide.Back, PrintType.Overlay, 0, 0, 1, null));
                }
            }

            return(graphicsData);
        }
Esempio n. 19
0
 private int MagEncode(ZebraCardPrinter zebraCardPrinter, MultiJobControlViewModel jobViewModel)
 {
     return(zebraCardPrinter.MagEncode(jobViewModel.SelectedQuantity, jobViewModel.Track1Data, jobViewModel.Track2Data, jobViewModel.Track3Data));
 }
        public async Task StartPolling(DiscoveredPrinter printer, List <JobInfo> jobInfoList)
        {
            await Task.Run(() => {
                Connection connection             = null;
                ZebraCardPrinter zebraCardPrinter = null;

                bool showAtmDialog = true;
                bool isFeeding     = false;

                try {
                    connection = printer.GetConnection();
                    connection.Open();

                    zebraCardPrinter = ZebraCardPrinterFactory.GetInstance(connection);

                    Stopwatch stopwatch = Stopwatch.StartNew();

                    foreach (JobInfo jobInfo in jobInfoList)
                    {
                        UpdateLog(jobInfo.JobId, $"Polling job status for job ID {jobInfo.JobId}...");
                    }

                    while (jobInfoList.Count > 0)
                    {
                        foreach (JobInfo jobInfo in jobInfoList.ToList())
                        {
                            JobStatusInfo jobStatusInfo = zebraCardPrinter.GetJobStatus(jobInfo.JobId);

                            if (!isFeeding)
                            {
                                stopwatch = Stopwatch.StartNew();
                            }

                            bool isAlarmInfoPresent = jobStatusInfo.AlarmInfo.Value > 0;
                            bool isErrorInfoPresent = jobStatusInfo.ErrorInfo.Value > 0;
                            isFeeding = jobStatusInfo.CardPosition.Contains("feeding");

                            string alarmInfo = isAlarmInfoPresent ? $"{jobStatusInfo.AlarmInfo.Value} ({jobStatusInfo.AlarmInfo.Description})" : jobStatusInfo.AlarmInfo.Value.ToString();
                            string errorInfo = isErrorInfoPresent ? $"{jobStatusInfo.ErrorInfo.Value} ({jobStatusInfo.ErrorInfo.Description})" : jobStatusInfo.ErrorInfo.Value.ToString();

                            string jobStatusMessage = $"Job ID {jobInfo.JobId}: status:{jobStatusInfo.PrintStatus}, position:{jobStatusInfo.CardPosition}, contact:{jobStatusInfo.ContactSmartCardStatus}, " +
                                                      $"contactless:{jobStatusInfo.ContactlessSmartCardStatus}, alarm:{alarmInfo}, error:{errorInfo}";

                            UpdateLog(jobInfo.JobId, jobStatusMessage, jobInfo.JobNumber, jobStatusInfo);

                            if (jobStatusInfo.PrintStatus.Equals("done_ok"))
                            {
                                UpdateLog(jobInfo.JobId, $"Job ID {jobInfo.JobId} completed.", jobInfo.JobNumber, jobStatusInfo);

                                showAtmDialog = true;
                                stopwatch     = Stopwatch.StartNew();
                                jobInfoList.Remove(jobInfo);
                            }
                            else if (jobStatusInfo.PrintStatus.Equals("done_error"))
                            {
                                UpdateLog(jobInfo.JobId, $"Job ID {jobInfo.JobId} completed with error: {jobStatusInfo.ErrorInfo.Description}", jobInfo.JobNumber, jobStatusInfo);

                                showAtmDialog = true;
                                stopwatch     = Stopwatch.StartNew();
                                jobInfoList.Remove(jobInfo);
                            }
                            else if (jobStatusInfo.PrintStatus.Contains("cancelled"))
                            {
                                if (isErrorInfoPresent)
                                {
                                    UpdateLog(jobInfo.JobId, $"Job ID {jobInfo.JobId} cancelled with error: {jobStatusInfo.ErrorInfo.Description}", jobInfo.JobNumber, jobStatusInfo);
                                }
                                else
                                {
                                    UpdateLog(jobInfo.JobId, $"Job ID {jobInfo.JobId} cancelled.", jobInfo.JobNumber, jobStatusInfo);
                                }

                                showAtmDialog = true;
                                stopwatch     = Stopwatch.StartNew();
                                jobInfoList.Remove(jobInfo);
                            }
                            else if (isAlarmInfoPresent)
                            {
                                MessageBoxResult messageBoxResult = MessageBox.Show($"Job ID {jobInfo.JobId} encountered alarm [{jobStatusInfo.AlarmInfo.Description}].\r\n" +
                                                                                    $"Either fix the alarm and click OK once the job begins again or click Cancel to cancel the job.", "Alarm Encountered", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
                                if (messageBoxResult == MessageBoxResult.Cancel)
                                {
                                    zebraCardPrinter.Cancel(jobInfo.JobId);
                                }
                            }
                            else if (isErrorInfoPresent)
                            {
                                zebraCardPrinter.Cancel(jobInfo.JobId);
                            }
                            else if (jobStatusInfo.ContactSmartCardStatus.Contains("at_station") || jobStatusInfo.ContactlessSmartCardStatus.Contains("at_station"))
                            {
                                MessageBoxResult messageBoxResult = MessageBox.Show("Please click OK to resume the job or Cancel to cancel the job.", "Card at Station", MessageBoxButton.OKCancel, MessageBoxImage.Information);
                                if (messageBoxResult == MessageBoxResult.Cancel)
                                {
                                    zebraCardPrinter.Cancel(jobInfo.JobId);
                                }
                                else
                                {
                                    zebraCardPrinter.Resume();
                                }
                            }
                            else if (isFeeding)
                            {
                                if (showAtmDialog && jobInfo.CardSource == CardSource.ATM)
                                {
                                    DialogHelper.ShowInsertCardDialog();
                                    showAtmDialog = false;
                                }
                                else if (stopwatch.ElapsedMilliseconds > CardFeedTimeoutMilliseconds)
                                {
                                    UpdateLog(jobInfo.JobId, $"Job ID {jobInfo.JobId} timed out waiting for a card and was cancelled.", jobInfo.JobNumber, jobStatusInfo);
                                    zebraCardPrinter.Cancel(jobInfo.JobId);
                                }
                            }

                            Thread.Sleep(500);
                        }
                    }
                } catch (Exception exception) {
                    MessageBoxHelper.ShowError($"Error polling job status: {exception.Message}");
                } finally {
                    ConnectionHelper.CleanUpQuietly(zebraCardPrinter, connection);
                }
            });
        }