/// <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);
        }
Beispiel #2
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");
            }
        }
        private void UpdateLog(int jobId, string message, MultiJobNumber?jobNumber, JobStatusInfo jobStatusInfo)
        {
            string lastMessageForJobId = jobIdLastMessageMap.ContainsKey(jobId) ? jobIdLastMessageMap[jobId] : null;

            if (lastMessageForJobId == null || !lastMessageForJobId.Equals(message))
            {
                WriteToLog(message);
                jobIdLastMessageMap[jobId] = message;
            }

            if (MultiJobStatusUpdated != null && jobNumber.HasValue)
            {
                MultiJobStatusUpdated.Invoke(jobNumber.Value, jobId, jobStatusInfo);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Loops until a job is done
        /// </summary>
        /// <param name="loopCount">number of tries before giving up</param>
        /// <returns>true if job done OK</returns>
        public bool PollJobForDoneOk(int loopCount)
        {
            JobStatusInfo jobStatusInfo = new JobStatusInfo();

            bool done  = false;
            long start = Math.Abs(Environment.TickCount);

            while (!done && loopCount > 0)
            {
                jobStatusInfo = printer.GetJobStatus(jobID);

                if (jobStatusInfo.PrintStatus.Contains("done_ok"))
                {
                    done = true;
                }
                else if (jobStatusInfo.PrintStatus.Contains("error") || jobStatusInfo.PrintStatus.Contains("cancelled"))
                {
                    break;
                }
                else if (jobStatusInfo.ErrorInfo.Value > 0)
                {
                    printer.Cancel(jobID);
                }
                else if (jobStatusInfo.CardPosition.Contains("feeding"))
                {
                    if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT)
                    {
                        printer.Cancel(jobID);
                        break;
                    }
                }
                loopCount--;
                if (!done)
                {
                    Thread.Sleep(1000);
                }
            }
            return(done);
        }
Beispiel #5
0
        /// <summary>
        /// Loops until a smart card is a encoding station
        /// </summary>
        /// <param name="loopCount">number of tries before giving up</param>
        /// <returns>true if card is at encoder station</returns>
        public bool PollJobForAtStation(int loopCount)
        {
            bool          atStation     = false;
            JobStatusInfo jobStatusInfo = new JobStatusInfo();

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

            while (!atStation && loopCount > 0)
            {
                jobStatusInfo = printer.GetJobStatus(jobID);

                if (jobStatusInfo.ContactlessSmartCardStatus.Contains("at_station") || jobStatusInfo.ContactSmartCardStatus.Contains("at_station"))
                {
                    atStation = true;
                }
                else if (jobStatusInfo.PrintStatus.Contains("error") || jobStatusInfo.PrintStatus.Contains("cancelled"))
                {
                    break;
                }
                else if (jobStatusInfo.ErrorInfo.Value > 0)
                {
                    printer.Cancel(jobID);
                }
                else if (jobStatusInfo.PrintStatus.Contains("in_progress") && jobStatusInfo.CardPosition.Contains("feeding"))
                {
                    if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT)
                    {
                        printer.Cancel(jobID);
                        break;
                    }
                }
                loopCount--;
                if (!atStation)
                {
                    Thread.Sleep(1000);
                }
            }
            return(atStation);
        }
        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);
            }
        }
        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);
                }
            });
        }
Beispiel #8
0
        private void JobStatusControl_MultiJobStatusUpdated(MultiJobNumber jobNumber, int jobId, JobStatusInfo jobStatusInfo)
        {
            if (jobStatusInfo != null)
            {
                string message = jobStatusInfo.PrintStatus;
                if (jobStatusInfo.PrintStatus.Equals("done_ok"))
                {
                    message = "Completed";
                }
                else if (jobStatusInfo.PrintStatus.Equals("done_error"))
                {
                    message = "Completed with error";
                }
                else if (jobStatusInfo.PrintStatus.Contains("cancelled"))
                {
                    message = jobStatusInfo.ErrorInfo.Value > 0 ? "Cancelled with error" : "Cancelled";
                }

                UpdateJobStatus(jobNumber, $"Job ID {jobId}: {message}");
            }
        }
Beispiel #9
0
        /// <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 async Task <short> PollJobStatusAsync()
        {
            JobStatusInfo jobStatusInfo = new JobStatusInfo();
            bool          isFeeding     = false;

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

            while (true)
            {
                jobStatusInfo = _zebraCardPrinter.GetJobStatus(_currentPrintJob);

                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})" : "";

                RaiseDeviceNotification($"Job {_currentPrintJob}: status:{jobStatusInfo.PrintStatus}, position:{jobStatusInfo.CardPosition}, alarm:{jobStatusInfo.AlarmInfo.Value}{alarmDesc}, error:{jobStatusInfo.ErrorInfo.Value}{errorDesc}",
                                        false, EventArgs.Empty);


                if (jobStatusInfo.PrintStatus.Contains("done_ok"))
                {
                    return(PrinterCodes.Success);
                }
                else if (jobStatusInfo.PrintStatus.Contains("error"))
                {
                    RaiseDeviceNotification($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.",
                                            true,
                                            EventArgs.Empty);
                    return(PrinterCodes.PrintJobError);
                }
                else if (jobStatusInfo.PrintStatus.Contains("cancelled"))
                {
                    RaiseDeviceNotification($"The job was cancelled.",
                                            true,
                                            EventArgs.Empty);
                    return(PrinterCodes.PrintJobCancelled);
                }
                else if (jobStatusInfo.ErrorInfo.Value > 0)
                {
                    RaiseDeviceNotification($"The job encountered an error [{jobStatusInfo.ErrorInfo.Description}] and was cancelled.",
                                            true,
                                            EventArgs.Empty);
                    _zebraCardPrinter.Cancel(_currentPrintJob);
                }
                else if (jobStatusInfo.PrintStatus.Contains("in_progress") && isFeeding)
                {
                    if (Math.Abs(Environment.TickCount) > start + CARD_FEED_TIMEOUT)
                    {
                        RaiseDeviceNotification("The job timed out waiting for a card and was cancelled.",
                                                true,
                                                EventArgs.Empty);

                        _zebraCardPrinter.Cancel(_currentPrintJob);
                    }
                }

                await Task.Delay(1000);
            }
        }