-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.cs
379 lines (345 loc) · 18.2 KB
/
Process.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Newtonsoft.Json;
using Reactive.Bindings;
namespace NFC_Reader_WPF
{
class status
{
public string status1 { get; set; }
}
public class Process
{
public ReactiveProperty<string> TXTNAME { get; set; } = new ReactiveProperty<string>();
public ReactiveProperty<string> ValueQRCode { get; set; }
public ReactiveProperty<string> TXTKANA { get; set; } = new ReactiveProperty<string>();
public ReactiveProperty<string> TXTDATE { get; set; } = new ReactiveProperty<string>();
public ReactiveProperty<string> TXTGENDER { get; set; } = new ReactiveProperty<string>();
public ReactiveProperty<string> TXTNFCCode { get; set; } = new ReactiveProperty<string>();
public ReactiveProperty<string> TXT_ID { get; set; } = new ReactiveProperty<string>();
public ReactiveProperty<int> CurrentProgress { get; set; }
public ReactiveProperty<object> IsProgressBarVisible { get; set; } = new ReactiveProperty<object>();
string BienPUT;
string _id = "";
string NFC_code = "";
bool checkID = false;
string memo;
public ReactiveCommand LoadInfo { get; set; } = new ReactiveCommand();
public ReactiveCommand WriteNFC { get; set; } = new ReactiveCommand();
public ReactiveCommand BtnReadNFC { get; set; } = new ReactiveCommand();
public Process()
{
TXTNAME = new ReactiveProperty<string>();
ValueQRCode = new ReactiveProperty<string>();
TXTKANA = new ReactiveProperty<string>();
TXTDATE = new ReactiveProperty<string>();
TXTGENDER = new ReactiveProperty<string>();
TXTNFCCode = new ReactiveProperty<string>();
TXT_ID = new ReactiveProperty<string>();
LoadInfo = new ReactiveCommand();
WriteNFC = new ReactiveCommand();
BtnReadNFC = new ReactiveCommand();
CurrentProgress = new ReactiveProperty<int>();
IsProgressBarVisible = new ReactiveProperty<object>();
IsProgressBarVisible.Value = Visibility.Visible;
LoadInfo.Subscribe(() =>
{
getdata();
});
WriteNFC.Subscribe(() =>
{
writeNFCCode();
});
BtnReadNFC.Subscribe(() =>
{
ReadNFCCode();
});
}
public void getdata()
{
// chuyen huong process bar the hien tien trinh cua ung dung theo thoi gian thuc
// Home
if (ValueQRCode.Value == "" || ValueQRCode.Value ==null)
{
MessageBox.Show("患者番号を入力してください! \n\n患者番号がすでに入力されている場合、患者が今日の予定であるかどうかを確認してください。", "システムからの通知!", MessageBoxButton.OK, MessageBoxImage.Warning);
}
else
{
try
{
Task.Run(async () =>
{
using (var client = new HttpClient())
{
CurrentProgress.Value = 0;
var MyIni = new IniFile("config.ini");
string url = MyIni.Read("url", "config").ToString();
string token = MyIni.Read("token", "config").ToString();
CurrentProgress.Value = 10;
string date = DateTime.Now.ToString("yyyy-MM-dd");
var request = new HttpRequestMessage(HttpMethod.Get, url + "api/books?date=" + date);
CurrentProgress.Value = 20;
request.Headers.Add("ContentType", "application/json");
request.Headers.Add("Authorization", "Bearer " + token);
CurrentProgress.Value = 30;
var response = await client.SendAsync(request);
CurrentProgress.Value = 40;
string a = await response.Content.ReadAsStringAsync();
var content = JsonConvert.DeserializeObject<List<PUT1Bien.MyArray>>(a);
CurrentProgress.Value = 50;
for (int i = 0; i < content.Count; i++)
{
if (content[i].card != null)
{
if (content[i].card.cid != null)
{
if (content[i].card.cid.ToString() == ValueQRCode.Value.ToString())
{
memo = content[i].card.memo.ToString();
checkID = true;
CurrentProgress.Value = 100;
TXTNAME.Value = content[i].card.name.ToString();
TXTKANA.Value = content[i].card.kana.ToString();
TXTDATE.Value = content[i].card.birthday.ToString();
TXTGENDER.Value = content[i].card.gender.ToString();
TXT_ID.Value = content[i].card._id.ToString();
_id = content[i].card._id.ToString();
}
}
}
}
if (checkID == false)
{
TXTNAME.Value = string.Empty;
TXTKANA.Value = string.Empty;
TXTDATE.Value = string.Empty;
TXTGENDER.Value = string.Empty;
TXT_ID.Value = string.Empty;
MessageBox.Show("申し訳ありませんが、患者は今日訪問する予定はありません!\n\n再度確認してください!", "システムからの通知!", MessageBoxButton.OK, MessageBoxImage.Warning);
}
checkID = false;
}
});
}
catch (Exception ex)
{
MessageBox.Show("Error! \n" + ex);
}
}
}
public void writeNFCCode()
{
if (NFC_code == "")
{
MessageBox.Show("NFCコードをまだ読んでいないので、リーダーにNFCコードを置いて、\n\n[NFCコードを読む]ボタンを押してください。", "システムからの通知!", MessageBoxButton.OK, MessageBoxImage.Error);
}
else
{
bool kiemtrachuoi = memo.Contains("(NFC)");
TXTNFCCode.Value = NFC_code;
try
{
Task.Run(async () =>
{
using (var client = new HttpClient())
{
if(memo == null || memo =="" )
{
BienPUT = "(NFC)"+NFC_code+"(end)";//kiem tra xem benh nhan da co ma NFC chua, co roi thi sua lai ma moi, chua co thi them ma moi
}else if(kiemtrachuoi == true)
{
var resuilt = MessageBox.Show("患者はすでにNFCコードを持っていますが、患者のNFCコードを既存のコードに置き換えたいですか?", "システムからの通知!", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (resuilt == MessageBoxResult.Yes)
{
int vitridautien = memo.IndexOf("(NFC)");
memo = memo.Substring(0, vitridautien); //kiem tra co thi xoa
BienPUT = memo+"(NFC)"+NFC_code+"(end)";
}else
{
BienPUT = memo;
}
}
else
{
BienPUT = memo + "\n\n(NFC)" + NFC_code + "(end)";
}
//MessageBox.Show("" + BienPUT);
var MyIni = new IniFile("config.ini");
string url = MyIni.Read("url", "config").ToString();
string token = MyIni.Read("token", "config").ToString();
client.DefaultRequestHeaders.Add("ContentType", "application/json");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
Hashtable hashtable = new Hashtable();
var parameters = new Hashtable
{
["memo"] = BienPUT
};
var json = JsonConvert.SerializeObject(parameters);
var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await client.PutAsync(url + "api/cards/" + TXT_ID.Value, httpContent);
if (response.IsSuccessStatusCode)
{
MessageBox.Show("NFCコードを患者 '" + ValueQRCode.Value + " 'に正常に更新しました!", "システムからの通知!", MessageBoxButton.OK, MessageBoxImage.Information);
TXTDATE.Value = string.Empty;
TXTGENDER.Value = string.Empty;
TXTKANA.Value = string.Empty;
TXTNAME.Value = string.Empty;
TXTNFCCode.Value = string.Empty;
TXT_ID.Value = string.Empty;
ValueQRCode.Value = string.Empty;
}
else
{
MessageBox.Show("NFCコードを患者' " + ValueQRCode.Value + " 'に更新できませんでした!!", "システムからの通知!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}).Wait();
}
catch (Exception ex)
{
MessageBox.Show("" + ex, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
public string ReadNFCCode()
{
string IDcard;
IntPtr hContext = IntPtr.Zero;
// ##################################################
// 1. SCardEstablishContext
// ##################################################
var Myini = new IniFile();
string QRcodeClean = Myini.Read("clean", "configuserup").ToString();
uint ret = nfcapi.SCardEstablishContext(pcsc_const.SCARD_SCOPE_USER, IntPtr.Zero, IntPtr.Zero, out hContext);
uint pcchReaders = 0;
// NFCリーダの文字列バッファのサイズを取得
ret = nfcapi.SCardListReaders(hContext, null, null, ref pcchReaders);
var readerName = "";
IntPtr hCard = IntPtr.Zero;
SCardEstablishContext();
SCardListReaders();
SCardConnect();
//SCardTransmit();
void SCardEstablishContext()
{
Console.WriteLine("***** 1. SCardEstablishContext *****");
if (ret != pcsc_const.SCARD_S_SUCCESS)
{
string message;
switch (ret)
{
case pcsc_const.SCARD_E_NO_SERVICE:
message = "サービスが起動されていません。";
break;
default:
message = "サービスに接続できません。code = " + ret;
break;
}
throw new ApplicationException(message);
}
if (hContext == IntPtr.Zero)
{
throw new ApplicationException("コンテキストの取得に失敗しました。");
}
Console.WriteLine(" サービスに接続しました。");
}
// ##################################################
// 2. SCardListReaders
// ##################################################
void SCardListReaders()
{
Console.WriteLine("***** 2. SCardListReaders *****");
if (ret != pcsc_const.SCARD_S_SUCCESS)
{
// 検出失敗
throw new ApplicationException("NFCリーダを確認できません");
}
// NFCリーダの文字列を取得
byte[] mszReaders = new byte[pcchReaders * 2]; // 1文字2byte
ret = nfcapi.SCardListReaders(hContext, null, mszReaders, ref pcchReaders);
if (ret != pcsc_const.SCARD_S_SUCCESS)
{
//検出失敗
throw new ApplicationException("NFCリーダの取得に失敗しました。");
}
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
string readerNameMultiString = unicodeEncoding.GetString(mszReaders);
// 認識したNDCリーダの最初の1台を使用
int nullindex = readerNameMultiString.IndexOf((char)0);
readerName = readerNameMultiString.Substring(0, nullindex);
Console.WriteLine(" NFCリーダを検出しました。 " + readerName);
}
// ##################################################
// 3. SCardConnect
// ##################################################
//
void SCardConnect()
{
//Console.WriteLine("***** 3. SCardConnect *****");
do
{
//Console.WriteLine("***** 3. SCardConnect *****");
//IntPtr hCard = IntPtr.Zero;
IntPtr activeProtocol = IntPtr.Zero;
ret = nfcapi.SCardConnect(hContext, readerName, pcsc_const.SCARD_SHARE_SHARED, pcsc_const.SCARD_PROTOCOL_T1, ref hCard, ref activeProtocol);
} while (ret != pcsc_const.SCARD_S_SUCCESS);
//Console.WriteLine(" カードに接続しました。");
SCardTransmit();
}
void SCardTransmit()
{
//Console.WriteLine("***** 4. SCardTransmit *****");//
uint maxRecvDataLen = 256;
var recvBuffer = new byte[maxRecvDataLen + 2];
var sendBuffer = new byte[] { 0xff, 0xca, 0x00, 0x00, 0x00 }; // ← IDmを取得するコマンド
nfcapi.SCARD_IO_REQUEST ioRecv = new nfcapi.SCARD_IO_REQUEST();
ioRecv.cbPciLength = 255;
int pcbRecvLength = recvBuffer.Length;
int cbSendLength = sendBuffer.Length;
IntPtr handle = nfcapi.LoadLibrary("Winscard.dll");
IntPtr pci = nfcapi.GetProcAddress(handle, "g_rgSCardT1Pci");
nfcapi.FreeLibrary(handle);
ret = nfcapi.SCardTransmit(hCard, pci, sendBuffer, cbSendLength, ioRecv, recvBuffer, ref pcbRecvLength);
//if (ret != pcsc_const.SCARD_S_SUCCESS)
//{
// throw new ApplicationException("NFCカードへの送信に失敗しました。code = " + ret);
//}
do
{
ret = nfcapi.SCardTransmit(hCard, pci, sendBuffer, cbSendLength, ioRecv, recvBuffer, ref pcbRecvLength);
if (ret != pcsc_const.SCARD_S_SUCCESS)
SCardConnect();
} while (ret != pcsc_const.SCARD_S_SUCCESS);
// 受信データからIDmを抽出する
// recvBuffer = IDm + SW1 + SW2 (SW = StatusWord)
// SW1 = 0x90 (144) SW1 = 0x00 (0) で正常だが、ここでは見ていない
string cardId = BitConverter.ToString(recvBuffer, 0, pcbRecvLength);
//Console.WriteLine(" カードからデータを取得しました。");
//MessageBox.Show(" 【IDm】:" + cardId);
Console.Beep(1220, 100);
int milisecond = 1000;
IDcard = cardId;
if (IDcard == "")
{
SCardConnect();
}
else
{
TXTNFCCode.Value = IDcard;
NFC_code = IDcard;
//Thread.Sleep(milisecond);
//IDcard = "";
//SCardConnect();
}
}
return IDcard;
}
}
}