RFID.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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 isDb_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. void showDeductInfo(RecordTransactionInfo* transactionInfo)
  97. {
  98. char ApprovalNo[10];
  99. char vemdata[65];
  100. char cardno[21];
  101. char sn[37];
  102. memset(ApprovalNo,'\0',sizeof(ApprovalNo));
  103. memset(vemdata,'\0',sizeof(vemdata));
  104. memset(cardno,'\0',sizeof(cardno));
  105. memset(sn,'\0',sizeof(sn));
  106. memcpy(ApprovalNo,&transactionInfo->pCreditCard.ApprovalNo[0],9);
  107. memcpy(vemdata,&transactionInfo->pCreditCard.VemData[0],64);
  108. memcpy(cardno,&transactionInfo->pCreditCard.CardNo[0],20);
  109. log_info("Gun[%d] TransactionId:%d DeductResult:%d IsDonateInvoice:%d Amount:%f Approva Num:[%s] VemData:[%s] CardNo:[%s]",
  110. transactionInfo->ConnectorID,
  111. transactionInfo->TransactionId,
  112. transactionInfo->DeductResult,
  113. transactionInfo->IsDonateInvoice,
  114. transactionInfo->Amount,
  115. ApprovalNo,
  116. vemdata,
  117. cardno);
  118. }
  119. bool RfidStopCharging(void)
  120. {
  121. //當前沒有選槍
  122. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  123. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  124. if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL ) {
  125. strcpy((char *)pSysConfig->UserId, "");
  126. return false;
  127. }
  128. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
  129. if (pDcChargingInfo->isRemoteStart) {
  130. } else {
  131. if (strcmp((char *)pSysConfig->UserId, (char *)pDcChargingInfo->StartUserId) == EQUAL) {
  132. ChargingTerminalProcess(pSysInfo->CurGunSelected);
  133. strcpy((char *)pSysConfig->UserId, "");
  134. log_info("index = %d, card number = %s, UserId = %s ",
  135. pSysInfo->CurGunSelected,
  136. pDcChargingInfo->StartUserId,
  137. pSysConfig->UserId);
  138. return true;
  139. } else {
  140. strcpy((char *)pSysConfig->UserId, "");
  141. return false;
  142. }
  143. }
  144. return false;
  145. }
  146. static void UserScanFunction(int gunIndex)
  147. {
  148. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  149. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  150. //struct ChargingInfoData *pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
  151. struct ChargingInfoData *pDcChargingInfo = NULL;
  152. ShmDcCommonData = (DcCommonInfo*)GetShmDcCommonData();
  153. // 當前非驗證的狀態
  154. if (IsAuthorizingMode()) {
  155. isAutorCompleteHandle(/*&_authorizeIndex*/);
  156. }
  157. // 先判斷現在是否可以提供刷卡
  158. // 1. 如果當前沒有槍是閒置狀態,則無提供刷卡功能
  159. // 2. 停止充電
  160. //if (pSysInfo->PageIndex == _LCM_ERROR) {
  161. if (pSysInfo->SystemPage == _PAGE_MAINTAIN) {
  162. strcpy((char *)pSysConfig->UserId, "");
  163. return;
  164. }
  165. if (strlen((char *)pSysConfig->UserId) <= 0) {
  166. return;
  167. }
  168. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  169. if ( (pSysInfo->SystemPage == _PAGE_AUTHORIZE ||
  170. pSysInfo->SystemPage == _PAGE_SENSING)) {
  171. log_info("// LCM => Authorizing");
  172. confirmSelGun(gunIndex);
  173. setSelGunWaitToAuthor(gunIndex);
  174. StartSystemTimeoutDet(Timeout_Authorizing);
  175. AuthorizingStart();
  176. pDcChargingInfo->SystemStatus = S_AUTHORIZING;
  177. // LCM => Authorizing
  178. //pSysInfo->SystemPage = _PAGE_AUTHORIZE;
  179. // 進入確認卡號狀態
  180. } else {
  181. //strcpy((char *)pSysConfig->UserId, "");
  182. }
  183. return;
  184. }
  185. bool GetIsCardScan(void)
  186. {
  187. return isCardScan;
  188. }
  189. void SetIsCardScan(bool value)
  190. {
  191. isCardScan = value;
  192. }
  193. void AuthorizeToCharge(int gunIndex)
  194. {
  195. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  196. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  197. struct ChargingInfoData* pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  198. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  199. if (pDcChargingInfo->SystemStatus == S_RESERVATION) {
  200. if (strcmp((char*)pSysConfig->UserId, (char*)ShmDcCommonData->pGunInfo[gunIndex].ReservationID) != EQUAL) {
  201. log_info("LCM => Authorize fail");
  202. ShmDcCommonData->TradeCancel = TRUE;
  203. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  204. ShmDcCommonData->PreAuth_Result = 0;
  205. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  206. strcpy((char*)pSysConfig->UserId, "");
  207. ClearAuthorizedFlag();
  208. return;
  209. }
  210. }
  211. if(isAuthorizedComplete())
  212. {
  213. if (canStartCharging()) {
  214. DetectPluginStart(gunIndex);
  215. if (ShmDcCommonData->AuthPass_flag[gunIndex] == TRUE &&
  216. ShmDcCommonData->TradeCancel == FALSE) {
  217. pSysInfo->SystemPage = _PAGE_PLUGIN;
  218. }
  219. } else {
  220. struct ChargingInfoData* pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  221. log_info("LCM => Authorize fail");
  222. ShmDcCommonData->TradeCancel = TRUE;
  223. ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
  224. ShmDcCommonData->PreAuth_Result = 0;
  225. if (!pDcChargingInfo->RemoteStartFlag || !pDcChargingInfo->isRemoteStart)
  226. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  227. strcpy((char *)pSysConfig->UserId, "");
  228. }
  229. ClearAuthorizedFlag();
  230. }
  231. }
  232. void ScannerCardProcess(int gunIndex)
  233. {
  234. //struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  235. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  236. struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
  237. struct ChargingInfoData *pDcChargingInfo = NULL;
  238. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  239. if (!isDetectPlugin(gunIndex) && (pSysInfo->SystemPage == _PAGE_SENSING ||
  240. pSysInfo->SystemPage == _PAGE_AUTHORIZE ) &&
  241. (pDcChargingInfo->SystemStatus == S_IDLE || pDcChargingInfo->SystemStatus == S_RESERVATION )&&
  242. pSysWarning->Level != WARN_LV_ER ) {
  243. //setSelGunWaitToAuthor(gunIndex);
  244. isCardScan = true;
  245. // 處理刷卡及驗證卡號的動作
  246. UserScanFunction(gunIndex);
  247. }
  248. if (pDcChargingInfo->SystemStatus == S_AUTHORIZING && pSysInfo->SystemPage == _PAGE_SENSING &&
  249. !ShmDcCommonData->is_RemoteStart[gunIndex] ) {
  250. AuthorizeToCharge(gunIndex);
  251. } else if (pSysInfo->SystemPage == _PAGE_AUTHORIZE_FAIL) {
  252. StartSystemTimeoutDet(Timeout_VerifyFail);
  253. isCardScan = false;
  254. } else {
  255. isCardScan = false;
  256. }
  257. }
  258. void storePayResult(uint8_t gunIndex)
  259. {
  260. memset(&LocalTransactionInfo, 0x00, sizeof(RecordTransactionInfo));
  261. memcpy(&LocalTransactionInfo, &ShmDcCommonData->TransactionInfo[gunIndex] ,sizeof(RecordTransactionInfo));
  262. }
  263. void getPayResult(uint8_t gunIndex)
  264. {
  265. memcpy(&ShmDcCommonData->TransactionInfo[gunIndex], &LocalTransactionInfo, sizeof(RecordTransactionInfo));
  266. }
  267. void storeParkingPayResult(uint8_t gunIndex)
  268. {
  269. memset(&LocalTransactionInfo, 0x00, sizeof(RecordTransactionInfo));
  270. memcpy(&LocalTransactionInfo, &ShmDcCommonData->ParkingInfo[gunIndex] ,sizeof(RecordTransactionInfo));
  271. }
  272. void getParkingPayResult(uint8_t gunIndex)
  273. {
  274. //memcpy(&ShmDcCommonData->ParkingInfo[gunIndex], &LocalTransactionInfo, sizeof(RecordTransactionInfo));
  275. memcpy( ShmDcCommonData->ParkingInfo[gunIndex].OccupancySN, LocalTransactionInfo.OccupancySN, 36);
  276. }
  277. void WritePayResult(int result ,uint8_t gunIndex)
  278. {
  279. if (result == TRUE)
  280. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_PASS;
  281. else
  282. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_FAIL;
  283. log_info("Gun[%d] TransactionId:%d DeductResult:%d IsDonateInvoice:%d Amount:%f VemData:%s Approve No:%s",
  284. gunIndex, ShmDcCommonData->TransactionInfo[gunIndex].TransactionId,
  285. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult,
  286. ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice,
  287. ShmDcCommonData->TransactionInfo[gunIndex].Amount,
  288. ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData,
  289. ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.ApprovalNo);
  290. }
  291. void PreAuthCompleteToCardReader(int fd,uint8_t gunIndex)
  292. {
  293. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  294. int result = 0;
  295. storePayResult(gunIndex);
  296. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount > 0 && ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1 &&
  297. ShmDcCommonData->pGunInfo[gunIndex].ParkingStatus == 0) {
  298. log_info("final cost less 1 : %f", ShmDcCommonData->TransactionInfo[gunIndex].Amount);
  299. ShmDcCommonData->TransactionInfo[gunIndex].Amount = 1;
  300. }
  301. result = CreditCardPreAuthComplete(fd,(int)LocalTransactionInfo.Amount, &pSysConfig->ModelName[0],
  302. &ShmDcCommonData->TransactionInfo[gunIndex].pCreditCard.VemData[0],
  303. &ShmDcCommonData->TransactionInfo[gunIndex]);
  304. getPayResult(gunIndex);
  305. //sleep(10);
  306. if (result > 0 ) {
  307. log_info("Credit Card Spend Money:%.1f", ShmDcCommonData->TransactionInfo[gunIndex].Amount);
  308. pSysInfo->SystemPage = _PAGE_COMPLETE;
  309. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_PASS;
  310. WritePayResult(TRUE,gunIndex);
  311. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  312. } else {
  313. log_info("PAYING FAIL");
  314. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  315. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  316. ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = _DEDUCT_COMPLETE_FAIL;
  317. WritePayResult(FALSE,gunIndex);
  318. }
  319. ShmDcCommonData->TransactionInfo[gunIndex].IsUpload = FALSE;
  320. //getPayResult(gunIndex);
  321. UpdateDeductInfoStatus(gunIndex, &ShmDcCommonData->TransactionInfo[gunIndex]);
  322. ShmDcCommonData->PreAuth_Result = result;
  323. }
  324. int CreditCardCancelPreAuth(int fd, uint8_t gunIndex)
  325. {
  326. if (ShmDcCommonData->pGunInfo[gunIndex].ParkingStatus) {
  327. return;
  328. }
  329. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  330. storePayResult(gunIndex);
  331. ShmDcCommonData->PreAuth_Result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  332. &LocalTransactionInfo.pCreditCard.ApprovalNo[0],
  333. &LocalTransactionInfo.pCreditCard.CardNo[0],
  334. &LocalTransactionInfo.pCreditCard.VemData[0]);
  335. //sleep(10);
  336. if (ShmDcCommonData->PreAuth_Result >= 0) {
  337. strcpy((char*)pSysConfig->UserId, "");
  338. //ShmDcCommonData->PayFinish[gunIndex] = TRUE;
  339. log_info("Gun%d Card Reader PreAuth Cancel Success",gunIndex);
  340. LocalTransactionInfo.DeductResult = _DEDUCT_CANCEL;
  341. } else if (ShmDcCommonData->PreAuth_Result < 0) {
  342. log_info("Gun%d Card Reader PreAuth Cancel Failure",gunIndex);
  343. LocalTransactionInfo.DeductResult = _DEDUCT_PREAUTH;
  344. }
  345. LocalTransactionInfo.IsUpload = FALSE;
  346. getPayResult(gunIndex);
  347. UpdateDeductInfoStatus(gunIndex, &LocalTransactionInfo);
  348. memset(&ShmDcCommonData->TransactionInfo[gunIndex], 0x00, sizeof(RecordTransactionInfo));
  349. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  350. return ShmDcCommonData->PreAuth_Result;
  351. }
  352. void ReDeductProcess(int fd)
  353. {
  354. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  355. int j;
  356. int result;
  357. int rededuct_gunIndex[128];
  358. RecordTransactionInfo deductInfo[128];
  359. int rededuct_num = 0;
  360. // 充電費補扣款
  361. rededuct_num = DB_GetMultiReDeductInfo(&rededuct_gunIndex[0], &deductInfo[0]);
  362. if (rededuct_num == 0) {
  363. //log_info("No Rededuct Information");
  364. } else {
  365. ShmDcCommonData->CreditCardUpload = TRUE;
  366. pSysInfo->SystemPage = _PAGE_PAYING;
  367. log_info("Rededuct Total Number:%d", rededuct_num);
  368. for (j = rededuct_num-1; j >= 0 ; j--) {
  369. sleep(10);
  370. log_info("Start Rededuct item [%d]",j);
  371. if (deductInfo[j].isIntoCharge == FALSE ||
  372. (deductInfo[j].Energy == 0 && deductInfo[j].Amount == 0) ||
  373. (deductInfo[j].LineStatus == _LINE_PAY_DONATE || deductInfo[j].LineStatus == _LINE_PAY_INVOICE)) {
  374. // 未進入充電或度數等於零
  375. result = CreditCardPreAuthCancel(fd, PREAUTHMONEY, &pSysConfig->ModelName[0],
  376. &deductInfo[j].pCreditCard.ApprovalNo[0],
  377. &deductInfo[j].pCreditCard.CardNo[0],
  378. &deductInfo[j].pCreditCard.VemData[0]);
  379. sleep(10);
  380. if (result > 0) {
  381. deductInfo[j].DeductResult = _DEDUCT_CANCEL;
  382. log_info("Backgroud PreAuthCancel OK");
  383. }
  384. } else {
  385. // 度數大於零
  386. if (deductInfo[j].Amount == 0 && deductInfo[j].Energy > 0) {
  387. log_info("Error Close Charging recount amount:%.1f",deductInfo[j].Amount);
  388. continue;
  389. }
  390. result = CreditCardPreAuthComplete(fd, (int)deductInfo[j].Amount, &pSysConfig->ModelName[0],
  391. &deductInfo[j].pCreditCard.VemData[0],
  392. &ShmDcCommonData->TransactionInfo[0].pCreditCard);
  393. if (result > 0) {
  394. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_PASS;
  395. log_info("Backgroud PreAuthComplete OK");
  396. } else if (result < 0) {
  397. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_FAIL;
  398. log_info("Backgroud ID:%d Amount:%d VemData:%s PreAuthComplete fail",
  399. deductInfo[j].TransactionId,
  400. (int)deductInfo[j].Amount,
  401. deductInfo[j].pCreditCard.VemData);
  402. }
  403. }
  404. deductInfo[j].IsUpload = FALSE;
  405. deductInfo[j].RedeductTime++;
  406. UpdateDeductInfoStatus(rededuct_gunIndex[j], &deductInfo[j]);
  407. }
  408. }
  409. // 佔位費補扣款
  410. /*
  411. rededuct_num = DB_GetParkingDeductResult(&rededuct_gunIndex[0], &deductInfo[0]);
  412. if (rededuct_num == 0) {
  413. log_info("No Parking Rededuct Information");
  414. } else {
  415. ShmDcCommonData->CreditCardUpload = TRUE;
  416. pSysInfo->SystemPage = _PAGE_PAYING;
  417. log_info("Parking Rededuct Total Number:%d", rededuct_num);
  418. for (j = rededuct_num-1; j >= 0 ; j--) {
  419. sleep(3);
  420. log_info("Start Parking Rededuct item [%d]",j);
  421. result = CreditCardPreAuthComplete(fd, (int)deductInfo[j].Amount, &pSysConfig->ModelName[0],
  422. &deductInfo[j].pCreditCard.VemData[0],
  423. &ShmDcCommonData->ParkingInfo[0].pCreditCard);
  424. if (result > 0) {
  425. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_PASS;
  426. log_info("Backgroud Parking PreAuthComplete OK");
  427. } else if (result < 0) {
  428. deductInfo[j].DeductResult = _DEDUCT_COMPLETE_FAIL;
  429. log_info("Backgroud Parking ID:%d Amount:%d VemData:%s PreAuthComplete fail",
  430. deductInfo[j].TransactionId,
  431. (int)deductInfo[j].Amount,
  432. deductInfo[j].pCreditCard.VemData);
  433. }
  434. deductInfo[j].IsUpload = FALSE;
  435. deductInfo[j].RedeductTime++;
  436. UpdateParkingDeductInfo(rededuct_gunIndex[j], &deductInfo[j]);
  437. }
  438. }
  439. */
  440. ShmDcCommonData->RoutineReduct = TRUE;
  441. }
  442. static int InitialRfidPort(void)
  443. {
  444. int fd = open(rfidPortName, O_RDWR);
  445. struct termios tios;
  446. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  447. if (fd != FAIL) {
  448. ioctl (fd, TCGETS, &tios);
  449. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  450. tios.c_lflag = 0;
  451. tios.c_iflag = 0;
  452. tios.c_oflag = 0;
  453. tios.c_cc[VMIN] = 0;
  454. tios.c_cc[VTIME] = (uint8_t) 1;
  455. tios.c_lflag = 0;
  456. tcflush(fd, TCIFLUSH);
  457. ioctl(fd, TCSETS, &tios);
  458. }
  459. if (fd < 0) {
  460. pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
  461. }
  462. return fd;
  463. }
  464. void RemoteStartCancelPreAuth(int sel_gun)
  465. {
  466. log_info("Remote start need to cancel PreAuth");
  467. memcpy(&ShmDcCommonData->RedeductBill, &ShmDcCommonData->TransactionInfo[sel_gun], sizeof(RecordTransactionInfo));
  468. memset(&ShmDcCommonData->TransactionInfo[sel_gun], 0x00, sizeof(RecordTransactionInfo));
  469. ShmDcCommonData->TransactionInfo[sel_gun].LineStatus = ShmDcCommonData->RedeductBill.LineStatus;
  470. }
  471. void PreAuthCreditCard(int fd, uint8_t gunIndex)
  472. {
  473. struct SysConfigData* pSysConfig = (struct SysConfigData*)GetShmSysConfigData();
  474. struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);;
  475. int result = 0;
  476. storePayResult(gunIndex);
  477. if (ShmDcCommonData->pGunInfo[gunIndex].ParkingStatus)
  478. result = CreditCardPreAuth(fd, LocalTransactionInfo.Amount,&pSysConfig->ModelName[0], &LocalTransactionInfo.pCreditCard);
  479. else
  480. result = CreditCardPreAuth(fd, PREAUTHMONEY,&pSysConfig->ModelName[0], &LocalTransactionInfo.pCreditCard);
  481. ShmDcCommonData->PreAuth_Result = result;
  482. pDcChargingInfo = (struct ChargingInfoData*)GetDcChargingInfoData(gunIndex);
  483. {
  484. if (!checkRemoteStart(gunIndex)) {
  485. if (!ShmDcCommonData->TradeCancel)
  486. StopSystemTimeoutDet();
  487. if (ShmDcCommonData->TradeCancel == FALSE && result > 0) {
  488. pSysInfo->SystemPage = _PAGE_SENSING;
  489. }
  490. }
  491. if (result > 0 && strcmp((char*)LocalTransactionInfo.pCreditCard.CardNo, "") != 0) {
  492. log_info("Gun%d PreAuth card:%s", gunIndex, LocalTransactionInfo.pCreditCard.CardNo);
  493. LocalTransactionInfo.DeductResult = _DEDUCT_PREAUTH;
  494. LocalTransactionInfo.IsUpload = FALSE;
  495. LocalTransactionInfo.ConnectorID = ShmDcCommonData->ConnectorID[gunIndex];
  496. //log_info("Gun%d Line Status:%d", LocalTransactionInfo.LineStatus);
  497. if (ShmDcCommonData->pGunInfo[gunIndex].ParkingStatus)
  498. InsertParkingDeductInfo(ShmDcCommonData->ConnectorID[gunIndex], &LocalTransactionInfo);
  499. else {
  500. ShmDcCommonData->AuthPass_flag[gunIndex] = TRUE;
  501. strncpy((char*)pSysConfig->UserId, (char*)LocalTransactionInfo.pCreditCard.CardNo, 20);
  502. InsertDeductInfo(ShmDcCommonData->ConnectorID[gunIndex], &LocalTransactionInfo);
  503. }
  504. getPayResult(gunIndex);
  505. //log_info("Gun%d PreAuth OK",gunIndex);
  506. } else if (result < 0) {
  507. if (ShmDcCommonData->TradeCancel == FALSE && !checkRemoteStart(gunIndex))
  508. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  509. ShmDcCommonData->AuthPass_flag[gunIndex] = FALSE;
  510. memset(&LocalTransactionInfo.pCreditCard, 0, sizeof(TransInfo));
  511. log_info("Gun%d PreAuth Fail",gunIndex);
  512. }
  513. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  514. }
  515. }
  516. void CreateRfidFork(void)
  517. {
  518. pid_t rfidRecPid;
  519. int donate = 0;
  520. rfidRecPid = fork();
  521. int sel_gun = 0;
  522. int linestatus = 0;
  523. if (rfidRecPid == 0) {
  524. //char localTime[128] = {0};
  525. struct timeb SeqEndTime;
  526. struct tm *tm;
  527. pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  528. int fd = -1;
  529. int isContinue = 1;
  530. //RFID rfid = {0};
  531. //RecordTransactionInfo deduct;
  532. fd = InitialRfidPort();
  533. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  534. ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
  535. ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
  536. struct ChargingInfoData *pDcChargingInfo = NULL;
  537. if (DeductDB_Open() != PASS) {
  538. isDb_ready = false;
  539. log_info("DeductDB_Open Fail");
  540. return;
  541. } else {
  542. isDb_ready = true;
  543. //InsertDeductInfo(0, &ShmDcCommonData->TransactionInfo);
  544. }
  545. if (ParkingDB_Open() != PASS) {
  546. isDb_ready = false;
  547. log_info("ParkingDB_Open Fail");
  548. return;
  549. } else {
  550. isDb_ready = true;
  551. //InsertDeductInfo(0, &ShmDcCommonData->TransactionInfo);
  552. }
  553. int gunIndex;
  554. //int uploadIndex = 0;
  555. //int ReAuthComplete_Index = 0;
  556. //log_info("RFID fork Child's PID is %d", getpid());
  557. int result;
  558. int is_idle = TRUE;
  559. while (isContinue) {
  560. result = 0;
  561. usleep(500000);
  562. ftime(&SeqEndTime);
  563. SeqEndTime.time = time(NULL);
  564. tm = localtime(&SeqEndTime.time);
  565. if (ShmDcCommonData->DebugFlag == TRUE ||
  566. ShmDcCommonData->is_RemoteStart[pSysInfo->CurGunSelected] == TRUE ||
  567. ShmDcCommonData->is_AutoStart[pSysInfo->CurGunSelected] == TRUE) {
  568. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  569. ShmDcCommonData->PreAuth_Result = 0;
  570. }
  571. is_idle = TRUE;
  572. for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
  573. pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
  574. // 檢查電樁狀態是否為idle狀態
  575. if (pDcChargingInfo->SystemStatus != S_IDLE ) {
  576. is_idle = FALSE;
  577. }
  578. // 檢查使用者是否進行線上支付操作並且線下支付已完成
  579. // 需進行取消預授權
  580. if (checkRemoteStart(gunIndex) && ShmDcCommonData->AuthPass_flag[gunIndex] &&
  581. !ShmDcCommonData->PayPass_flag[gunIndex]) {
  582. RemoteStartCancelPreAuth(gunIndex);
  583. ShmDcCommonData->AuthPass_flag[gunIndex] = FALSE;
  584. }
  585. if (ShmDcCommonData->StopCharge[gunIndex] == TRUE && pDcChargingInfo->Replug_flag == TRUE) {
  586. // Remote Start of AutoStart ByPass Credit Card Reader
  587. if (ShmDcCommonData->DebugFlag == TRUE ||
  588. ShmDcCommonData->is_RemoteStart[gunIndex] == TRUE ||
  589. ShmDcCommonData->is_AutoStart[gunIndex] == TRUE) {
  590. continue;
  591. }
  592. log_info("Gun %d Not Into Charging cancel Trade",gunIndex);
  593. StopGunInfoTimeoutDet(gunIndex);
  594. ShmDcCommonData->TradeCancel = TRUE;
  595. pSysInfo->SystemPage = _PAGE_SENSING;
  596. StartSystemTimeoutDet(Timeout_TradeCancel);
  597. CreditCardCancelPreAuth(fd, gunIndex);
  598. pSysInfo->CurGunSelected = gunIndex;
  599. pSysInfo->SystemPage = _PAGE_PLUGOUT;
  600. StopSystemTimeoutDet();
  601. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  602. } else {
  603. if (ShmDcCommonData->StopCharge[gunIndex] && ShmDcCommonData->finalcost_flag[gunIndex] &&
  604. ShmDcCommonData->PreAuth_Config != _CREDITCARD_CANCEL) {
  605. pSysInfo->CurGunSelected = gunIndex;
  606. StopGunInfoTimeoutDet(gunIndex); //Timeout_FinalCost
  607. // Remote Start of AutoStart ByPass Credit Card Reader
  608. if (ShmDcCommonData->DebugFlag == TRUE || checkRemoteStart(gunIndex) ||
  609. ShmDcCommonData->is_AutoStart[gunIndex]) {
  610. if (ShmDcCommonData->is_AutoStart[gunIndex]) {
  611. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  612. }
  613. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  614. continue;
  615. }
  616. // 實際扣款
  617. if (ShmDcCommonData->TransactionInfo[gunIndex].Amount < 1) {
  618. log_info("Final Cost less 1 , Cancel Trade!!");
  619. if (CreditCardCancelPreAuth(fd, gunIndex) > 0) {
  620. pSysInfo->SystemPage = _PAGE_COMPLETE;
  621. ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
  622. } else {
  623. pSysInfo->SystemPage = _PAGE_PAYFAIL;
  624. ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
  625. }
  626. } else {
  627. ShmDcCommonData->PreAuth_Config = _CREDITCARD_PREAUTHCOMPLETE;
  628. PreAuthCompleteToCardReader(fd, gunIndex);
  629. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  630. }
  631. ShmDcCommonData->StopCharge[gunIndex] = FALSE;
  632. }
  633. }
  634. } // for
  635. if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_SALE /*&& ShmDcCommonData->pGunInfo[pSysInfo->CurGunSelected].isParking*/) {
  636. sel_gun = pSysInfo->CurGunSelected;
  637. ShmDcCommonData->PreAuth_Result = 0;
  638. storeParkingPayResult(sel_gun);
  639. ShmDcCommonData->PreAuth_Result = CreditCardSale(fd,(int)ShmDcCommonData->ParkingInfo[sel_gun].Amount,
  640. &pSysConfig->ModelName[0],
  641. &ShmDcCommonData->ParkingInfo[sel_gun].pCreditCard);
  642. StopSystemTimeoutDet(); //Timeout_ScanCard
  643. getParkingPayResult(sel_gun);
  644. if (ShmDcCommonData->PreAuth_Result > 0 ) {
  645. log_info("Gun%d Parking Fee Sale Success",sel_gun);
  646. pSysInfo->SystemPage = _PAGE_PLUGIN;
  647. ShmDcCommonData->ParkingInfo[sel_gun].IsUpload = FALSE;
  648. //ShmDcCommonData->pGunInfo[sel_gun].GetParkingBill = FALSE;
  649. ShmDcCommonData->ParkingInfo[sel_gun].DeductResult = _DEDUCT_SALE_PASS;
  650. InsertParkingDeductInfo(ShmDcCommonData->ConnectorID[sel_gun], &ShmDcCommonData->ParkingInfo[sel_gun]);
  651. } else {
  652. log_info("Gun%d Parking Fee Sale Fail",sel_gun);
  653. if (!ShmDcCommonData->TradeCancel) {
  654. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  655. }
  656. }
  657. ShmDcCommonData->PreAuth_Result = 0;
  658. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  659. } else if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_CANCEL) {
  660. // 取消預授權
  661. CreditCardCancelPreAuth(fd,pSysInfo->CurGunSelected);
  662. } else if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_PREAUTH ) {
  663. // 預授權
  664. sel_gun = pSysInfo->CurGunSelected;
  665. if (ShmDcCommonData->pGunInfo[sel_gun].isParking) {
  666. log_info("In Parking Status can't enable PreAuth function");
  667. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  668. ShmDcCommonData->AuthPass_flag[gunIndex] = FALSE;
  669. ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
  670. } else
  671. PreAuthCreditCard(fd,sel_gun);
  672. // 佔位費預授權並扣款
  673. /*
  674. if (ShmDcCommonData->PreAuth_Result > 0 ) {
  675. log_info("Gun%d Parking Fee PreAuthComplete Success",sel_gun);
  676. pSysInfo->SystemPage = _PAGE_PLUGIN;
  677. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  678. } else {
  679. log_info("Gun%d Parking Fee PreAuthComplete Fail",sel_gun);
  680. if (!ShmDcCommonData->TradeCancel) {
  681. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  682. StartSystemTimeoutDet(Timeout_VerifyFail);
  683. }
  684. }
  685. if (ShmDcCommonData->pGunInfo[sel_gun].ParkingStatus && ShmDcCommonData->PreAuth_Result > 0 &&
  686. !ShmDcCommonData->TradeCancel && ShmDcCommonData->PreAuth_Config != _CREDITCARD_CANCEL ) {
  687. ShmDcCommonData->PreAuth_Result = 0;
  688. sleep(10);
  689. PreAuthCompleteToCardReader(fd, sel_gun);
  690. if (ShmDcCommonData->PreAuth_Result > 0 ) {
  691. log_info("Gun%d Parking Fee PreAuthComplete Success",sel_gun);
  692. pSysInfo->SystemPage = _PAGE_PLUGIN;
  693. StartSystemTimeoutDet(Timeout_ReturnViewPage);
  694. } else {
  695. log_info("Gun%d Parking Fee PreAuthComplete Fail",sel_gun);
  696. if (!ShmDcCommonData->TradeCancel) {
  697. pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
  698. StartSystemTimeoutDet(Timeout_VerifyFail);
  699. }
  700. }
  701. ShmDcCommonData->PreAuth_Result = 0;
  702. }*/
  703. }
  704. // 每30分鐘檢查補扣款
  705. if (tm->tm_min %30 == 0 && tm->tm_sec == 0) {
  706. ShmDcCommonData->RoutineReduct = FALSE;
  707. }
  708. if ((is_idle == TRUE &&
  709. pSysInfo->SystemPage == _PAGE_IDLE &&
  710. ShmDcCommonData->RoutineReduct == FALSE) || ShmDcCommonData->Exe_ReDeduct) {
  711. ReDeductProcess(fd);
  712. pSysInfo->SystemPage = _PAGE_IDLE;
  713. ShmDcCommonData->CreditCardUpload = FALSE;
  714. ShmDcCommonData->Exe_ReDeduct = 0;
  715. }
  716. // 每日晚上11點30分結帳
  717. if (is_idle == TRUE && pSysInfo->SystemPage == _PAGE_IDLE &&
  718. ((tm->tm_hour == 15 && tm->tm_min > 30 && ShmDcCommonData->RoutineSettlement == FALSE) ||
  719. ShmDcCommonData->UnionSettlement)) {
  720. StopSystemTimeoutDet();
  721. if (ShmDcCommonData->UnionSettlement)
  722. ShmDcCommonData->UnionSettlement = 0;
  723. else
  724. ShmDcCommonData->RoutineSettlement = TRUE;
  725. ShmDcCommonData->CreditCardUpload = TRUE;
  726. pSysInfo->SystemPage = _PAGE_PAYING;
  727. result = CreditCardUnionSettlement(fd, &pSysConfig->ModelName[0], &LocalTransactionInfo.pCreditCard);
  728. if (result > 0) {
  729. log_info("CreditCardUnionSettlement OK");
  730. }
  731. else
  732. log_info("CreditCardUnionSettlement FAIL");
  733. sleep(90);
  734. pSysInfo->SystemPage = _PAGE_IDLE;
  735. ShmDcCommonData->CreditCardUpload = FALSE;
  736. }
  737. if (tm->tm_hour == 16 && tm->tm_min == 0) {
  738. ShmDcCommonData->RoutineSettlement = FALSE;
  739. }
  740. }
  741. }
  742. }