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); }
private void CreateFormatTableDialog() { SetButtonState(retrieveFormatsButton, false); SetFormatListState(false); string formatName = (string)storedFilesList.SelectedValue; Connection printerConnection = null; Task.Run(() => { try { printerConnection = connectionSelector.GetConnection(); printerConnection.Open(); ZebraPrinter printer = ZebraPrinterFactory.GetInstance(printerConnection); byte[] formatData = printer.RetrieveFormatFromPrinter(formatName); fieldDescDataVars = printer.GetVariableFields(Encoding.UTF8.GetString(formatData)).ToList(); fieldDescDataVars = FormatFieldDescriptionDataVars(fieldDescDataVars); formatVariables = new ObservableCollection <FormatVariable>(); for (int i = 0; i < fieldDescDataVars.Count; ++i) { formatVariables.Add(new FormatVariable { FieldName = fieldDescDataVars[i].FieldName, FieldValue = "" }); } try { if (printerConnection != null) { printerConnection.Close(); } } catch (ConnectionException) { } ShowStoredFormatDialog(formatName); } catch (Exception e) { MessageBoxCreator.ShowError(e.Message, "Communication Error"); SetButtonState(retrieveFormatsButton, true); SetFormatListState(true); } finally { try { if (printerConnection != null && printerConnection.Connected) { printerConnection.Close(); } } catch (ConnectionException) { } } }); }
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) { } }); } }
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; }); } }