RFID.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  465. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  466. continue;
  467. }
  468. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  469. log_info("Gun %d Not Into Charging cancel Trade",gunIndex);
  470. StopGunInfoTimeoutDet(gunIndex);
  471. ShmDcCommonData->TradeCancel = TRUE;
  472. pSysInfo->SystemPage = _PAGE_SENSING;
  473. StartSystemTimeoutDet(Timeout_TradeCancel);
  474. CreditCardCancelPreAuth(fd, gunIndex);
  475. pSysInfo->CurGunSelected = gunIndex;
  476. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  477. StopSystemTimeoutDet();
  478. } else {
  479. if (ShmDcCommonData->StopCharge[gunIndex] && ShmDcCommonData->finalcost_flag[gunIndex] &&
  480. ShmDcCommonData->PreAuth_Config != _CREDITCARD_CANCEL) {
  481. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  482. pSysInfo->CurGunSelected = gunIndex;
  483. pSysInfo->SystemPage = _PAGE_PAYING;
  484. StopGunInfoTimeoutDet(gunIndex); //Timeout_FinalCost
  485. // Remote Start of AutoStart ByPass Credit Card Reader
  486. if (ShmDcCommonData->DebugFlag == TRUE ||
  487. checkRemoteStart(gunIndex) ||
  488. ShmDcCommonData->is_AutoStart[gunIndex]) {
  489. pSysInfo->SystemPage = _PAGE_COMPLETE;
  490. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  491. continue;
  492. }
  493. // 實際扣款
  494. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  495. log_info("Final Cost less 1 , Cancel Trade!!");
  496. if (CreditCardCancelPreAuth(fd, gunIndex) > 0) {
  497. pSysInfo->SystemPage = _PAGE_COMPLETE;
  498. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  499. } else {
  500. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  501. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  502. }
  503. } else {
  504. PreAuthCompleteToCardReader(fd, gunIndex);
  505. }
  506. }
  507. }
  508. } // for
  509. // 取消預授權
  510. if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_CANCEL) {
  511. CreditCardCancelPreAuth(fd,pSysInfo->CurGunSelected);
  512. // 預授權
  513. } else if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_PREAUTH /* && ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected]*/) {
  514. sel_gun = pSysInfo->CurGunSelected;
  515. donate = (int)ShmDcCommonData->TransactionInfo[sel_gun].IsDonateInvoice;
  516. linestatus = (int)ShmDcCommonData->TransactionInfo[sel_gun].LineStatus;
  517. memset(&ShmDcCommonData->TransactionInfo[sel_gun], 0x00, sizeof(RecordTransactionInfo));
  518. ShmDcCommonData->TransactionInfo[sel_gun].IsDonateInvoice = (unsigned char)donate;
  519. ShmDcCommonData->TransactionInfo[sel_gun].LineStatus = linestatus;
  520. result = CreditCardPreAuth(fd, PREAUTHMONEY,&pSysConfig->ModelName[0], &ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard);
  521. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(sel_gun);
  522. {
  523. if (!checkRemoteStart(sel_gun)) {
  524. if (!ShmDcCommonData->TradeCancel)
  525. StopSystemTimeoutDet();
  526. if (ShmDcCommonData->TradeCancel == FALSE && result > 0) {
  527. pSysInfo->SystemPage = _PAGE_SENSING;
  528. }
  529. if (result > 0)
  530. sleep(3);
  531. }
  532. ShmDcCommonData->PreAuth_Result = result;
  533. //result = 1;
  534. if (result > 0 && strcmp((char*)ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard.CardNo, "") != 0) {
  535. ShmDcCommonData->AuthPass_flag[sel_gun] = TRUE;
  536. strncpy((char*)pSysConfig->UserId, (char*)ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard.CardNo, 20);
  537. log_info("Authorize card:%s", pSysConfig->UserId);
  538. ShmDcCommonData->TransactionInfo[sel_gun].DeductResult = _DEDUCT_PREAUTH;
  539. ShmDcCommonData->TransactionInfo[sel_gun].IsUpload = FALSE;
  540. ShmDcCommonData->TransactionInfo[sel_gun].ConnectorID = ShmDcCommonData->ConnectorID[sel_gun];
  541. log_info("Line Status:%d", ShmDcCommonData->TransactionInfo[sel_gun].LineStatus);
  542. InsertDeductInfo(ShmDcCommonData->ConnectorID[sel_gun], &ShmDcCommonData->TransactionInfo[sel_gun]);
  543. log_info("PreAuth OK");
  544. } else if (result < 0) {
  545. if (ShmDcCommonData->TradeCancel == FALSE && !checkRemoteStart(sel_gun))
  546. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  547. ShmDcCommonData->AuthPass_flag[sel_gun] = FALSE;
  548. memset(&ShmDcCommonData->TransactionInfo[sel_gun].pCreditCard, 0, sizeof(TransInfo));
  549. log_info("PreAuth Fail");
  550. }
  551. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  552. }
  553. }
  554. // 每30分鐘檢查補扣款
  555. if (tm->tm_min %30 == 0 && tm->tm_sec == 0) {
  556. ShmDcCommonData->RoutineReduct = FALSE;
  557. }
  558. if ((is_idle == TRUE &&
  559. (pSysInfo->SystemPage == _PAGE_IDLE || pSysInfo->SystemPage == _PAGE_SELECT_GUN) &&
  560. ShmDcCommonData->RoutineReduct == FALSE) || ShmDcCommonData->Exe_ReDeduct) {
  561. ReDeductProcess(fd);
  562. pSysInfo->SystemPage = _PAGE_IDLE;
  563. ShmDcCommonData->CreditCardUpload = FALSE;
  564. ShmDcCommonData->Exe_ReDeduct = 0;
  565. }
  566. // 每日晚上11點30分結帳
  567. if (is_idle == TRUE && (pSysInfo->SystemPage == _PAGE_IDLE || pSysInfo->SystemPage == _PAGE_SELECT_GUN) &&
  568. ((tm->tm_hour == 15 && tm->tm_min > 30 && ShmDcCommonData->RoutineSettlement == FALSE) ||
  569. ShmDcCommonData->UnionSettlement)) {
  570. StopSystemTimeoutDet();
  571. if (ShmDcCommonData->UnionSettlement)
  572. ShmDcCommonData->UnionSettlement = 0;
  573. else
  574. ShmDcCommonData->RoutineSettlement = TRUE;
  575. ShmDcCommonData->CreditCardUpload = TRUE;
  576. pSysInfo->SystemPage = _PAGE_PAYING;
  577. result = CreditCardUnionSettlement(fd, &pSysConfig->ModelName[0], &LocalTransactionInfo.pCreditCard);
  578. if (result > 0) {
  579. log_info("CreditCardUnionSettlement OK");
  580. }
  581. else
  582. log_info("CreditCardUnionSettlement FAIL");
  583. sleep(90);
  584. pSysInfo->SystemPage = _PAGE_IDLE;
  585. ShmDcCommonData->CreditCardUpload = FALSE;
  586. }
  587. if (tm->tm_hour == 16 && tm->tm_min == 0) {
  588. ShmDcCommonData->RoutineSettlement = FALSE;
  589. }
  590. /*
  591. // 刷卡判斷
  592. if (pSysConfig->OfflinePolicy == _OFFLINE_POLICY_NO_CHARGING ||
  593. !pSysConfig->isRFID) {
  594. continue;
  595. }
  596. if (getRequestCardSN(fd, 0, &rfid) == false) {
  597. continue;
  598. }
  599. //log_info("Get Card..-%s- ", pSysConfig->UserId);
  600. if (strlen((char *)pSysConfig->UserId) != 0) {
  601. continue;
  602. }
  603. if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_LITTLE) {
  604. switch (rfid.snType) {
  605. case RFID_SN_TYPE_6BYTE:
  606. sprintf((char *) pSysConfig->UserId,
  607. "%02X%02X%02X%02X%02X%02X",
  608. rfid.currentCard[0], rfid.currentCard[1],
  609. rfid.currentCard[2], rfid.currentCard[3],
  610. rfid.currentCard[4], rfid.currentCard[5]);
  611. break;
  612. case RFID_SN_TYPE_7BYTE:
  613. sprintf((char *) pSysConfig->UserId,
  614. "%02X%02X%02X%02X%02X%02X%02X",
  615. rfid.currentCard[0], rfid.currentCard[1],
  616. rfid.currentCard[2], rfid.currentCard[3],
  617. rfid.currentCard[4], rfid.currentCard[5],
  618. rfid.currentCard[6]);
  619. break;
  620. case RFID_SN_TYPE_10BYTE:
  621. sprintf((char *) pSysConfig->UserId,
  622. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  623. rfid.currentCard[0], rfid.currentCard[1],
  624. rfid.currentCard[2], rfid.currentCard[3],
  625. rfid.currentCard[4], rfid.currentCard[5],
  626. rfid.currentCard[6], rfid.currentCard[7],
  627. rfid.currentCard[8], rfid.currentCard[9]);
  628. break;
  629. case RFID_SN_TYPE_4BYTE:
  630. sprintf((char *) pSysConfig->UserId,
  631. "%02X%02X%02X%02X",
  632. rfid.currentCard[0], rfid.currentCard[1],
  633. rfid.currentCard[2], rfid.currentCard[3]);
  634. break;
  635. }
  636. } else if (pSysConfig->RfidCardNumEndian == RFID_ENDIAN_BIG) {
  637. switch (rfid.snType) {
  638. case RFID_SN_TYPE_6BYTE:
  639. sprintf((char *) pSysConfig->UserId,
  640. "%02X%02X%02X%02X%02X%02X",
  641. rfid.currentCard[5], rfid.currentCard[4],
  642. rfid.currentCard[3], rfid.currentCard[2],
  643. rfid.currentCard[1], rfid.currentCard[0]);
  644. break;
  645. case RFID_SN_TYPE_7BYTE:
  646. sprintf((char *) pSysConfig->UserId,
  647. "%02X%02X%02X%02X%02X%02X%02X",
  648. rfid.currentCard[6], rfid.currentCard[5],
  649. rfid.currentCard[4], rfid.currentCard[3],
  650. rfid.currentCard[2], rfid.currentCard[1],
  651. rfid.currentCard[0]);
  652. break;
  653. case RFID_SN_TYPE_10BYTE:
  654. sprintf((char *) pSysConfig->UserId,
  655. "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  656. rfid.currentCard[9], rfid.currentCard[8],
  657. rfid.currentCard[7], rfid.currentCard[6],
  658. rfid.currentCard[5], rfid.currentCard[4],
  659. rfid.currentCard[3], rfid.currentCard[2],
  660. rfid.currentCard[1], rfid.currentCard[0]);
  661. break;
  662. case RFID_SN_TYPE_4BYTE:
  663. sprintf((char *) pSysConfig->UserId,
  664. "%02X%02X%02X%02X",
  665. rfid.currentCard[3], rfid.currentCard[2],
  666. rfid.currentCard[1], rfid.currentCard[0]);
  667. break;
  668. }
  669. }
  670. log_info("card number = %s", pSysConfig->UserId);
  671. */
  672. }
  673. }
  674. }