hardware_cmd.cpp 23.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 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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
#include "stdafx.h"

#include "etradeclient/hardware/hardware_cmd.h"

#include <sstream>

#include <boost/algorithm/string.hpp>
#include <boost/property_tree/json_parser.hpp>

//#include "etradeclient/boost_patch/property_tree/json_parser.hpp" // WARNIING! Make sure to include our patched version.
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/string_converter.h"
#include "etradeclient/utility/application_config.h"

#include "etradeclient/hardware/pin_pad.h"
#include "etradeclient/hardware/id_card_reader.h"
#include "etradeclient/hardware/dili_card_device.h"
#include "etradeclient/hardware/bank_card_reader.h"
#include "etradeclient/hardware/password_machine.h"
#include "ETradeClient/utility/openssl_aes_cbc.h"
#include "../hardware/AES.h"
#include "etradeclient/utility/win_http.h"
#include "etradeclient/utility/logging.h"
#include "etradeclient/utility/url_regex.h"
#include "etradeclient/utility/url_config.h"

namespace StatusCode
{
	static const std::string OK = ""; // 操作成功.

	static const std::string PINPAD_CONNECT_FAILED = "100"; // 连接密码键盘失败. 
	static const std::string PINPAD_TIMEOUT = "101"; // 用户密码输入超时. 
	static const std::string PINPAD_CANCELLED = "102"; // 用户取消密码输入. 
	static const std::string PINPAD_READ_FAILED = "103"; // 读取密码失败. 

	static const std::string ID_CARD_READER_CONNECT_FAILED = "200"; // 连接身份证读卡器失败. 
	static const std::string ID_CARD_READER_VERIFY_FAILED = "201"; // 身份证验证失败.
	static const std::string ID_CARD_READER_READ_FAILED = "202"; // 读取身份证信息失败.

	static const std::string CPU_CARD_DEVICE_CONNECT_FAILED = "300"; // 打开CPU卡读卡器失败. 
	static const std::string CPU_CARD_DEVICE_CANNOT_FIND_CARD = "301"; // 无法找到卡片,请放卡到读卡器上(或重新放置卡片).
	
	static const std::string DILI_CARD_DEVICE_ACTIVATE_CARD_FAILED = "302"; // 卡片激活失败, 请检查该卡是否已处于激活状态.
	static const std::string DILI_CARD_DEVICE_RESET_CARD_FAILED = "303"; // 卡片撤销激活失败, 请检查该卡是否已处于未激活状态.
	static const std::string DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED = "304"; // 读取卡片基本信息失败.
	static const std::string DILI_CARD_DEVICE_READ_SERVICE_INFO_FAILED = "305"; // 读取卡片业务信息失败.
	static const std::string DILI_CARD_DEVICE_WRITE_SERVICE_INFO_FAILED = "306"; // 写入卡片业务信息失败.
	static const std::string DILI_CARD_DEVICE_WRITE_UNMATCHED_CARD = "307"; // 写卡卡片的序列号不匹配.

	static const std::string PWD_MACHINE_CONNECT_FAILED = "401"; // 连接加密机失败.
	static const std::string PWD_MACHINE_GET_KEY_FAILED = "402"; // 从加密机读取密钥失败.

	static const std::string BANK_CARD_READ_CARD_NUM_FAILED = "501"; // 读取银行卡卡号失败.

	static const std::string SCANNER_READ_DATA_FAILED = "601"; //扫码枪扫描数据失败。
}

namespace 
{
	const PINPad& PINPad_()
	{
		static PINPad pin_pad;
		return pin_pad;
	}

	const IDCardReader& IDCardReader_()
	{
		static IDCardReader id_card_reader;
		return id_card_reader;
	}

	const DILICard::RWDevice& DILICardRWDevice()
	{
		static DILICard::RWDevice dili_card_device;
		return dili_card_device;
	}

	const PWDMachine& PWDMachine_()
	{
		static PWDMachine pwd_machine;
		return pwd_machine;
	}

	const BankCardReader& BankCardReader_()
	{
		static BankCardReader bank_card_reader;
		return bank_card_reader;
	}

	std::string g_sCardNum;//用于办卡时,保证操作的卡片是同一张
}

