From dfc9c8857ee92a6ae5c9166bb8635e902ef0e1ae Mon Sep 17 00:00:00 2001 From: anomen Date: Tue, 2 Jun 2015 16:34:44 +0200 Subject: [PATCH] raspi - customs - sensors --- raspi/customs/usr/local/bin/sensors | 110 +++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 28 deletions(-) diff --git a/raspi/customs/usr/local/bin/sensors b/raspi/customs/usr/local/bin/sensors index b9491b2..0dffaf1 100755 --- a/raspi/customs/usr/local/bin/sensors +++ b/raspi/customs/usr/local/bin/sensors @@ -9,15 +9,26 @@ import glob import subprocess import datetime -VERSION=0.2 +VERSION=0.3 ALTITUDE=0 +PID_FILE='/run/shm/sensors.pid' def parseArguments(): - parser = argparse.ArgumentParser(description='Raspberry Pi sensors reader.') - parser.add_argument('-l', '--log', dest='log', help='single line output for log',action='store_true') - parser.add_argument('-a', '--altitude', dest='altitude', help='altitude for computing relative pressure', default=300) + parser = argparse.ArgumentParser(description='Raspberry Pi sensors reader.', + usage='%%(prog)s %s [options]' % VERSION) + parser.add_argument('-l', '--log', dest='log', action='store_true', + help='single line output for log') + parser.add_argument('-a', '--altitude', dest='altitude', default=300, + help='altitude for computing relative pressure, default value is 300m') + parser.add_argument('-p', '--pidfile', nargs='?', dest='pidfile', const=PID_FILE, + help='Run only if pid-file is not present, by default it is %s.' % PID_FILE) + parser.add_argument('-e', dest='external', action='store_true', + help='Print only external sensors.') + parser.add_argument('-i', dest='internal', action='store_true', + help='Print only internal sensors.') + args = parser.parse_args() @@ -25,9 +36,7 @@ def parseArguments(): def usage(): - print "sensors", VERSION, "print Raspberry Pi sensor data" - print - print "Usage: sensors [-l] [-a alt]" + parser.print_help() sys.exit(1) def readfile(filename): @@ -38,6 +47,25 @@ def readfile(filename): f.close() return data +def check_pid_file(pidfile): + return None + +def create_pid_file(pidfile): + f = None + try: + fd = os.open(pidfile, os.O_WRONLY | os.O_CREAT | os.O_EXCL) + f = os.fdopen(fd, "w") + f.write(str(os.getpid())) + except OSError, e: + return pidfile + finally: + if (f): + f.close() + return None + +def delete_pid_file(pidfile): + os.remove(pidfile) + def f2s(data): return '{0:.1f}'.format(data); @@ -101,36 +129,62 @@ def get_date(): return today.strftime('%Y-%m-%d %H:%M') -def log_line(): +def log_line(internal, external): result = get_date(); - rdata = get_raspi_data() - result += ' ' + rdata[0]; - - w1devices = glob.glob('/sys/bus/w1/devices/*/w1_slave') - w1devices.sort() - for w1dev in w1devices: - t = get_w1_data(w1dev) - result += ' ' + f2s(t[1]) - - bmpdata = get_bmp085_data('/sys/bus/i2c/drivers/bmp085/1-0077') - result += ' ' + f2s(bmpdata[2]) + if (internal): + rdata = get_raspi_data() + result += ' ' + rdata[0]; + else: + result += ' *' + + + if (external): + w1devices = glob.glob('/sys/bus/w1/devices/*/w1_slave') + w1devices.sort() + for w1dev in w1devices: + t = get_w1_data(w1dev) + result += ' ' + f2s(t[1]) + else: + result += ' * *' + + if (internal): + bmpdata = get_bmp085_data('/sys/bus/i2c/drivers/bmp085/1-0077') + result += ' ' + f2s(bmpdata[2]) + else: + result += ' *' return result def main(): global ALTITUDE args = parseArguments() - ALTITUDE = int(args.altitude) - if (args.log): - print log_line() - return + if (args.pidfile): + exist_pid = create_pid_file(args.pidfile) + if (exist_pid): + print "Cannot create pid file %s" % exist_pid + sys.exit(1) + + try: + ALTITUDE = int(args.altitude) + + if (args.log): + print log_line( + not args.external or args.internal, + not args.internal or args.external) + return + + if (not args.external or args.internal): + print "Internal: " + raspi() + bmp085() + if (not args.internal or args.external): + print "External: " + w1() + finally: + if (args.pidfile): + delete_pid_file(args.pidfile) - print "Internal: " - raspi() - bmp085() - print "External: " - w1() if __name__ =='__main__':main() -- 2.11.4.GIT