|
@@ -15,12 +15,12 @@
|
|
|
|
|
|
#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>
|
|
@@ -46,6 +46,17 @@ struct StatusCodeData *ShmStatusCodeData;
|
|
|
|
|
|
sqlite3 *localDb;
|
|
|
|
|
|
+struct network_previous_status
|
|
|
+{
|
|
|
+ int rssiWifi;
|
|
|
+ int rssi4g;
|
|
|
+ uint8_t isInternet:1;
|
|
|
+ uint8_t isOcppConnected:1;
|
|
|
+ uint8_t ethIsInternet:1;
|
|
|
+ uint8_t maln0IsInternet:1;
|
|
|
+ uint8_t ppp0IsInternet:1;
|
|
|
+}netPreviousStatus;
|
|
|
+
|
|
|
void trim(char *s);
|
|
|
int mystrcmp(char *p1,char *p2);
|
|
|
void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
|
|
@@ -250,16 +261,12 @@ int InitShareMemory()
|
|
|
//creat ShmSysConfigAndInfo
|
|
|
if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
|
|
|
{
|
|
|
- #ifdef SystemLogMessage
|
|
|
DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
|
|
|
- #endif
|
|
|
result = FAIL;
|
|
|
}
|
|
|
else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
|
|
|
{
|
|
|
- #ifdef SystemLogMessage
|
|
|
DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
|
|
|
- #endif
|
|
|
result = FAIL;
|
|
|
}
|
|
|
else
|
|
@@ -268,16 +275,12 @@ int InitShareMemory()
|
|
|
//creat ShmStatusCodeData
|
|
|
if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
|
|
|
{
|
|
|
- #ifdef SystemLogMessage
|
|
|
DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
|
|
|
- #endif
|
|
|
result = FAIL;
|
|
|
}
|
|
|
else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
|
|
|
{
|
|
|
- #ifdef SystemLogMessage
|
|
|
DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
|
|
|
- #endif
|
|
|
result = FAIL;
|
|
|
}
|
|
|
else
|
|
@@ -286,16 +289,12 @@ int InitShareMemory()
|
|
|
//creat ShmStatusCodeData
|
|
|
if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
|
|
|
{
|
|
|
- #ifdef SystemLogMessage
|
|
|
DEBUG_ERROR("shmget ShmStatusCodeData NG");
|
|
|
- #endif
|
|
|
result = FAIL;
|
|
|
}
|
|
|
else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
|
|
|
{
|
|
|
- #ifdef SystemLogMessage
|
|
|
DEBUG_ERROR("shmat ShmStatusCodeData NG");
|
|
|
- #endif
|
|
|
result = FAIL;
|
|
|
}
|
|
|
else
|
|
@@ -308,7 +307,7 @@ int InitShareMemory()
|
|
|
//==========================================
|
|
|
// SQLite routne
|
|
|
//==========================================
|
|
|
-int DB_Open(sqlite3 *db)
|
|
|
+int DB_Event_Open(sqlite3 *db)
|
|
|
{
|
|
|
int result = PASS;
|
|
|
char* errMsg = NULL;
|
|
@@ -345,7 +344,7 @@ int DB_Open(sqlite3 *db)
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
-int DB_Insert_Record(sqlite3 *db, uint8_t *statusCode)
|
|
|
+int DB_Event_Insert_Record(sqlite3 *db, uint8_t *statusCode)
|
|
|
{
|
|
|
int result = PASS;
|
|
|
char* errMsg = NULL;
|
|
@@ -361,16 +360,11 @@ int DB_Insert_Record(sqlite3 *db, uint8_t *statusCode)
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- DEBUG_INFO( "Local event record database open successfully.\n");
|
|
|
if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
|
|
|
{
|
|
|
result = FAIL;
|
|
|
DEBUG_INFO( "Insert local event record error message: %s\n", errMsg);
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- DEBUG_INFO( "Insert local event record successfully\n");
|
|
|
- }
|
|
|
|
|
|
sprintf(sqlStr, "delete from event_record where idx < (select idx from event_record order by idx desc limit 1)-2000;");
|
|
|
if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
|
|
@@ -378,9 +372,126 @@ int DB_Insert_Record(sqlite3 *db, uint8_t *statusCode)
|
|
|
result = FAIL;
|
|
|
DEBUG_INFO( "delete local event_record error message: %s\n", errMsg);
|
|
|
}
|
|
|
+
|
|
|
+ sqlite3_close(db);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+int DB_Network_Open(sqlite3 *db)
|
|
|
+{
|
|
|
+ int result = PASS;
|
|
|
+ char* errMsg = NULL;
|
|
|
+ char* createRecordSql="CREATE TABLE IF NOT EXISTS network_record("
|
|
|
+ "idx integer primary key AUTOINCREMENT, "
|
|
|
+ "occurDatetime text NOT NULL, "
|
|
|
+ "isInternet text NOT NULL, "
|
|
|
+ "isOcppConnected text NOT NULL, "
|
|
|
+ "isEth0Internet text NOT NULL, "
|
|
|
+ "isMlan0Internet text NOT NULL, "
|
|
|
+ "isPpp0Internet text NOT NULL, "
|
|
|
+ "rssiMlan0 text NOT NULL, "
|
|
|
+ "rssiPpp0 text NOT NULL"
|
|
|
+ ");";
|
|
|
+
|
|
|
+ //sqlite3_config(SQLITE_CONFIG_URI, 1);
|
|
|
+ if(sqlite3_open(DB_FILE, &db))
|
|
|
+ {
|
|
|
+ result = FAIL;
|
|
|
+ DEBUG_ERROR( "Can't open database: %s\n", sqlite3_errmsg(db));
|
|
|
+ sqlite3_close(db);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ DEBUG_INFO( "Local network status record database open successfully.\n");
|
|
|
+
|
|
|
+ if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
|
|
|
+ {
|
|
|
+ result = FAIL;
|
|
|
+ DEBUG_ERROR( "Create local network status record table error message: %s\n", errMsg);
|
|
|
+ }
|
|
|
else
|
|
|
{
|
|
|
- DEBUG_INFO( "delete local event record successfully\n");
|
|
|
+ DEBUG_INFO( "Opened local network status record table successfully\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ sqlite3_close(db);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+int DB_Network_Insert_Record(sqlite3 *db)
|
|
|
+{
|
|
|
+ int result = PASS;
|
|
|
+ char* errMsg = NULL;
|
|
|
+ char sqlStr[1024];
|
|
|
+
|
|
|
+ if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D')
|
|
|
+ {
|
|
|
+ sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
|
|
|
+ ShmSysConfigAndInfo->SysInfo.InternetConn,
|
|
|
+ ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi,
|
|
|
+ ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi,
|
|
|
+ ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
|
|
|
+ }
|
|
|
+ else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
|
|
|
+ {
|
|
|
+ sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
|
|
|
+ ShmSysConfigAndInfo->SysInfo.InternetConn,
|
|
|
+ ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi,
|
|
|
+ 0,
|
|
|
+ ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi,
|
|
|
+ 0);
|
|
|
+ }
|
|
|
+ else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
|
|
|
+ {
|
|
|
+ sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
|
|
|
+ ShmSysConfigAndInfo->SysInfo.InternetConn,
|
|
|
+ ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
|
|
|
+ 0,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi,
|
|
|
+ 0,
|
|
|
+ ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sprintf(sqlStr, "insert into network_record(occurDatetime, isInternet, isOcppConnected, isEth0Internet, isMlan0Internet, isPpp0Internet, rssiMlan0, rssiPpp0) values(CURRENT_TIMESTAMP, '%d', '%d', '%d', '%d', '%d', '%d', '%d');",
|
|
|
+ ShmSysConfigAndInfo->SysInfo.InternetConn,
|
|
|
+ ShmSysConfigAndInfo->SysInfo.OcppConnStatus,
|
|
|
+ !ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 0,
|
|
|
+ 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ if(sqlite3_open(DB_FILE, &db))
|
|
|
+ {
|
|
|
+ result = FAIL;
|
|
|
+ DEBUG_INFO( "Can't open database: %s\n", sqlite3_errmsg(db));
|
|
|
+ sqlite3_close(db);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
|
|
|
+ {
|
|
|
+ result = FAIL;
|
|
|
+ DEBUG_INFO( "Insert local network status record error message: %s\n", errMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ sprintf(sqlStr, "delete from network_record where idx < (select idx from network_record order by idx desc limit 1)-20000;");
|
|
|
+ if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
|
|
|
+ {
|
|
|
+ result = FAIL;
|
|
|
+ DEBUG_INFO( "delete local network_record error message: %s\n", errMsg);
|
|
|
}
|
|
|
|
|
|
sqlite3_close(db);
|
|
@@ -410,9 +521,14 @@ int main(void)
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- if(DB_Open(localDb) != PASS)
|
|
|
+ if(DB_Event_Open(localDb) != PASS)
|
|
|
{
|
|
|
- DEBUG_ERROR("Local database initial fail.\n");
|
|
|
+ DEBUG_ERROR("Local event database initial fail.\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ if(DB_Network_Open(localDb) != PASS)
|
|
|
+ {
|
|
|
+ DEBUG_ERROR("Local network status database initial fail.\n");
|
|
|
}
|
|
|
|
|
|
for(;;)
|
|
@@ -444,7 +560,7 @@ int main(void)
|
|
|
}
|
|
|
|
|
|
EVENT_INFO("%s\n", EventCodeTmp);
|
|
|
- DB_Insert_Record(localDb, EventCodeTmp);
|
|
|
+ DB_Event_Insert_Record(localDb, EventCodeTmp);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -476,7 +592,7 @@ int main(void)
|
|
|
AddFaultCodeToBuf(EventCodeDisp);
|
|
|
}
|
|
|
EVENT_INFO("%s\n", EventCodeTmp);
|
|
|
- DB_Insert_Record(localDb, EventCodeTmp);
|
|
|
+ DB_Event_Insert_Record(localDb, EventCodeTmp);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -508,12 +624,31 @@ int main(void)
|
|
|
AddFaultCodeToBuf(EventCodeDisp);
|
|
|
}
|
|
|
EVENT_INFO("%s\n", EventCodeTmp);
|
|
|
- DB_Insert_Record(localDb, EventCodeTmp);
|
|
|
+ DB_Event_Insert_Record(localDb, EventCodeTmp);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if((netPreviousStatus.isInternet != ShmSysConfigAndInfo->SysInfo.InternetConn) ||
|
|
|
+ (netPreviousStatus.isOcppConnected != ShmSysConfigAndInfo->SysInfo.OcppConnStatus) ||
|
|
|
+ (netPreviousStatus.ethIsInternet != ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet) ||
|
|
|
+ (netPreviousStatus.maln0IsInternet != ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) ||
|
|
|
+ (netPreviousStatus.ppp0IsInternet != ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) ||
|
|
|
+ (netPreviousStatus.rssiWifi != ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi) ||
|
|
|
+ (netPreviousStatus.rssi4g != ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi))
|
|
|
+ {
|
|
|
+
|
|
|
+ DB_Network_Insert_Record(localDb);
|
|
|
+ netPreviousStatus.isInternet = ShmSysConfigAndInfo->SysInfo.InternetConn;
|
|
|
+ netPreviousStatus.isOcppConnected = ShmSysConfigAndInfo->SysInfo.OcppConnStatus;
|
|
|
+ netPreviousStatus.ethIsInternet = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet;
|
|
|
+ netPreviousStatus.maln0IsInternet = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi;
|
|
|
+ netPreviousStatus.ppp0IsInternet = ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi;
|
|
|
+ netPreviousStatus.rssiWifi = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi;
|
|
|
+ netPreviousStatus.rssi4g = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi;
|
|
|
+ }
|
|
|
+
|
|
|
usleep(100000);
|
|
|
}
|
|
|
|