ReadPINPadCmd::Reply ReadPINPadCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取用户输入密码。");
	const uint8_t PWD_MAX_LEN = ApplicationConfig::Instance().PINPadPWDLen();
	const uint8_t TIMEOUT = ApplicationConfig::Instance().PINPadTimeout();
	auto& pin_pad = PINPad_();
	std::string pwd("");
	Reply reply;
	do
	{
		try
		{
			if (!pin_pad.Connect())
			{
				LOG_ERROR(L"连接密码键盘失败。");
				reply.error_code = StatusCode::PINPAD_CONNECT_FAILED;
				break;
			}
			pin_pad.DisplayBuiltInText(PINPad::BuiltInText::kEnterPWD);
			pin_pad.PlayVoice(PINPad::BuiltInVoice::kEnterPWD);
			pin_pad.SetPWDMaxLen(PWD_MAX_LEN);
			PINPad::StatusCode res = pin_pad.ReadUserInputPWD(PINPad::DispMode::kEncrypted, PINPad::DispLine::kL2, TIMEOUT, pwd);
			if (PINPad::StatusCode::kCancelled == res)
			{
				reply.error_code = StatusCode::PINPAD_CANCELLED;
				LOG_ERROR(L"用户取消密码输入。");
			}
			else if (PINPad::StatusCode::kTimeout == res)
			{
				reply.error_code = StatusCode::PINPAD_TIMEOUT;
				LOG_ERROR(L"用户输入密码超时。");
			}
			else if (PINPad::StatusCode::kUnknown == res)
			{
				reply.error_code = StatusCode::PINPAD_READ_FAILED;
				LOG_ERROR(L"读取用户输入密码失败,请确认密码键盘连接正常!");
			}
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取用户输入密码发生异常,异常信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::PINPAD_READ_FAILED;
			break;
		}
	} while (0);
	pin_pad.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取用户输入密码成功。");

	reply.data.put("password", pwd);
	return reply;
}

ReadPINPadCmd::Reply ReadIDCardCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取身份证信息。");
	auto& id_card_reader = IDCardReader_();
	Reply reply;
	IDCardInfo id_card_info;
	do
	{
		try
		{
			if (!id_card_reader.Connect())
			{
				LOG_ERROR(L"建立身份证读卡器连接失败!");
				reply.error_code = StatusCode::ID_CARD_READER_CONNECT_FAILED;
				break;
			}
			if (!id_card_reader.VerifyCard())
			{
				LOG_ERROR(L"未找到身份证或身份证验证失败!");
				reply.error_code = StatusCode::ID_CARD_READER_VERIFY_FAILED;
				break;
			}
			id_card_info = id_card_reader.ReadCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取身份证信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::ID_CARD_READER_READ_FAILED;
			break;
		}
	} while (0);
	id_card_reader.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取身份证信息成功。");

	reply.data.put("id", wstr_2_str(id_card_info.id_code));
	reply.data.put("name", wstr_2_str(id_card_info.name)); // Use 'wstr_2_str' to convert to UTF-8 string.
	reply.data.put("gender", wstr_2_str(id_card_info.gender));
	reply.data.put("nation", wstr_2_str(id_card_info.nation));
	reply.data.put("birthday", wstr_2_str(id_card_info.birth_date));
	reply.data.put("address", wstr_2_str(id_card_info.address));
	reply.data.put("portrait_img_path", wstr_2_str(id_card_info.portrait_img_path));
	reply.data.put("depart", wstr_2_str(id_card_info.depart));
	reply.data.put("start_date", wstr_2_str(id_card_info.start_date));
	reply.data.put("end_date", wstr_2_str(id_card_info.end_date));
	return reply;
}

