From eeaad512a9a690eb0307ce6e2d355e29c91eb46f Mon Sep 17 00:00:00 2001
From: Michael Carlberg <c@rlberg.se>
Date: Mon, 9 Jan 2017 14:58:14 +0100
Subject: [PATCH] fix(battery): Calculation of remaining dis-/charge seconds

- Optimize calculation
- Do not allow division by zero

Refs #303
---
 src/modules/battery.cpp | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/src/modules/battery.cpp b/src/modules/battery.cpp
index 0ef9033b..a5b0e410 100644
--- a/src/modules/battery.cpp
+++ b/src/modules/battery.cpp
@@ -62,17 +62,22 @@ namespace modules {
     }
 
     m_rate_reader = make_unique<rate_reader>([this] {
-      unsigned long rate{std::strtoul(file_util::contents(m_frate).c_str(), nullptr, 10) / 1000UL};
+      unsigned long rate{std::strtoul(file_util::contents(m_frate).c_str(), nullptr, 10)};
       unsigned long volt{std::strtoul(file_util::contents(m_fvoltage).c_str(), nullptr, 10) / 1000UL};
-      unsigned long now{std::strtoul(file_util::contents(m_fcapnow).c_str(), nullptr, 10) / 1000UL};
-      unsigned long max{std::strtoul(file_util::contents(m_fcapfull).c_str(), nullptr, 10) / 1000UL};
+      unsigned long now{std::strtoul(file_util::contents(m_fcapnow).c_str(), nullptr, 10)};
+      unsigned long max{std::strtoul(file_util::contents(m_fcapfull).c_str(), nullptr, 10)};
       unsigned long cap{read(*m_state_reader) ? max - now : now};
 
       if (rate && volt && cap) {
-        return 3600UL * (cap * 1000UL / volt) / (rate * 1000UL / volt);
-      } else {
-        return 0UL;
+        auto remaining = (cap / volt);
+        auto current_rate = (rate / volt);
+
+        if (remaining && current_rate) {
+          return 3600UL * remaining / current_rate;
+        }
       }
+
+      return 0UL;
     });
 
     // Load state and capacity level