Browse Source

2020.01.30 / Folus Wen

Actions:
1. Local charging record function implement.
2. Charging cycle info update function implement.

Files:
1. As follow commit history

Image version: D0.04.XX.XXXX.PH
Image checksum: XXXXXXXX

Hardware PWB P/N : XXXXXXX
Hardware Version : XXXXXXX
FolusWen 5 years ago
parent
commit
e9f1bdb02a

BIN
EVSE/Modularization/Module_4g


BIN
EVSE/Modularization/Module_Wifi


BIN
EVSE/Modularization/OcppBackend


BIN
EVSE/Modularization/WebService


BIN
EVSE/Modularization/libInfypwr_PsuCommObj.a


BIN
EVSE/Modularization/libModule_RFID.a


BIN
EVSE/Modularization/libModule_Upgrade.a


BIN
EVSE/Modularization/libPhihong_PsuCommObj.a


BIN
EVSE/Modularization/logPackTools


+ 4 - 3
EVSE/Projects/AW-Regular/Apps/Makefile

@@ -2,8 +2,9 @@
 export PATH=/bin:/sbin:/usr/bin:$(SDK_PATH_TARGET)/usr/bin:$PATH
 
 #define library variable
-Lib_Module_RFID = -L../../../Modularization -lModule_RFID
+Lib_Module_RFID = "-L../../../Modularization" -lModule_RFID
 Lib_Module_Upgrade = "-L../../../Modularization" -lModule_Upgrade
+Lib_SQLite3 = "-L../../../Modularization/ocppfiles" -lsqlite3
 
 all: CopyFile apps
 apps: Module_InternalComm_Task Module_FactoryConfig_Task Module_EventLogging_Task Module_AlarmDetect_Task Module_CSU_Task Module_Speaker_Task
@@ -53,8 +54,8 @@ Module_AlarmDetect_Task:
 Module_CSU_Task:
 	@echo "===== Module_CSU_Task ============================================"
 	rm -f main 
-	$(CC) "-I../../" "-include../../../Modularization/Module_Upgrade.h" "-include../../../Modularization/Module_RFID.h" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "./main.c"
-	$(CC) -o main main.o ${Lib_Module_RFID} ${Lib_Module_Upgrade}
+	$(CC) "-I../../" "-include../../../Modularization/ocppfiles/sqlite3.h" "-include../../../Modularization/Module_Upgrade.h" "-include../../../Modularization/Module_RFID.h" -O0 -g3 -Wall -c -fmessage-length=0 -o main.o "./main.c"
+	$(CC) -o main main.o ${Lib_Module_RFID} ${Lib_Module_Upgrade} ${Lib_SQLite3}
 	rm -f *.o
 	mv -f main ../Images/root		
 	@echo \

+ 1 - 1
EVSE/Projects/AW-Regular/Apps/Module_FactoryConfig.c

@@ -268,7 +268,7 @@ int main(int argc, char *argv[])
 	SysConfig.OfflinePolicy = 0;			// 0: Local list	1: PH RFID		2: Free		3: Deny
 	SysConfig.OfflineMaxChargeEnergy = 0;	// 0: Same as MaxChargeEnergy	Other: 1~65535KWH
 	SysConfig.OfflineMaxChargeDuration = 0; // 0: Same as MaxChargeDuration Other: 1~65535 minutes
-	strcpy((char*)SysConfig.OcppServerURL, "ws://192.168.0.246:8080/ocpp/");
+	strcpy((char*)SysConfig.OcppServerURL, "ws://test.evsocket.phihong.com.cn:2012/");
 	sprintf((char*)SysConfig.ChargeBoxId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
 
 

+ 122 - 7
EVSE/Projects/AW-Regular/Apps/main.c

@@ -78,7 +78,7 @@ struct Charger					*ShmCharger;
 struct timeb					startTime[AC_QUANTITY][10];
 struct timeb					startChargingTime[AC_QUANTITY];
 struct timeb					endChargingTime[AC_QUANTITY];
-
+sqlite3 *localDb;
 
 //=================================
 // Common routine
@@ -184,6 +184,17 @@ int DiffTimeb(struct timeb ST, struct timeb ET)
 	return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
 }
 
+void getDateTimeString(char* result)
+{
+	time_t CurrentTime;
+	struct tm *tm;
+
+	CurrentTime = time(NULL);
+	tm=localtime(&CurrentTime);
+
+	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);
+}
+
 //======================================================
 // Check interface status
 //======================================================
@@ -393,6 +404,96 @@ int CreatShareMemory()
     return result;
 }
 
