ConnectionBuilderView.xaml.cs
6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/***********************************************
* CONFIDENTIAL AND PROPRIETARY
*
* The source code and other information contained herein is the confidential and exclusive property of
* ZIH Corp. and is subject to the terms and conditions in your end user license agreement.
* This source code, and any other information contained herein, shall not be copied, reproduced, published,
* displayed or distributed, in whole or in part, in any medium, by any means, for any purpose except as
* expressly permitted under such license agreement.
*
* Copyright ZIH Corp. 2018
*
* ALL RIGHTS RESERVED
***********************************************/
using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Printer;
using Zebra.Windows.DevDemo.Utils;
using ZebraConnectionBuilder = Zebra.Sdk.Comm.ConnectionBuilder;
using ZebraPrinterStatus = Zebra.Sdk.Printer.PrinterStatus;
namespace Zebra.Windows.DevDemo.Demos.ConnectionBuilder {
/// <summary>
/// Interaction logic for ConnectionBuilderView.xaml
/// </summary>
public partial class ConnectionBuilderView : UserControl {
private ConnectionBuilderViewModel viewModel;
private Connection connection = null;
public ConnectionBuilderView() {
InitializeComponent();
viewModel = DataContext as ConnectionBuilderViewModel;
}
private void TestConnectionString() {
Task.Run(() => {
try {
ClearProgress();
connection = ZebraConnectionBuilder.Build(GetConnectionStringForSdk());
PublishProgress("Connection string evaluated as class type " + connection.GetType().Name);
connection.Open();
PublishProgress("Connection opened successfully");
if (IsAttemptingStatusConnection()) {
ZebraPrinterLinkOs printer = ZebraPrinterFactory.GetLinkOsPrinter(connection);
PublishProgress("Created a printer, attempting to retrieve status");
ZebraPrinterStatus status = printer.GetCurrentStatus();
PublishProgress("Is printer ready to print? " + status.isReadyToPrint);
} else {
ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);
PublishProgress("Created a printer, attempting to print a config label");
printer.PrintConfigurationLabel();
}
PublishProgress("Closing connection");
} catch (ConnectionException) {
MessageBoxCreator.ShowError("Connection could not be opened", "Error");
} catch (ZebraPrinterLanguageUnknownException) {
MessageBoxCreator.ShowError("Could not create printer", "Error");
} finally {
if (connection != null) {
try {
connection.Close();
} catch (ConnectionException) { } finally {
connection = null;
SetTestButtonState(true);
}
} else {
SetTestButtonState(true);
}
}
});
}
private void SetTestButtonState(bool state) {
Application.Current.Dispatcher.Invoke(() => {
testConnectionStringButton.IsEnabled = state;
});
}
private void ClearProgress() {
Application.Current.Dispatcher.Invoke(() => {
logData.Text = "Log:\nTesting string: " + GetConnectionStringForSdk() + "\n";
});
}
private string GetConnectionStringForSdk() {
string finalConnectionString = "";
Application.Current.Dispatcher.Invoke(() => {
string selectedPrefix = "";
if (connectionPrefixDropdown.SelectedIndex > 0) {
selectedPrefix = connectionPrefixDropdown.SelectedValue + ":";
}
string userSuppliedDescriptionString = usbDriverIpAddress.Text;
finalConnectionString = selectedPrefix + userSuppliedDescriptionString;
});
return finalConnectionString;
}
private bool IsAttemptingStatusConnection() {
return connection.GetType().Name.Contains("Status");
}
private void PublishProgress(string progress) {
Application.Current.Dispatcher.Invoke(() => {
logData.Text = logData.Text + progress + Environment.NewLine;
});
}
private void ConnectionPrefixDropdown_SelectionChanged(object sender, SelectionChangedEventArgs e) {
connectionString.Text = GetConnectionStringForSdk();
SetAddressTextBlock();
}
private void SetAddressTextBlock() {
switch(connectionPrefixDropdown.SelectedValue) {
case ConnectionPrefix.Tcp:
case ConnectionPrefix.TcpMulti:
case ConnectionPrefix.TcpStatus:
AddressTextBlock.Text = "IP Address:";
break;
case ConnectionPrefix.Bluetooth:
case ConnectionPrefix.BluetoothMulti:
AddressTextBlock.Text = "BT Address:";
break;
case ConnectionPrefix.Usb:
AddressTextBlock.Text = "USB Driver:";
break;
case ConnectionPrefix.UsbDirect:
AddressTextBlock.Text = "Symbolic Name:";
break;
default:
AddressTextBlock.Text = "Address:";
break;
};
}
private void TestConnectionStringButton_Click(object sender, RoutedEventArgs e) {
try {
viewModel.LogData = "Log:\n\n";
SetTestButtonState(false);
TestConnectionString();
} catch (Exception ex) {
MessageBoxCreator.ShowError(ex.Message, "Connection Builder Error");
}
}
private void UsbDriverIpAddress_KeyUp(object sender, KeyEventArgs e) {
connectionString.Text = GetConnectionStringForSdk();
}
}
}