diff options
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()) + |