From a6c73968e0faa3b56caea4050669b310d3fb2a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yann=20B=C3=BCchau?= Date: Mon, 25 Jul 2016 14:40:55 +0200 Subject: [PATCH] automatic logging to CSV file works! --- etc/co2monitor/co2monitor.conf | 4 ++ usr/lib/co2monitor/co2device.py | 7 +++- usr/lib/co2monitor/co2monitor-service | 54 ++++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/etc/co2monitor/co2monitor.conf b/etc/co2monitor/co2monitor.conf index afb03b9..946f69a 100644 --- a/etc/co2monitor/co2monitor.conf +++ b/etc/co2monitor/co2monitor.conf @@ -7,3 +7,7 @@ logfile = /var/log/co2monitor.log # one of debug - info - warning - error or critial # defaults to debug which is the most verbose loglevel = debug + +[data-logging] +# data logfile +datafolder = /var/cache/co2monitor/data diff --git a/usr/lib/co2monitor/co2device.py b/usr/lib/co2monitor/co2device.py index 4c0abf4..5fded54 100644 --- a/usr/lib/co2monitor/co2device.py +++ b/usr/lib/co2monitor/co2device.py @@ -129,7 +129,7 @@ def read(self): # read data from co2monitor ( this takes a couple of seconds ) try: - logger.debug("reading from device '{}'".format(self.device)) + #logger.debug("reading from device '{}'".format(self.device)) data = self.read_raw() except IOError: logger.error("Could not read from device '{}'".format(self.device)) @@ -171,5 +171,10 @@ def read(self): self.last_temp = measure['temperature'] else: measure['temperature'] = self.last_temp + + # rudimentary check + if not measure['co2'] is None: + if measure['co2'] > 5000: + measure['co2'] = None return measure diff --git a/usr/lib/co2monitor/co2monitor-service b/usr/lib/co2monitor/co2monitor-service index 7430856..89ba9fe 100755 --- a/usr/lib/co2monitor/co2monitor-service +++ b/usr/lib/co2monitor/co2monitor-service @@ -2,6 +2,8 @@ import logging import configparser import sys, os, glob +import csv +import time # add /usr/lib/co2monitor to the module paths sys.path.insert(1,os.path.join(os.path.dirname(os.path.realpath(__file__)), @@ -18,11 +20,21 @@ logger = logging.getLogger(__name__) ########################## class co2monitorService(object): def __init__(self): + # set up configi + self.config_setup() + + # set up logging + self.logging_setup() + + # set up config + def config_setup(self): # read config configfiles = glob.glob("/etc/co2monitor/*") self.config = configparser.ConfigParser() self.config.read(configfiles) + # set up logging according to config + def logging_setup(self): # initialize logging # set loglevel possiblities loglevels = { @@ -54,6 +66,43 @@ class co2monitorService(object): def setup_device(self, device): self.device = co2device.co2device(device) + # loop + def logloop(self): + datafolder = self.config.get('data-logging','datafolder') + fieldnames = ['time','temperature','co2'] + datafile = "{folder}/co2monitor-{time}.csv".format( + time = time.strftime("%Y%m%d%H%M%S",time.localtime()), + folder = datafolder + ) + + with open(datafile, 'w') as csvfile: # open file + logger.info("opened {} for data logging.".format(datafile)) + writer = csv.DictWriter(csvfile, fieldnames = fieldnames) + + writer.writeheader() + measold = {} + while True: # read as long as you can + meas = self.device.read() # read from device + # set time + meas['time'] = time.strftime("%Y-%m-%d %H:%M:%S", + time.localtime()) + + # check if this measurement introduces something new + if meas.get('temperature',None) != \ + measold.get('temperature',None) or \ + meas.get('co2',None) != measold.get('co2',None): + # measurement is not only None + if not meas.get('temperature',None) is None or \ + not meas.get('co2',None) is None: + writer.writerow(meas) # write + csvfile.flush() # writeout + + + # update old measurements + measold.update(meas) + + + ### main program ### @@ -70,5 +119,6 @@ if __name__ == "__main__": else: service.setup_device(device) - while True: - logger.info(service.device.read()) + # start the logloop + service.logloop() +