Skip to content

Commit

Permalink
automatic logging to CSV file works!
Browse files Browse the repository at this point in the history
  • Loading branch information
Yann Büchau committed Jul 25, 2016
1 parent c03d066 commit a6c7396
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
4 changes: 4 additions & 0 deletions etc/co2monitor/co2monitor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion usr/lib/co2monitor/co2device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
54 changes: 52 additions & 2 deletions usr/lib/co2monitor/co2monitor-service
Original file line number Diff line number Diff line change
Expand Up @@ -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__)),
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 ###
Expand All @@ -70,5 +119,6 @@ if __name__ == "__main__":
else:
service.setup_device(device)

while True:
logger.info(service.device.read())
# start the logloop
service.logloop()

0 comments on commit a6c7396

Please sign in to comment.