RFID.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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 getPayResult(uint8_t gunIndex)
  256. {
  257. memcpy(&ShmDcCommonData->TransactionInfo[gunIndex], &LocalTransactionInfo, sizeof(RecordTransactionInfo));
  258. memset(&LocalTransactionInfo, 0, sizeof(RecordTransactionInfo));
  259. }
  260. void PreAuthCompleteToCardReader(int fd,uint8_t gunIndex)
  261. {
  262. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  263. int result = 0;
  264. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount > 0 && ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  265. log_info("final cost less 1 : %f", ShmDcCommonData->TransactionInfo[gunIndex].Amount);
  266. ShmDcCommonData->TransactionInfo[gunIndex].Amount = 1;
  267. }
  268. storePayResult(gunIndex);
  269. result = CreditCardPreAuthComplete(fd,(int)ShmDcCommonData->TransactionInfo[gunIndex].Amount, &pSysConfig->ModelName[0],
  270. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData[0],
  271. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard);
  272. //sleep(10);
  273. if (result > 0 ) {
  274. log_info("Credit Card Spend Money:%.1f", LocalTransactionInfo.Amount);
  275. pSysInfo->SystemPage = _PAGE_COMPLETE;
  276. LocalTransactionInfo.DeductResult = _DEDUCT_COMPLETE_PASS;
  277. WritePayResult(TRUE,gunIndex);
  278. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  279. } else {
  280. log_info("PAYING FAIL");
  281. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  282. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  283. LocalTransactionInfo.DeductResult = _DEDUCT_COMPLETE_FAIL;
  284. WritePayResult(FALSE,gunIndex);
  285. }
  286. ShmDcCommonData->TransactionInfo[gunIndex].IsUpload = FALSE;
  287. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  288. }
  289. int CreditCardCancelPreAuth(int fd, uint8_t gunIndex)
  290. {
  291. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  292. ShmDcCommonData->PreAuth_Result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  293. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.ApprovalNo[0],
  294. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.CardNo[0],
  295. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData[0]);
  296. //sleep(10);
  297. if (ShmDcCommonData->PreAuth_Result >= 0) {
  298. strcpy((char*)pSysConfig->UserId, "");
  299. //ShmDcCommonData->PayFinish[gunIndex] = TRUE;
  300. log_info("Card Reader PreAuth Cancel Success");
  301. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_CANCEL;
  302. } else if (ShmDcCommonData->PreAuth_Result < 0) {
  303. log_info("Card Reader PreAuth Cancel Failure");
  304. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_PREAUTH;
  305. }
  306. ShmDcCommonData->TransactionInfo[gunIndex].IsUpload = FALSE;
  307. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  308. memset(&ShmDcCommonData->TransactionInfo[gunIndex], 0x00, sizeof(RecordTransactionInfo));
  309. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  310. return ShmDcCommonData->PreAuth_Result;
  311. }
  312. void ReDeductProcess(int fd)
  313. {
  314. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  315. int j;
  316. int result;
  317. int rededuct_gunIndex[128];
  318. RecordTransactionInfo deductInfo[128];
  319. int rededuct_num = 0;
  320. rededuct_num = DB_GetMultiReDeductInfo(&rededuct_gunIndex[0], &deductInfo[0]);
  321. if (rededuct_num == 0) {
  322. //log_info("No Rededuct Information");
  323. } else {
  324. ShmDcCommonData->CreditCardUpload = TRUE;
  325. pSysInfo->SystemPage = _PAGE_PAYING;
  326. log_info("Rededuct Total Number:%d", rededuct_num);
  327. for (j = rededuct_num-1; j >= 0 ; j--) {
  328. sleep(10);
  329. log_info("Start Rededuct item [%d]",j);
  330. if (deductInfo[j].isIntoCharge == FALSE ||
  331. (deductInfo[j].Energy == 0 && deductInfo[j].Amount == 0) ||
  332. (deductInfo[j].LineStatus == _LINE_PAY_DONATE || deductInfo[j].LineStatus == _LINE_PAY_INVOICE)) {
  333. // 未進入充電或度數等於零
  334. result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  335. &deductInfo[j].pCreditCard.ApprovalNo[0],
  336. &deductInfo[j].pCreditCard.CardNo[0],
  337. &deductInfo[j].pCreditCard.VemData[0]);
  338. sleep(10);
  339. if (result > 0) {
  340. deductInfo[j].DeductResult = _DEDUCT_CANCEL;
  341. log_info("Backgroud PreAuthCancel OK");
  342. }
  343. } else {
  344. // 度數大於零
  345. if (deductInfo[j].Amount == 0 && deductInfo[j].Energy > 0) {
  346. log_info("Error Close Charging recount amount:%.1f",deductInfo[j].Amount);
  347. continue;
  348. }
  349. result = CreditCardPreAuthComplete(fd, (int)deductInfo[j].Amount, &pSysConfig->ModelName[0],
  350. &deductInfo[j].pCreditCard.VemData[0],
  351. &ShmDcCommonData->TransactionInfo[0].pCreditCard);
  352. if (result > 0) {
  353. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_PASS;
  354. log_info("Backgroud PreAuthComplete OK");
  355. } else if (result < 0) {
  356. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_FAIL;
  357. log_info("Backgroud ID:%d Amount:%d VemData:%s PreAuthComplete fail",
  358. deductInfo[j].TransactionId,
  359. (int)deductInfo[j].Amount,
  360. deductInfo[j].pCreditCard.VemData);
  361. }
  362. }
  363. deductInfo[j].IsUpload = FALSE;
  364. deductInfo[j].RedeductTime++;
  365. UpdateDeductInfoStatus(rededuct_gunIndex[j], &deductInfo[j]);
  366. }
  367. }
  368. ShmDcCommonData->RoutineReduct = TRUE;
  369. }
  370. static int InitialRfidPort(void)
  371. {
  372. int fd = open(rfidPortName, O_RDWR);
  373. struct termios tios;
  374. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  375. if (fd != FAIL) {
  376. ioctl (fd, TCGETS, &tios);
  377. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  378. tios.c_lflag = 0;
  379. tios.c_iflag = 0;
  380. tios.c_oflag = 0;
  381. tios.c_cc[VMIN] = 0;
  382. tios.c_cc[VTIME] = (uint8_t) 1;
  383. tios.c_lflag = 0;
  384. tcflush(fd, TCIFLUSH);
  385. ioctl(fd, TCSETS, &tios);
  386. }
  387. if (fd < 0) {
  388. pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
  389. }
  390. return fd;
  391. }
  392. void RemoteStartCancelPreAuth(int sel_gun)
  393. {
  394. log_info("Remote start need to cancel PreAuth");
  395. memcpy(&ShmDcCommonData->RedeductBill, &ShmDcCommonData->TransactionInfo[sel_gun], sizeof(RecordTransactionInfo));
  396. memset(&ShmDcCommonData->TransactionInfo[sel_gun], 0x00, sizeof(RecordTransactionInfo));
  397. ShmDcCommonData->TransactionInfo[sel_gun].LineStatus = ShmDcCommonData->RedeductBill.LineStatus;
  398. }
  399. void CreateRfidFork(void)
  400. {
  401. pid_t rfidRecPid;
  402. int donate = 0;
  403. rfidRecPid = fork();
  404. int sel_gun = 0;
  405. int linestatus = 0;
  406. if (rfidRecPid == 0) {
  407. //char localTime[128] = {0};
  408. struct timeb SeqEndTime;
  409. struct tm *tm;
  410. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  411. int fd = -1;
  412. int isContinue = 1;
  413. //RFID rfid = {0};
  414. //RecordTransactionInfo deduct;
  415. fd = InitialRfidPort();
  416. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  417. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  418. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  419. struct ChargingInfoData *pDcChargingInfo = NULL;
  420. if (DeductDB_Open() != PASS) {
  421. isDeductDb_ready = false;
  422. } else {
  423. isDeductDb_ready = true;
  424. //InsertDeductInfo(0, &ShmDcCommonData->TransactionInfo);
  425. }
  426. int gunIndex;
  427. //int uploadIndex = 0;
  428. //int ReAuthComplete_Index = 0;
  429. //log_info("RFID fork Child's PID is %d", getpid());
  430. int result;
  431. int is_idle = TRUE;
  432. while (isContinue) {
  433. result = 0;
  434. usleep(500000);
  435. ftime(&SeqEndTime);
  436. SeqEndTime.time = time(NULL);
  437. tm = localtime(&SeqEndTime.time);
  438. if (ShmDcCommonData->DebugFlag == TRUE ||
  439. ShmDcCommonData->is_RemoteStart[pSysInfo->CurGunSelected] == TRUE ||
  440. ShmDcCommonData->is_AutoStart[pSysInfo->CurGunSelected] == TRUE) {
  441. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  442. ShmDcCommonData->PreAuth_Result = 0;
  443. }
  444. is_idle = TRUE;
  445. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  446. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  447. // 檢查電樁狀態是否為idle狀態
  448. if (pDcChargingInfo->SystemStatus != S_IDLE ) {
  449. is_idle = FALSE;
  450. }
  451. // 檢查使用者是否進行線上支付操作並且線下支付已完成
  452. // 需進行取消預授權
  453. if (checkRemoteStart(gunIndex) && ShmDcCommonData->AuthPass_flag[gunIndex] &&
  454. !ShmDcCommonData->PayPass_flag[gunIndex]) {
  455. RemoteStartCancelPreAuth(gunIndex);
  456. ShmDcCommonData->AuthPass_flag[gunIndex] = FALSE;
  457. }
  458. if (ShmDcCommonData->StopCharge[gunIndex] == TRUE && pDcChargingInfo->Replug_flag == TRUE) {
  459. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  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. continue;
  465. }
  466. log_info("Gun %d Not Into Charging cancel Trade",gunIndex);
  467. StopGunInfoTimeoutDet(gunIndex);
  468. ShmDcCommonData->TradeCancel = TRUE;
  469. pSysInfo->SystemPage = _PAGE_SENSING;
  470. StartSystemTimeoutDet(Timeout_TradeCancel);
  471. CreditCardCancelPreAuth(fd, gunIndex);
  472. pSysInfo->CurGunSelected = gunIndex;
  473. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  474. StopSystemTimeoutDet();
  475. } else {
  476. if (ShmDcCommonData->StopCharge[gunIndex] && ShmDcCommonData->finalcost_flag[gunIndex] &&
  477. ShmDcCommonData->PreAuth_Config != _CREDITCARD_CANCEL) {
  478. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  479. pSysInfo->CurGunSelected = gunIndex;
  480. StopGunInfoTimeoutDet(gunIndex); //Timeout_FinalCost
  481. // Remote Start of AutoStart ByPass Credit Card Reader
  482. if (ShmDcCommonData->DebugFlag == TRUE || checkRemoteStart(gunIndex) ||
  483. ShmDcCommonData->is_AutoStart[gunIndex]) {
  484. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  485. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  486. }
  487. continue;
  488. }
  489. // 實際扣款
  490. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  491. log_info("Final Cost less 1 , Cancel Trade!!");
  492. if (CreditCardCancelPreAuth(fd, gunIndex) > 0) {
  493. pSysInfo->SystemPage = _PAGE_COMPLETE;
  494. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  495. } else {
  496. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  497. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  498. }
  499. } else {
  500. PreAuthCompleteToCardReader(fd, gunIndex);
  501. }
  502. }
  503. }
  504. } // for
  505. // 取消預授權
  506. if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_CANCEL) {
  507. CreditCardCancelPreAuth(fd,pSysInfo->CurGunSelected);
  508. // 預授權
  509. } else if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_PREAUTH /* && ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected]*/) {
  510. sel_gun = pSysInfo->CurGunSelected;
  511. donate = (int)ShmDcCommonData->TransactionInfo[sel_gun].IsDonateInvoice;
  512. linestatus = (int)ShmDcCommonData->TransactionInfo[sel_gun].LineStatus;
  513. memset(&ShmDcCommonData->TransactionInfo[sel_gun], 0x00, sizeof(RecordTransactionInfo));
  514. ShmDcCommonData->TransactionInfo[sel_gun].IsDonateInvoice = (unsigned char)donate;
  515. ShmDcCommonData->TransactionInfo[sel_gun].LineStatus = linestatus;
  516. result = CreditCardPreAuth(fd, PREAUTHMONEY,&pSysConfig->ModelName[0], &ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard);
  517. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(sel_gun);
  518. {
  519. if (!checkRemoteStart(sel_gun)) {
  520. if (!ShmDcCommonData->TradeCancel)
  521. StopSystemTimeoutDet();
  522. if (ShmDcCommonData->TradeCancel == FALSE && result > 0) {
  523. pSysInfo->SystemPage = _PAGE_SENSING;
  524. }
  525. /*
  526. if (result > 0) {
  527. storePayResult(sel_gun);
  528. log_info("sleep");
  529. sleep(3);
  530. log_info("Wake");
  531. getPayResult(sel_gun);
  532. }*/
  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. }