ReadPINPadCmd::Reply ActivateDILICardCmd::Execute(const std::string& input)
{
	Reply reply;
	LOG_TRACE(L"卡片激活。");
	
	std::string card_sn;
	std::string key_DCCK, key_F0015_DACK, key_F0015_DEAK, key_F0015_DAMK1;
	DILICard::BasicInfo card_basic_info;
	bool bIsEncrypt = ApplicationConfig::Instance().IsEncrypt();
	uint8_t u8EncrypType = ApplicationConfig::Instance().EncryptType();
	auto& dili_card_device = DILICardRWDevice();
	do 
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}

		card_sn = stuCardBasicInfo.chip_num + std::string("44494c49") + stuCardBasicInfo.chip_num;

		if (bIsEncrypt)
		{
			const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
			const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
			const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

			if (u8EncrypType == 0)
			{
				auto& pwd_machine = PWDMachine_();

				if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
				{
					LOG_ERROR(L"连接加密机失败!");
					reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
				}

				try
				{
					key_DCCK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kDCCK>(), card_sn, TIMEOUT);
					key_F0015_DACK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DACK>(), card_sn, TIMEOUT);
					key_F0015_DEAK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DEAK>(), card_sn, TIMEOUT);
					key_F0015_DAMK1 = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DAMK1>(), card_sn, TIMEOUT);
				}
				catch (std::exception& ex)
				{
					LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
					reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
					break;
				}

				pwd_machine.Disconnect();
			} 
			else if (u8EncrypType == 1)
			{
				try
				{
					key_DCCK = AES_CBC::Encrypt(card_sn, 1);
					key_F0015_DACK = AES_CBC::Encrypt(card_sn, 2);
					key_F0015_DEAK = AES_CBC::Encrypt(card_sn, 3);
					key_F0015_DAMK1 = AES_CBC::Encrypt(card_sn, 4);
				}
				catch (std::exception& ex)
				{
					LOG_FATAL(L"软加密密钥失败");
					reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
					break;
				}
			}
		}
		else
		{
			key_DCCK = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0";
			key_F0015_DACK = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6";
			key_F0015_DEAK = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7";
			key_F0015_DAMK1 = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8";
		}

		try
		{
			dili_card_device.ActivateCard(card_sn, key_DCCK, key_F0015_DACK, key_F0015_DEAK, key_F0015_DAMK1);
			card_basic_info = dili_card_device.ReadCardBasicInfo();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"卡片激活失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_ACTIVATE_CARD_FAILED;
			break;
		}
		
	} while (0);
	
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"卡片激活成功。");

	reply.data.put("chipNo", card_basic_info.chip_num);
	reply.data.put("deviceId", card_basic_info.device_id);
	reply.data.put("type", card_basic_info.type_code);
	reply.data.put("issuerCode", card_basic_info.issuer_code);
	reply.data.put("verifyCode", card_basic_info.verify_code);
	return reply;
}

ReadPINPadCmd::Reply ResetDILICardCmd::Execute(const std::string& input)
{
	//const uint8_t BEEP_TIME = 20; // 20 * 10ms
	

	LOG_TRACE(L"卡片撤销激活。");
	auto& dili_card_device = DILICardRWDevice();
	
	Reply reply;
	std::string card_sn;
	std::string key_DCCK, key_F0015_DACK;
	DILICard::BasicInfo card_basic_info;

	uint8_t u8EncrypType = ApplicationConfig::Instance().EncryptType();
	bool bIsEncrypt = ApplicationConfig::Instance().IsEncrypt();

	do
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}
		card_sn = stuCardBasicInfo.device_id;

		if (bIsEncrypt)
		{
			if (u8EncrypType == 0)
			{
				const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
				const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
				const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

				auto& pwd_machine = PWDMachine_();

				if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
				{
					LOG_ERROR(L"连接加密机失败!");
					reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
					break;
				}

				try
				{
					key_DCCK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kDCCK>(), card_sn, TIMEOUT);
					key_F0015_DACK = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DACK>(), card_sn, TIMEOUT);
				}
				catch (std::exception& ex)
				{
					LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
					reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
					break;
				}

				pwd_machine.Disconnect();
			}
			else if (u8EncrypType == 1)
			{
				key_DCCK = AES_CBC::Encrypt(card_sn, 1);
				key_F0015_DACK = AES_CBC::Encrypt(card_sn, 2);
			}
		}
		else
		{
			key_DCCK = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0";
			key_F0015_DACK = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF6";
		}
		
		try
		{
			if (bIsEncrypt)
			{
				dili_card_device.ResetCard(key_DCCK, key_F0015_DACK);
			}
			card_basic_info = dili_card_device.ReadCardBasicInfo();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"卡片撤销激活失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_RESET_CARD_FAILED;
			break;
		}
	} while (0);
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"卡片撤销激活成功。");

	reply.data.put("chipNo", card_basic_info.chip_num);
	reply.data.put("deviceId", card_basic_info.device_id);
	reply.data.put("type", card_basic_info.type_code);
	reply.data.put("issuerCode", card_basic_info.issuer_code);
	reply.data.put("verifyCode", card_basic_info.verify_code);
	return reply;
}

