Added code to query HeartDB every 10 minutes 04/101104/3
authorKunal <kunal.chawla@samsung.com>
Wed, 30 Nov 2016 08:49:13 +0000 (17:49 +0900)
committerSomin Kim <somin926.kim@samsung.com>
Wed, 30 Nov 2016 09:48:17 +0000 (18:48 +0900)
Change-Id: I62086cb8a5b1f92d5434956e9e5213477cb2910d
Signed-off-by: Kunal <kunal.chawla@samsung.com>
src/battery-stats/BatteryMonitor.cpp
src/battery-stats/BatteryMonitor.h

index 9a11562..7098c9f 100644 (file)
@@ -34,7 +34,9 @@ static int __lastPercentTime = 0;
 static int __lastHeartTimestamp = 0;
 static bool __isCharging = 0;
 static std::vector<Json> __batteryTimeInfoVec;
+static bool __timerRunning = false;
 
+#define DB_QUERY_DELAY 600000
 
 BatteryMonitor::BatteryMonitor()
 {
@@ -133,7 +135,12 @@ void BatteryMonitor::__batteryChangeCb(device_callback_e type, void* value, void
                battTimeInfo.set(NULL, BATTERY_END_TIME, currentTime);
                __batteryTimeInfoVec.push_back(battTimeInfo);
 
-               instance->processBatteryUsage();
+               bool dataRemaining = instance->processBatteryUsage();
+               if (dataRemaining && !__timerRunning) {
+                               __timerRunning = true;
+                               _D("Start timer to request HEART data");
+                               g_timeout_add(DB_QUERY_DELAY, __timeoutCb, instance);
+               }
        }
 
        __lastPercentTime = currentTime;
@@ -163,6 +170,18 @@ void BatteryMonitor::__chargerChangeCb(device_callback_e type, void* value, void
        }
 }
 
+gboolean BatteryMonitor::__timeoutCb(gpointer data) {
+       BatteryMonitor* instance = static_cast<BatteryMonitor*>(data);
+
+       bool dataRemaining = instance->processBatteryUsage();
+       if (!dataRemaining) {
+               _D("Stop timer, no more data to process");
+               __timerRunning = false;
+       }
+
+       return dataRemaining ? TRUE : FALSE;
+}
+
 int BatteryMonitor::start()
 {
        __loadLastInfo();
@@ -173,6 +192,9 @@ int BatteryMonitor::start()
        error = device_add_callback(DEVICE_CALLBACK_BATTERY_CHARGING, __chargerChangeCb, NULL);
        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);
        return ERR_NONE;
 }
 
@@ -219,9 +241,10 @@ bool BatteryMonitor::__getLastCpuUsageTable(CpuUsageMap* lastCpuUsage)
        return true;
 }
 
+//Return false if all is processed, true otherwise
 bool BatteryMonitor::processBatteryUsage()
 {
-       IF_FAIL_RETURN_TAG(__batteryTimeInfoVec.size() > 0, true, _D, "All per-app battery usages are already calculated");
+       IF_FAIL_RETURN_TAG(__batteryTimeInfoVec.size() > 0, false, _D, "All per-app battery usages are already calculated");
 
        int totalStartTime;
        int totalEndTime;
@@ -231,12 +254,12 @@ bool BatteryMonitor::processBatteryUsage()
        // Read cpu table from heart db for time span of stacked in __batteryTimeInfoVec
        HeartDbReader heartReader;
        bool ret = heartReader.open();
-       IF_FAIL_RETURN_TAG(ret, false, _E, "Failed to open heart db");
+       IF_FAIL_RETURN_TAG(ret, true, _E, "Failed to open heart db");
 
        std::vector<Json> cpuUsageLog;
        ret = heartReader.readCpuUsageLog(totalStartTime, totalEndTime, &cpuUsageLog);
        heartReader.close();
-       IF_FAIL_RETURN_TAG(ret, false, _E, "Cannot read from heart cpu table");
+       IF_FAIL_RETURN_TAG(ret, true, _E, "Cannot read from heart cpu table");
        IF_FAIL_RETURN_TAG(cpuUsageLog.size() > 0, true, _W, "Heart cpu data is not prepared yet (%d ~ %d)", totalStartTime, totalEndTime);
        _D("Read %d rows from heart cpu table from %d to %d", cpuUsageLog.size(), totalStartTime, totalEndTime);
 
@@ -287,7 +310,12 @@ bool BatteryMonitor::processBatteryUsage()
        _D("Total %d time intervals, %d intervals are calculated", __batteryTimeInfoVec.size(), i);
        __batteryTimeInfoVec.erase(__batteryTimeInfoVec.begin(), __batteryTimeInfoVec.begin() + i);
 
-       return true;
+       if (__batteryTimeInfoVec.size() == 0) {
+               return false;
+       }
+       else {
+               return true;
+       }
 }
 
 bool BatteryMonitor::__insertBatteryUsageLog(std::vector<Json>& usage)
index 7ec34f8..2ac3db8 100644 (file)
@@ -40,6 +40,7 @@ namespace ctx {
                bool __loadLastInfo();
                bool __updateLastInfo();
 
+               static int __timeoutCb(void* data);
                static void __batteryChangeCb(device_callback_e type, void* value, void* userData);
                static void __chargerChangeCb(device_callback_e type, void* value, void* userData);