From 08d4cd4870c7acaf3a453d1400b64d77d462437b Mon Sep 17 00:00:00 2001
From: Kelly Brazil <kellyjonbrazil@gmail.com>
Date: Tue, 30 Mar 2021 16:34:01 -0700
Subject: [PATCH] add time calculations to acpi parser

---
 EXAMPLES.md                         | 34 ++++++++++++++-
 docs/parsers/acpi.md                | 50 +++++++++++++---------
 jc/parsers/acpi.py                  | 65 ++++++++++++++++++++---------
 tests/fixtures/generic/acpi-V.json  |  2 +-
 tests/fixtures/generic/acpi-V2.json |  2 +-
 tests/fixtures/generic/acpi-V3.json |  2 +-
 tests/fixtures/generic/acpi-V4.json |  2 +-
 7 files changed, 114 insertions(+), 43 deletions(-)

diff --git a/EXAMPLES.md b/EXAMPLES.md
index a1d2c8f5..a9eb7319 100644
--- a/EXAMPLES.md
+++ b/EXAMPLES.md
@@ -13,7 +13,11 @@ acpi -V | jc --acpi -p          # or:  jc -p acpi -V
     "until_charged": "00:29:20",
     "design_capacity_mah": 2110,
     "last_full_capacity": 2271,
