#!/usr/bin/env python3
import sys
import time

from common.realtime import Ratekeeper
from common.filter_simple import FirstOrderFilter
from tools.zookeeper import Zookeeper

if __name__ == "__main__":
  z = Zookeeper()
  z.set_device_power(True)
  z.set_device_ignition(False)

  duration = None
  if len(sys.argv) > 1:
    duration = int(sys.argv[1])

  rate = 123
  rk = Ratekeeper(rate, print_delay_threshold=None)
  fltr = FirstOrderFilter(0, 5, 1. / rate, initialized=False)

  try:
    start_time = time.monotonic()
    while duration is None or time.monotonic() - start_time < duration:
      fltr.update(z.read_power())
      if rk.frame % rate == 0:
        print(f"{fltr.x:.2f} W")
      rk.keep_time()
  except KeyboardInterrupt:
    pass

@@ -38,16 +38,32 @@
 ort time
+%0Aimport datetime
 %0A%0Afrom c
@@ -497,15 +497,26 @@
 %0A%0A  
-try:%0A  
+measurements = %5B%5D%0A
   st
@@ -542,16 +542,24 @@
 otonic()
+%0A%0A  try:
 %0A    whi
@@ -721,16 +721,52 @@
 2f%7D W%22)%0A
+        measurements.append(fltr.x)%0A
       rk
@@ -815,8 +815,168 @@
    pass%0A
+%0A  t = datetime.timedelta(seconds=time.monotonic() - start_time)%0A  avg = sum(measurements) / len(measurements)%0A  print(f%22%5CnAverage power: %7Bavg:.2f%7DW over %7Bt%7D%22)%0A

add avg power to power_monitor.py