SmartCardView.xaml.cs
3.7 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
/***********************************************
* 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.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Zebra.Sdk.Comm;
using Zebra.Sdk.Device;
using Zebra.Sdk.Printer;
using Zebra.Windows.DevDemo.Utils;
namespace Zebra.Windows.DevDemo.Demos.SmartCard {
/// <summary>
/// Interaction logic for SmartCardView.xaml
/// </summary>
public partial class SmartCardView : UserControl {
public SmartCardView() {
InitializeComponent();
}
private void RunSmartCardDemo(bool sendData) {
try {
SetButtonStates(false);
ReadSmartCard(sendData);
} catch (Exception e) {
MessageBoxCreator.ShowError(e.Message, "Smart Card Error");
}
}
private void ReadSmartCard(bool sendData) {
Connection connection = null;
Task.Run(() => {
try {
UpdateSmartCardOutput("");
Application.Current.Dispatcher.Invoke(() => {
connection = connectionSelector.GetConnection();
});
connection.Open();
ZebraPrinter printer = ZebraPrinterFactory.GetInstance(connection);
SmartcardReader smartcardReader = SmartcardReaderFactory.Create(printer);
if (smartcardReader != null) {
byte[] response = sendData ? smartcardReader.DoCommand("8010000008") : smartcardReader.GetATR();
UpdateSmartCardOutput(string.Concat(response.Select(x => x.ToString("x2"))));
smartcardReader.Close();
} else {
MessageBoxCreator.ShowError("Printer does not have a smart card reader", "Smart Card Error");
}
} catch (ConnectionException e) {
MessageBoxCreator.ShowError(e.Message, "Connection Error");
} catch (ZebraPrinterLanguageUnknownException e) {
MessageBoxCreator.ShowError(e.Message, "Connection Error");
} finally {
if (connection != null) {
try {
connection.Close();
} catch (ConnectionException) { }
}
SetButtonStates(true);
}
});
}
private void UpdateSmartCardOutput(string newMessage) {
Application.Current.Dispatcher.Invoke(() => {
responseData.Text = newMessage;
});
}
private void SetButtonStates(bool enabled) {
Application.Current.Dispatcher.Invoke(() => {
sendDataButton.IsEnabled = enabled;
sendAtrButton.IsEnabled = enabled;
});
}
private void SendDataButton_Click(object sender, RoutedEventArgs e) {
RunSmartCardDemo(true);
}
private void SendAtrButton_Click(object sender, RoutedEventArgs e) {
RunSmartCardDemo(false);
}
}
}