-    "last_full_capacity_percent": 100
+    "last_full_capacity_percent": 100,
+    "until_charged_hours": 0,
+    "until_charged_minutes": 29,
+    "until_charged_seconds": 20,
+    "until_charged_total_seconds": 1760
   },
   {
     "type": "Adapter",
@@ -54,6 +58,34 @@ acpi -V | jc --acpi -p          # or:  jc -p acpi -V
     "messages": [
       "Processor 0 of 10"
     ]
+  },
+  {
+    "type": "Cooling",
+    "id": 2,
+    "messages": [
+      "x86_pkg_temp no state information available"
+    ]
+  },
+  {
+    "type": "Cooling",
+    "id": 3,
+    "messages": [
+      "Processor 0 of 10"
+    ]
+  },
+  {
+    "type": "Cooling",
+    "id": 4,
+    "messages": [
+      "intel_powerclamp no state information available"
+    ]
+  },
+  {
+    "type": "Cooling",
+    "id": 5,
+    "messages": [
+      "Processor 0 of 10"
+    ]
   }
 ]
 ```
diff --git a/docs/parsers/acpi.md b/docs/parsers/acpi.md
index fbc42d6e..0e2f918a 100644
--- a/docs/parsers/acpi.md
+++ b/docs/parsers/acpi.md
@@ -31,7 +31,11 @@ Examples:
         "until_charged": "00:29:20",
         "design_capacity_mah": 2110,
         "last_full_capacity": 2271,
-        "last_full_capacity_percent": 100
+        "last_full_capacity_percent": 100,
+        "until_charged_hours": 0,
+        "until_charged_minutes": 29,
+        "until_charged_seconds": 20,
+        "until_charged_total_seconds": 1760
       },
       {
         "type": "Adapter",
@@ -209,29 +213,37 @@ Returns:
 
     [
       {
-        "type":                         string,
-        "id":                           integer,
-        "state":                        string,
-        "charge_percent":               integer,
-        "until_charged":                string,
-        "charge_remaining"              string,
-        "design_capacity_mah":          integer,
-        "last_full_capacity":           integer,
-        "last_full_capacity_percent":   integer,
-        "on-line":                      boolean,
-        "mode":                         string,
-        "temperature":                  float,
-        "temperature_unit":             string,
+        "type":                             string,
+        "id":                               integer,
+        "state":                            string,
+        "charge_percent":                   integer,
+        "until_charged":                    string,
+        "until_charged_hours":              integer,
+        "until_charged_minuts":             integer,
+        "until_charged_seconds":            integer,
+        "until_charged_total_seconds":      integer,
+        "charge_remaining":                 string,
+        "charge_remaining_hours":           integer,
+        "charge_remaining_minutes":         integer,
+        "charge_remaining_seconds":         integer,
+        "charge_remaining_total_seconds":   integer,
+        "design_capacity_mah":              integer,
+        "last_full_capacity":               integer,
+        "last_full_capacity_percent":       integer,
+        "on-line":                          boolean,
+        "mode":                             string,
+        "temperature":                      float,
+        "temperature_unit":                 string,
         "trip_points": [
           {
-            "id":                       integer,
-            "switches_to_mode":         string,
-            "temperature":              float,
-            "temperature_unit":         string
+            "id":                           integer,
+            "switches_to_mode":             string,
+            "temperature":                  float,
+            "temperature_unit":             string
           }
         ],
         "messages": [
-                                        string
+                                            string
         ]
       }
     ]
diff --git a/jc/parsers/acpi.py b/jc/parsers/acpi.py
index db2400d4..be0bbd7c 100644
--- a/jc/parsers/acpi.py
+++ b/jc/parsers/acpi.py
@@ -29,7 +29,11 @@ Examples:
         "until_charged": "00:29:20",
         "design_capacity_mah": 2110,
         "last_full_capacity": 2271,
-        "last_full_capacity_percent": 100
+        "last_full_capacity_percent": 100,
+        "until_charged_hours": 0,
+        "until_charged_minutes": 29,
+        "until_charged_seconds": 20,
+        "until_charged_total_seconds": 1760
       },
       {
         "type": "Adapter",
@@ -214,29 +218,37 @@ def process(proc_data):
 
         [
           {
-            "type":                         string,
-            "id":                           integer,
-            "state":                        string,
-            "charge_percent":               integer,
-            "until_charged":                string,
-            "charge_remaining"              string,
-            "design_capacity_mah":          integer,
-            "last_full_capacity":           integer,
-            "last_full_capacity_percent":   integer,
-            "on-line":                      boolean,
-            "mode":                         string,
-            "temperature":                  float,
-            "temperature_unit":             string,
+            "type":                             string,
+            "id":                               integer,
+            "state":                            string,
+            "charge_percent":                   integer,
+            "until_charged":                    string,
+            "until_charged_hours":              integer,
+            "until_charged_minuts":             integer,
+            "until_charged_seconds":            integer,
+            "until_charged_total_seconds":      integer,
+            "charge_remaining":                 string,
+            "charge_remaining_hours":           integer,
+            "charge_remaining_minutes":         integer,
+            "charge_remaining_seconds":         integer,
+            "charge_remaining_total_seconds":   integer,
+            "design_capacity_mah":              integer,
+            "last_full_capacity":               integer,
+            "last_full_capacity_percent":       integer,
+            "on-line":                          boolean,
+            "mode":                             string,
+            "temperature":                      float,
+            "temperature_unit":                 string,
             "trip_points": [
               {
-                "id":                       integer,
-                "switches_to_mode":         string,
-                "temperature":              float,
-                "temperature_unit":         string
+                "id":                           integer,
+                "switches_to_mode":             string,
+                "temperature":                  float,
+                "temperature_unit":             string
               }
             ],
             "messages": [
-                                            string
+                                                string
             ]
           }
         ]
@@ -278,6 +290,21 @@ def process(proc_data):
                         except (ValueError):
                             tp[key] = None
 
+    for entry in proc_data:
+        if 'until_charged' in entry:
+            entry['until_charged_hours'] = int(entry['until_charged'].split(':')[0])
+            entry['until_charged_minutes'] = int(entry['until_charged'].split(':')[1])
+            entry['until_charged_seconds'] = int(entry['until_charged'].split(':')[2])
+            entry['until_charged_total_seconds'] = (entry['until_charged_hours'] * 3600) + \
+                (entry['until_charged_minutes'] * 60) + entry['until_charged_seconds']
+
+        if 'charge_remaining' in entry:
+            entry['charge_remaining_hours'] = int(entry['charge_remaining'].split(':')[0])
+            entry['charge_remaining_minutes'] = int(entry['charge_remaining'].split(':')[1])
+            entry['charge_remaining_seconds'] = int(entry['charge_remaining'].split(':')[2])
+            entry['charge_remaining_total_seconds'] = (entry['charge_remaining_hours'] * 3600) + \
+                (entry['charge_remaining_minutes'] * 60) + entry['charge_remaining_seconds']
+
     return proc_data
 
 
diff --git a/tests/fixtures/generic/acpi-V.json b/tests/fixtures/generic/acpi-V.json
index e6d17ad7..f7fa995e 100644
--- a/tests/fixtures/generic/acpi-V.json
+++ b/tests/fixtures/generic/acpi-V.json
@@ -1 +1 @@
-[{"type": "Battery", "id": 0, "state": "Charging", "charge_percent": 71, "until_charged": "00:29:20", "design_capacity_mah": 2110, "last_full_capacity": 2271, "last_full_capacity_percent": 100}, {"type": "Battery", "id": 1, "state": "Discharging", "charge_percent": 98, "charge_remaining": "01:43:14", "design_capacity_mah": 4400, "last_full_capacity": 3013, "last_full_capacity_percent": 68}, {"type": "Battery", "id": 2, "state": "Discharging", "charge_percent": 0}, {"type": "Battery", "id": 3, "state": "Full", "charge_percent": 100}, {"type": "Adapter", "id": 0, "on-line": true}, {"type": "Adapter", "id": 1, "on-line": false}, {"type": "Thermal", "id": 0, "mode": "ok", "temperature": 46.0, "temperature_unit": "C", "trip_points": [{"id": 0, "switches_to_mode": "critical", "temperature": 127.0, "temperature_unit": "C"}, {"id": 1, "switches_to_mode": "hot", "temperature": 127.0, "temperature_unit": "C"}]}, {"type": "Thermal", "id": 1, "mode": "ok", "temperature": 55.0, "temperature_unit": "C", "trip_points": [{"id": 0, "switches_to_mode": "critical", "temperature": 130.0, "temperature_unit": "C"}, {"id": 1, "switches_to_mode": "hot", "temperature": 100.0, "temperature_unit": "C"}]}, {"type": "Cooling", "id": 0, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 1, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 2, "messages": ["x86_pkg_temp no state information available"]}, {"type": "Cooling", "id": 3, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 4, "messages": ["intel_powerclamp no state information available", "another message"]}, {"type": "Cooling", "id": 5, "messages": ["Processor 0 of 10"]}]
+[{"type":"Battery","id":0,"state":"Charging","charge_percent":71,"until_charged":"00:29:20","design_capacity_mah":2110,"last_full_capacity":2271,"last_full_capacity_percent":100,"until_charged_hours":0,"until_charged_minutes":29,"until_charged_seconds":20,"until_charged_total_seconds":1760},{"type":"Battery","id":1,"state":"Discharging","charge_percent":98,"charge_remaining":"01:43:14","design_capacity_mah":4400,"last_full_capacity":3013,"last_full_capacity_percent":68,"charge_remaining_hours":1,"charge_remaining_minutes":43,"charge_remaining_seconds":14,"charge_remaining_total_seconds":6194},{"type":"Battery","id":2,"state":"Discharging","charge_percent":0},{"type":"Battery","id":3,"state":"Full","charge_percent":100},{"type":"Adapter","id":0,"on-line":true},{"type":"Adapter","id":1,"on-line":false},{"type":"Thermal","id":0,"mode":"ok","temperature":46.0,"temperature_unit":"C","trip_points":[{"id":0,"switches_to_mode":"critical","temperature":127.0,"temperature_unit":"C"},{"id":1,"switches_to_mode":"hot","temperature":127.0,"temperature_unit":"C"}]},{"type":"Thermal","id":1,"mode":"ok","temperature":55.0,"temperature_unit":"C","trip_points":[{"id":0,"switches_to_mode":"critical","temperature":130.0,"temperature_unit":"C"},{"id":1,"switches_to_mode":"hot","temperature":100.0,"temperature_unit":"C"}]},{"type":"Cooling","id":0,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":1,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":2,"messages":["x86_pkg_temp no state information available"]},{"type":"Cooling","id":3,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":4,"messages":["intel_powerclamp no state information available","another message"]},{"type":"Cooling","id":5,"messages":["Processor 0 of 10"]}]
diff --git a/tests/fixtures/generic/acpi-V2.json b/tests/fixtures/generic/acpi-V2.json
index 33b75ae2..228e125a 100644
--- a/tests/fixtures/generic/acpi-V2.json
+++ b/tests/fixtures/generic/acpi-V2.json
@@ -1 +1 @@
-[{"type": "Battery", "id": 0, "state": "Charging", "charge_percent": 71, "until_charged": "00:29:20", "design_capacity_mah": 2110, "last_full_capacity": 2271, "last_full_capacity_percent": 100}, {"type": "Adapter", "id": 0, "on-line": true}, {"type": "Thermal", "id": 0, "mode": "ok", "temperature": 46.0, "temperature_unit": "C", "trip_points": [{"id": 0, "switches_to_mode": "critical", "temperature": 127.0, "temperature_unit": "C"}, {"id": 1, "switches_to_mode": "hot", "temperature": 127.0, "temperature_unit": "C"}]}, {"type": "Cooling", "id": 0, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 1, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 2, "messages": ["x86_pkg_temp no state information available"]}, {"type": "Cooling", "id": 3, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 4, "messages": ["intel_powerclamp no state information available"]}, {"type": "Cooling", "id": 5, "messages": ["Processor 0 of 10"]}]
+[{"type":"Battery","id":0,"state":"Charging","charge_percent":71,"until_charged":"00:29:20","design_capacity_mah":2110,"last_full_capacity":2271,"last_full_capacity_percent":100,"until_charged_hours":0,"until_charged_minutes":29,"until_charged_seconds":20,"until_charged_total_seconds":1760},{"type":"Adapter","id":0,"on-line":true},{"type":"Thermal","id":0,"mode":"ok","temperature":46.0,"temperature_unit":"C","trip_points":[{"id":0,"switches_to_mode":"critical","temperature":127.0,"temperature_unit":"C"},{"id":1,"switches_to_mode":"hot","temperature":127.0,"temperature_unit":"C"}]},{"type":"Cooling","id":0,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":1,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":2,"messages":["x86_pkg_temp no state information available"]},{"type":"Cooling","id":3,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":4,"messages":["intel_powerclamp no state information available"]},{"type":"Cooling","id":5,"messages":["Processor 0 of 10"]}]
diff --git a/tests/fixtures/generic/acpi-V3.json b/tests/fixtures/generic/acpi-V3.json
index 0ada3f76..86a37e9b 100644
--- a/tests/fixtures/generic/acpi-V3.json
+++ b/tests/fixtures/generic/acpi-V3.json
@@ -1 +1 @@
-[{"type": "Battery", "id": 0, "state": "Discharging", "charge_percent": 98, "charge_remaining": "01:43:14", "design_capacity_mah": 4400, "last_full_capacity": 3013, "last_full_capacity_percent": 68}, {"type": "Battery", "id": 1, "state": "Discharging", "charge_percent": 0}, {"type": "Adapter", "id": 0, "on-line": false}, {"type": "Thermal", "id": 0, "mode": "ok", "temperature": 60.5, "temperature_unit": "C", "trip_points": [{"id": 0, "switches_to_mode": "critical", "temperature": 84.0, "temperature_unit": "C"}]}, {"type": "Cooling", "id": 0, "messages": ["Processor 0 of 3"]}, {"type": "Cooling", "id": 1, "messages": ["intel_powerclamp no state information available"]}, {"type": "Cooling", "id": 2, "messages": ["Processor 0 of 3"]}, {"type": "Cooling", "id": 3, "messages": ["x86_pkg_temp no state information available"]}, {"type": "Cooling", "id": 4, "messages": ["Processor 0 of 3"]}, {"type": "Cooling", "id": 5, "messages": ["LCD 0 of 15"]}, {"type": "Cooling", "id": 6, "messages": ["Processor 0 of 3"]}]
+[{"type":"Battery","id":0,"state":"Discharging","charge_percent":98,"charge_remaining":"01:43:14","design_capacity_mah":4400,"last_full_capacity":3013,"last_full_capacity_percent":68,"charge_remaining_hours":1,"charge_remaining_minutes":43,"charge_remaining_seconds":14,"charge_remaining_total_seconds":6194},{"type":"Battery","id":1,"state":"Discharging","charge_percent":0},{"type":"Adapter","id":0,"on-line":false},{"type":"Thermal","id":0,"mode":"ok","temperature":60.5,"temperature_unit":"C","trip_points":[{"id":0,"switches_to_mode":"critical","temperature":84.0,"temperature_unit":"C"}]},{"type":"Cooling","id":0,"messages":["Processor 0 of 3"]},{"type":"Cooling","id":1,"messages":["intel_powerclamp no state information available"]},{"type":"Cooling","id":2,"messages":["Processor 0 of 3"]},{"type":"Cooling","id":3,"messages":["x86_pkg_temp no state information available"]},{"type":"Cooling","id":4,"messages":["Processor 0 of 3"]},{"type":"Cooling","id":5,"messages":["LCD 0 of 15"]},{"type":"Cooling","id":6,"messages":["Processor 0 of 3"]}]
diff --git a/tests/fixtures/generic/acpi-V4.json b/tests/fixtures/generic/acpi-V4.json
index c57f0d7a..4aaeef6e 100644
--- a/tests/fixtures/generic/acpi-V4.json
+++ b/tests/fixtures/generic/acpi-V4.json
@@ -1 +1 @@
-[{"type": "Battery", "id": 0, "state": "Full", "charge_percent": 100, "design_capacity_mah": 1969, "last_full_capacity": 2118, "last_full_capacity_percent": 100}, {"type": "Adapter", "id": 0, "on-line": true}, {"type": "Thermal", "id": 0, "mode": "ok", "temperature": 45.0, "temperature_unit": "C", "trip_points": [{"id": 0, "switches_to_mode": "critical", "temperature": 127.0, "temperature_unit": "C"}, {"id": 1, "switches_to_mode": "hot", "temperature": 127.0, "temperature_unit": "C"}]}, {"type": "Cooling", "id": 0, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 1, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 2, "messages": ["x86_pkg_temp no state information available"]}, {"type": "Cooling", "id": 3, "messages": ["Processor 0 of 10"]}, {"type": "Cooling", "id": 4, "messages": ["intel_powerclamp no state information available"]}, {"type": "Cooling", "id": 5, "messages": ["Processor 0 of 10"]}]
+[{"type":"Battery","id":0,"state":"Full","charge_percent":100,"design_capacity_mah":1969,"last_full_capacity":2118,"last_full_capacity_percent":100},{"type":"Adapter","id":0,"on-line":true},{"type":"Thermal","id":0,"mode":"ok","temperature":45.0,"temperature_unit":"C","trip_points":[{"id":0,"switches_to_mode":"critical","temperature":127.0,"temperature_unit":"C"},{"id":1,"switches_to_mode":"hot","temperature":127.0,"temperature_unit":"C"}]},{"type":"Cooling","id":0,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":1,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":2,"messages":["x86_pkg_temp no state information available"]},{"type":"Cooling","id":3,"messages":["Processor 0 of 10"]},{"type":"Cooling","id":4,"messages":["intel_powerclamp no state information available"]},{"type":"Cooling","id":5,"messages":["Processor 0 of 10"]}]