From 2eaa8d33a8ef792a9c10543fe2551e0680ab97b7 Mon Sep 17 00:00:00 2001 From: momo Date: Mon, 24 Oct 2011 10:38:29 +0200 Subject: //shack/strom: initial commit --- shack/strom/main.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 shack/strom/main.py (limited to 'shack/strom/main.py') diff --git a/shack/strom/main.py b/shack/strom/main.py new file mode 100644 index 00000000..e1a85d02 --- /dev/null +++ b/shack/strom/main.py @@ -0,0 +1,50 @@ +#! /usr/bin/python +# -*- coding utf-8 -*- + +from __future__ import division + +import re + + +class Reader(object): + _re = re.compile(r'^(?P\d-\d:\d+\.\d+\.\d+\*\d+)\((?P\S+?)(?:\*[VAW])?\)$') + + def _convert_periode(value): + return int(value, 16) / 100 + + fields = { + '1-0:1.8.0*255': ('overall', float), + '1-0:31.7.0*255': ('l1_strom', float), + '1-0:32.7.0*255': ('l1_spannung', float), + '1-0:51.7.0*255': ('l2_strom', float), + '1-0:52.7.0*255': ('l2_spannung', float), + '1-0:71.7.0*255': ('l3_strom', float), + '1-0:72.7.0*255': ('l3_spannung', float), + '1-0:96.50.0*1': ('periode', _convert_periode), + } + + def __init__(self, f): + self._file = f + + def __iter__(self): + data = {} + for line in self._file: + line = line.strip() + if line == '!': + yield data + data = {} + continue + r = self._re.match(line) + if not r: + continue + field = self.fields.get(r.group('field')) + if field: + data[field[0]] = field[1](r.group('value')) + #uncomment to print unmapped values + #else: + # print r.groups() + + +data_file = open('testdata') +for data in Reader(data_file): + print data -- cgit v1.2.3