changeset 3:535076e31660

Parse the result.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 20 Nov 2013 14:45:16 +1030
parents 8f1a773a3cd5
children 10a16898903d
files iec1107.py
diffstat 1 files changed, 33 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/iec1107.py	Wed Nov 20 13:16:56 2013 +1030
+++ b/iec1107.py	Wed Nov 20 14:45:16 2013 +1030
@@ -4,12 +4,15 @@
 # http://www.domoticaforum.eu/viewtopic.php?f=71&t=7489
 #
 
+import datetime
 import exceptions
+import re
 import serial
 import sys
 import time
 
 baudtable = {'0' : 300, '1' : 600, '2' : 1200, '3' : 2400, '4' : 4800, '5' : 9600, '6' : 19200}
+parsere = re.compile('([0-9A-Z](\.[0-9A-Z]){1,})\((.*)\)')
 
 class Error(exceptions.BaseException):
     pass
@@ -76,10 +79,11 @@
         # Read result lines
         while True:
             line = s.readline()
-            cksum ^= reduce(lambda x, y: x ^ y, map(ord, line))
             if len(line) == 0:
                 raise Error('Timeout during message')
-            if line == '!':
+
+            cksum ^= reduce(lambda x, y: x ^ y, map(ord, line))
+            if line.strip() == '!':
                 break
             lines.append(line)
 
@@ -97,6 +101,33 @@
         self.rawreading = lines
         del s
 
+        self.parse()
+        self.readdate = datetime.datetime.now()
+
+    def parse(self):
+        for l in self.rawreading:
+            m = parsere.match(l)
+            if m == None:
+                raise Error('Unable to parse result \"%s\"' % (l))
+
+            (code, xxx, value) = m.groups()
+            if code == 'C.1':
+                self.meterid, date = value.split('(')
+                # XXX: The meter I have is an hour slow
+                self.meterdate = datetime.datetime.strptime(date, '%H:%M %d-%m-%y')
+            elif code == '1.8.0':
+                self.importWh = int(value[0:-3])
+            elif code[0:4] == '1.8.':
+                # Differing tarrifs which I don't care about
+                pass
+            elif code == '2.8.0':
+                self.exportWh = int(value[0:-3])
+            else:
+                print 'Unknown code', code
+
+    def __str__(self):
+        return 'Time: %s, Meter: %s, Import: %d Wh, Export: %d Wh' % (self.readdate.strftime('%Y/%m/%d %H:%M'),
+                                                             self.meterid, self.importWh, self.exportWh)
 def main():
     if len(sys.argv) != 2:
         print 'Bad usage'