changeset 10:c1892bd1460a

Handle SI unit prefixes.
author Daniel O'Connor <darius@dons.net.au>
date Thu, 21 Nov 2013 12:34:13 +1030
parents 139e267c9965
children d3b5e4251950
files iec1107.py
diffstat 1 files changed, 25 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/iec1107.py	Thu Nov 21 00:01:28 2013 +1030
+++ b/iec1107.py	Thu Nov 21 12:34:13 2013 +1030
@@ -138,18 +138,40 @@
             (code, xxx, value) = m.groups()
             if code == 'C.1':
                 self.meterid, date = value.split('(')
-                # XXX: The meter I have is an hour slow
+                # XXX: The meter doesn't handle DST, assume the PC is correct
                 self.meterdate = datetime.datetime.strptime(date, '%H:%M %d-%m-%y')
             elif code == '1.8.0':
-                self.importWh = int(value[0:-3])
+                self.importWh = self.parsevalue(value)
             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])
+                self.exportWh = self.parsevalue(value)
             else:
                 print 'Unknown code', code
 
+    @staticmethod
+    def parsevalue(value):
+        count, units = value.split('*')
+
+        if units[0] == 'm':
+            exp = -3
+        elif units[0] == 'u':
+            exp = -6
+        elif units[0] == 'n':
+            exp = -9
+        elif units[0] == 'k':
+            exp = 3
+        elif units[0] == 'M':
+            exp = 6
+        elif units[0] == 'G':
+            exp = 9
+        elif units[1] == 'T':
+            exp = 12
+        else:
+            exp = 1
+        return float(count) * 10 ** exp
+
     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)