Commits

Ashley Felton committed 06f5996

Alter temp logging fuunc.

  • Participants
  • Parent commits 3188a39

Comments (0)

Files changed (3)

 Flask==0.9
+python-dateutil==2.1
 #!/usr/bin/python
+import os
 import subprocess
 import time
-from datetime import datetime
+from datetime import datetime, date
+
+
+TEMP_LOGS = '/var/log/temps'
 
 
 def read_onewire_temp():
 
 def log_temp():
     # Open a file with today's date in /var/log/temps, write the temp to it.
-    f = open('/var/log/temps/temps_{0}.log'.format(datetime.strftime(datetime.now(), '%Y%m%d')), 'a')
-    f.write('{0},{1}\n'.format(datetime.now().isoformat(), read_onewire_temp()))
+    logfile = '/'.join([TEMP_LOGS, 'temps_{0}.log'.format(datetime.strftime(datetime.now(), '%Y%m%d'))])
+    f = open(logfile, 'a')
+    f.write('{0},{1}\n'.format(datetime.utcnow().isoformat(), read_onewire_temp()))
     f.close()
 
 
+def read_log(log_date=None):
+    # log_date should be a valid Python date object.
+    if log_date:
+        # Test to see if a log file exists for the nominated date.
+        logfile = '/'.join([TEMP_LOGS, 'temps_{0}.log'.format(datetime.strftime(log_date, '%Y%m%d'))])
+    else:
+        # Try using today's date instead.
+        logfile = '/'.join([TEMP_LOGS, 'temps_{0}.log'.format(datetime.strftime(date.today(), '%Y%m%d'))])
+    if os.path.exists(logfile):
+        # Open the logfile and iterate over it. Return a list of tuples:
+        # (datetime_string, float)
+        temps = []
+        f = open(logfile, 'r')
+        for l in f.readlines():
+            l = l.strip().split(',')  # Strip newline char, split on comma.
+            temps.append((l[0], float(l[1])))
+        return temps
+    else:
+        return None
+
+
 if __name__ == "__main__":
     log_temp()
 from flask import request, render_template
-from temper import app
+from temper import app, utils
+from datetime import datetime
+
 
 
 @app.route('/')
 def index():
+    temps_list = utils.read_log()
     return render_template('index.html')