123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639 |
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <string.h>
- #include <stdint.h>
- #include "../Config.h"
- #include "../Log/log.h"
- #include "../Define/define.h"
- #include "../ShareMemory/shmMem.h"
- #include "../SelectGun/SelectGun.h"
- #include "../DataBase/DataBase.h"
- #include "main.h"
- #include "../timeout.h"
- static DcCommonInfo *ShmDcCommonData = NULL;
- static SelectGunInfo *ShmSelectGunInfo = NULL;
- static struct SysInfoData *pSysInfo = NULL;
- #define PREAUTHMONEY 1000
- bool isDeductDb_ready;
- static char *rfidPortName = "/dev/ttyS2";
- int fd = -1;
- static bool isCardScan = false;
- static bool canStartCharging(void)
- {
- uint8_t index = 0;
- char buf2[16] = "";
- memset(buf2, 0, ARRAY_SIZE(buf2));
- struct OCPP16Data *ShmOCPP16Data = (struct OCPP16Data *)GetShmOCPP16Data();
- for (index = 0; index < strlen((char *)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status); index++) {
- sprintf(buf2 + (index - 1) * 2, "%02X", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status[index]);
- }
- sprintf(buf2, "%s", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
-
- if (strcmp(buf2, "Accepted") == EQUAL) {
- return true;
- }
- return false;
- }
- bool isAutorCompleteHandle()
- {
-
- struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
-
-
-
- if (isAuthorizedComplete()) {
-
- if (canStartCharging() == false) {
- strcpy((char *)pSysConfig->UserId, "");
- ClearAuthorizedFlag();
- }
- return true;
- }
- return false;
- }
- bool RfidStopCharging(void)
- {
-
- struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
- struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
- if (getConfirmSelectedGun(pSysInfo->CurGunSelected) == FAIL ) {
- strcpy((char *)pSysConfig->UserId, "");
- return false;
- }
- struct ChargingInfoData *pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
- if (pDcChargingInfo->isRemoteStart) {
- } else {
- if (strcmp((char *)pSysConfig->UserId, (char *)pDcChargingInfo->StartUserId) == EQUAL) {
- ChargingTerminalProcess(pSysInfo->CurGunSelected);
- strcpy((char *)pSysConfig->UserId, "");
- log_info("index = %d, card number = %s, UserId = %s ",
- pSysInfo->CurGunSelected,
- pDcChargingInfo->StartUserId,
- pSysConfig->UserId);
- return true;
- } else {
- strcpy((char *)pSysConfig->UserId, "");
- return false;
- }
- }
- return false;
- }
- static void UserScanFunction(void)
- {
- bool idleReq = false;
- uint8_t i = 0;
- uint8_t stopReq = NO_DEFINE;
- char value[32] = {0};
- static uint8_t _authorizeIndex = NO_DEFINE;
- struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
- struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
- struct ChargingInfoData *pAcChargingInfo = (struct ChargingInfoData *)GetAcChargingInfoData(0);
- struct ChargingInfoData *pDcChargingInfo = NULL;
- GunIndexInfo *pGunIndexInfo = (GunIndexInfo *)GetGunIndexInfo();
-
- if (IsAuthorizingMode()) {
- isAutorCompleteHandle();
- }
-
-
-
-
-
-
- if (pSysInfo->SystemPage == _PAGE_MAINTAIN) {
- strcpy((char *)pSysConfig->UserId, "");
- return;
- }
-
- if (strlen((char *)pSysConfig->UserId) <= 0) {
- return;
- }
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
-
-
- if ( (pSysInfo->SystemPage == _PAGE_AUTHORIZE ||
- pSysInfo->SystemPage == _PAGE_SENSING) &&
- pDcChargingInfo->SystemStatus == S_IDLE) {
- log_info("// LCM => Authorizing");
- confirmSelGun(pSysInfo->CurGunSelected);
- setSelGunWaitToAuthor(pSysInfo->CurGunSelected);
- StartSystemTimeoutDet(Timeout_Authorizing);
- pDcChargingInfo->SystemStatus = S_AUTHORIZING;
- AuthorizingStart();
-
-
-
- } else {
-
- }
- return;
- }
- bool GetIsCardScan(void)
- {
- return isCardScan;
- }
- void SetIsCardScan(bool value)
- {
- isCardScan = value;
- }
- void AuthorizeToCharge()
- {
- struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
- struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
- SelectGunInfo *ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
- struct ChargingInfoData *pDcChargingInfo = NULL;
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
- ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
- if(isAuthorizedComplete())
- {
-
- StartSystemTimeoutDet(Timeout_WaitBalance);
- if (ShmSelectGunInfo->PricesInfo[pSysInfo->CurGunSelected].Balance != FAIL_BALANCE_PRICES) {
- StopSystemTimeoutDet();
-
- if (canStartCharging() && ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] == TRUE) {
-
-
-
-
- DetectPluginStart();
- pSysInfo->SystemPage = _PAGE_PLUGIN;
-
-
- } else {
-
-
-
- }
- }
- ClearAuthorizedFlag();
- }
- }
- void ScannerCardProcess(void)
- {
- struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
- struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
- struct WARNING_CODE_INFO *pSysWarning = (struct WARNING_CODE_INFO *)GetShmSysWarningInfo();
- struct ChargingInfoData *pDcChargingInfo = NULL;
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(pSysInfo->CurGunSelected);
- if (!isDetectPlugin() && (pSysInfo->SystemPage == _PAGE_SENSING ||
- pSysInfo->SystemPage == _PAGE_AUTHORIZE ) &&
- pDcChargingInfo->SystemStatus == S_IDLE &&
- pSysWarning->Level != WARN_LV_ER
- ) {
-
- isCardScan = true;
-
- UserScanFunction();
- }
- if (pDcChargingInfo->SystemStatus == S_AUTHORIZING && pSysInfo->SystemPage == _PAGE_SENSING ) {
- AuthorizeToCharge();
- } else if (pSysInfo->SystemPage == _PAGE_AUTHORIZE_FAIL) {
- StartSystemTimeoutDet(Timeout_VerifyFail);
- isCardScan = false;
- } else if ( pSysInfo->SystemPage == _PAGE_PLUGIN) {
- StartSystemTimeoutDet(Timeout_WaitPlug);
- } else {
- isCardScan = false;
- }
- }
- void WritePayResult(int result ,uint8_t gunIndex)
- {
- ShmDcCommonData->TransactionInfo[gunIndex].Amount = ShmDcCommonData->finalcost[gunIndex];
- memcpy(ShmDcCommonData->TransactionInfo[gunIndex].ApprovalNo , ShmDcCommonData->pCreditCard[gunIndex].ApprovalNo,9);
- memcpy(ShmDcCommonData->TransactionInfo[gunIndex].CardNo , ShmDcCommonData->pCreditCard[gunIndex].CardNo,20);
- ShmDcCommonData->TransactionInfo[gunIndex].DeductResult = result;
- ShmDcCommonData->TransactionInfo[gunIndex].IsDonateInvoice = ShmDcCommonData->donate_flag[gunIndex];
- ShmDcCommonData->TransactionInfo[gunIndex].TransactionId = ShmSelectGunInfo->PricesInfo[gunIndex].TransactionId;
- memcpy(&ShmDcCommonData->TransactionInfo[gunIndex].VemData,&ShmDcCommonData->pCreditCard[gunIndex].VemData,64);
- }
- void PreAuthCompleteToCardReader(int fd,uint8_t gunIndex)
- {
- int result = 0;
- result = CreditCardPreAuthComplete(fd,(int)ShmDcCommonData->finalcost[pSysInfo->CurGunSelected] ,"TCC Test",
- &ShmDcCommonData->pCreditCard[gunIndex].VemData[0],
- &ShmDcCommonData->pCreditCard[gunIndex]);
-
- if (result > 0 ) {
- log_info("Credit Card Spend Money:%.1f",ShmDcCommonData->finalcost[gunIndex]);
- pSysInfo->SystemPage = _PAGE_COMPLETE;
- WritePayResult(TRUE,gunIndex);
- ShmDcCommonData->PayPass_flag[gunIndex] = TRUE;
- } else {
- log_info("PAYING FAIL");
- ShmDcCommonData->PayPass_flag[gunIndex] = FALSE;
- pSysInfo->SystemPage = _PAGE_PAYFAIL;
- WritePayResult(FALSE,gunIndex);
- }
- if (isDeductDb_ready == TRUE) {
- InsertDeductInfo(gunIndex,&ShmDcCommonData->TransactionInfo[gunIndex]);
- }
- }
- static int InitialRfidPort(void)
- {
- int fd = open(rfidPortName, O_RDWR);
- struct termios tios;
- struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
- if (fd != FAIL) {
- ioctl (fd, TCGETS, &tios);
- tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
- tios.c_lflag = 0;
- tios.c_iflag = 0;
- tios.c_oflag = 0;
- tios.c_cc[VMIN] = 0;
- tios.c_cc[VTIME] = (uint8_t) 1;
- tios.c_lflag = 0;
- tcflush(fd, TCIFLUSH);
- ioctl(fd, TCSETS, &tios);
- }
- if (fd < 0) {
- pAlarmCode->AlarmEvents.bits.RfidModuleCommFail = 1;
- }
- return fd;
- }
- void CreateRfidFork(void)
- {
- pid_t rfidRecPid;
- rfidRecPid = fork();
- if (rfidRecPid == 0) {
- char localTime[128] = {0};
- struct timeb SeqEndTime;
- struct tm *tm;
- pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
- int isContinue = 1;
- RFID rfid = {0};
- RecordTransactionInfo deduct;
- fd = InitialRfidPort();
- struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
- ShmDcCommonData = (DcCommonInfo *)GetShmDcCommonData();
- ShmSelectGunInfo = (SelectGunInfo *)GetShmSelectGunInfo();
- struct ChargingInfoData *pDcChargingInfo = NULL;
- ShmDcCommonData->reupload_deduct_status = 0;
- int gunIndex;
- int uploadIndex = 0;
- int ReAuthComplete_Index = 0;
- if(DeductDB_Open() != PASS)
- {
- isDeductDb_ready = false;
- }
- else
- {
- isDeductDb_ready = true;
- }
-
- int result;
- int is_idle = FALSE;
- while (isContinue) {
- result = 0;
- usleep(500000);
- ftime(&SeqEndTime);
- SeqEndTime.time = time(NULL);
- tm = localtime(&SeqEndTime.time);
-
- if (tm->tm_hour == 23 && tm->tm_min == 00 && tm->tm_sec == 0) {
- result = CreditCardUnionSettlement(fd,"TCC Test",&ShmDcCommonData->pCreditCard[0]);
- if (result > 0) {
- log_info("CreditCardUnionSettlement OK");
- } else
- log_info("CreditCardUnionSettlement FAIL");
- }
- if (tm->tm_min%2 == 0 && pSysInfo->SystemPage == _PAGE_IDLE) {
- if(ShmDcCommonData->reupload_deduct_status == 0) {
-
- if (DB_GetReUploadDeduct(&uploadIndex,&ShmDcCommonData->UploadRedectInfo) == PASS) {
- ShmDcCommonData->reupload_deduct_status = 1;
-
- } else {
-
- }
- } else if (ShmDcCommonData->reupload_deduct_status == 2) {
- UpdateDeductInfoStatus(&uploadIndex,&ShmDcCommonData->UploadRedectInfo);
- ShmDcCommonData->reupload_deduct_status = 0;
- }
- }
- if (pSysInfo->SystemPage == _PAGE_AUTHORIZE && ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected] == FALSE) {
- result = CreditCardSearch(fd,"TCC Test",&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected]);
-
- StartSystemTimeoutDet(Timeout_ScanCard);
- if (result > 0) {
-
- strncpy((char *)pSysConfig->UserId,ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].CardNo,20);
- log_info("Authorize card:%s",pSysConfig->UserId);
- StopSystemTimeoutDet();
- ShmDcCommonData->GetCardNo[pSysInfo->CurGunSelected] = TRUE;
- } else if (result < 0) {
-
- log_info("No Card Scanning...");
- }
- }
- is_idle = FALSE;
- for (gunIndex = 0; gunIndex < pSysConfig->TotalConnectorCount; gunIndex++) {
- pDcChargingInfo = (struct ChargingInfoData *)GetDcChargingInfoData(gunIndex);
- if (pDcChargingInfo->SystemStatus == S_IDLE) {
- is_idle = TRUE;
- }
- if (ShmDcCommonData->StopCharge[gunIndex] == TRUE && ShmDcCommonData->finalcost_flag[gunIndex]) {
- if (ShmDcCommonData->finalcost[gunIndex] <= 0) {
- } else {
- ShmDcCommonData->StopCharge[gunIndex] = FALSE;
- StopGunInfoTimeoutDet(gunIndex);
- PreAuthCompleteToCardReader(fd,gunIndex);
- ShmDcCommonData->PayFinish[gunIndex] = TRUE;
- }
- }
- }
- if (is_idle == TRUE && tm->tm_min%15 == 0 &&
- pSysInfo->SystemPage == _PAGE_IDLE && ShmDcCommonData->reupload_deduct_status == 0) {
- sleep(1);
- if (DB_GetReDeductInfo(&ReAuthComplete_Index,&ShmDcCommonData->ReAuthComplete) == PASS ) {
- result = CreditCardPreAuthComplete(fd,(int)ShmDcCommonData->ReAuthComplete.Amount ,"TCC Test",
- &ShmDcCommonData->ReAuthComplete.VemData[0],
- &ShmDcCommonData->pCreditCard[0]);
- if (result > 0) {
- ShmDcCommonData->ReAuthComplete.DeductResult = YES;
- ShmDcCommonData->ReAuthComplete.IsUpload = NO;
- UpdateDeductInfoStatus(&ReAuthComplete_Index,&ShmDcCommonData->ReAuthComplete);
- log_info("PreAuthComplete OK");
- } else if ( result < 0) {
- log_info("Backgroud ID:%d Amount:%d VemData:%s PreAuthComplete fail",
- ShmDcCommonData->ReAuthComplete.TransactionId,
- (int)ShmDcCommonData->ReAuthComplete.Amount,
- ShmDcCommonData->ReAuthComplete.VemData);
- }
- } else {
- log_info("DB_GetReDeductInfo Fail");
- }
- }
- if(ShmDcCommonData->PreAuth_Config == _CREDITCARD_CANCEL) {
- ShmDcCommonData->PreAuth_Result = CreditCardPreAuthCancel(fd,PREAUTHMONEY,"TCC Test",
- &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].ApprovalNo[0],
- &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].CardNo[0],
- &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected].VemData[0]);
- if (ShmDcCommonData->PreAuth_Result > 0 ) {
- strcpy((char *)pSysConfig->UserId,"");
- log_info("Credit Card Cancel Stop");
- memset(&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected], 0, sizeof(TransInfo));
- } else if (ShmDcCommonData->PreAuth_Result < 0) {
- log_info("Credit Card Cancel Fail");
- }
- ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
- } else if (ShmDcCommonData->PreAuth_Config == _CREDITCARD_PREAUTH) {
- result = CreditCardPreAuth(fd, PREAUTHMONEY,"TCC Test", &ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected]);
-
- if (result > 0) {
- ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] = TRUE;
-
-
- log_info("PreAuth OK");
- } else if (result < 0) {
- struct ChargingInfoData *pDcChargingInfo = NULL;
-
- pSysInfo->SystemPage = _PAGE_AUTHORIZE_FAIL;
-
- ShmDcCommonData->AuthPass_flag[pSysInfo->CurGunSelected] = FALSE;
- memset(&ShmDcCommonData->pCreditCard[pSysInfo->CurGunSelected], 0, sizeof(TransInfo));
- log_info("PreAuth Fail");
- ShmDcCommonData->PreAuth_Config = _CREDITCARD_CANCEL;
- }
- ShmDcCommonData->PreAuth_Config = _CREDITCARD_IDLE;
- }
-
- }
- }
- }
|