RFID.c 26 KB

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