示例#1
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;
            }
        }
        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();
            }
        }
示例#3
0
        /// <summary>
        /// Opens a connection to a discovered printer
        /// </summary>
        /// <returns>true if connection is successful</returns>
        public bool OpenConnection()
        {
            bool connected = false;

            try {
                connection = UsbPrinter.GetConnection();
                if (connection == null)
                {
                    throw new Exception("Unable to connect to an USB printer");
                }

                connection.Open();
                printer = ZebraCardPrinterFactory.GetZmotifPrinter(connection);
                if (printer == null)
                {
                    throw new Exception("Unable to get an instance to an USB printer");
                }

                connected = true;
            } catch (Exception ex) {
                connection   = null;
                printerError = ex.Message;
            }
            return(connected);
        }
示例#4
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();
                    });
                }
            });
        }
示例#5
0
        private string GetConfigurationSettings(ref string errMsg)
        {
            string xmlDoc = string.Empty;

            try {
                ZebraPrinterZmotif zmotifPrn = ZebraCardPrinterFactory.GetZmotifPrinter(connection);
                xmlDoc = zmotifPrn.SettingsHelper.GetConfiguration();
            } catch (Exception ex) {
                errMsg = ex.Message;
            }
            return(xmlDoc);
        }
示例#6
0
        private bool SetContactlessHFLFSmartCardOffset(int value, out string errMsg)
        {
            bool wasSet = false;

            errMsg = string.Empty;
            try {
                ZebraPrinterZmotif zmotifPrn = ZebraCardPrinterFactory.GetZmotifPrinter(connection);
                zmotifPrn.SettingsHelper.SetContactlessSmartCardOffset("MIFARE", value);
                wasSet = true;
            } catch (Exception ex) {
                errMsg = ex.Message;
            }
            return(wasSet);
        }
示例#7
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 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);
        }
        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();
                    });
                }
            });
        }
示例#10
0
        /// <summary>
        /// Gets smart card offset ranges based on card type
        /// </summary>
        /// <param name="cardType">contact or contactless</param>
        /// <param name="min">minimum offset value</param>
        /// <param name="max">maximum offset value</param>
        /// <returns>true if successful</returns>
        public bool GetSmartOffsetRange(string cardType, out int min, out int max)
        {
            bool gotRange = false;

            min = max = 0;

            try {
                string range = string.Empty;

                ZebraPrinterZmotif zmotifPrn = ZebraCardPrinterFactory.GetZmotifPrinter(this.connection);
                switch (cardType)
                {
                case "contact":
                    range = zmotifPrn.GetSettingRange("mech_adjustments.card_smart_card_x_offset");
                    break;

                case "hf":
                    range = zmotifPrn.GetSettingRange("mech_adjustments.card_smart_card_hf_x_offset");
                    break;

                case "lf":
                    range = zmotifPrn.GetSettingRange("mech_adjustments.card_smart_card_lf_x_offset");
                    break;

                case "uhf":
                    range = zmotifPrn.GetSettingRange("mech_adjustments.card_smart_card_uhf_x_offset");
                    break;
                }
                string[] strArray = range.Split('-');
                if (strArray.Length.Equals(3))
                {
                    min      = Convert.ToInt32(strArray[1]) * -1;
                    max      = Convert.ToInt32(strArray[2]);
                    gotRange = true;
                }
            } catch {
            }
            return(gotRange);
        }
        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);
            }
        }
示例#12
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);
            }
        }
        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);
                }
            });
        }