Эх сурвалжийг харах

[Improve][Modularization][Module_OcppBackend / Module_OcppBackend20 / logPackTools]

2021.04.01 / Folus Wen

Actions:
1. Log pack to system.zip with password and charging_event.zip without password.

Files:
1. As follow commit history

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

Hardware PWB P/N : XXXXXXX
Hardware Version : XXXXXXX
FolusWen 4 жил өмнө
parent
commit
50701f2a04

+ 1 - 1
EVSE/Modularization/Makefile

@@ -53,7 +53,7 @@ WebServiceLib:
 	$(CC) -D $(Project) -I ../GPL/json-c-json-c-0.13.1-20180305/release/include/json-c -I ../Projects -O0 -g3 -Wall -c -fmessage-length=0 -o WebService.o -ljson-c -lm -w WebService.c
 	$(CC) -L ../GPL/json-c-json-c-0.13.1-20180305/release/lib -o WebService WebService.o -ljson-c -lm
 	rm -f WebService.o
-	$(CC) -D $(Project) -O0 -g3 -Wall -c -fmessage-length=0 -o logPackTools.o logPackTools.c
+	$(CC) -D $(Project) -I ../Projects -O0 -g3 -Wall -c -fmessage-length=0 -o logPackTools.o logPackTools.c
 	$(CC) -o logPackTools logPackTools.o
 	rm -f logPackTools.o
 	mv -f WebService ../rootfs/root/

+ 90 - 48
EVSE/Modularization/logPackTools.c

@@ -15,20 +15,21 @@
 
 #include 	<unistd.h>
 #include 	<stdarg.h>
-#include    <stdio.h>      /*標準輸入輸出定義*/
-#include    <stdlib.h>     /*標準函數庫定義*/
-#include    <unistd.h>     /*Unix 標準函數定義*/
-#include    <fcntl.h>      /*檔控制定義*/
-#include    <termios.h>    /*PPSIX 終端控制定義*/
-#include    <errno.h>      /*錯誤號定義*/
+#include    <stdio.h>
+#include    <stdlib.h>
+#include    <unistd.h>
+#include    <fcntl.h>
+#include    <termios.h>
+#include    <errno.h>
 #include 	<errno.h>
 #include 	<string.h>
 #include	<time.h>
 #include	<ctype.h>
 #include 	<ifaddrs.h>
-#include	"../Projects/define.h"
+#include 	<math.h>
+#include	"define.h"
+
 
-#include <math.h>
 #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
 #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
 #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
@@ -46,7 +47,7 @@ int mystrcmp(char *p1, char *p2);
 void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
 void split(char **arr, char *str, const char *del);
 
-#ifdef SystemLogMessage
+
 int StoreLogMsg(const char *fmt, ...) {
 	char Buf[4096 + 256];
 	char buffer[4096];
@@ -66,7 +67,7 @@ int StoreLogMsg(const char *fmt, ...) {
 			tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
 			tm->tm_min, tm->tm_sec, buffer, tm->tm_year + 1900, tm->tm_mon + 1);
 	system(Buf);
-#ifdef Debug
+#ifdef ConsloePrintLog
 	printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year + 1900,
 			tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
 			buffer);
@@ -74,7 +75,6 @@ int StoreLogMsg(const char *fmt, ...) {
 
 	return rc;
 }
