#!/usr/bin/env python3 from apscheduler.schedulers.background import BlockingScheduler from usv_driver import Usv from urllib.request import urlopen from pyinflux.client import Line import sys import argparse import logging from time import sleep INTERVAL=24 # execution takes about 6s parser = argparse.ArgumentParser() parser.add_argument('--url', help='URL') parser.add_argument('--serial', help='Serial port', default='/dev/ttyU0') parser.add_argument('--baud', help='Serial baudrate', default='600') parser.add_argument('--param', help='Parameter JSON file', default='./usv_param.json') args = parser.parse_args(sys.argv[1:]) logging.basicConfig(level=logging.DEBUG, filename='log/usv_scheduler.log', format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') usv = Usv(args.serial, int(args.baud), args.param) def timed_job(): parameters = usv.get_parameters() data = [] for name in parameters: line = Line('usv_parameters', {'name': name, 'id': 'LI????VA' }, {'value': parameters[name]}) data.append(line) logging.info("insert: {}".format(",".join( "{}={}".format(name,parameters[name]) for name in parameters))) try: post_data = '\n'.join(map(str, data)) logging.info("urlopen begin: {}".format(post_data)) with urlopen(args.url, post_data.encode('utf-8')) as fh: logging.info(fh.read().decode('utf-8')) logging.info("urlopen done") except Exception as e: logging.error("Errr while trying to insert", e) while True: timed_job() sleep(INTERVAL)