+//===============================================
+// SQLite3 related routine
+//===============================================
+int DB_Open(sqlite3 *db)
+{
+	int result = PASS;
+	char* errMsg = NULL;
+	char* createSql="CREATE TABLE IF NOT EXISTS charging_record("
+					"idx integer primary key AUTOINCREMENT, "
+					"reservationId text, "
+					"transactionId text, "
+					"startMethod text, "
+					"userId text, "
+					"dateTimeStart text, "
+					"dateTimeStop text,"
+					"socStart text, "
+					"socStop text, "
+					"chargeEnergy text, "
+					"stopReason text"
+					");";
+
+	//sqlite3_config(SQLITE_CONFIG_URI, 1);
+	if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
+	{
+		result = FAIL;
+		DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
+		sqlite3_close(db);
+	}
+	else
+	{
+		DEBUG_INFO( "Local charging record database open successfully.\r\n");
+		if (sqlite3_exec(db, createSql, 0, 0, &errMsg) != SQLITE_OK)
+		{
+			result = FAIL;
+			DEBUG_INFO( "Create local charging record table error message: %s\n", errMsg);
+		}
+		else
+		{
+			DEBUG_INFO( "Opened local charging record table successfully\n");
+		}
+		sqlite3_close(db);
+	}
+
+	return result;
+}
+
+int DB_Insert_Record(sqlite3 *db, int gun_index)
+{
+	int result = PASS;
+	char* errMsg = NULL;
+	char insertSql[1024];
+
+	sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
+					   "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
+					   ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
+					   ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
+					   ShmOCPP16Data->StopTransaction[gun_index].StopReason);
+
+	if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
+	{
+		result = FAIL;
+		DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
+		sqlite3_close(db);
+	}
+	else
+	{
+		DEBUG_INFO( "Local charging record database open successfully.\r\n");
+		if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
+		{
+			result = FAIL;
+			DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
+		}
+		else
+		{
+			DEBUG_INFO( "Insert local charging record successfully\n");
+		}
+		sqlite3_close(db);
+	}
+
+	return result;
+}
+
+
 //======================================================
 // Peripheral initial
 //======================================================
@@ -778,6 +879,8 @@ int StoreUsrConfigData(struct SysConfigData *UsrData)
 
 int Initialization()
 {
+	int result = PASS;
+
 	LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
 
 	InitGPIO();
@@ -786,9 +889,15 @@ int Initialization()
 
 	rfidFd = InitRfidPort();
 
-	DEBUG_INFO("Initialization OK\r\n");
+	if((DB_Open(localDb) != PASS) || (rfidFd < 0))
+		result = FAIL;
 
-	return PASS;
+	if(result == PASS)
+		DEBUG_INFO("Initialization OK.\r\n");
+	else
+		DEBUG_INFO("Initialization Fail.\r\n");
+
+	return result;
 }
 
 //=====================================================
@@ -1623,7 +1732,6 @@ int main(void)
 						ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
 						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
 						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
-						setLedMotion(gun_index,LED_ACTION_IDLE);
 					}
 
 					if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_FREE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
@@ -1756,7 +1864,6 @@ int main(void)
 
 					if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
 					{
-
 						 // Authorization timeout process.
 						ShmCharger->gun_info[gun_index].rfidReq = OFF;
 						setChargerMode(gun_index, SYS_MODE_IDLE);
@@ -1823,7 +1930,9 @@ int main(void)
 					else if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
 					{
 						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].OutputEnergy = 0;
-						ShmOCPP16Data->StartTransaction[gun_index].MeterStart = ((float)ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
+						getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
+						ShmOCPP16Data->StartTransaction[gun_index].MeterStart = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
 						memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
 
 						memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
@@ -1971,7 +2080,8 @@ int main(void)
 						ShmOCPP16Data->StopTransaction[gun_index].MeterStop = ((float)ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0) - ShmOCPP16Data->StartTransaction[gun_index].MeterStart;
 						ftime(&endChargingTime[gun_index]);
 						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index]);
-
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmOCPP16Data->StartTransaction[gun_index].MeterStart)/100.0;
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].OutputEnergy = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmOCPP16Data->StartTransaction[gun_index].MeterStart)/100.0;
 						// Charging profile
 
 					}
@@ -2027,7 +2137,10 @@ int main(void)
 							}
 
 							memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
+							ShmOCPP16Data->StopTransaction[gun_index].MeterStop = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
 							ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
+
+							getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
 						}
 
 						setRelay(gun_index,OFF);
@@ -2039,6 +2152,7 @@ int main(void)
 							ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
 							ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
 
+							DB_Insert_Record(localDb, gun_index);
 							setChargerMode(gun_index, SYS_MODE_IDLE);
 						}
 					}
@@ -2160,6 +2274,7 @@ int main(void)
 								sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
 								ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
 								DEBUG_WARN("Firmware upgrade fail.\r\n");
+								system("/usr/bin/run_evse_restart.sh");
 							}
 						}
 					}

+ 1 - 0
EVSE/Projects/AW-Regular/Apps/main.h

@@ -39,6 +39,7 @@
 #include	<stdbool.h>
 #include	<stddef.h>
 #include	<stdint.h>
+#include	<sqlite3.h>
 
 //===================================
 //	Define CP State constant

BIN
EVSE/Projects/AW-Regular/Images/FactoryDefaultConfig.bin


BIN
EVSE/Projects/AW-Regular/Images/ramdisk.gz


BIN
EVSE/rootfs/root/Module_4g


BIN
EVSE/rootfs/root/Module_Wifi


BIN
EVSE/rootfs/root/OcppBackend


BIN
EVSE/rootfs/root/WebService


BIN
EVSE/rootfs/root/logPackTools