RFID.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include "../Config.h"
  6. #include "../Log/log.h"
  7. #include "../Define/define.h"
  8. #include "../ShareMemory/shmMem.h"
  9. #include "../SelectGun/SelectGun.h"
  10. #include "../DataBase/DataBase.h"
  11. #include "main.h"
  12. #include "../timeout.h"
  13. static DcCommonInfo *ShmDcCommonData = NULL;
  14. static SelectGunInfo *ShmSelectGunInfo = NULL;
  15. static struct SysInfoData *pSysInfo = NULL;
  16. #define PREAUTHMONEY 1000
  17. bool isDeductDb_ready;
  18. //------------------------------------------------------------------------------
  19. static char *rfidPortName = "/dev/ttyS2";
  20. int fd = -1;
  21. static bool isCardScan = false;
  22. //------------------------------------------------------------------------------
  23. static bool canStartCharging(void)
  24. {
  25. uint8_t index = 0;
  26. char buf2[16] = "";
  27. memset(buf2, 0, ARRAY_SIZE(buf2));
  28. struct OCPP16Data *ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  29. for (index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++) {
  30. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  31. }
  32. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  33. // 因為無法得知實際的長度,所以只能用搜尋的方式
  34. if (strcmp(buf2, "Accepted") == EQUAL) {
  35. return true;
  36. }
  37. return false;
  38. }
  39. bool isAutorCompleteHandle(/*uint8_t *authorizeIndex*/)
  40. {
  41. // uint8_t i = 0;
  42. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  43. //struct ChargingInfoData *pDcChargingInfo = NULL;
  44. //struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  45. // 透過後臺停止充電的判斷
  46. if (isAuthorizedComplete()) {
  47. // 判斷後台回覆狀態
  48. if (canStartCharging() == false) {
  49. strcpy((char *)pSysConfig->UserId, "");
  50. ClearAuthorizedFlag();
  51. }
  52. return true;
  53. }
  54. return false;
  55. /*
  56. if (*(authorizeIndex) != NO_DEFINE) {
  57. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(*(authorizeIndex));
  58. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  59. strcmp((char *)pDcChargingInfo->StartUserId, "") != EQUAL) {
  60. // 先找 AC
  61. if (*(authorizeIndex) == DEFAULT_AC_INDEX) {
  62. AcChargingTerminalProcess();
  63. } else {
  64. ChargingTerminalProcess(*(authorizeIndex));
  65. }
  66. }
  67. strcpy((char *)pSysConfig->UserId, "");
  68. *(authorizeIndex) = NO_DEFINE;
  69. }
  70. ClearAuthorizedFlag();
  71. } /*else if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  72. // 白名單驗證
  73. for (i = 0; i < 10; i++) {
  74. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], "") == EQUAL) {
  75. continue;
  76. }
  77. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], (char *)pSysConfig->UserId) == EQUAL) {
  78. ChargingTerminalProcess(*(authorizeIndex));
  79. strcpy((char *)pSysConfig->UserId, "");
  80. ClearAuthorizedFlag();
  81. break;
  82. }
  83. }
  84. }*/
  85. }
  86. bool RfidStopCharging(void)
  87. {
  88. //當前沒有選槍
  89. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  90. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  91. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL ) {
  92. strcpy((char *)pSysConfig->UserId, "");
  93. return false;
  94. }
  95. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  96. if (pDcChargingInfo->isRemoteStart) {
  97. } else {
  98. if (strcmp((char *)pSysConfig->UserId, (char *)pDcChargingInfo->StartUserId) == EQUAL) {
  99. ChargingTerminalProcess(pSysInfo->CurGunSelected);
  100. strcpy((char *)pSysConfig->UserId, "");
  101. log_info("index = %d, card number = %s, UserId = %s ",
  102. pSysInfo->CurGunSelected,
  103. pDcChargingInfo->StartUserId,
  104. pSysConfig->UserId);
  105. return true;
  106. } else {
  107. strcpy((char *)pSysConfig->UserId, "");
  108. return false;
  109. }
  110. }
  111. return false;
  112. }
  113. static void UserScanFunction(void)
  114. {
  115. bool idleReq = false;
  116. uint8_t i = 0;
  117. uint8_t stopReq = NO_DEFINE;
  118. char value[32] = {0};
  119. static uint8_t _authorizeIndex = NO_DEFINE;
  120. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  121. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  122. struct ChargingInfoData *pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  123. struct ChargingInfoData *pDcChargingInfo = NULL;
  124. GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
  125. // 當前非驗證的狀態
  126. if (IsAuthorizingMode()) {
  127. isAutorCompleteHandle(/*&_authorizeIndex*/);
  128. }
  129. //當前沒有選槍
  130. /*
  131. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL) {
  132. strcpy((char *)pSysConfig->UserId, "");
  133. return;
  134. }*/
  135. // 先判斷現在是否可以提供刷卡
  136. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  137. // 2. 停止充電
  138. //if (pSysInfo->PageIndex == _LCM_ERROR) {
  139. if (pSysInfo->SystemPage == _PAGE_MAINTAIN) {
  140. strcpy((char *)pSysConfig->UserId, "");
  141. return;
  142. }
  143. /*
  144. for (i = 0; i < pSysConfig->TotalConnectorCount; i++) {
  145. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(i);
  146. if (pDcChargingInfo->SystemStatus == S_CHARGING) {
  147. stopReq = i;
  148. }
  149. if ((pDcChargingInfo->SystemStatus == S_AUTHORIZING &&
  150. pDcChargingInfo->IsAvailable == YES) ||
  151. (pGunIndexInfo->AcGunIndex > 0 &&
  152. pAcChargingInfo->SystemStatus == S_AUTHORIZING &&
  153. pAcChargingInfo->IsAvailable)
  154. ) {
  155. idleReq = true;
  156. }
  157. }
  158. if (pGunIndexInfo->AcGunIndex > 0 &&
  159. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX &&
  160. pAcChargingInfo->SystemStatus == S_CHARGING) {
  161. stopReq = DEFAULT_AC_INDEX;
  162. }
  163. */
  164. if (strlen((char *)pSysConfig->UserId) <= 0) {
  165. return;
  166. }
  167. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  168. // Stop Charging For AC
  169. /*if (pGunIndexInfo->AcGunIndex > 0 &&
  170. stopReq == DEFAULT_AC_INDEX &&
  171. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  172. log_info("ac stop charging ");
  173. log_info("index = %d, card number = %s, UserId = %s ",
  174. pSysInfo->CurGunSelectedByAc,
  175. pAcChargingInfo->StartUserId,
  176. pSysConfig->UserId);
  177. memset(value, 0, sizeof(value));
  178. memcpy(value,
  179. (uint8_t *)pAcChargingInfo->StartUserId,
  180. ARRAY_SIZE(pAcChargingInfo->StartUserId));
  181. if (strcmp((char *)pSysConfig->UserId, value) == EQUAL) {
  182. AcChargingTerminalProcess();
  183. }
  184. strcpy((char *)pSysConfig->UserId, "");
  185. } else if (stopReq < pSysConfig->TotalConnectorCount &&
  186. pDcChargingInfo->SystemStatus == S_CHARGING &&
  187. (pGunIndexInfo->AcGunIndex <= 0 ||
  188. (pGunIndexInfo->AcGunIndex > 0 &&
  189. pSysInfo->CurGunSelectedByAc == NO_DEFINE))
  190. ) {
  191. // Stop Charging
  192. if (RfidStopCharging())
  193. return;
  194. if (pGunIndexInfo->AcGunIndex > 0 &&
  195. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) {
  196. _authorizeIndex = pSysInfo->CurGunSelectedByAc;
  197. } else {
  198. _authorizeIndex = pSysInfo->CurGunSelected;
  199. }
  200. strcpy((char *)pSysConfig->UserId, "");
  201. return;
  202. } else if (idleReq) {
  203. if (pSysConfig->TotalConnectorCount > 1 &&
  204. stopReq != 255 &&
  205. pSysInfo->IsAlternatvieConf == YES) {
  206. idleReq = false;
  207. strcpy((char *)pSysConfig->UserId, "");
  208. } else if ((pGunIndexInfo->AcGunIndex > 0 &&
  209. pSysInfo->CurGunSelectedByAc == DEFAULT_AC_INDEX) ||
  210. pDcChargingInfo->SystemStatus == S_AUTHORIZING) {*/
  211. if ( (pSysInfo->SystemPage == _PAGE_AUTHORIZE ||
  212. pSysInfo->SystemPage == _PAGE_SENSING) &&
  213. pDcChargingInfo->SystemStatus == S_IDLE) {
  214. log_info("// LCM => Authorizing");
  215. confirmSelGun(pSysInfo->CurGunSelected);
  216. setSelGunWaitToAuthor(pSysInfo->CurGunSelected);
  217. StartSystemTimeoutDet(Timeout_Authorizing);
  218. pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  219. AuthorizingStart();
  220. // LCM => Authorizing
  221. //pSysInfo->SystemPage = _PAGE_AUTHORIZE;
  222. // 進入確認卡號狀態
  223. } else {
  224. //strcpy((char *)pSysConfig->UserId, "");
  225. }
  226. return;
  227. }
  228. bool GetIsCardScan(void)
  229. {
  230. return isCardScan;
  231. }
  232. void SetIsCardScan(bool value)
  233. {
  234. isCardScan = value;
  235. }
  236. void AuthorizeToCharge()
  237. {
  238. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  239. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  240. SelectGunInfo *ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  241. struct ChargingInfoData *pDcChargingInfo = NULL;
  242. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  243. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  244. if(isAuthorizedComplete())
  245. {
  246. // StopSystemTimeoutDet();
  247. // StartSystemTimeoutDet(Timeout_WaitBalance);
  248. // if (ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance != FAIL_BALANCE_PRICES)
  249. {
  250. // StopSystemTimeoutDet();
  251. // 判斷後台回覆狀態
  252. if (canStartCharging()) {
  253. // LCM => Authorize complete
  254. //pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  255. //pSysInfo->SystemPage = _PAGE_PLUGIN;
  256. //log_info("Wait Gun(%d) plugin",pSysInfo->CurGunSelected);
  257. if (ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] == TRUE) {
  258. DetectPluginStart();
  259. pSysInfo->SystemPage = _PAGE_PLUGIN;
  260. log_info("Authorize Complete can plugin gun");
  261. }
  262. //log_info("Gun(%d) Balance: %f",pSysInfo->CurGunSelected,
  263. // ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance);
  264. } else {
  265. //log_info("LCM => Authorize fail");
  266. //pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  267. //strcpy((char *)pSysConfig->UserId, "");
  268. }
  269. }
  270. ClearAuthorizedFlag();
  271. }
  272. }
  273. void ScannerCardProcess(void)
  274. {
  275. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  276. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  277. struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  278. struct ChargingInfoData *pDcChargingInfo = NULL;
  279. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  280. if (!isDetectPlugin() && (pSysInfo->SystemPage == _PAGE_SENSING ||
  281. pSysInfo->SystemPage == _PAGE_AUTHORIZE ) &&
  282. pDcChargingInfo->SystemStatus == S_IDLE &&
  283. pSysWarning->Level != WARN_LV_ER /*&&
  284. pSysConfig->AuthorisationMode == AUTH_MODE_ENABLE*/) {
  285. //setSelGunWaitToAuthor(pSysInfo->CurGunSelected);
  286. isCardScan = true;
  287. // 處理刷卡及驗證卡號的動作
  288. UserScanFunction();
  289. }
  290. if (pDcChargingInfo->SystemStatus == S_AUTHORIZING && pSysInfo->SystemPage == _PAGE_SENSING ) {
  291. AuthorizeToCharge();
  292. } else if (pSysInfo->SystemPage == _PAGE_AUTHORIZE_FAIL) {
  293. StartSystemTimeoutDet(Timeout_VerifyFail);
  294. isCardScan = false;
  295. } else if ( pSysInfo->SystemPage == _PAGE_PLUGIN) {
  296. StartSystemTimeoutDet(Timeout_WaitPlug);
  297. } else {
  298. isCardScan = false;
  299. }
  300. }
  301. void WritePayResult(int result ,uint8_t gunIndex)
  302. {
  303. ShmDcCommonData->TransactionInfo[gunIndex].Amount = ShmDcCommonData->finalcost[gunIndex];
  304. memcpy(ShmDcCommonData->TransactionInfo[gunIndex].ApprovalNo , ShmDcCommonData->pCreditCard[gunIndex].ApprovalNo,9);
  305. memcpy(ShmDcCommonData->TransactionInfo[gunIndex].CardNo , ShmDcCommonData->pCreditCard[gunIndex].CardNo,20);
  306. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = result;
  307. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = ShmDcCommonData->donate_flag[gunIndex];
  308. ShmDcCommonData->TransactionInfo[gunIndex].TransactionId = ShmSelectGunInfo->PricesInfo[gunIndex].TransactionId;
  309. memcpy(&ShmDcCommonData->TransactionInfo[gunIndex].VemData,&ShmDcCommonData->pCreditCard[gunIndex].VemData,64);
  310. }
  311. void PreAuthCompleteToCardReader(int fd,uint8_t gunIndex)
  312. {
  313. int result = 0;
  314. result = CreditCardPreAuthComplete(fd,(int)ShmDcCommonData->finalcost[pSysInfo->CurGunSelected] ,"TCC Test",
  315. &ShmDcCommonData->pCreditCard[gunIndex].VemData[0],
  316. &ShmDcCommonData->pCreditCard[gunIndex]);
  317. //result = 1;
  318. if (result > 0 ) {
  319. log_info("Credit Card Spend Money:%.1f",ShmDcCommonData->finalcost[gunIndex]);
  320. pSysInfo->SystemPage = _PAGE_COMPLETE;
  321. WritePayResult(TRUE,gunIndex);
  322. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  323. } else {
  324. log_info("PAYING FAIL");
  325. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  326. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  327. WritePayResult(FALSE,gunIndex);
  328. }
  329. if (isDeductDb_ready == TRUE) {
  330. InsertDeductInfo(gunIndex,&ShmDcCommonData->TransactionInfo[gunIndex]);
  331. }
  332. }
  333. static int InitialRfidPort(void)
  334. {
  335. int fd = open(rfidPortName, O_RDWR);
  336. struct termios tios;
  337. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  338. if (fd != FAIL) {
  339. ioctl (fd, TCGETS, &tios);
  340. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  341. tios.c_lflag = 0;
  342. tios.c_iflag = 0;
  343. tios.c_oflag = 0;
  344. tios.c_cc[VMIN] = 0;
  345. tios.c_cc[VTIME] = (uint8_t) 1;
  346. tios.c_lflag = 0;
  347. tcflush(fd, TCIFLUSH);
  348. ioctl(fd, TCSETS, &tios);
  349. }
  350. if (fd < 0) {
  351. pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
  352. }
  353. return fd;
  354. }
  355. void CreateRfidFork(void)
  356. {
  357. pid_t rfidRecPid;
  358. rfidRecPid = fork();
  359. if (rfidRecPid == 0) {
  360. char localTime[128] = {0};
  361. struct timeb SeqEndTime;
  362. struct tm *tm;
  363. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  364. int isContinue = 1;
  365. RFID rfid = {0};
  366. RecordTransactionInfo deduct;
  367. fd = InitialRfidPort();
  368. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  369. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  370. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  371. struct ChargingInfoData *pDcChargingInfo = NULL;
  372. ShmDcCommonData->reupload_deduct_status = 0;
  373. int gunIndex;
  374. int uploadIndex = 0;
  375. int ReAuthComplete_Index = 0;
  376. if(DeductDB_Open() != PASS)
  377. {
  378. isDeductDb_ready = false;
  379. }
  380. else
  381. {
  382. isDeductDb_ready = true;
  383. }
  384. //log_info("RFID fork Child's PID is %d", getpid());
  385. int result;
  386. int is_idle = FALSE;
  387. while (isContinue) {
  388. result = 0;
  389. usleep(500000);
  390. ftime(&SeqEndTime);
  391. SeqEndTime.time = time(NULL);
  392. tm = localtime(&SeqEndTime.time);
  393. // 每日結帳
  394. if (tm->tm_hour == 23 && tm->tm_min == 00 && tm->tm_sec == 0) {
  395. result = CreditCardUnionSettlement(fd,"TCC Test",&ShmDcCommonData->pCreditCard[0]);
  396. if (result > 0) {
  397. log_info("CreditCardUnionSettlement OK");
  398. } else
  399. log_info("CreditCardUnionSettlement FAIL");
  400. }
  401. if (tm->tm_min%2 == 0 && pSysInfo->SystemPage == _PAGE_IDLE) {
  402. if(ShmDcCommonData->reupload_deduct_status == 0) {
  403. // 嘗試傳送未上傳成功到後台ID
  404. if (DB_GetReUploadDeduct(&uploadIndex,&ShmDcCommonData->UploadRedectInfo) == PASS) {
  405. ShmDcCommonData->reupload_deduct_status = 1;
  406. //log_info("deduct uploading");
  407. } else {
  408. //log_info("no deduct upload");
  409. }
  410. } else if (ShmDcCommonData->reupload_deduct_status == 2) {
  411. UpdateDeductInfoStatus(&uploadIndex,&ShmDcCommonData->UploadRedectInfo);
  412. ShmDcCommonData->reupload_deduct_status = 0;
  413. }
  414. }
  415. if (pSysInfo->SystemPage == _PAGE_AUTHORIZE && ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected] == FALSE) {
  416. result = CreditCardSearch(fd,"TCC Test",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected]);
  417. //result = 1;
  418. StartSystemTimeoutDet(Timeout_ScanCard);
  419. if (result > 0) {
  420. //strcpy((char *)pSysConfig->UserId,"AutoStartCharging");
  421. strncpy((char *)pSysConfig->UserId,ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].CardNo,20);
  422. log_info("Authorize card:%s",pSysConfig->UserId);
  423. StopSystemTimeoutDet();
  424. ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected] = TRUE;
  425. } else if (result < 0) {
  426. //pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  427. log_info("No Card Scanning...");
  428. }
  429. }
  430. is_idle = FALSE;
  431. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  432. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  433. if (pDcChargingInfo->SystemStatus == S_IDLE) {
  434. is_idle = TRUE;
  435. }
  436. if (ShmDcCommonData->finalcost_flag[gunIndex]) {
  437. StopGunInfoTimeoutDet(gunIndex); //Timeout_FinalCost
  438. pSysInfo->SystemPage = _PAGE_PAYING;
  439. if (ShmDcCommonData->finalcost[gunIndex] < 1) {
  440. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  441. pSysInfo->SystemPage = _PAGE_COMPLETE;
  442. } else {
  443. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  444. PreAuthCompleteToCardReader(fd,gunIndex);
  445. ShmDcCommonData->PayFinish[gunIndex] = TRUE;
  446. }
  447. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  448. }
  449. } // for
  450. if (is_idle == TRUE && tm->tm_min%15 == 0 &&
  451. pSysInfo->SystemPage == _PAGE_IDLE && ShmDcCommonData->reupload_deduct_status == 0) {
  452. sleep(1);
  453. if (DB_GetReDeductInfo(&ReAuthComplete_Index,&ShmDcCommonData->ReAuthComplete) == PASS ) {
  454. result = CreditCardPreAuthComplete(fd,(int)ShmDcCommonData->ReAuthComplete.Amount ,"TCC Test",
  455. &ShmDcCommonData->ReAuthComplete.VemData[0],
  456. &ShmDcCommonData->pCreditCard[0]);
  457. sleep(10);
  458. if (result > 0) {
  459. ShmDcCommonData->ReAuthComplete.DeductResult = YES;
  460. ShmDcCommonData->ReAuthComplete.IsUpload = NO;
  461. UpdateDeductInfoStatus(&ReAuthComplete_Index,&ShmDcCommonData->ReAuthComplete);
  462. log_info("PreAuthComplete OK");
  463. } else if ( result < 0) {
  464. log_info("Backgroud ID:%d Amount:%d VemData:%s PreAuthComplete fail",
  465. ShmDcCommonData->ReAuthComplete.TransactionId,
  466. (int)ShmDcCommonData->ReAuthComplete.Amount,
  467. ShmDcCommonData->ReAuthComplete.VemData);
  468. }
  469. } else {
  470. log_info("DB_GetReDeductInfo Fail");
  471. }
  472. }
  473. if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_CANCEL) {
  474. log_info("PreAuth Cancel ApprovalNo:%s",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].ApprovalNo[0]);
  475. log_info("PreAuth Cancel CardNo:%s",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].CardNo[0]);
  476. log_info("PreAuth Cancel VemData:%s",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].VemData[0]);
  477. ShmDcCommonData->PreAuth_Result = CreditCardPreAuthCancel(fd,PREAUTHMONEY,"TCC Test",
  478. &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].ApprovalNo[0],
  479. &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].CardNo[0],
  480. &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].VemData[0]);
  481. sleep(10);
  482. if (ShmDcCommonData->PreAuth_Result > 0 ) {
  483. strcpy((char *)pSysConfig->UserId,"");
  484. log_info("Credit Card Cancel Stop");
  485. ShmDcCommonData->TradeCancel = FALSE;
  486. ShmDcCommonData->finalcost_flag[gunIndex] = FALSE;
  487. memset(&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected], 0, sizeof(TransInfo));
  488. } else if (ShmDcCommonData->PreAuth_Result < 0) {
  489. log_info("Credit Card Cancel Fail");
  490. }
  491. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  492. } else if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_PREAUTH) {
  493. result = CreditCardPreAuth(fd, PREAUTHMONEY,"TCC Test", &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected]);
  494. log_info("PreAuth ApprovalNo:%s",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].ApprovalNo[0]);
  495. log_info("PreAuth CardNo:%s",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].CardNo[0]);
  496. log_info("PreAuth VemData:%s",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].VemData[0]);
  497. //result = 1;
  498. if (result > 0) {
  499. sleep(10);
  500. ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] = TRUE;
  501. //pSysInfo->SystemPage = _PAGE_PLUGIN;
  502. //strncpy((char *)pSysConfig->UserId,ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].CardNo,20);
  503. log_info("PreAuth OK");
  504. } else if (result < 0) {
  505. struct ChargingInfoData *pDcChargingInfo = NULL;
  506. //pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  507. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  508. //pDcChargingInfo->SystemStatus = S_IDLE;
  509. ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] = FALSE;
  510. memset(&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected], 0, sizeof(TransInfo));
  511. log_info("PreAuth Fail");
  512. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  513. }
  514. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  515. }
  516. /*
  517. // 刷卡判斷
  518. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  519. !pSysConfig->isRFID) {
  520. continue;
  521. }
  522. if (getRequestCardSN(fd, 0, &rfid) == false) {
  523. continue;
  524. }
  525. //log_info("Get Card..-%s- ", pSysConfig->UserId);
  526. if (strlen((char *)pSysConfig->UserId) != 0) {
  527. continue;
  528. }
  529. if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_LITTLE) {
  530. switch (rfid.snType) {
  531. case RFID_SN_TYPE_6BYTE:
  532. sprintf((char *) pSysConfig->UserId,
  533. "%02X%02X%02X%02X%02X%02X",
  534. rfid.currentCard[0], rfid.currentCard[1],
  535. rfid.currentCard[2], rfid.currentCard[3],
  536. rfid.currentCard[4], rfid.currentCard[5]);
  537. break;
  538. case RFID_SN_TYPE_7BYTE:
  539. sprintf((char *) pSysConfig->UserId,
  540. "%02X%02X%02X%02X%02X%02X%02X",
  541. rfid.currentCard[0], rfid.currentCard[1],
  542. rfid.currentCard[2], rfid.currentCard[3],
  543. rfid.currentCard[4], rfid.currentCard[5],
  544. rfid.currentCard[6]);
  545. break;
  546. case RFID_SN_TYPE_10BYTE:
  547. sprintf((char *) pSysConfig->UserId,
  548. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  549. rfid.currentCard[0], rfid.currentCard[1],
  550. rfid.currentCard[2], rfid.currentCard[3],
  551. rfid.currentCard[4], rfid.currentCard[5],
  552. rfid.currentCard[6], rfid.currentCard[7],
  553. rfid.currentCard[8], rfid.currentCard[9]);
  554. break;
  555. case RFID_SN_TYPE_4BYTE:
  556. sprintf((char *) pSysConfig->UserId,
  557. "%02X%02X%02X%02X",
  558. rfid.currentCard[0], rfid.currentCard[1],
  559. rfid.currentCard[2], rfid.currentCard[3]);
  560. break;
  561. }
  562. } else if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_BIG) {
  563. switch (rfid.snType) {
  564. case RFID_SN_TYPE_6BYTE:
  565. sprintf((char *) pSysConfig->UserId,
  566. "%02X%02X%02X%02X%02X%02X",
  567. rfid.currentCard[5], rfid.currentCard[4],
  568. rfid.currentCard[3], rfid.currentCard[2],
  569. rfid.currentCard[1], rfid.currentCard[0]);
  570. break;
  571. case RFID_SN_TYPE_7BYTE:
  572. sprintf((char *) pSysConfig->UserId,
  573. "%02X%02X%02X%02X%02X%02X%02X",
  574. rfid.currentCard[6], rfid.currentCard[5],
  575. rfid.currentCard[4], rfid.currentCard[3],
  576. rfid.currentCard[2], rfid.currentCard[1],
  577. rfid.currentCard[0]);
  578. break;
  579. case RFID_SN_TYPE_10BYTE:
  580. sprintf((char *) pSysConfig->UserId,
  581. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  582. rfid.currentCard[9], rfid.currentCard[8],
  583. rfid.currentCard[7], rfid.currentCard[6],
  584. rfid.currentCard[5], rfid.currentCard[4],
  585. rfid.currentCard[3], rfid.currentCard[2],
  586. rfid.currentCard[1], rfid.currentCard[0]);
  587. break;
  588. case RFID_SN_TYPE_4BYTE:
  589. sprintf((char *) pSysConfig->UserId,
  590. "%02X%02X%02X%02X",
  591. rfid.currentCard[3], rfid.currentCard[2],
  592. rfid.currentCard[1], rfid.currentCard[0]);
  593. break;
  594. }
  595. }
  596. log_info("card number = %s", pSysConfig->UserId);
  597. */
  598. }
  599. }
  600. }