summaryrefslogtreecommitdiff
path: root/inserter.py
blob: 70088c93edbf895373d0f8ec0664c839ea63edea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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())