Ejemplo n.º 1
0
        private async Task <string> RetrieveFormatContentAsync(FormatViewModel format)
        {
            string     formatContent = null;
            Connection connection    = null;

            try {
                await Task.Factory.StartNew(() => {
                    connection = ConnectionCreator.Create(ViewModel.SelectedPrinter);
                    connection.Open();

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    formatContent = Encoding.UTF8.GetString(printer.RetrieveFormatFromPrinter(format.PrinterPath));
                });
            } catch (Exception e) {
                await AlertCreator.ShowErrorAsync(this, e.Message);
            } finally {
                await Task.Factory.StartNew(() => {
                    try {
                        connection?.Close();
                    } catch (ConnectionException) { }
                });
            }

            return(formatContent);
        }
Ejemplo n.º 2
0
        private async void SelectButton_Clicked(object sender, EventArgs eventArgs)
        {
            if (!viewModel.IsSelectingPrinter && Navigation.NavigationStack.Count > 0 && Navigation.NavigationStack.Last().GetType() == typeof(SelectPrinterPage))
            {
                await Task.Factory.StartNew(() => {
                    viewModel.IsSelectingPrinter = true;
                });

                try {
                    DiscoveredPrinter selectedPrinter = (DiscoveredPrinter)PrinterList.SelectedItem;
                    Connection        connection      = null;

                    try {
                        await Task.Factory.StartNew(() => {
                            connection = ConnectionCreator.Create(selectedPrinter);
                            connection.Open();
                        });
                    } catch (Exception e) {
                        await AlertCreator.ShowErrorAsync(this, e.Message);

                        return;
                    } finally {
                        await Task.Factory.StartNew(() => {
                            try {
                                connection?.Close();
                            } catch (ConnectionException) { }
                        });
                    }

                    await Task.Factory.StartNew(() => {
                        mainPage.ViewModel.SelectedPrinter = selectedPrinter;
                    });

                    mainPage.RefreshFormatLists();

                    await Navigation.PopAsync();
                } finally {
                    await Task.Factory.StartNew(() => {
                        viewModel.IsSelectingPrinter = false;
                    });
                }
            }
        }
Ejemplo n.º 3
0
        private async Task AddPrinterFormatsToFormatListAsync()
        {
            Connection connection = null;

            try {
                List <FormatViewModel> formats = new List <FormatViewModel>();

                await Task.Factory.StartNew(() => {
                    connection = ConnectionCreator.Create(ViewModel.SelectedPrinter);
                    connection.Open();

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    string[] formatPrinterPaths = printer.RetrieveFileNames(new string[] { "ZPL" });
                    foreach (string formatPrinterPath in formatPrinterPaths)
                    {
                        int colonIndex     = formatPrinterPath.IndexOf(":");
                        string driveLetter = formatPrinterPath.Substring(0, colonIndex);
                        string name        = formatPrinterPath.Substring(colonIndex + 1, formatPrinterPath.LastIndexOf(".") - colonIndex - 1);
                        string extension   = formatPrinterPath.Substring(formatPrinterPath.LastIndexOf(".") + 1);

                        if (!string.Equals(driveLetter, "Z", StringComparison.OrdinalIgnoreCase))   // Ignore all formats stored on printer's Z drive
                        {
                            formats.Add(new FormatViewModel {
                                DriveLetter         = driveLetter,
                                Name                = name,
                                Extension           = extension,
                                Content             = null, // Populate this later, upon navigating to print format page
                                Source              = FormatSource.Printer,
                                OnSaveButtonClicked = new Command(async(object parameter) => {
                                    if (!ViewModel.IsSavingFormat && !ViewModel.IsDeletingFormat && !ViewModel.IsStoredFormatListRefreshing)
                                    {
                                        FormatViewModel format = parameter as FormatViewModel;

                                        await Task.Factory.StartNew(() => {
                                            ViewModel.IsSavingFormat = true;
                                            format.IsSaving          = true;
                                        });

                                        await SavePrinterFormatAsync(format);

                                        await Task.Factory.StartNew(() => {
                                            format.IsSaving          = false;
                                            ViewModel.IsSavingFormat = false;
                                        });

                                        await RefreshStoredFormatListAsync();
                                    }
                                }),
                                OnPrintButtonClicked = new Command(async(object parameter) => {
                                    if (!ViewModel.IsSavingFormat)
                                    {
                                        FormatViewModel format = parameter as FormatViewModel;
                                        await PushPageAsync(new PrintFormatPage(ViewModel.SelectedPrinter, format));
                                    }
                                })
                            });
                        }
                    }

                    formats.ForEach(ViewModel.PrinterFormatList.Add);
                });
            } catch (Exception e) {
                await AlertCreator.ShowErrorAsync(this, e.Message);
            } finally {
                await Task.Factory.StartNew(() => {
                    try {
                        connection?.Close();
                    } catch (ConnectionException) { }
                });
            }
        }
