Przeglądaj źródła

[Add][AW-CCS][main]

    2021.05.21 / Folus Wen

    Actions:
    1. Charging record insert to local db if power drop.
    2. Charging receipt info create to JSON string.

    Files:
    1. As follow commit history

    Image version: D0.53.XX.XXXX.XX
    Image checksum: XXXXXXXX

    Hardware PWB P/N : XXXXXXX
    Hardware Version : XXXXXXX
FolusWen 3 lat temu
rodzic
commit
6450ac032a
2 zmienionych plików z 240 dodań i 10 usunięć
  1. 3 2
      EVSE/Projects/AW-CCS/Apps/Makefile
  2. 237 8
      EVSE/Projects/AW-CCS/Apps/main.c

+ 3 - 2
EVSE/Projects/AW-CCS/Apps/Makefile

@@ -6,6 +6,7 @@ Lib_Module_RFID = "-L../../../Modularization" -lModule_RFID
 Lib_Module_Upgrade = "-L../../../Modularization" -lModule_Upgrade
 Lib_Module_RatedCurrent = "-L../../../Modularization" -lModule_RatedCurrent
 Lib_SQLite3 = "-L../../../Modularization/ocppfiles" -lsqlite3
+Lib_JSONC = "-L../../../GPL/json-c-json-c-0.13.1-20180305/release/lib" -ljson-c
 
 EXI_ENGINE= CCS/v2g/api/api.c \
 	    CCS/v2g/appHandshake/appHandEXIDatatypes.c \
@@ -103,8 +104,8 @@ Module_AlarmDetect_Task:
 Module_CSU_Task:
 	@echo "===== Module_CSU_Task ============================================"
 	rm -f main 
-	$(CC) -D $(Project) "-I../../" "-include../../../Modularization/ocppfiles/sqlite3.h" "-include../../../Modularization/Module_Upgrade.h" "-include../../../Modularization/Module_RFID.h" "-include../../../Modularization/Module_RatedCurrent.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} ${Lib_Module_RatedCurrent}
+	$(CC) -D $(Project) "-I../../" "-include../../../GPL/json-c-json-c-0.13.1-20180305/release/include/json-c/json.h" "-include../../../Modularization/ocppfiles/sqlite3.h" "-include../../../Modularization/Module_Upgrade.h" "-include../../../Modularization/Module_RFID.h" "-include../../../Modularization/Module_RatedCurrent.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} ${Lib_Module_RatedCurrent} ${Lib_JSONC}
 	rm -f *.o
 	mv -f main ../Images/root		
 	@echo \

+ 237 - 8
EVSE/Projects/AW-CCS/Apps/main.c

@@ -1194,6 +1194,42 @@ int CreatShareMemory()
 //===============================================
 // SQLite3 related routine
 //===============================================
