Explorar o código

[Improve][Added][AW-Regular][main.c]
2022-03-08 / Eason Yang
Action
1. Added: DB_Open() function. add createRecordBufSql database.
2. Added: DB_Insert_Record() function. delet buffer function.
3. Improve: PresentChargedDuration keeps couting when the system is under terminated mode.
4. Improve: Reconnection logic for etehrent. After retrying 3 times and checking OCPP status.

File
1. main.c
Action 1
Action 2
Action 3
Action 4

FIRMWARE VERSION:V0.70.XX.XXXX.PX

8009 %!s(int64=3) %!d(string=hai) anos
pai
achega
d8f31631d0
Modificáronse 1 ficheiros con 105 adicións e 8 borrados
  1. 105 8
      EVSE/Projects/AW-Regular/Apps/main.c

+ 105 - 8
EVSE/Projects/AW-Regular/Apps/main.c

@@ -344,6 +344,19 @@ void getDateTimeString(char* result)
 	sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
 }
 
+int getCurrentYear()
+{
+	int result = 0;
+	time_t CurrentTime;
+	struct tm *tm;
+
+	CurrentTime = time(NULL);
+	tm=localtime(&CurrentTime);
+	result = (tm->tm_year + 1900);
+
+	return result;
+}
+
 unsigned long long getAvailableMemory()
 {
     long pages = sysconf(_SC_AVPHYS_PAGES);
@@ -1355,6 +1368,19 @@ int DB_Open(sqlite3 *db)
 						  "stopReason text"
 						  ");";
 
+	char* createRecordBufSql="CREATE TABLE IF NOT EXISTS charging_record_buffer("
+						  "reservationId text, "
+						  "transactionId text, "
+						  "startMethod text, "
+						  "userId text, "
+						  "dateTimeStart text, "
+						  "dateTimeStop text,"
+						  "socStart text, "
+						  "socStop text, "
+						  "chargeEnergy text, "
+						  "stopReason text"
+						  ");";
+
 	char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
 					   "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
 					   "`item` TEXT NOT NULL, "
@@ -1382,6 +1408,16 @@ int DB_Open(sqlite3 *db)
 			DEBUG_INFO( "Opened local charging record table successfully\n");
 		}
 
+		if (sqlite3_exec(db, createRecordBufSql, 0, 0, &errMsg) != SQLITE_OK)
+		{
+			result = FAIL;
+			DEBUG_ERROR( "Create local charging record buffer table error message: %s\n", errMsg);
+		}
+		else
+		{
+			DEBUG_INFO( "Opened local charging record buffer table successfully\n");
+		}
+
 		if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
 		{
 			result = FAIL;
@@ -1444,6 +1480,19 @@ int DB_Insert_Record(sqlite3 *db, int gun_index)
 	else
 	{
 		DEBUG_INFO( "Local charging record database open successfully.\n");
+
+		// Delete buffer
+		if(sqlite3_exec(db, "delete from charging_record_buffer", 0, 0, &errMsg) != SQLITE_OK)
+		{
+			result = FAIL;
+			DEBUG_INFO( "Delete local charging record buffer error message: %s\n", errMsg);
+		}
+		else
+		{
+			DEBUG_INFO( "Delete local charging record buffer successfully\n");
+		}
+
+		// Insert charging record
 		if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
 		{
 			result = FAIL;
@@ -1454,7 +1503,7 @@ int DB_Insert_Record(sqlite3 *db, int gun_index)
 			DEBUG_INFO( "Insert local charging record successfully\n");
 		}
 
-		sprintf(sqlStr, "delete from charging_record where idx < (select idx from charging_record order by idx desc limit 1)-2000;");
+		sprintf(sqlStr, "delete from charging_record where (idx < (select idx from charging_record order by idx desc limit 1)-2000) and (dateTimeStop < '%04d.01.01 00:00:00');", (getCurrentYear()-3));
 		if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
 		{
 			result = FAIL;
@@ -1490,7 +1539,7 @@ int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
 
 		sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
 		DEBUG_INFO("sqlStr= %s\n", sqlStr);
-		if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
+		if(sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
 		{
 			result = FAIL;
 			DEBUG_INFO( "update config error message: %s\n", errMsg);
@@ -1912,7 +1961,7 @@ void InitEthernet()
 	pid_t pid;
 	uint8_t cnt_pingDNS_Fail;
 	char tmpbuf[256];
-	//unsigned int address;
+	unsigned int natInterface = 0;
 
 	//Init Eth0 for internet
 	memset(tmpbuf,0,256);
@@ -2060,6 +2109,23 @@ void InitEthernet()
 				{
 					system("/sbin/ifmetric ppp0 2");
 				}
+
+				if(isKernelSupportNAT() == YES)
+				{
+					if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
+					{
+						if(natInterface != 1)
+						{
+							system("/sbin/iptables -t nat -F");
+							system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o eth0 -j MASQUERADE");
+							natInterface = 1;
+						}
+					}
+					else
+					{
+						system("/sbin/iptables -t nat -F");
+					}
+				}
 			}
 			else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
 			{
@@ -2086,6 +2152,34 @@ void InitEthernet()
 					system("/sbin/ifmetric eth0 1");
 					system("/sbin/ifmetric ppp0 0");
 				}
+
+				if(isKernelSupportNAT() == YES)
+				{
+					if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
+					{
+						if(natInterface != 2)
+						{
+							system("/sbin/iptables -t nat -F");
+							system("/sbin/iptables -A POSTROUTING -t nat -s 192.168.10.0/24 -o ppp0 -j MASQUERADE");
+							natInterface = 2;
+						}
+					}
+					else
+					{
+						system("/sbin/iptables -t nat -F");
+					}
+				}
+			}
+			else
+			{
+				if(isKernelSupportNAT() == YES)
+				{
+					if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2)
+					{
+						system("/sbin/iptables -t nat -F");
+						natInterface = 0;
+					}
+				}
 			}
 
 			sleep(5);
@@ -4404,6 +4498,7 @@ int main(void)
 							memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
 							ocpp_copy_userid_to_starttransaction(gun_index);
 							ocpp_set_starttransaction_req(gun_index, ON);
+							ftime(&startChargingTime[gun_index]);
 
 							setChargerMode(gun_index, SYS_MODE_CHARGING);
 						}
@@ -4414,7 +4509,7 @@ int main(void)
 					if(isModeChange(gun_index))
 					{
 						ShmCharger->gun_info[gun_index].rfidReq = OFF;
-						ftime(&startChargingTime[gun_index]);
+						//ftime(&startChargingTime[gun_index]);
 						ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
 						ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
 						ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
@@ -4706,10 +4801,6 @@ int main(void)
 					if(isModeChange(gun_index))
 					{
 						setLedMotion(gun_index, LED_ACTION_STOP);
-						if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
-						{
-							ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
-						}
 
 						getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
 						startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
@@ -4720,6 +4811,12 @@ int main(void)
 						sleep(1);
 					}
 
+					ftime(&endChargingTime[gun_index]);
+					//if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
+					//{
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
+					//}
+
 					// End authorize pass
 					if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
 					   (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||