diff options
author | Marc Lasch <mlasch@mailbox.org> | 2017-05-01 21:24:54 +0200 |
---|---|---|
committer | Marc Lasch <mlasch@mailbox.org> | 2017-05-01 21:24:54 +0200 |
commit | 112f478b2fc9f5365a5e2111cde1decfb0ceff0d (patch) | |
tree | 87fbd25c5d34c85ca8eaf8a59ba18d0c8e082944 /inserter.py | |
parent | 80d80c305eb3f1227ebd88bdfb2840deb9b43682 (diff) | |
download | usv_tools-112f478b2fc9f5365a5e2111cde1decfb0ceff0d.tar.gz usv_tools-112f478b2fc9f5365a5e2111cde1decfb0ceff0d.zip |
Major library update
Diffstat (limited to 'inserter.py')
-rw-r--r-- | inserter.py | 68 |
1 files changed, 68 insertions, 0 deletions
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()) + |