hardware_cmd.cpp 13 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
#include "stdafx.h"

#include "etradeclient/hardware/hardware_cmd.h"

#include <sstream>

#include <boost/algorithm/string.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/hardware/DLCamCardManager.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"; // 读取银行卡卡号失败.
}

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;
	}

	const DLCamCardManager& DLCamCardManager_()
	{
		static DLCamCardManager CamCardManager;
		return CamCardManager;
	}
}

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)
{
	//临时只读基本信息 add by liuye 
	LOG_TRACE(L"园区卡开卡操作。");
	auto cCamCard = DLCamCardManager_();
	Reply reply;
	DILICard::BasicInfo card_basic_info;
	try
	{
		LOG_TRACE(L"连接读卡器。");
		cCamCard.Connect();
		LOG_TRACE(L"寻卡操作。");
		cCamCard.FindAndResetCard();
		LOG_TRACE(L"切换主目录。");
		cCamCard.SelectMF();
		LOG_TRACE(L"切换文件。");
		cCamCard.SelectFile();
		LOG_TRACE(L"读取卡片基本信息。");
		card_basic_info = cCamCard.ReadCardBasicInfo();
	}
	catch (std::exception& ex)
	{
		reply.error_code = StatusCode::DILI_CARD_DEVICE_ACTIVATE_CARD_FAILED;
		LOG_FATAL(L"读取园区卡信息失败,错误信息: " + str_2_wstr(ex.what()));
	}
	cCamCard.DisConnect();

	if (boost::iequals(reply.error_code, StatusCode::OK))
	{
		LOG_TRACE(L"园区卡卡片激活成功。");
	}
	else
	{
		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)
{
	//临时只读基本信息 add by liuye 
	LOG_TRACE(L"撤销园区卡激活操作。");
	auto cCamCard = DLCamCardManager_();
	Reply reply;
	DILICard::BasicInfo card_basic_info;
	try
	{
		LOG_TRACE(L"连接读卡器。");
		cCamCard.Connect();
		LOG_TRACE(L"寻卡操作。");
		cCamCard.FindAndResetCard();
		LOG_TRACE(L"切换主目录。");
		cCamCard.SelectMF();
		LOG_TRACE(L"切换文件。");
		cCamCard.SelectFile();
		LOG_TRACE(L"读取卡片基本信息。");
		card_basic_info = cCamCard.ReadCardBasicInfo();
	}
	catch (std::exception& ex)
	{
		reply.error_code = StatusCode::DILI_CARD_DEVICE_RESET_CARD_FAILED;
		LOG_FATAL(L"读取园区卡信息失败,错误信息: " + str_2_wstr(ex.what()));
	}
	cCamCard.DisConnect();

	if (boost::iequals(reply.error_code, StatusCode::OK))
	{
		LOG_TRACE(L"撤销园区卡激活操作成功。");
	}
	else
	{
		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)
{
	//临时只读基本信息 add by liuye 
	LOG_TRACE(L"读取园区卡基本信息操作。");
	auto cCamCard = DLCamCardManager_();
	Reply reply;
	DILICard::BasicInfo card_basic_info;
	try
	{
		LOG_TRACE(L"连接读卡器。");
		cCamCard.Connect();
		LOG_TRACE(L"寻卡操作。");
		cCamCard.FindAndResetCard();
		LOG_TRACE(L"切换主目录。");
		cCamCard.SelectMF();
		LOG_TRACE(L"切换文件。");
		cCamCard.SelectFile();
		LOG_TRACE(L"读取卡片基本信息。");
		card_basic_info = cCamCard.ReadCardBasicInfo();
	}
	catch (std::exception& ex)
	{
		reply.error_code = StatusCode::DILI_CARD_DEVICE_READ_BASIC_INFO_FAILED;
		LOG_FATAL(L"读取园区卡信息失败,错误信息: " + str_2_wstr(ex.what()));
	}
	cCamCard.DisConnect();

	if (boost::iequals(reply.error_code, StatusCode::OK))
	{
		LOG_TRACE(L"读取园区卡信息成功。");
	}
	else
	{
		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 ReadDILICardServiceInfoCmd::Execute(const std::string& input)
{
	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();
	const uint8_t BEEP_TIME = 20; // 20 * 10ms
	LOG_TRACE(L"读取卡片业务信息。");
	auto& dili_card_device = DILICardRWDevice();
	auto& pwd_machine = PWDMachine_();
	Reply reply;
	std::string card_sn;
	std::string key_r;
	std::string service_data;
	do
	{
		if (!dili_card_device.Connect())
		{
			LOG_ERROR(L"打开CPU卡读卡器失败!");
			reply.error_code = StatusCode::CPU_CARD_DEVICE_CONNECT_FAILED;
			break;
		}
		if (!pwd_machine.Connect(PWD_MACHINE_IP, PWD_MACHINE_PORT))
		{
			LOG_ERROR(L"连接加密机失败!");
			reply.error_code = StatusCode::PWD_MACHINE_CONNECT_FAILED;
			break;
		}
		dili_card_device.Beep(BEEP_TIME);
		try { card_sn = 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 { 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;
		}

		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();
	pwd_machine.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)
{
	//临时方法,不写入业务数据 add by liuye 
	Reply reply;
	auto cCamCard = DLCamCardManager_();
	DILICard::BasicInfo card_basic_info;
	try
	{
		LOG_TRACE(L"连接读卡器。");
		cCamCard.Connect();
	}
	catch (std::exception& ex)
	{
		reply.error_code = StatusCode::DILI_CARD_DEVICE_WRITE_SERVICE_INFO_FAILED;
		LOG_FATAL(L"读取园区卡信息失败,错误信息: " + str_2_wstr(ex.what()));
	}
	cCamCard.DisConnect();
	LOG_TRACE(L"写入业务数据操作成功!");
	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;
}