summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-x.gitignore4
-rwxr-xr-xusv_driver.py66
-rw-r--r--usv_param.json11
-rwxr-xr-xusv_scheduler.py49
4 files changed, 128 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 5684153..354491d 100755
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,5 @@
*.py[cod]
-
+*~
# C extensions
*.so
@@ -33,4 +33,4 @@ nosetests.xml
# Mr Developer
.mr.developer.cfg
.project
-.pydevproject \ No newline at end of file
+.pydevproject
diff --git a/usv_driver.py b/usv_driver.py
new file mode 100755
index 0000000..c8ba8bc
--- /dev/null
+++ b/usv_driver.py
@@ -0,0 +1,66 @@
+#!/usr/bin/env python3
+
+import serial
+import json
+import re
+
+class Usv(object):
+ def __init__(self, device, baudrate, json_file):
+ self._serial_port = serial.Serial(device,
+ baudrate=baudrate)
+
+ # force a clean prompt
+ self._serial_port.write(b"\r")
+ self._read_until_prompt()
+ #print("cleaned prompt")
+ with open(json_file) as fhandle:
+ self.json_params = json.load(fhandle)
+
+
+ def _read_until_prompt(self):
+ # 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
+
+ def get_parameters(self):
+ # [{'name': 'acvoltsin', 'index': 1},
+ results = {}
+
+ for parameter in self.json_params:
+ outbuf = "d %s\r" % parameter['num']
+ self._serial_port.write(outbuf.encode())
+ inbuf = self._read_until_prompt()
+
+ sbuf = ""
+ for char in inbuf:
+ sbuf += char.decode("utf-8")
+ #print("-->%s<--" % sbuf)
+
+ regex = parameter['regex']
+ match = re.search(regex, sbuf, re.MULTILINE)
+
+ if match:
+ name = parameter['name']
+ value = float(match.group(1))
+
+ results.setdefault(name, value)
+ #print("MATCH: %s: %f" % (name,value))
+
+ return results
+
+
+
+if __name__ == "__main__":
+ import pprint
+
+ usv = Usv("/dev/ttyU0", 600, "usv_param.json")
+ parameters = usv.get_parameters()
+ pprint.pprint(parameters)
+
diff --git a/usv_param.json b/usv_param.json
new file mode 100644
index 0000000..39e5698
--- /dev/null
+++ b/usv_param.json
@@ -0,0 +1,11 @@
+[{"name": "acvoltsin", "num": 1, "regex": "01\\ V\\ In\\ +([0-9]+[.]?[0-9]*)"},
+{"name": "acvoltsout", "num": 2, "regex": "02\\ V\\ Out\\ +([0-9]+[.]?[0-9]*)"},
+{"name": "vaout", "num": 5, "regex": "05\\ VA\\ Out\\ +([0-9]+[.]?[0-9]*)"},
+{"name": "vbattery", "num": 7, "regex": "07\\ V\\ Batt\\ +([0-9]+[.]?[0-9]*)"},
+{"name": "frequency", "num": 8, "regex": "08\\ Freq\\ ([0-9]+[.]?[0-9]*)\\ Hz"},
+{"name": "fullload", "num": 16, "regex": "16\\ FullLoad%\\ ([0-9]+[.]?[0-9]*)"},
+{"name": "watts", "num": 17, "regex": "17\\ Watts\\ +([0-9]+[.]?[0-9]*)"},
+{"name": "powerfactor", "num": 18, "regex": "18\\ PF\\ +([0-9]+[.]?[0-9]*)\\ ----"},
+{"name": "crestfactor", "num": 19, "regex": "19\\ CrestF\\ +([0-9]+[.]?[0-9]*)"},
+{"name": "powerout", "num": 20, "regex": "20\\ #PwrOut\\ +([0-9]+[.]?[0-9]*)"},
+{"name": "inverterminutes", "num": 23, "regex": "23\\ InvMin\\ +([0-9]+[.]?[0-9]*)"}]
diff --git a/usv_scheduler.py b/usv_scheduler.py
new file mode 100755
index 0000000..1dccc5f
--- /dev/null
+++ b/usv_scheduler.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python3
+
+from apscheduler.schedulers.background import BlockingScheduler
+from usv_driver import Usv
+from influxdb import InfluxDBClient
+
+import sys
+import argparse
+import logging
+
+INTERVAL=14 # execution takes about 6s
+
+logging.basicConfig(level=logging.INFO)
+
+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('--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)
+
+usv = Usv(args.serial, int(args.baud), args.param)
+
+@scheduler.scheduled_job('interval', seconds=INTERVAL)
+def timed_job():
+ parameters = usv.get_parameters()
+ data = []
+ for name in parameters:
+ data.append({
+ 'measurement': 'usv_parameters',
+ 'tags': {
+ 'name': name,
+ 'id': 'LI????VA',
+ },
+ 'fields': {'value': parameters[name]}
+ });
+ logging.info("insert: %s: %s" % (name, parameters[name]))
+
+ db_client.write_points(data)
+
+scheduler.start()