Ejemplo n.º 4
0
        private async Task SavePrinterFormatAsync(FormatViewModel format)
        {
            PrintStationDatabase.StoredFormat storedFormat = null;
            Connection connection         = null;
            bool       fileWriteAttempted = false;
            bool       linePrintEnabled   = false;

            try {
                await Task.Factory.StartNew(async() => {
                    connection = ConnectionCreator.Create(ViewModel.SelectedPrinter);
                    connection.Open();

                    string originalPrinterLanguage = SGD.GET(PrintFormatPage.DeviceLanguagesSgd, connection);
                    linePrintEnabled = "line_print".Equals(originalPrinterLanguage, StringComparison.OrdinalIgnoreCase);

                    if (linePrintEnabled)
                    {
                        SGD.SET(PrintFormatPage.DeviceLanguagesSgd, "zpl", connection);
                    }

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    string formatContent = Encoding.UTF8.GetString(printer.RetrieveFormatFromPrinter(format.PrinterPath));

                    fileWriteAttempted = true;
                    storedFormat       = new PrintStationDatabase.StoredFormat {
                        DriveLetter = format.DriveLetter,
                        Name        = format.Name,
                        Extension   = format.Extension,
                        Content     = formatContent
                    };
                    await App.Database.SaveStoredFormatAsync(storedFormat);
                });
            } catch (Exception e) {
                await Task.Factory.StartNew(async() => {
                    if (fileWriteAttempted && storedFormat != null)
                    {
                        await App.Database.DeleteStoredFormatByIdAsync(storedFormat.Id);
                    }
                });

                await AlertCreator.ShowErrorAsync(this, e.Message);
            } finally {
                if (linePrintEnabled)
                {
                    await Task.Factory.StartNew(() => {
                        try {
                            connection?.Open();
                            SGD.SET(PrintFormatPage.DeviceLanguagesSgd, "line_print", connection);
                        } catch (ConnectionException) { }
                    });
                }

                await Task.Factory.StartNew(() => {
                    try {
                        connection?.Close();
                    } catch (ConnectionException) { }
                });
            }
        }
Ejemplo n.º 5
0
        public MainPage()
        {
            InitializeComponent();

            BindingContext = ViewModel;

            if (Device.RuntimePlatform != Device.WPF || !DependencyService.Get <IPlatformHelper>().IsWindows10())
            {
                ToolbarItems.Remove(BluetoothHelpButton); // Bluetooth help dialog is specific to Windows 10
            }

            try {
                if (DependencyService.Get <INfcManager>().IsNfcAvailable())
                {
                    DependencyService.Get <INfcManager>().TagScanned += async(object sender, string nfcData) => {
                        Device.BeginInvokeOnMainThread(async() => {
                            await Navigation.PopToRootAsync(); // Must call Navigation.PopToRootAsync() on UI thread on Windows tablets
                        });

                        await Task.Factory.StartNew(() => {
                            ViewModel.IsSelectingNfcPrinter = true;
                            ViewModel.SelectedPrinter       = null;

                            try {
                                ViewModel.StoredFormatList.Clear();
                            } catch (NotImplementedException) {
                                ViewModel.StoredFormatList.Clear(); // Workaround for Xamarin.Forms.Platform.WPF issue: https://github.com/xamarin/Xamarin.Forms/issues/3648
                            }

                            try {
                                ViewModel.PrinterFormatList.Clear();
                            } catch (NotImplementedException) {
                                ViewModel.PrinterFormatList.Clear(); // Workaround for Xamarin.Forms.Platform.WPF issue: https://github.com/xamarin/Xamarin.Forms/issues/3648
                            }
                        });

                        Connection        connection = null;
                        DiscoveredPrinter printer    = null;
                        bool success = false;

                        try {
                            await Task.Factory.StartNew(() => {
                                printer = NfcDiscoverer.DiscoverPrinter(nfcData);

                                connection = printer.GetConnection();
                                try {
                                    connection.Open(); // Test connection
                                } catch (Exception e) {
                                    throw new ConnectionException("Could not open connection. Please check your printer and device settings and try again.", e);
                                }

                                success = true;
                            });
                        } catch (Exception e) {
                            await AlertCreator.ShowErrorAsync(this, e.Message);
                        } finally {
                            await Task.Factory.StartNew(() => {
                                try {
                                    connection?.Close();
                                } catch (Exception) { }

                                if (success)
                                {
                                    ViewModel.SelectedPrinter       = printer;
                                    ViewModel.IsSelectingNfcPrinter = false;
                                    RefreshFormatLists();
                                }
                                else
                                {
                                    ViewModel.IsSelectingNfcPrinter = false;
                                }
                            });
                        }
                    };
                }
            } catch (TypeLoadException) { } // NFC is not supported on Windows 7

            InitializeStoredFormats();
        }
