From 112f478b2fc9f5365a5e2111cde1decfb0ceff0d Mon Sep 17 00:00:00 2001 From: Marc Lasch Date: Mon, 1 May 2017 21:24:54 +0200 Subject: Major library update --- inserter.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 inserter.py (limited to 'inserter.py') diff --git a/inserter.py b/inserter.py new file mode 100644 index 0000000..70088c9 --- /dev/null +++ b/inserter.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import sys +import argparser + +from time import time, sleep +from urllib.request import urlopen + +from usv import Usv +from pyinfluxdb import Line + +class UsvInserter(object): + TIME = 10 + + def __init__(self, port, url): + self.port = port + self.url = url + + self.usv = Usv(port) + self.duration = 0 + + def job(self): + start = time() + + fields = self.usv.read() + + data = [] + for name, value in fields.items(): + line = Line( + 'usv_parameters', + {'name': name, 'id': 'LI????VA' }, + {'value': value} + ) + data.append(line) + + post_data = '\n'.join(map(str, data)) + + print(post_data) + + with urlopen(self.url, post_data.encode('utf-8')) as fh: + print(fh.read().decode('utf-8')) + + self.duration = time() - start + + def get_duration(self): + return self.duration + + +if __name__ == '__main__': + """ inserter """ + + parser = argparse.ArgumentParser() + parser.add_argument('--url', help='URL') + parser.add_argument('--port', help='Serial port device', + default='/dev/ttyUSV') + args = parser.parse_args(sys.argv[1:]) + + usv_inserter = UsvInserter(args.port, args.url) + + print('Start inserter with url {} on port {}'.format(args.url, args.port)) + + while True: + usv_inserter.job() + + if usv_inserter.get_duration() > 0: + sleep(UsvInserter.TIME - UsvInserter.get_duration()) + -- cgit v1.2.1