diff --git a/app/datalog.py b/app/datalog.py index d5a8a27..20ba071 100644 --- a/app/datalog.py +++ b/app/datalog.py @@ -69,16 +69,22 @@ class DataLogger: for i in range(SENSOR_BASEID, SENSOR_BASEID + 8): (count, data) = self.ioif.bb_i2c_zip(P_SDA, [I2C_SET_ADDR, i, I2C_START, I2C_READ, SENSOR_DATALEN, I2C_STOP, I2C_END]) columns.append(str(i)) - if count > 0: - data_fast[i-SENSOR_BASEID] += int(data[0]) + int(data[1]) / 256.0 - data_perm[i-SENSOR_BASEID] += int(data[0]) + int(data[1]) / 256.0 + if count > 1: + sign = 1 + if data[0] > 128: + print "sign" + data[0] = data[0]-128 + sign = -1 + data_fast[i-SENSOR_BASEID] += sign * (int(data[0]) + (int(data[1]) / 256.0)) + data_perm[i-SENSOR_BASEID] += sign * (int(data[0]) + (int(data[1]) / 256.0)) else: data_fast[i-SENSOR_BASEID] = np.nan data_perm[i-SENSOR_BASEID] = np.nan + time.sleep(0.05) infoline = np.empty((1, 9), dtype=object) - if n_fast % 2 == 0: + if n_fast % 10 == 0: infoline[0, 1:9] = data_fast[0:8] / n_fast n_fast = 0 infoline[0, 0] = dt.datetime.now() @@ -86,7 +92,7 @@ class DataLogger: self.savetofile(False, pdataline) data_fast = np.zeros(8) - if n_perm % 120 == 0: + if n_perm % 100 == 0: infoline[0, 1:9] = data_perm[0:8] / n_perm n_perm = 0 infoline[0, 0] = dt.datetime.now() @@ -96,7 +102,7 @@ class DataLogger: n_fast += 1 n_perm += 1 - time.sleep(0.5) + def savetofile(self, permanent, data): @@ -104,7 +110,7 @@ class DataLogger: date = dt.datetime.now().strftime('%Y%m%d') exists = os.path.isfile('app/static/logdata_' + date + '.csv') with open('app/static/logdata_' + date + '.csv', 'a+') as f: - data.to_csv(f, header=False, index=False) + data.to_csv(f, header=not exists, index=False) else: exists = os.path.isfile('/tmp/baroness_logdata_fast.csv') with open('/tmp/baroness_logdata_fast.csv', 'a+') as f: @@ -121,4 +127,4 @@ if __name__ == "__main__": print "creating datalogger" logger = DataLogger() while(1): - time.sleep(1) \ No newline at end of file + time.sleep(1) diff --git a/app/plot.py b/app/plot.py index 091cd91..0109e94 100644 --- a/app/plot.py +++ b/app/plot.py @@ -1,9 +1,11 @@ -#import matplotlib #speed? -#matplotlib.use('GTKAgg') +import matplotlib #speed? +matplotlib.use('TkAgg') from matplotlib import pyplot as plt from matplotlib import units +from matplotlib import patheffects from matplotlib.dates import WeekdayLocator, DayLocator, HourLocator, DateFormatter, drange, MONDAY +from matplotlib import rcParams as rcp import numpy as np from user import User from database import * @@ -27,12 +29,12 @@ class Plotter: ptime = 0 while self.active: logging.info("Plotter plotting stuff") - logfiles = list() - logfiles += glob.glob("static/logdata*.csv") + logfiles += glob.glob("app/static/logdata*.csv") logfiles.append("/tmp/baroness_logdata_fast.csv") + print logfiles if logfiles: - plot_log(logfiles, hours = 2) + plot_log(logfiles, hours = 4) # plotter not fully integrated yet #if ptime % 30 or self.joblist: @@ -136,11 +138,11 @@ def plot_total(user=None): fils = "app/static/total%03d.png" % user.id fill = "app/static/total%03d_big.png" % user.id plt.title(tit) - logging.info("plot plot_total " + str(datetime.datetime.now())) + logging.info("Plot plot_total " + str(datetime.datetime.now())) #480x320 fig.set_size_inches(4.8, 3.2) plt.savefig(fils, dpi=100) - logging.info("end plot_total " + str(datetime.datetime.now())) + logging.info("Plot: End plot_total " + str(datetime.datetime.now())) #fig.set_size_inches(4.8, 3.2) #plt.savefig(fill, dpi=400) @@ -154,7 +156,7 @@ def plot_log(logfiles, hours): for logfile in logfiles: try: d = pd.read_csv(logfile, parse_dates=[0]) - if data: + if data is not None: data = pd.concat((data, d)) else: data = d @@ -163,36 +165,43 @@ def plot_log(logfiles, hours): return 0 #ipdb.set_trace() + data = data.sort('time') + data = data[data.time > begin] data = data.set_index(pd.DatetimeIndex(data.time)) data = data.drop("time", axis=1) #ipdb.set_trace() plt.xkcd() + rcp['path.effects'] = [patheffects.withStroke(linewidth=0)] #all columns + for item, frame in data.iteritems(): + if frame is None or frame.empty or frame.isnull().values.all(): + continue f = plt.figure() #f.patch.set_facecolor("#ccefff") #f.patch.set_alpha("0.0") logging.debug("column " + str(item) + " " + frame.name) - plt.plot(data.index.to_pydatetime(), frame, "b") + plt.plot(data.index.to_pydatetime(), frame)#"b" ax = plt.gca() #ax.grid(True, linewidth=1.0) - plt.xlim(begin, end) - ax.spines['right'].set_visible(False) - ax.spines['top'].set_visible(False) + #plt.xlim(begin, end) + #ax.spines['right'].set_visible(False) + #ax.spines['top'].set_visible(False) ax.yaxis.set_ticks_position('none')#('left') ax.xaxis.set_ticks_position('none')#('bottom') - f.autofmt_xdate() - - #plt.tick_params(which='minor', length=4) - #plt.tick_params(which='major', length=5) - #ipdb.set_trace() - ax.fmt_xdata = DateFormatter('%d.%m') - #ax.xaxis.set_major_locator(WeekdayLocator(MONDAY)) - #ax.xaxis.set_major_locator(DayLocator()) - ax.xaxis.set_major_formatter(DateFormatter('%d.%m')) - - + try: #might not work if few data available + f.autofmt_xdate() + #plt.tick_params(which='minor', length=4) + #plt.tick_params(which='major', length=5) + #ipdb.set_trace() + #ax.fmt_xdata = AutoDateFormatter('%d.%m/%H') + #ax.xaxis.set_major_locator(WeekdayLocator(MONDAY)) + ax.xaxis.set_major_locator(AutoDateLocator()) + ax.xaxis.set_major_formatter(AutoDateFormatter(defaultfmt='%d.%m')) + #f.autofmt_xdate() + except: + logging.warning("Plot: Few data!") #plt.xlabel('Datum') #plt.ylabel('Temperatur / C') outfile = "app/static/log_" + str(item) + ".png" @@ -290,4 +299,4 @@ def plot_list(duration): #1024x768 #fig.set_size_inches(10.24, 7.68) #plt.savefig('app/static/bierliste.png', dpi=100) - logging.info("Plot End plot_list " + str(datetime.datetime.now())) \ No newline at end of file + logging.info("Plot: End plot_list " + str(datetime.datetime.now())) diff --git a/app/rfid.py b/app/rfid.py index a143c2f..94c60f0 100644 --- a/app/rfid.py +++ b/app/rfid.py @@ -1,3 +1,4 @@ +import logging try: import MFRC522 except: @@ -6,14 +7,13 @@ except: import signal import thread import time -import logging class RFID: def __init__(self, callbackf): logging.info("RFID Reader initialized!") - self.reader = MFRC522.MFRC522() + self.reader = MFRC522.MFRC522(spd=500000) signal.signal(signal.SIGINT, self.stop) self.callback = callbackf self.loop = True