ReadPINPadCmd::Reply ReadDILICardBasicInfoCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取卡片基本信息。");
	auto& dili_card_device = DILICardRWDevice();
	Reply reply;
	DILICard::BasicInfo card_basic_info;
	do
	{
		try 
		{ 
			dili_card_device.Connect(); 
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		try { card_basic_info = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}

		
	} while (0);
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取卡片基本信息成功。");

	reply.data.put("chipNo", card_basic_info.chip_num);
	reply.data.put("deviceId", card_basic_info.device_id);
	reply.data.put("type", card_basic_info.type_code);
	reply.data.put("issuerCode", card_basic_info.issuer_code);
	reply.data.put("verifyCode", card_basic_info.verify_code);
	g_sCardNum = card_basic_info.device_id;
	return reply;
}

ReadPINPadCmd::Reply ReadDILICardServiceInfoCmd::Execute(const std::string& input)
{
	bool bIsEncrypt = ApplicationConfig::Instance().IsEncrypt();
	uint8_t u8EncrypType = ApplicationConfig::Instance().EncryptType();
	LOG_TRACE(L"读取卡片业务信息。");
	auto& dili_card_device = DILICardRWDevice();
	
	Reply reply;
	std::string card_sn;
	std::string key_r;
	std::string service_data;
	do
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}
		card_sn = stuCardBasicInfo.device_id;

		if (bIsEncrypt)
		{
			if (u8EncrypType == 0)
			{
				auto& pwd_machine = PWDMachine_();
				const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
				const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
				const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

				if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
				{
					LOG_ERROR(L"连接加密机失败!");
					reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
					break;
				}

				try { key_r = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DEAK>(), card_sn, TIMEOUT); }
				catch (std::exception& ex)
				{
					LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
					reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
					break;
				}

				pwd_machine.Disconnect();
			}
			else if (u8EncrypType == 1)
			{
				key_r = AES_CBC::Encrypt(card_sn, 3);
			}
		}
		else
		{
			key_r = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7";
		}

		try { service_data = dili_card_device.ReadCardServiceInfo(key_r); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片业务信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_SERVICE_INFO_FAILED;
			break;
		}
	} while (0);
	
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取卡片业务信息成功。");

	reply.data.put("", service_data);
	return reply;
}

ReadPINPadCmd::Reply WriteDILICardServiceInfoCmd::Execute(const std::string& input)
{
	const char DATA_SPLITER = ':';
	bool bIsEncrypt = ApplicationConfig::Instance().IsEncrypt();
	uint8_t u8EncrypType = ApplicationConfig::Instance().EncryptType();

	LOG_TRACE(L"写入卡片业务信息。");
	auto& dili_card_device = DILICardRWDevice();
	
	Reply reply;
	std::string card_sn;
	std::string key_w;
	do
	{
		try
		{
			dili_card_device.Connect();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"连接读卡器失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		try
		{
			dili_card_device.FindCard();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"寻卡失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CANNOT_FIND_CARD;
			break;
		}

		DILICard::BasicInfo stuCardBasicInfo;
		try { stuCardBasicInfo = dili_card_device.ReadCardBasicInfo(); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取卡片基本信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
			break;
		}
		card_sn = stuCardBasicInfo.device_id;

		std::size_t index = input.find(DATA_SPLITER);
		if (std::string::npos == index) // NOT found.
		{
			LOG_ERROR(L"写入卡片业务信息失败,错误信息:请求数据错误。");
			reply.error_code = StatusCode::DILI_CARD_DEVICE_WRITE_SERVICE_INFO_FAILED;
			break;
		}
		std::string device_id = g_sCardNum;//用于办卡时,保证操作的卡片是同一张;
		std::string service_data = input.substr(index + 1, input.size() - index - 1);

		// 验证操作的是否是同一张卡,即device_id是否匹配
		if (!boost::iequals(card_sn, device_id))
		{
			LOG_ERROR(L"写卡卡片的序列号不匹配!");
			reply.error_code = StatusCode::DILI_CARD_DEVICE_WRITE_UNMATCHED_CARD;
			break;
		}

		if (bIsEncrypt)
		{
			if (u8EncrypType == 0)
			{
				auto& pwd_machine = PWDMachine_();

				const std::string PWD_MACHINE_IP = ApplicationConfig::Instance().PWDMachineIP();
				const uint32_t PWD_MACHINE_PORT = ApplicationConfig::Instance().PWDMachinePort();
				const uint8_t TIMEOUT = ApplicationConfig::Instance().PWDMachineTimeout();

				if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
				{
					LOG_ERROR(L"连接加密机失败!");
					reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
					break;
				}

				try { key_w = pwd_machine.GetCardPassword(DILICard::CardKeyCode<DILICard::KeyCode::kF0015_DAMK1>(), card_sn, TIMEOUT); }
				catch (std::exception& ex)
				{
					LOG_FATAL(L"从加密机读取密钥失败,错误信息: " + str_2_wstr(ex.what()));
					reply.error_code = StatusCode::PWD_MACHINE_GET_KEY_FAILED;
					break;
				}
				
				pwd_machine.Disconnect();
			}
			else if (u8EncrypType == 1)
			{
				key_w = AES_CBC::Encrypt(card_sn, 4);
			}
		}
		else
		{
			key_w = "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF8";
		}
		
		try { dili_card_device.WriteCardServiceInfo(key_w, service_data); }
		catch (std::exception& ex)
		{
			LOG_FATAL(L"写入卡片业务信息失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::DILI_CARD_DEVICE_WRITE_SERVICE_INFO_FAILED;
			break;
		}

		
	} while (0);
	
	dili_card_device.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"写入卡片业务信息成功。");
	reply.data.put("", "");
	return reply;
}

ReadPINPadCmd::Reply ReadBankCardNumCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取银行卡号。");
	auto& bank_card_reader = BankCardReader_();
	std::string bank_card_no("");
	Reply reply;
	do
	{
		try
		{
			if (!bank_card_reader.Connect())
			{
				LOG_ERROR(L"连接银行卡读卡器失败。");
				reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
				break;
			}
			bank_card_no = bank_card_reader.ReadBankCardNum();
		}
		catch (std::exception& ex)
		{
			LOG_FATAL(L"读取银行卡号失败,错误信息: " + str_2_wstr(ex.what()));
			reply.error_code = StatusCode::BANK_CARD_READ_CARD_NUM_FAILED;
			break;
		}
	} while (0);
	bank_card_reader.Disconnect();
	if (boost::iequals(reply.error_code, StatusCode::OK))
		LOG_TRACE(L"读取银行卡号成功。");

	reply.data.put("chipNo", bank_card_no);
	return reply;
}