-#endif
 
 int DiffTimeb(struct timeb ST, struct timeb ET) {
 	//return milli-second
@@ -177,6 +177,7 @@ int isDirectory(const char *path) {
 		return 0;
 	return S_ISDIR(statbuf.st_mode);
 }
+
 //==========================================
 // Init all share memory
 //==========================================
@@ -221,39 +222,45 @@ int InitShareMemory() {
 //================================================
 // Main process
 //================================================
-int main(int argc, char *argv[]) {
+int main(int argc, char *argv[])
+{
 	int CCSID=0;
 	int isCCS=isDirectory("/Storage/root");
-	if(isCCS==0){
-		if (InitShareMemory() == FAIL) {
-			#ifdef SystemLogMessage
-				DEBUG_ERROR("InitShareMemory NG\n");
-			#endif
-			if (ShmStatusCodeData != NULL) {
+	if(isCCS==0)
+	{
+		if (InitShareMemory() == FAIL)
+		{
+			DEBUG_ERROR("InitShareMemory NG\n");
+
+			if (ShmStatusCodeData != NULL)
+			{
 				ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
 			}
 			sleep(5);
 			return 0;
 		}
 	}
-	if(strcmp(argv[1], "log") == 0){
-		if(isCCS==1){
-			//get ip to distinguish main or ccs
+
+	if(strcmp(argv[1], "log") == 0)
+	{
+		if(isCCS==1)
+		{
+			// get ip to distinguish main or ccs
 			char MyIpBuf[32]={0};
 			FILE *fpRead;
 
-			//使用shell command來取得ip值    
+			// Get IP address by shell command
 			char* command=(char*)"ifconfig eth0|grep 'inet addr' |awk -F \":\" '{print $2}' |awk '{print $1}'";
 			char* renewCh;
 
 			fpRead = popen(command, "r");
 			fgets(MyIpBuf, 32 , fpRead);
 
-			//記得作pclose()的動作
+			// Have to pclose()
 			if(fpRead != NULL)
 				pclose(fpRead);
-		 
-			//最後檢查取出的字串當中是否有多餘的換行,若有直接取代為'\0'作結尾
+
+			// Replace \n to '0'
 			renewCh=strstr(MyIpBuf,"\n");
 			if(renewCh)
 				*renewCh= '\0';
@@ -269,62 +276,96 @@ int main(int argc, char *argv[]) {
 
 		unsigned char			ModelName[64];;
 		unsigned char			SerialNo[64];;
-		if(isCCS==0){
+		if(isCCS==0)
+		{
 			memcpy(ModelName,ShmSysConfigAndInfo->SysConfig.ModelName,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
 			printf("%s", ModelName);
 			memcpy(SerialNo,ShmSysConfigAndInfo->SysConfig.SerialNumber,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
 			printf("%s", SerialNo);
 		}
-		if(CCSID!=0){
+
+		if(CCSID!=0)
+		{
 			sprintf((char*)ModelName,"CCS%d",CCSID);
 			printf("%s", ModelName);
 		}
+
 		int year,month,i,ty,tm,cnt;
 		char pwd[70];
-		char cmd[1024];
-		time_t tt = time(0); //獲取當前時間
-		struct tm *pst = localtime(&tt); //把time_t類型轉換為struct tm類型
+		char cmd[2048];
+		time_t tt = time(0); // Get current time
+		struct tm *pst = localtime(&tt); // Transfer time_t to struct tm
 		year = pst->tm_year + 1900;
 		month = pst->tm_mon + 1;
-		// sprintf(cmd,"rm -f /mnt/%s-%s-*.zip \n",ModelName,SerialNo);
-		// system(cmd);
+
 		system("rm -f /mnt/*.zip");
-		if((argc == 3) && isdigit(*argv[2])){
+		if((argc == 3) && isdigit(*argv[2]))
+		{
 			cnt=atoi(argv[2]);
 		}
-		else{
+		else
+		{
 			cnt=6;
 		}
-		if(isCCS==0){
+
+		if(isCCS==0)
+		{
 			sprintf(pwd," --password %04d%02d%s",year,month,SerialNo);
 		}
-		else{
-//			strcpy(pwd,"");
-		}
-		// sprintf(cmd,"zip -9 %s \t /mnt/%s-%s-%04d%02d%02d%02d%02d%02d.zip",pwd,ModelName,SerialNo,year,month,day,hour,min,sec);
-		sprintf(cmd,"zip -9 %s \t /mnt/log.zip",pwd);
-		for(i=0;i<cnt;i++){
-			if(month-i<1){
+		else
+		{}
+
+		// Pack system log
+		sprintf(cmd,"zip -9 %s \t /mnt/system.zip",pwd);
+		for(i=0;i<cnt;i++)
+		{
+			if(month-i<1)
+			{
 				tm=month-i+12;
 				ty=year-1;
 			}
-			else{
+			else
+			{
 				tm=month-i;
 				ty=year;
 			}
-//			sprintf(cmd,"%s \t /Storage/ChargeLog/*%04d*%02d*",cmd,ty,tm);
-			sprintf(cmd,"%s \t /Storage/EventLog/*%04d*%02d*",cmd,ty,tm);
 			sprintf(cmd,"%s \t /Storage/SystemLog/*%04d*%02d*",cmd,ty,tm);
 			sprintf(cmd,"%s \t /Storage/OCPP/*%04d*%02d*",cmd,ty,tm);
 		}
-		sprintf(cmd,"%s \t /Storage/ChargeLog/*.db",cmd);
 		sprintf(cmd,"%s \t /Storage/OCPP/*.db",cmd);
-		sprintf(cmd,"%s \t /Storage/EventLog/*.db",cmd);
+
 		if(isCCS==0){
 			sprintf(cmd,"%s \t /Storage/CCS*.zip",cmd);
 		}
 		system(cmd);
-		if(isCCS==1){
+		
+		// Pack Event & Charging Log
+		sprintf(cmd,"zip -9 \t /mnt/charging_event.zip");
+		for(i=0;i<cnt;i++)
+		{
+			if(month-i<1)
+			{
+				tm=month-i+12;
+				ty=year-1;
+			}
+			else
+			{
+				tm=month-i;
+				ty=year;
+			}
+			sprintf(cmd,"%s \t /Storage/EventLog/*%04d*%02d*",cmd,ty,tm);
+		}
+		sprintf(cmd,"%s \t /Storage/ChargeLog/*.db",cmd);
+		sprintf(cmd,"%s \t /Storage/EventLog/*.db",cmd);
+		system(cmd);
+
+		// Combine all zip file to one
+		system("exec zip -9 /mnt/log.zip /mnt/system.zip /mnt/charging_event.zip");
+		system("exec rm -f /mnt/system.zip /mnt/charging_event.zip");
+
+
+		if(isCCS==1)
+		{
 			const char* server="192.168.0.10";
 			const char* username="vern";
 			const char* password="vern@delta";
@@ -332,6 +373,7 @@ int main(int argc, char *argv[]) {
 			sprintf(cmd,"/usr/bin/ftpput -u %s -p %s %s /Storage/CCS%d.zip /mnt/log.zip",username,password,server,CCSID);
 			system(cmd);
 		}
+		
 		printf("Log packing is done!\n");
 	}
 }

+ 33 - 6
EVSE/Modularization/ocpp20/MessageHandler.c

@@ -12108,13 +12108,14 @@ void* GetLogProcess(void* data)
 	if((sscanf((char*)oldestTimestamp, "%4d-%2d-%2dT%2d:%2d:%2d", &tmStart.tm_year, &tmStart.tm_mon, &tmStart.tm_mday, &tmStart.tm_hour, &tmStart.tm_min, &tmStart.tm_sec) == 6) &&
 	   (sscanf((char*)latestTimestamp, "%4d-%2d-%2dT%2d:%2d:%2d", &tmStop.tm_year, &tmStop.tm_mon, &tmStop.tm_mday, &tmStop.tm_hour, &tmStop.tm_min, &tmStop.tm_sec) == 6))
 	{
-		sprintf(cmdBuf, "exec zip -9 --password %04d%02d%s /mnt/log.zip", tmNow->tm_year+1900, tmNow->tm_mon+1, ShmSysConfigAndInfo->SysConfig.SerialNumber);
+		// Pack system log
+		memset(cmdBuf, 0x00, ARRAY_SIZE(cmdBuf));
+		sprintf(cmdBuf, "exec zip -9 --password %04d%02d%s /mnt/system.zip", tmNow->tm_year+1900, tmNow->tm_mon+1, ShmSysConfigAndInfo->SysConfig.SerialNumber);
 		targetYear = tmStart.tm_year;
 		targetMonth = tmStart.tm_mon;
 
 		do
 		{
-			sprintf(cmdBuf, "%s /Storage/EventLog/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
 			sprintf(cmdBuf, "%s /Storage/SystemLog/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
 			sprintf(cmdBuf, "%s /Storage/OCPP/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
 
@@ -12130,12 +12131,38 @@ void* GetLogProcess(void* data)
 		}while((targetYear < (tmNow->tm_year+1900)) ||
 			   ((targetYear <= (tmNow->tm_year+1900)) && (targetMonth <= (tmNow->tm_mon+1))));
 
-		sprintf(cmdBuf, "%s \t /Storage/ChargeLog/*.db", cmdBuf);
-		sprintf(cmdBuf, "%s \t /Storage/OCPP/*.db", cmdBuf);
-		sprintf(cmdBuf, "%s \t /Storage/EventLog/*.db", cmdBuf);
-		sprintf(cmdBuf, "%s \t /Storage/CCS*.zip", cmdBuf);
+		sprintf(cmdBuf, "%s /Storage/CCS*.zip", cmdBuf);
+		system(cmdBuf);
+
+		// Pack charging & event log
+		memset(cmdBuf, 0x00, ARRAY_SIZE(cmdBuf));
+		sprintf(cmdBuf, "exec zip -9 /mnt/charging_event.zip");
+		targetYear = tmStart.tm_year;
+		targetMonth = tmStart.tm_mon;
+
+		do
+		{
+			sprintf(cmdBuf, "%s /Storage/EventLog/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
+
+			if(targetMonth+1>=13)
+			{
+				targetYear += 1;
+				targetMonth = 1;
+			}
+			else
+			{
+				targetMonth += 1;
+			}
+		}while((targetYear < (tmNow->tm_year+1900)) ||
+			   ((targetYear <= (tmNow->tm_year+1900)) && (targetMonth <= (tmNow->tm_mon+1))));
 
+		sprintf(cmdBuf, "%s /Storage/ChargeLog/*.db", cmdBuf);
+		sprintf(cmdBuf, "%s /Storage/EventLog/*.db", cmdBuf);
 		system(cmdBuf);
+
+		// Combine log file
+		system("exec zip -9 /mnt/log.zip /mnt/system.zip /mnt/charging_event.zip");
+		system("exec rm -f /mnt/system.zip /mnt/charging_event.zip");
 	}
 	else
 	{

+ 33 - 6
EVSE/Modularization/ocppfiles/MessageHandler.c

@@ -8138,13 +8138,14 @@ void* GetDiagnosticsProcess(void* data)
 	if((sscanf((char*)startTimestr, "%4d-%2d-%2dT%2d:%2d:%2d", &tmStart.tm_year, &tmStart.tm_mon, &tmStart.tm_mday, &tmStart.tm_hour, &tmStart.tm_min, &tmStart.tm_sec) == 6) &&
 	   (sscanf((char*)stopTimestr, "%4d-%2d-%2dT%2d:%2d:%2d", &tmStop.tm_year, &tmStop.tm_mon, &tmStop.tm_mday, &tmStop.tm_hour, &tmStop.tm_min, &tmStop.tm_sec) == 6))
 	{
-		sprintf(cmdBuf, "exec zip -9 --password %04d%02d%s /mnt/log.zip", tmNow->tm_year+1900, tmNow->tm_mon+1, ShmSysConfigAndInfo->SysConfig.SerialNumber);
+		// Pack system log
+		memset(cmdBuf, 0x00, ARRAY_SIZE(cmdBuf));
+		sprintf(cmdBuf, "exec zip -9 --password %04d%02d%s /mnt/system.zip", tmNow->tm_year+1900, tmNow->tm_mon+1, ShmSysConfigAndInfo->SysConfig.SerialNumber);
 		targetYear = tmStart.tm_year;
 		targetMonth = tmStart.tm_mon;
 
 		do
 		{
-			sprintf(cmdBuf, "%s /Storage/EventLog/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
 			sprintf(cmdBuf, "%s /Storage/SystemLog/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
 			sprintf(cmdBuf, "%s /Storage/OCPP/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
 
@@ -8160,12 +8161,38 @@ void* GetDiagnosticsProcess(void* data)
 		}while((targetYear < (tmNow->tm_year+1900)) ||
 			   ((targetYear <= (tmNow->tm_year+1900)) && (targetMonth <= (tmNow->tm_mon+1))));
 
-		sprintf(cmdBuf, "%s \t /Storage/ChargeLog/*.db", cmdBuf);
-		sprintf(cmdBuf, "%s \t /Storage/OCPP/*.db", cmdBuf);
-		sprintf(cmdBuf, "%s \t /Storage/EventLog/*.db", cmdBuf);
-		sprintf(cmdBuf, "%s \t /Storage/CCS*.zip", cmdBuf);
+		sprintf(cmdBuf, "%s /Storage/CCS*.zip", cmdBuf);
+		system(cmdBuf);
+
+		// Pack charging & event log
+		memset(cmdBuf, 0x00, ARRAY_SIZE(cmdBuf));
+		sprintf(cmdBuf, "exec zip -9 /mnt/charging_event.zip");
+		targetYear = tmStart.tm_year;
+		targetMonth = tmStart.tm_mon;
+
+		do
+		{
+			sprintf(cmdBuf, "%s /Storage/EventLog/*%04d*%02d*", cmdBuf, targetYear, targetMonth);
+
+			if(targetMonth+1>=13)
+			{
+				targetYear += 1;
+				targetMonth = 1;
+			}
+			else
+			{
+				targetMonth += 1;
+			}
+		}while((targetYear < (tmNow->tm_year+1900)) ||
+			   ((targetYear <= (tmNow->tm_year+1900)) && (targetMonth <= (tmNow->tm_mon+1))));
 
+		sprintf(cmdBuf, "%s /Storage/ChargeLog/*.db", cmdBuf);
+		sprintf(cmdBuf, "%s /Storage/EventLog/*.db", cmdBuf);
 		system(cmdBuf);
+
+		// Combine log file
+		system("exec zip -9 /mnt/log.zip /mnt/system.zip /mnt/charging_event.zip");
+		system("exec rm -f /mnt/system.zip /mnt/charging_event.zip");
 	}
 	else
 	{