RFID.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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 <math.h>
  12. #include "main.h"
  13. #include "../timeout.h"
  14. static DcCommonInfo *ShmDcCommonData = NULL;
  15. static SelectGunInfo *ShmSelectGunInfo = NULL;
  16. static struct SysInfoData *pSysInfo = NULL;
  17. #define PREAUTHMONEY 888
  18. bool isDeductDb_ready;
  19. static RecordTransactionInfo LocalTransactionInfo;
  20. //------------------------------------------------------------------------------
  21. static char *rfidPortName = "/dev/ttyS2";
  22. static bool isCardScan = false;
  23. //------------------------------------------------------------------------------
  24. int checkRemoteStart(int gunIndex)
  25. {
  26. if (ShmDcCommonData->is_RemoteStart[gunIndex] ||
  27. ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_PAY_INVOICE ||
  28. ShmDcCommonData->TransactionInfo[gunIndex].LineStatus == _LINE_PAY_DONATE) {
  29. return TRUE;
  30. }
  31. return FALSE;
  32. }
  33. static bool canStartCharging(void)
  34. {
  35. uint8_t index = 0;
  36. char buf2[16] = "";
  37. memset(buf2, 0, ARRAY_SIZE(buf2));
  38. struct OCPP16Data *ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
  39. for (index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++) {
  40. sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
  41. }
  42. sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  43. // 因為無法得知實際的長度,所以只能用搜尋的方式
  44. if (strcmp(buf2, "Accepted") == EQUAL) {
  45. return true;
  46. }
  47. return false;
  48. }
  49. bool isAutorCompleteHandle(/*uint8_t *authorizeIndex*/)
  50. {
  51. // uint8_t i = 0;
  52. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  53. //struct ChargingInfoData *pDcChargingInfo = NULL;
  54. //struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  55. // 透過後臺停止充電的判斷
  56. if (isAuthorizedComplete()) {
  57. // 判斷後台回覆狀態
  58. if (canStartCharging() == false) {
  59. strcpy((char *)pSysConfig->UserId, "");
  60. ClearAuthorizedFlag();
  61. }
  62. return true;
  63. }
  64. return false;
  65. /*
  66. if (*(authorizeIndex) != NO_DEFINE) {
  67. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(*(authorizeIndex));
  68. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST &&
  69. strcmp((char *)pDcChargingInfo->StartUserId, "") != EQUAL) {
  70. // 先找 AC
  71. if (*(authorizeIndex) == DEFAULT_AC_INDEX) {
  72. AcChargingTerminalProcess();
  73. } else {
  74. ChargingTerminalProcess(*(authorizeIndex));
  75. }
  76. }
  77. strcpy((char *)pSysConfig->UserId, "");
  78. *(authorizeIndex) = NO_DEFINE;
  79. }
  80. ClearAuthorizedFlag();
  81. } else if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_LOCAL_LIST) {
  82. // 白名單驗證
  83. for (i = 0; i < 10; i++) {
  84. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], "") == EQUAL) {
  85. continue;
  86. }
  87. if (strcmp((char *)pSysConfig->LocalWhiteCard[i], (char *)pSysConfig->UserId) == EQUAL) {
  88. ChargingTerminalProcess(*(authorizeIndex));
  89. strcpy((char *)pSysConfig->UserId, "");
  90. ClearAuthorizedFlag();
  91. break;
  92. }
  93. }
  94. }*/
  95. }
  96. bool RfidStopCharging(void)
  97. {
  98. //當前沒有選槍
  99. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  100. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  101. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL ) {
  102. strcpy((char *)pSysConfig->UserId, "");
  103. return false;
  104. }
  105. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  106. if (pDcChargingInfo->isRemoteStart) {
  107. } else {
  108. if (strcmp((char *)pSysConfig->UserId, (char *)pDcChargingInfo->StartUserId) == EQUAL) {
  109. ChargingTerminalProcess(pSysInfo->CurGunSelected);
  110. strcpy((char *)pSysConfig->UserId, "");
  111. log_info("index = %d, card number = %s, UserId = %s ",
  112. pSysInfo->CurGunSelected,
  113. pDcChargingInfo->StartUserId,
  114. pSysConfig->UserId);
  115. return true;
  116. } else {
  117. strcpy((char *)pSysConfig->UserId, "");
  118. return false;
  119. }
  120. }
  121. return false;
  122. }
  123. static void UserScanFunction(int gunIndex)
  124. {
  125. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  126. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  127. //struct ChargingInfoData *pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  128. struct ChargingInfoData *pDcChargingInfo = NULL;
  129. ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  130. // 當前非驗證的狀態
  131. if (IsAuthorizingMode()) {
  132. isAutorCompleteHandle(/*&_authorizeIndex*/);
  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. if (strlen((char *)pSysConfig->UserId) <= 0) {
  143. return;
  144. }
  145. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  146. if ( (pSysInfo->SystemPage == _PAGE_AUTHORIZE ||
  147. pSysInfo->SystemPage == _PAGE_SENSING)) {
  148. log_info("// LCM => Authorizing");
  149. confirmSelGun(gunIndex);
  150. setSelGunWaitToAuthor(gunIndex);
  151. StartSystemTimeoutDet(Timeout_Authorizing);
  152. AuthorizingStart();
  153. pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  154. // LCM => Authorizing
  155. //pSysInfo->SystemPage = _PAGE_AUTHORIZE;
  156. // 進入確認卡號狀態
  157. } else {
  158. //strcpy((char *)pSysConfig->UserId, "");
  159. }
  160. return;
  161. }
  162. bool GetIsCardScan(void)
  163. {
  164. return isCardScan;
  165. }
  166. void SetIsCardScan(bool value)
  167. {
  168. isCardScan = value;
  169. }
  170. void AuthorizeToCharge(int gunIndex)
  171. {
  172. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  173. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  174. struct ChargingInfoData* pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  175. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  176. if (pDcChargingInfo->SystemStatus == S_RESERVATION) {
  177. if (strcmp((char*)pSysConfig->UserId, (char*)ShmDcCommonData->pGunInfo[gunIndex].ReservationID) != EQUAL) {
  178. log_info("LCM => Authorize fail");
  179. ShmDcCommonData->TradeCancel = TRUE;
  180. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  181. ShmDcCommonData->PreAuth_Result = 0;
  182. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  183. strcpy((char*)pSysConfig->UserId, "");
  184. ClearAuthorizedFlag();
  185. return;
  186. }
  187. }
  188. if(isAuthorizedComplete())
  189. {
  190. if (canStartCharging()) {
  191. DetectPluginStart(gunIndex);
  192. if (ShmDcCommonData->AuthPass_flag[gunIndex] == TRUE &&
  193. ShmDcCommonData->TradeCancel == FALSE) {
  194. pSysInfo->SystemPage = _PAGE_PLUGIN;
  195. }
  196. } else {
  197. struct ChargingInfoData* pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  198. log_info("LCM => Authorize fail");
  199. ShmDcCommonData->TradeCancel = TRUE;
  200. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  201. ShmDcCommonData->PreAuth_Result = 0;
  202. if (!pDcChargingInfo->RemoteStartFlag || !pDcChargingInfo->isRemoteStart)
  203. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  204. strcpy((char *)pSysConfig->UserId, "");
  205. }
  206. ClearAuthorizedFlag();
  207. }
  208. }
  209. void ScannerCardProcess(int gunIndex)
  210. {
  211. //struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  212. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  213. struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  214. struct ChargingInfoData *pDcChargingInfo = NULL;
  215. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  216. if (!isDetectPlugin(gunIndex) && (pSysInfo->SystemPage == _PAGE_SENSING ||
  217. pSysInfo->SystemPage == _PAGE_AUTHORIZE ) &&
  218. (pDcChargingInfo->SystemStatus == S_IDLE || pDcChargingInfo->SystemStatus == S_RESERVATION )&&
  219. pSysWarning->Level != WARN_LV_ER ) {
  220. //setSelGunWaitToAuthor(gunIndex);
  221. isCardScan = true;
  222. // 處理刷卡及驗證卡號的動作
  223. UserScanFunction(gunIndex);
  224. }
  225. if (pDcChargingInfo->SystemStatus == S_AUTHORIZING && pSysInfo->SystemPage == _PAGE_SENSING &&
  226. !ShmDcCommonData->is_RemoteStart[gunIndex] ) {
  227. AuthorizeToCharge(gunIndex);
  228. } else if (pSysInfo->SystemPage == _PAGE_AUTHORIZE_FAIL) {
  229. StartSystemTimeoutDet(Timeout_VerifyFail);
  230. isCardScan = false;
  231. } else {
  232. isCardScan = false;
  233. }
  234. }
  235. void WritePayResult(int result ,uint8_t gunIndex)
  236. {
  237. memcpy(&ShmDcCommonData->TransactionInfo[gunIndex], &LocalTransactionInfo, sizeof(RecordTransactionInfo));
  238. if (result == TRUE)
  239. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_PASS;
  240. else
  241. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_FAIL;
  242. log_info("Gun[%d] TransactionId:%d DeductResult:%d IsDonateInvoice:%d Amount:%f VemData:%s",
  243. gunIndex, ShmDcCommonData->TransactionInfo[gunIndex].TransactionId,
  244. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult,
  245. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice,
  246. ShmDcCommonData->TransactionInfo[gunIndex].Amount,
  247. ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData);
  248. }
  249. void storePayResult(uint8_t gunIndex)
  250. {
  251. memset(&LocalTransactionInfo, 0, sizeof(RecordTransactionInfo));
  252. memcpy(&LocalTransactionInfo, &ShmDcCommonData->TransactionInfo[gunIndex] ,sizeof(RecordTransactionInfo));
  253. //LocalTransactionInfo.Amount = ShmDcCommonData->finalcost[gunIndex];
  254. }
  255. void PreAuthCompleteToCardReader(int fd,uint8_t gunIndex)
  256. {
  257. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  258. int result = 0;
  259. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount > 0 && ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  260. log_info("final cost less 1 : %f", ShmDcCommonData->TransactionInfo[gunIndex].Amount);
  261. ShmDcCommonData->TransactionInfo[gunIndex].Amount = 1;
  262. }
  263. storePayResult(gunIndex);
  264. result = CreditCardPreAuthComplete(fd,(int)ShmDcCommonData->TransactionInfo[gunIndex].Amount, &pSysConfig->ModelName[0],
  265. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData[0],
  266. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard);
  267. //sleep(10);
  268. if (result > 0 ) {
  269. log_info("Credit Card Spend Money:%.1f", LocalTransactionInfo.Amount);
  270. pSysInfo->SystemPage = _PAGE_COMPLETE;
  271. LocalTransactionInfo.DeductResult = _DEDUCT_COMPLETE_PASS;
  272. WritePayResult(TRUE,gunIndex);
  273. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  274. } else {
  275. log_info("PAYING FAIL");
  276. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  277. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  278. LocalTransactionInfo.DeductResult = _DEDUCT_COMPLETE_FAIL;
  279. WritePayResult(FALSE,gunIndex);
  280. }
  281. ShmDcCommonData->TransactionInfo[gunIndex].IsUpload = FALSE;
  282. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  283. }
  284. int CreditCardCancelPreAuth(int fd, uint8_t gunIndex)
  285. {
  286. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  287. ShmDcCommonData->PreAuth_Result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  288. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.ApprovalNo[0],
  289. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.CardNo[0],
  290. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData[0]);
  291. //sleep(10);
  292. if (ShmDcCommonData->PreAuth_Result >= 0) {
  293. strcpy((char*)pSysConfig->UserId, "");
  294. //ShmDcCommonData->PayFinish[gunIndex] = TRUE;
  295. log_info("Card Reader PreAuth Cancel Success");
  296. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_CANCEL;
  297. } else if (ShmDcCommonData->PreAuth_Result < 0) {
  298. log_info("Card Reader PreAuth Cancel Failure");
  299. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_PREAUTH;
  300. }
  301. ShmDcCommonData->TransactionInfo[gunIndex].IsUpload = FALSE;
  302. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  303. memset(&ShmDcCommonData->TransactionInfo[gunIndex], 0x00, sizeof(RecordTransactionInfo));
  304. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  305. return ShmDcCommonData->PreAuth_Result;
  306. }
  307. void ReDeductProcess(int fd)
  308. {
  309. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  310. int j;
  311. int result;
  312. int rededuct_gunIndex[128];
  313. RecordTransactionInfo deductInfo[128];
  314. int rededuct_num = 0;
  315. rededuct_num = DB_GetMultiReDeductInfo(&rededuct_gunIndex[0], &deductInfo[0]);
  316. if (rededuct_num == 0) {
  317. //log_info("No Rededuct Information");
  318. } else {
  319. ShmDcCommonData->CreditCardUpload = TRUE;
  320. pSysInfo->SystemPage = _PAGE_PAYING;
  321. log_info("Rededuct Total Number:%d", rededuct_num);
  322. for (j = rededuct_num-1; j >= 0 ; j--) {
  323. sleep(10);
  324. /*
  325. if (pSysInfo->SystemPage != _PAGE_IDLE && !ShmDcCommonData->Exe_ReDeduct) {
  326. ShmDcCommonData->RoutineReduct = FALSE;
  327. break;
  328. }
  329. */
  330. log_info("Start Rededuct item [%d]",j);
  331. if (deductInfo[j].isIntoCharge == FALSE ||
  332. (deductInfo[j].Energy == 0 && deductInfo[j].Amount == 0) ||
  333. (deductInfo[j].LineStatus == _LINE_PAY_DONATE || deductInfo[j].LineStatus == _LINE_PAY_INVOICE)) {
  334. // 未進入充電或度數等於零
  335. result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  336. &deductInfo[j].pCreditCard.ApprovalNo[0],
  337. &deductInfo[j].pCreditCard.CardNo[0],
  338. &deductInfo[j].pCreditCard.VemData[0]);
  339. sleep(10);
  340. if (result > 0) {
  341. deductInfo[j].DeductResult = _DEDUCT_CANCEL;
  342. log_info("Backgroud PreAuthCancel OK");
  343. }
  344. } else {
  345. // 度數大於零
  346. if (deductInfo[j].Amount == 0 && deductInfo[j].Energy > 0) {
  347. log_info("Error Close Charging recount amount:%.1f",deductInfo[j].Amount);
  348. continue;
  349. }
  350. result = CreditCardPreAuthComplete(fd, (int)deductInfo[j].Amount, &pSysConfig->ModelName[0],
  351. &deductInfo[j].pCreditCard.VemData[0],
  352. &ShmDcCommonData->TransactionInfo[0].pCreditCard);
  353. if (result > 0) {
  354. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_PASS;
  355. log_info("Backgroud PreAuthComplete OK");
  356. } else if (result < 0) {
  357. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_FAIL;
  358. log_info("Backgroud ID:%d Amount:%d VemData:%s PreAuthComplete fail",
  359. deductInfo[j].TransactionId,
  360. (int)deductInfo[j].Amount,
  361. deductInfo[j].pCreditCard.VemData);
  362. }
  363. }
  364. deductInfo[j].IsUpload = FALSE;
  365. deductInfo[j].RedeductTime++;
  366. UpdateDeductInfoStatus(rededuct_gunIndex[j], &deductInfo[j]);
  367. }
  368. }
  369. ShmDcCommonData->RoutineReduct = TRUE;
  370. }
  371. static int InitialRfidPort(void)
  372. {
  373. int fd = open(rfidPortName, O_RDWR);
  374. struct termios tios;
  375. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  376. if (fd != FAIL) {
  377. ioctl (fd, TCGETS, &tios);
  378. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  379. tios.c_lflag = 0;
  380. tios.c_iflag = 0;
  381. tios.c_oflag = 0;
  382. tios.c_cc[VMIN] = 0;
  383. tios.c_cc[VTIME] = (uint8_t) 1;
  384. tios.c_lflag = 0;
  385. tcflush(fd, TCIFLUSH);
  386. ioctl(fd, TCSETS, &tios);
  387. }
  388. if (fd < 0) {
  389. pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
  390. }
  391. return fd;
  392. }
  393. void RemoteStartCancelPreAuth(int sel_gun)
  394. {
  395. log_info("Remote start need to cancel PreAuth");
  396. memcpy(&ShmDcCommonData->RedeductBill, &ShmDcCommonData->TransactionInfo[sel_gun], sizeof(RecordTransactionInfo));
  397. memset(&ShmDcCommonData->TransactionInfo[sel_gun], 0x00, sizeof(RecordTransactionInfo));
  398. ShmDcCommonData->TransactionInfo[sel_gun].LineStatus = ShmDcCommonData->RedeductBill.LineStatus;
  399. }
  400. void CreateRfidFork(void)
  401. {
  402. pid_t rfidRecPid;
  403. int donate = 0;
  404. rfidRecPid = fork();
  405. int sel_gun = 0;
  406. int linestatus = 0;
  407. if (rfidRecPid == 0) {
  408. //char localTime[128] = {0};
  409. struct timeb SeqEndTime;
  410. struct tm *tm;
  411. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  412. int fd = -1;
  413. int isContinue = 1;
  414. //RFID rfid = {0};
  415. //RecordTransactionInfo deduct;
  416. fd = InitialRfidPort();
  417. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  418. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  419. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  420. struct ChargingInfoData *pDcChargingInfo = NULL;
  421. if (DeductDB_Open() != PASS) {
  422. isDeductDb_ready = false;
  423. } else {
  424. isDeductDb_ready = true;
  425. //InsertDeductInfo(0, &ShmDcCommonData->TransactionInfo);
  426. }
  427. int gunIndex;
  428. //int uploadIndex = 0;
  429. //int ReAuthComplete_Index = 0;
  430. //log_info("RFID fork Child's PID is %d", getpid());
  431. int result;
  432. int is_idle = TRUE;
  433. while (isContinue) {
  434. result = 0;
  435. usleep(500000);
  436. ftime(&SeqEndTime);
  437. SeqEndTime.time = time(NULL);
  438. tm = localtime(&SeqEndTime.time);
  439. if (ShmDcCommonData->DebugFlag == TRUE ||
  440. ShmDcCommonData->is_RemoteStart[pSysInfo->CurGunSelected] == TRUE ||
  441. ShmDcCommonData->is_AutoStart[pSysInfo->CurGunSelected] == TRUE) {
  442. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  443. ShmDcCommonData->PreAuth_Result = 0;
  444. }
  445. is_idle = TRUE;
  446. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  447. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  448. // 檢查電樁狀態是否為idle狀態
  449. if (pDcChargingInfo->SystemStatus != S_IDLE ) {
  450. is_idle = FALSE;
  451. }
  452. // 檢查使用者是否進行線上支付操作並且線下支付已完成
  453. // 需進行取消預授權
  454. if (checkRemoteStart(gunIndex) && ShmDcCommonData->AuthPass_flag[gunIndex] &&
  455. !ShmDcCommonData->PayPass_flag[gunIndex]) {
  456. RemoteStartCancelPreAuth(gunIndex);
  457. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  458. }
  459. if (ShmDcCommonData->StopCharge[gunIndex] == TRUE && pDcChargingInfo->Replug_flag == TRUE) {
  460. // Remote Start of AutoStart ByPass Credit Card Reader
  461. if (ShmDcCommonData->DebugFlag == TRUE ||
  462. ShmDcCommonData->is_RemoteStart[gunIndex] == TRUE ||
  463. ShmDcCommonData->is_AutoStart[gunIndex] == TRUE) {
  464. if (pSysInfo->CurGunSelected == gunIndex)
  465. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  466. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  467. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  468. continue;
  469. }
  470. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  471. log_info("Gun %d Not Into Charging cancel Trade",gunIndex);
  472. StopGunInfoTimeoutDet(gunIndex);
  473. ShmDcCommonData->TradeCancel = TRUE;
  474. pSysInfo->SystemPage = _PAGE_SENSING;
  475. StartSystemTimeoutDet(Timeout_TradeCancel);
  476. CreditCardCancelPreAuth(fd, gunIndex);
  477. pSysInfo->CurGunSelected = gunIndex;
  478. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  479. StopSystemTimeoutDet();
  480. } else {
  481. if (ShmDcCommonData->StopCharge[gunIndex] && ShmDcCommonData->finalcost_flag[gunIndex] &&
  482. ShmDcCommonData->PreAuth_Config != _CREDITCARD_CANCEL) {
  483. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  484. pSysInfo->CurGunSelected = gunIndex;
  485. pSysInfo->SystemPage = _PAGE_PAYING;
  486. StopGunInfoTimeoutDet(gunIndex); //Timeout_FinalCost
  487. // Remote Start of AutoStart ByPass Credit Card Reader
  488. if (ShmDcCommonData->DebugFlag == TRUE ||
  489. checkRemoteStart(gunIndex) ||
  490. ShmDcCommonData->is_AutoStart[gunIndex]) {
  491. pSysInfo->SystemPage = _PAGE_COMPLETE;
  492. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  493. continue;
  494. }
  495. // 實際扣款
  496. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  497. log_info("Final Cost less 1 , Cancel Trade!!");
  498. if (CreditCardCancelPreAuth(fd, gunIndex) > 0) {
  499. pSysInfo->SystemPage = _PAGE_COMPLETE;
  500. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  501. } else {
  502. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  503. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  504. }
  505. } else {
  506. PreAuthCompleteToCardReader(fd, gunIndex);
  507. }
  508. }
  509. }
  510. } // for
  511. // 取消預授權
  512. if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_CANCEL) {
  513. CreditCardCancelPreAuth(fd,pSysInfo->CurGunSelected);
  514. // 預授權
  515. } else if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_PREAUTH /* && ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected]*/) {
  516. sel_gun = pSysInfo->CurGunSelected;
  517. donate = (int)ShmDcCommonData->TransactionInfo[sel_gun].IsDonateInvoice;
  518. linestatus = (int)ShmDcCommonData->TransactionInfo[sel_gun].LineStatus;
  519. memset(&ShmDcCommonData->TransactionInfo[sel_gun], 0x00, sizeof(RecordTransactionInfo));
  520. ShmDcCommonData->TransactionInfo[sel_gun].IsDonateInvoice = (unsigned char)donate;
  521. ShmDcCommonData->TransactionInfo[sel_gun].LineStatus = linestatus;
  522. result = CreditCardPreAuth(fd, PREAUTHMONEY,&pSysConfig->ModelName[0], &ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard);
  523. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(sel_gun);
  524. {
  525. if (!checkRemoteStart(sel_gun)) {
  526. if (!ShmDcCommonData->TradeCancel)
  527. StopSystemTimeoutDet();
  528. if (ShmDcCommonData->TradeCancel == FALSE && result > 0) {
  529. pSysInfo->SystemPage = _PAGE_SENSING;
  530. }
  531. if (result > 0)
  532. sleep(3);
  533. }
  534. ShmDcCommonData->PreAuth_Result = result;
  535. //result = 1;
  536. if (result > 0 && strcmp((char*)ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard.CardNo, "") != 0) {
  537. ShmDcCommonData->AuthPass_flag[sel_gun] = TRUE;
  538. strncpy((char*)pSysConfig->UserId, (char*)ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard.CardNo, 20);
  539. log_info("Authorize card:%s", pSysConfig->UserId);
  540. ShmDcCommonData->TransactionInfo[sel_gun].DeductResult = _DEDUCT_PREAUTH;
  541. ShmDcCommonData->TransactionInfo[sel_gun].IsUpload = FALSE;
  542. ShmDcCommonData->TransactionInfo[sel_gun].ConnectorID = ShmDcCommonData->ConnectorID[sel_gun];
  543. log_info("Line Status:%d", ShmDcCommonData->TransactionInfo[sel_gun].LineStatus);
  544. InsertDeductInfo(ShmDcCommonData->ConnectorID[sel_gun], &ShmDcCommonData->TransactionInfo[sel_gun]);
  545. log_info("PreAuth OK");
  546. } else if (result < 0) {
  547. if (ShmDcCommonData->TradeCancel == FALSE && !checkRemoteStart(sel_gun))
  548. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  549. ShmDcCommonData->AuthPass_flag[sel_gun] = FALSE;
  550. memset(&ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard, 0, sizeof(TransInfo));
  551. log_info("PreAuth Fail");
  552. }
  553. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  554. }
  555. }
  556. // 每30分鐘檢查補扣款
  557. if (tm->tm_min %30 == 0 && tm->tm_sec == 0) {
  558. ShmDcCommonData->RoutineReduct = FALSE;
  559. }
  560. if ((is_idle == TRUE &&
  561. pSysInfo->SystemPage == _PAGE_IDLE &&
  562. ShmDcCommonData->RoutineReduct == FALSE) || ShmDcCommonData->Exe_ReDeduct) {
  563. ReDeductProcess(fd);
  564. pSysInfo->SystemPage = _PAGE_IDLE;
  565. ShmDcCommonData->CreditCardUpload = FALSE;
  566. ShmDcCommonData->Exe_ReDeduct = 0;
  567. }
  568. // 每日晚上11點30分結帳
  569. if (is_idle == TRUE && pSysInfo->SystemPage == _PAGE_IDLE &&
  570. ((tm->tm_hour == 15 && tm->tm_min > 30 && ShmDcCommonData->RoutineSettlement == FALSE) ||
  571. ShmDcCommonData->UnionSettlement)) {
  572. StopSystemTimeoutDet();
  573. if (ShmDcCommonData->UnionSettlement)
  574. ShmDcCommonData->UnionSettlement = 0;
  575. else
  576. ShmDcCommonData->RoutineSettlement = TRUE;
  577. ShmDcCommonData->CreditCardUpload = TRUE;
  578. pSysInfo->SystemPage = _PAGE_PAYING;
  579. result = CreditCardUnionSettlement(fd, &pSysConfig->ModelName[0], &LocalTransactionInfo.pCreditCard);
  580. if (result > 0) {
  581. log_info("CreditCardUnionSettlement OK");
  582. }
  583. else
  584. log_info("CreditCardUnionSettlement FAIL");
  585. sleep(90);
  586. pSysInfo->SystemPage = _PAGE_IDLE;
  587. ShmDcCommonData->CreditCardUpload = FALSE;
  588. }
  589. if (tm->tm_hour == 16 && tm->tm_min == 0) {
  590. ShmDcCommonData->RoutineSettlement = FALSE;
  591. }
  592. /*
  593. // 刷卡判斷
  594. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  595. !pSysConfig->isRFID) {
  596. continue;
  597. }
  598. if (getRequestCardSN(fd, 0, &rfid) == false) {
  599. continue;
  600. }
  601. //log_info("Get Card..-%s- ", pSysConfig->UserId);
  602. if (strlen((char *)pSysConfig->UserId) != 0) {
  603. continue;
  604. }
  605. if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_LITTLE) {
  606. switch (rfid.snType) {
  607. case RFID_SN_TYPE_6BYTE:
  608. sprintf((char *) pSysConfig->UserId,
  609. "%02X%02X%02X%02X%02X%02X",
  610. rfid.currentCard[0], rfid.currentCard[1],
  611. rfid.currentCard[2], rfid.currentCard[3],
  612. rfid.currentCard[4], rfid.currentCard[5]);
  613. break;
  614. case RFID_SN_TYPE_7BYTE:
  615. sprintf((char *) pSysConfig->UserId,
  616. "%02X%02X%02X%02X%02X%02X%02X",
  617. rfid.currentCard[0], rfid.currentCard[1],
  618. rfid.currentCard[2], rfid.currentCard[3],
  619. rfid.currentCard[4], rfid.currentCard[5],
  620. rfid.currentCard[6]);
  621. break;
  622. case RFID_SN_TYPE_10BYTE:
  623. sprintf((char *) pSysConfig->UserId,
  624. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  625. rfid.currentCard[0], rfid.currentCard[1],
  626. rfid.currentCard[2], rfid.currentCard[3],
  627. rfid.currentCard[4], rfid.currentCard[5],
  628. rfid.currentCard[6], rfid.currentCard[7],
  629. rfid.currentCard[8], rfid.currentCard[9]);
  630. break;
  631. case RFID_SN_TYPE_4BYTE:
  632. sprintf((char *) pSysConfig->UserId,
  633. "%02X%02X%02X%02X",
  634. rfid.currentCard[0], rfid.currentCard[1],
  635. rfid.currentCard[2], rfid.currentCard[3]);
  636. break;
  637. }
  638. } else if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_BIG) {
  639. switch (rfid.snType) {
  640. case RFID_SN_TYPE_6BYTE:
  641. sprintf((char *) pSysConfig->UserId,
  642. "%02X%02X%02X%02X%02X%02X",
  643. rfid.currentCard[5], rfid.currentCard[4],
  644. rfid.currentCard[3], rfid.currentCard[2],
  645. rfid.currentCard[1], rfid.currentCard[0]);
  646. break;
  647. case RFID_SN_TYPE_7BYTE:
  648. sprintf((char *) pSysConfig->UserId,
  649. "%02X%02X%02X%02X%02X%02X%02X",
  650. rfid.currentCard[6], rfid.currentCard[5],
  651. rfid.currentCard[4], rfid.currentCard[3],
  652. rfid.currentCard[2], rfid.currentCard[1],
  653. rfid.currentCard[0]);
  654. break;
  655. case RFID_SN_TYPE_10BYTE:
  656. sprintf((char *) pSysConfig->UserId,
  657. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  658. rfid.currentCard[9], rfid.currentCard[8],
  659. rfid.currentCard[7], rfid.currentCard[6],
  660. rfid.currentCard[5], rfid.currentCard[4],
  661. rfid.currentCard[3], rfid.currentCard[2],
  662. rfid.currentCard[1], rfid.currentCard[0]);
  663. break;
  664. case RFID_SN_TYPE_4BYTE:
  665. sprintf((char *) pSysConfig->UserId,
  666. "%02X%02X%02X%02X",
  667. rfid.currentCard[3], rfid.currentCard[2],
  668. rfid.currentCard[1], rfid.currentCard[0]);
  669. break;
  670. }
  671. }
  672. log_info("card number = %s", pSysConfig->UserId);
  673. */
  674. }
  675. }
  676. }