ReadPINPadCmd::Reply DecodeScannerCmd::Execute(const std::string& input)
{
	LOG_TRACE(L"读取扫码枪数据。");
	Reply reply;
	/*unsigned char *in_data = new unsigned char[input.size()];
	unsigned char *out_data = new unsigned char[input.size()];
	memset(in_data, 0, input.size());
	memset(out_data, 0, input.size());
	memcpy(in_data, input.c_str(), input.size());

	AESDecode(in_data, input.size(), out_data);

	std::string s_len;
	for (int index = 0; index < 4; ++index)
	{
		s_len += out_data[index];
	}
	int len = std::stoi(s_len);

	std::string real_data;
	for (int index = 4; index < len; ++index)
	{
		real_data += out_data[index];
	}

	delete in_data;
	delete out_data;

	reply.data.put("plaint_data", real_data);*/

	auto& url_cfg = URLConfig::Instance();
	const uint32_t kHTTPOK = 200;
	WinHttp win_http;
	win_http.ConnectHost(url_cfg.DecodeHost(), url_cfg.Port(), url_cfg.IsHttps());
	auto& request = win_http.OpenRequest(WinHttp::Method::POST, url_cfg.DecodePath());
	request.SetPostData("{\"data\":\"" + input + "\"}");
	request.Send();
	uint32_t status_code = request.GetResponseStatus();
	if (kHTTPOK != status_code)
	{
		LOG_ERROR(L"网络请求错误! 错误码: " + std::to_wstring(status_code));
		reply.error_code = StatusCode::SCANNER_READ_DATA_FAILED;
		return reply;
	}
	std::string response_body = request.ReadResponseBody();
	if (response_body.empty())
	{
		LOG_ERROR(L"获取服务器响应数据失败,请确保网络连接正常!");
		reply.error_code = StatusCode::SCANNER_READ_DATA_FAILED;
		return reply;
	}

	namespace PT = boost::property_tree;
	bool login_res = false;
	try //Parse the configuration file
	{
		PT::ptree ptree, pchildtree;
		std::stringstream ss;
		ss << response_body;
		PT::read_json(ss, ptree);
		pchildtree = ptree.get_child("data");
		std::string res = pchildtree.get<std::string>("data");
		reply.data.put("plaint_data", res);
	}
	catch (...)
	{
		LOG_ERROR(L"解析服务器返回的登录结果信息时出错!请确认返回数据不为空,返回的数据格式为正确的Json格式!");
		reply.error_code = StatusCode::SCANNER_READ_DATA_FAILED;
		return reply;
	}
	return reply;
}