summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Lasch <mlasch@mailbox.org>2017-04-16 17:08:31 +0200
committerMarc Lasch <mlasch@mailbox.org>2017-04-16 17:08:31 +0200
commitd50ca49bb9dcf5640487df2044b62d63432b6764 (patch)
tree5ba8de4732f2a2143ba704aa67ff2ac534ac5df0
parentb4c39e0ea54791d3c56d9d42b1368bd3dbce5be3 (diff)
downloadusv_tools-d50ca49bb9dcf5640487df2044b62d63432b6764.tar.gz
usv_tools-d50ca49bb9dcf5640487df2044b62d63432b6764.zip
Minor changes
-rwxr-xr-xusv_driver.py49
-rwxr-xr-xusv_scheduler.py66
2 files changed, 52 insertions, 63 deletions
diff --git a/usv_driver.py b/usv_driver.py
index 5cb1167..bdf8656 100755
--- a/usv_driver.py
+++ b/usv_driver.py
@@ -8,27 +8,40 @@ class Usv(object):
def __init__(self, device, baudrate, json_file):
self._serial_port = serial.Serial(device,
baudrate=baudrate,
- timeout=10)
+ timeout=0.8)
# force a clean prompt
- self._serial_port.write(b"\r")
- self._read_until_prompt()
+ #self._serial_port.write(b"\r")
+ self._read_until_prompt("\r")
#print("cleaned prompt")
with open(json_file) as fhandle:
self.json_params = json.load(fhandle)
- def _read_until_prompt(self):
+ def _read_until_prompt(self, outbuf):
# match on "\n=>"
- prompt = False
- inbuf = []
- while not prompt:
- inbuf.append(self._serial_port.read(1))
- if len(inbuf) > 2:
- if inbuf[-3] == b"\n" and \
- inbuf[-2] == b"=" and inbuf[-1] == b">":
- prompt = True
- return inbuf
+ self._serial_port.write(outbuf.encode())
+
+ inbuf = self._serial_port.read(100)
+ sbuf = inbuf.decode("utf-8")
+
+ #prompt = False
+ #inbuf = []
+ #while not prompt:
+ # pending = self._serial_port.read(1)
+ # if len(pending) == 0:
+ # self._serial_port.write(b"\r")
+ # else:
+ # inbuf.append(pending)
+ # print(pending)
+ #if len(sbuf) > 2:
+ # if sbuf[-3] == "\n" and \
+ # sbuf[-2] == "=" and sbuf[-1] == ">":
+ # return sbuf
+ # else:
+ # sbuf = self._read_until_prompt(outbuf)
+
+ return sbuf
def get_parameters(self):
# [{'name': 'acvoltsin', 'index': 1},
@@ -36,16 +49,10 @@ class Usv(object):
for parameter in self.json_params:
outbuf = "d %s\r" % parameter['num']
- self._serial_port.write(outbuf.encode())
- inbuf = self._read_until_prompt()
+ inbuf = self._read_until_prompt(outbuf)
- sbuf = ""
- for char in inbuf:
- sbuf += char.decode("utf-8")
- #print("-->%s<--" % sbuf)
-
regex = parameter['regex']
- match = re.search(regex, sbuf, re.MULTILINE)
+ match = re.search(regex, inbuf, re.MULTILINE)
if match:
name = parameter['name']
diff --git a/usv_scheduler.py b/usv_scheduler.py
index 09bcf2b..95accaf 100755
--- a/usv_scheduler.py
+++ b/usv_scheduler.py
@@ -1,70 +1,52 @@
#!/usr/bin/env python3
-
-from time import sleep
-
from apscheduler.schedulers.background import BlockingScheduler
from usv_driver import Usv
-from influxdb import InfluxDBClient
+from urllib.request import urlopen
+from pyinflux.client import Line
import sys
import argparse
import logging
+from time import sleep
-INTERVAL=14 # execution takes about 6s
-
+INTERVAL=24 # execution takes about 6s
parser = argparse.ArgumentParser()
-parser.add_argument('--host', help='Database host', default='localhost')
-parser.add_argument('--port', help='Database port', default='8086')
-parser.add_argument('--db', help='Database name', default='test')
+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:])
-#scheduler = BlockingScheduler(timezone="Europe/Berlin")
-
-db_client = InfluxDBClient(host=args.host,
- port=args.port,
- database=args.db)
-
-logging.basicConfig(level=logging.ERROR, filename='log/usv_scheduler.log')
+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)
-usv_lock = False
-
-#@scheduler.scheduled_job('interval', seconds=INTERVAL)
def timed_job():
- global usv_lock
-
- if usv_lock:
- logging.info("JOB CURRENTLY LOCKED")
- return
-
- usv_lock = True
parameters = usv.get_parameters()
data = []
for name in parameters:
- data.append({
- 'measurement': 'usv_parameters',
- 'tags': {
- 'name': name,
- 'id': 'LI????VA',
- },
- 'fields': {'value': parameters[name]}
- });
- print("insert: %s" % ",".join("%s=%s" % \
- (name,parameters[name]) 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:
- db_client.write_points(data)
- except Exception as e:
- logging.error("%s" % e)
+ 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")
- usv_lock = False
+ except Exception as e:
+ logging.error("Errr while trying to insert", e)
while True:
timed_job()
- sleep(24)
+ sleep(INTERVAL)
-#scheduler.start()