RFID.c 34 KB

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