Ejemplo n.º 6
0
        private async Task PopulateVariableFieldListAsync()
        {
            await Task.Factory.StartNew(() => {
                viewModel.IsVariableFieldListRefreshing = true;

                try {
                    viewModel.FormatVariableList.Clear();
                } catch (NotImplementedException) {
                    viewModel.FormatVariableList.Clear(); // Workaround for Xamarin.Forms.Platform.WPF issue: https://github.com/xamarin/Xamarin.Forms/issues/3648
                }
            });

            Connection connection       = null;
            bool       linePrintEnabled = false;

            try {
                await Task.Factory.StartNew(() => {
                    connection = ConnectionCreator.Create(selectedPrinter);
                    connection.Open();

                    string originalPrinterLanguage = SGD.GET(DeviceLanguagesSgd, connection);
                    linePrintEnabled = "line_print".Equals(originalPrinterLanguage, StringComparison.OrdinalIgnoreCase);

                    if (linePrintEnabled)
                    {
                        SGD.SET(DeviceLanguagesSgd, "zpl", connection);
                    }

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    if (format.Source == FormatSource.Printer)
                    {
                        format.Content = Encoding.UTF8.GetString(printer.RetrieveFormatFromPrinter(format.PrinterPath));
                    }

                    FieldDescriptionData[] variableFields = printer.GetVariableFields(format.Content);
                    foreach (FieldDescriptionData variableField in variableFields)
                    {
                        viewModel.FormatVariableList.Add(new FormatVariable {
                            Name  = variableField.FieldName ?? $"Field {variableField.FieldNumber}",
                            Index = variableField.FieldNumber,
                        });
                    }
                });
            } catch (Exception e) {
                await AlertCreator.ShowErrorAsync(this, e.Message);
            } finally {
                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    try {
                        connection?.Close();
                    } catch (ConnectionException) { }

                    viewModel.IsVariableFieldListRefreshing = false;
                });
            }
        }
Ejemplo n.º 7
0
        private async void PrintButton_Clicked(object sender, EventArgs eventArgs)
        {
            await Task.Factory.StartNew(() => {
                viewModel.IsSendingPrintJob = true;
            });

            Connection connection       = null;
            bool       linePrintEnabled = false;

            try {
                await Task.Factory.StartNew(() => {
                    connection = ConnectionCreator.Create(selectedPrinter);
                    connection.Open();

                    string originalPrinterLanguage = SGD.GET(DeviceLanguagesSgd, connection);
                    linePrintEnabled = "line_print".Equals(originalPrinterLanguage, StringComparison.OrdinalIgnoreCase);

                    if (linePrintEnabled)
                    {
                        SGD.SET(DeviceLanguagesSgd, "zpl", connection);
                    }

                    ZebraPrinter printer             = ZebraPrinterFactory.GetInstance(connection);
                    ZebraPrinterLinkOs linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printer);

                    string errorMessage = GetPrinterStatusErrorMessage(printer.GetCurrentStatus());
                    if (errorMessage != null)
                    {
                        throw new PrinterNotReadyException($"{errorMessage}. Please check your printer and try again.");
                    }
                    else
                    {
                        if (format.Source != FormatSource.Printer)
                        {
                            connection.Write(Encoding.UTF8.GetBytes(format.Content));
                        }

                        linkOsPrinter.PrintStoredFormatWithVarGraphics(format.PrinterPath, BuildFormatVariableDictionary(), "UTF-8");
                    }
                });

                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    viewModel.IsSendingPrintJob = false;
                });
            } catch (PrinterNotReadyException e) {
                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    viewModel.IsSendingPrintJob = false;
                });

                await AlertCreator.ShowAsync(this, "Printer Error", e.Message);
            } catch (Exception e) {
                if (linePrintEnabled)
                {
                    await ResetPrinterLanguageToLinePrintAsync(connection);
                }

                await Task.Factory.StartNew(() => {
                    viewModel.IsSendingPrintJob = false;
                });

                await AlertCreator.ShowErrorAsync(this, e.Message);
            } finally {
                await Task.Factory.StartNew(() => {
                    try {
                        connection?.Close();
                    } catch (ConnectionException) { }
                });
            }
        }