+int getReceiptInfo(uint8_t gun_index, char *receiptInfo)
+{
+	int result = PASS;
+	double totalEnergy=0.0;
+	double totalPrice=0.0;
+
+	json_object *receipt = json_object_new_object();
+	json_object *receiptAry = json_object_new_array();
+
+	for(uint8_t idxHour=0;idxHour<ARRAY_SIZE(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod);idxHour++)
+	{
+		json_object *receiptDetail = json_object_new_object();
+
+		json_object_object_add(receiptDetail, "hour", json_object_new_int(idxHour));
+		json_object_object_add(receiptDetail, "chargedEnergy", json_object_new_double(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[idxHour]));
+		json_object_object_add(receiptDetail, "periodPrice", json_object_new_double(ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[idxHour]*ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour]));
+
+		totalEnergy += ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[idxHour];
+		totalPrice += ShmCharger->gun_info[gun_index].presentChargedEnergyPeriod[idxHour]*ShmSysConfigAndInfo->SysConfig.BillingData.Fee[idxHour];
+
+		json_object_array_add(receiptAry, receiptDetail);
+	}
+	json_object_object_add(receipt, "receiptDetail", receiptAry);
+	json_object_object_add(receipt, "totalChargedEnergy", json_object_new_double(totalEnergy));
+	json_object_object_add(receipt, "totalPrice", json_object_new_double(totalPrice));
+	json_object_object_add(receipt, "serviceProvider", json_object_new_string((char*)ShmSysConfigAndInfo->SysConfig.chargePointVendor));
+	json_object_object_add(receipt, "serviceProviderAddress", json_object_new_string("Address"));
+	json_object_object_add(receipt, "serviceProviderTel", json_object_new_string("TEL"));
+	json_object_object_add(receipt, "chargerBoxId", json_object_new_string((char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
+
+	sprintf(receiptInfo, "%s", json_object_to_json_string_ext(receipt, JSON_C_TO_STRING_PLAIN));
+	json_object_put(receipt);
+
+	return result;
+}
+
 int DB_Open(sqlite3 *db)
 {
 	int result = PASS;
@@ -1213,6 +1249,20 @@ int DB_Open(sqlite3 *db)
 						  "finalCost 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, "
+						  "finalCost text"
+						  ");";
+
 	char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
 					   "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
 					   "`item` TEXT NOT NULL, "
@@ -1240,6 +1290,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;
@@ -1256,11 +1316,166 @@ int DB_Open(sqlite3 *db)
 	return result;
 }
 
+int DB_Check_Record_Buf(sqlite3 *db, int gun_index)
+{
+	int result = PASS;
+	char* errMsg = NULL;
+	char sqlStr[4096];
+	char **rs;
+	int	 rows, cols;
+
+	if(sqlite3_open(DB_FILE, &db))
+	{
+		result = FAIL;
+		DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
+		sqlite3_close(db);
+	}
+	else
+	{
+		sqlite3_get_table(db, "select * from charging_record_buffer", &rs, &rows, &cols, &errMsg);
+
+		if(rows>0)
+		{
+			// Copy record buffer
+			for(int idxRow=1;idxRow<=rows;idxRow++)
+			{
+				if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
+				{
+					result = false;
+				}
+
+				sprintf(sqlStr, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
+		 					    "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');",
+								rs[(idxRow*cols)+0],
+								rs[(idxRow*cols)+1],
+								rs[(idxRow*cols)+2],
+								rs[(idxRow*cols)+3],
+								rs[(idxRow*cols)+4],
+								rs[(idxRow*cols)+5],
+								rs[(idxRow*cols)+6],
+								rs[(idxRow*cols)+7],
+								rs[(idxRow*cols)+8],
+								rs[(idxRow*cols)+9],
+								rs[(idxRow*cols)+10]);
+
+				if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
+				{
+					result = FAIL;
+					DEBUG_INFO( "Copy local charging record buffer error message: %s\n", errMsg);
+				}
+				else
+				{
+					DEBUG_INFO( "Copy local charging record buffer 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");
+			}
+		}
+		else
+		{
+			DEBUG_INFO("There is not any charging record buffer.\n", gun_index);
+		}
+
+		sqlite3_free_table(rs);
+		sqlite3_close(db);
+	}
+
+	return result;
+}
+
+int DB_Update_Record_Buf(sqlite3 *db, int gun_index)
+{
+	int result = PASS;
+	char* errMsg = NULL;
+	char sqlStr[4096];
+
+	if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
+	{
+		sprintf(sqlStr, "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
+ 					    "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%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,
+					    "Power Loss",
+					    ShmOCPP16Data->Cost.FinalCost[gun_index].description);
+	}
+	else if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_20)
+	{
+		sprintf(sqlStr, "insert into charging_record_buffer(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason, finalCost) "
+ 					    "values('%d', '%s', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s', '%f');",
+					    ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
+					    ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.transactionId,
+					    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,
+					    "Power Loss",
+					    ShmOCPP20Data->CostUpdated.totalCost);
+	}
+
+	if(sqlite3_open(DB_FILE, &db))
+	{
+		result = FAIL;
+		DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
+		sqlite3_close(db);
+	}
+	else
+	{
+		// 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 record buffer
+		if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
+		{
+			result = FAIL;
+			DEBUG_INFO( "Insert local charging record buffer error message: %s\n", errMsg);
+		}
+		else
+		{
+			DEBUG_INFO( "Insert local charging record buffer successfully\n");
+		}
+
+		sqlite3_close(db);
+	}
+
+	return result;
+}
+
 int DB_Insert_Record(sqlite3 *db, int gun_index)
 {
 	int result = PASS;
 	char* errMsg = NULL;
-	char sqlStr[1024];
+	char sqlStr[4096];
+	char receiptInfo[2048];
+
+	getReceiptInfo(gun_index, receiptInfo);
+	DEBUG_INFO("Receipt Info: %s\n", receiptInfo);
 
 	if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
 	{
@@ -1294,6 +1509,7 @@ int DB_Insert_Record(sqlite3 *db, int gun_index)
 					    ShmOCPP20Data->TransactionEvent[gun_index].transactionInfo.stoppedReason,
 					    ShmOCPP20Data->CostUpdated.totalCost);
 	}
+	//DEBUG_INFO("sqlStr= %s\n", sqlStr);
 
 	if(sqlite3_open(DB_FILE, &db))
 	{
@@ -1303,7 +1519,18 @@ 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;
@@ -1346,10 +1573,8 @@ int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
 	}
 	else
 	{
-		DEBUG_INFO( "Local charging record database open successfully.\n");
-
 		sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
-		DEBUG_INFO("sqlStr= %s\n", sqlStr);
+		//DEBUG_INFO("sqlStr= %s\n", sqlStr);
 		if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
 		{
 			result = FAIL;
@@ -1385,7 +1610,6 @@ int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
 	}
 	else
 	{
-		DEBUG_INFO( "Local config query database open successfully.\n");
 		sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
 
 		if(rows>0)
@@ -1411,6 +1635,7 @@ int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
 	return result;
 }
 
+
 //======================================================
 // Peripheral initial
 //======================================================
@@ -2770,7 +2995,7 @@ void setRequest(unsigned char gun_index,unsigned char isOn)
 		if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
 		{
 			ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
-			DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
+			DEBUG_INFO("Gun-%d permission request: ON \n",gun_index);
 		}
 	}
 	else
@@ -2778,7 +3003,7 @@ void setRequest(unsigned char gun_index,unsigned char isOn)
 		if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
 		{
 			ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
-			DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
+			DEBUG_INFO("Gun-%d permission request: OFF \n",gun_index);
 		}
 	}
 }
@@ -4429,6 +4654,7 @@ int main(void)
 						ShmCharger->gun_info[gun_index].isDoEvReadyOnce = OFF;
 						ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
 						if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
+						DB_Check_Record_Buf(localDb, gun_index);
 					}
 
 					if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
@@ -5109,6 +5335,9 @@ int main(void)
 							DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
 							DEBUG_INFO("===============================================================\n");
 							ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
+							
+							getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
+							DB_Update_Record_Buf(localDb, gun_index);
 						}
 					}