static int __lastResetTime = 0;
static int __lastPercent = -1;
static int __lastPercentTime = 0;
-static int __lastHeartTimestamp = 0;
+static int __lastBatteryTimeInfoDeleteTime = 0;
static bool __isCharging = 0;
-static std::vector<Json> __batteryTimeInfoVec;
static bool __timerRunning = false;
+static std::vector<Json> __batteryTimeInfoVec;
-#define DB_QUERY_DELAY 600000
+#define HEART_DB_QUERY_DELAY 600000
+#define BATTERY_TIME_INFO_DELETE_DELAY 10 * 3600
+
+#define UPDATE_BATTERY_LOGS_QUERY \
+ "UPDATE " BATTERY_TEMP_TIME_INFO " SET " BATTERY_TO_BE_PROCESSED " = %d WHERE " BATTERY_START_TIME " >= %d AND " BATTERY_END_TIME " <= %d"
BatteryMonitor::BatteryMonitor()
{
records[0].get(NULL, BATTERY_LAST_RESET_TIME, &__lastResetTime);
records[0].get(NULL, BATTERY_LAST_PERCENT, &__lastPercent);
records[0].get(NULL, BATTERY_LAST_PERCENT_TIME, &__lastPercentTime);
- records[0].get(NULL, BATTERY_LAST_HEART_TIMESTAMP, &__lastHeartTimestamp);
+ records[0].get(NULL, BATTERY_TIME_INFO_LAST_DELETE_TIME, &__lastBatteryTimeInfoDeleteTime);
}
records.clear();
__batteryTimeInfoVec.clear();
- ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO , &__batteryTimeInfoVec);
-
- ret = __dbMgr.executeSync("DELETE FROM " BATTERY_TEMP_TIME_INFO, &records);
-
+ ret = __dbMgr.executeSync("SELECT * FROM " BATTERY_TEMP_TIME_INFO " WHERE " BATTERY_TO_BE_PROCESSED " = 1", &__batteryTimeInfoVec);
return true;
}
bool BatteryMonitor::__updateLastInfo()
{
- if (__batteryTimeInfoVec.size() > 0) {
- for (unsigned int i = 0; i < __batteryTimeInfoVec.size(); i++) {
- int64_t rowId;
- __dbMgr.insertSync(BATTERY_TEMP_TIME_INFO, __batteryTimeInfoVec.at(i), &rowId);
- }
- }
-
- IF_FAIL_RETURN_TAG(__lastResetTime != 0 || __lastPercentTime != 0, false, _W, "Last info doesn't exist");
-
- char *query = sqlite3_mprintf(
- "INSERT OR REPLACE INTO " BATTERY_LAST_INFO_TABLE " (" \
- BATTERY_ROW_ID ", " BATTERY_LAST_RESET_TIME ", " \
- BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME ", " BATTERY_LAST_HEART_TIMESTAMP ") VALUES (%s, %d, %d, %d, %d)",
- DEFAULT_ROW_ID_STR, __lastResetTime, __lastPercent, __lastPercentTime, __lastHeartTimestamp);
-
- std::vector<Json> records;
- bool ret = __dbMgr.executeSync(query, &records);
+ char *query = sqlite3_mprintf("INSERT OR REPLACE INTO " BATTERY_LAST_INFO_TABLE " (" \
+ BATTERY_ROW_ID ", " BATTERY_LAST_PERCENT ", " BATTERY_LAST_PERCENT_TIME \
+ ", " BATTERY_TIME_INFO_LAST_DELETE_TIME ", " BATTERY_LAST_RESET_TIME ") VALUES (%s, %d, %d, %d, %d)",
+ DEFAULT_ROW_ID_STR, __lastPercent, __lastPercentTime, __lastBatteryTimeInfoDeleteTime, __lastResetTime);
+ bool ret = __dbMgr.executeSync(query, NULL);
sqlite3_free(query);
- IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to update battery stat last info");
-
- return true;
+ return ret;
}
void BatteryMonitor::__batteryChangeCb(device_callback_e type, void* value, void* userData)
battTimeInfo.set(NULL, BATTERY_AMOUNT, percent);
battTimeInfo.set(NULL, BATTERY_START_TIME, __lastPercentTime);
battTimeInfo.set(NULL, BATTERY_END_TIME, currentTime);
+ battTimeInfo.set(NULL, BATTERY_TO_BE_PROCESSED, 1);
__batteryTimeInfoVec.push_back(battTimeInfo);
+ int64_t rowId;
+ (instance->__dbMgr).insertSync(BATTERY_TEMP_TIME_INFO, battTimeInfo, &rowId);
+
bool dataRemaining = instance->processBatteryUsage();
if (dataRemaining && !__timerRunning) {
__timerRunning = true;
_D("Start timer to request HEART data");
- g_timeout_add(DB_QUERY_DELAY, __timeoutCb, instance);
+ g_timeout_add(HEART_DB_QUERY_DELAY, __timeoutCb, instance);
}
}
__lastPercentTime = currentTime;
__lastPercent = percent;
+
+ bool ret = instance->__updateLastInfo();
+ IF_FAIL_VOID_TAG(ret, _E, "Failed to update last percent and last percent times");
}
void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void* userData)
__isCharging = intptr_t(value);
IF_FAIL_VOID(!__isCharging);
+ BatteryMonitor* instance = static_cast<BatteryMonitor*>(userData);
int percent;
if (__lastPercent < 0) {
__lastResetTime = CURRENT_TIME;
__lastPercentTime = __lastResetTime;
_D("Charger is disconnected after fully charged. ResetTime: %d", __lastResetTime);
+
+ bool ret = instance->__updateLastInfo();
+ IF_FAIL_VOID_TAG(ret, _E, "Failed to update last reset time and last percent time");
}
}
IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to set battery charging change cb");
__timerRunning = true;
- _D("Start timer to request HEAERT data");
- g_timeout_add(DB_QUERY_DELAY, __timeoutCb, this);
+ _D("Start timer to request HEART data");
+ g_timeout_add(HEART_DB_QUERY_DELAY, __timeoutCb, this);
return ERR_NONE;
}
int BatteryMonitor::stop()
{
- __updateLastInfo();
-
int error = device_remove_callback(DEVICE_CALLBACK_BATTERY_CAPACITY, __batteryChangeCb);
IF_FAIL_RETURN_TAG(error == DEVICE_ERROR_NONE, ERR_OPERATION_FAILED, _E, "Failed to remove callback for battery capacity");
_D("Read %d rows from heart cpu table from %d to %d", cpuUsageLog.size(), totalStartTime, totalEndTime);
// Get the last timestamp of HEART cpu data and maximum app times tables in cache
- cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &__lastHeartTimestamp);
+ int lastHeartTimestamp;
+ cpuUsageLog.back().get(NULL, BATTERY_TIMESTAMP, &lastHeartTimestamp);
CpuUsageMap lastCpuUsage;
ret = __getLastCpuUsageTable(&lastCpuUsage);
row.get(NULL, BATTERY_START_TIME, &startTime);
row.get(NULL, BATTERY_END_TIME, &endTime);
- if (endTime > __lastHeartTimestamp) {
+ if (endTime > lastHeartTimestamp) {
_W("[%d] Heart cpu data is not prepared yet (%d ~ %d)", i, startTime, endTime);
break;
}
}
}
- // Insert log of last times of apps
+ // Insert log of last times of apps, and update battery infos ToBeProcessed field
if (i != 0) {
ret = __insertLastCpuUsageLog(lastCpuUsage);
if (!ret) {
_E("Failed to insert last Cpu Usage of apps");
}
+
+ int processedStartTime = totalStartTime;
+ int processedEndTime;
+ __batteryTimeInfoVec[i-1].get(NULL, BATTERY_END_TIME, &processedEndTime);
+ char *sql = sqlite3_mprintf(UPDATE_BATTERY_LOGS_QUERY, 0, processedStartTime, processedEndTime);
+ __dbMgr.executeSync(sql, NULL);
+ sqlite3_free(sql);
}
// Remove completed time info
_D("Total %d time intervals, %d intervals are calculated", __batteryTimeInfoVec.size(), i);
__batteryTimeInfoVec.erase(__batteryTimeInfoVec.begin(), __batteryTimeInfoVec.begin() + i);
+ __deleteBatteryTimeInfo();
if (__batteryTimeInfoVec.size() == 0) {
return false;
return __lastResetTime;
}
+
+void BatteryMonitor::__deleteBatteryTimeInfo()
+{
+ int batteryTimeInfoDeleteTime = CURRENT_TIME - BATTERY_TIME_INFO_DELETE_DELAY;
+ if (__lastBatteryTimeInfoDeleteTime > batteryTimeInfoDeleteTime) {
+ _D("Old battery time info already deleted");
+ return;
+ }
+
+ std::string query("DELETE FROM " BATTERY_TEMP_TIME_INFO " WHERE StartTime <= ");
+ query += std::to_string(batteryTimeInfoDeleteTime);
+
+ __dbMgr.executeSync(query.c_str(), NULL);
+ __lastBatteryTimeInfoDeleteTime = CURRENT_TIME;
+
+ bool ret = __updateLastInfo();
+ IF_FAIL_VOID_TAG(ret, _E, "Failed to update last battery time info deletion time");
+ _D("Old battery time info deleted");
+}