diff options
author | momo <momo+github@mo2-systems.com> | 2011-10-24 10:38:29 +0200 |
---|---|---|
committer | momo <momo+github@mo2-systems.com> | 2011-10-24 10:38:29 +0200 |
commit | 2eaa8d33a8ef792a9c10543fe2551e0680ab97b7 (patch) | |
tree | 0cc4291ae2345fe74d55770176a4915fbdd4a2b7 /shack/strom/main.py | |
parent | 691e77d4e0c55bffa1f8c6ecc761d3de9b30062d (diff) |
//shack/strom: initial commit
Diffstat (limited to 'shack/strom/main.py')
-rw-r--r-- | shack/strom/main.py | 50 |
1 files changed, 50 insertions, 0 deletions
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<field>\d-\d:\d+\.\d+\.\d+\*\d+)\((?P<value>\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 |