Mercurial > ~darius > hgwebdir.cgi > iec1107
comparison iec1107.py @ 10:c1892bd1460a
Handle SI unit prefixes.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Thu, 21 Nov 2013 12:34:13 +1030 |
parents | 08b192a6e189 |
children | 156313694bbb |
comparison
equal
deleted
inserted
replaced
9:139e267c9965 | 10:c1892bd1460a |
---|---|
136 raise Error('Unable to parse result \"%s\"' % (l)) | 136 raise Error('Unable to parse result \"%s\"' % (l)) |
137 | 137 |
138 (code, xxx, value) = m.groups() | 138 (code, xxx, value) = m.groups() |
139 if code == 'C.1': | 139 if code == 'C.1': |
140 self.meterid, date = value.split('(') | 140 self.meterid, date = value.split('(') |
141 # XXX: The meter I have is an hour slow | 141 # XXX: The meter doesn't handle DST, assume the PC is correct |
142 self.meterdate = datetime.datetime.strptime(date, '%H:%M %d-%m-%y') | 142 self.meterdate = datetime.datetime.strptime(date, '%H:%M %d-%m-%y') |
143 elif code == '1.8.0': | 143 elif code == '1.8.0': |
144 self.importWh = int(value[0:-3]) | 144 self.importWh = self.parsevalue(value) |
145 elif code[0:4] == '1.8.': | 145 elif code[0:4] == '1.8.': |
146 # Differing tarrifs which I don't care about | 146 # Differing tarrifs which I don't care about |
147 pass | 147 pass |
148 elif code == '2.8.0': | 148 elif code == '2.8.0': |
149 self.exportWh = int(value[0:-3]) | 149 self.exportWh = self.parsevalue(value) |
150 else: | 150 else: |
151 print 'Unknown code', code | 151 print 'Unknown code', code |
152 | |
153 @staticmethod | |
154 def parsevalue(value): | |
155 count, units = value.split('*') | |
156 | |
157 if units[0] == 'm': | |
158 exp = -3 | |
159 elif units[0] == 'u': | |
160 exp = -6 | |
161 elif units[0] == 'n': | |
162 exp = -9 | |
163 elif units[0] == 'k': | |
164 exp = 3 | |
165 elif units[0] == 'M': | |
166 exp = 6 | |
167 elif units[0] == 'G': | |
168 exp = 9 | |
169 elif units[1] == 'T': | |
170 exp = 12 | |
171 else: | |
172 exp = 1 | |
173 return float(count) * 10 ** exp | |
152 | 174 |
153 def __str__(self): | 175 def __str__(self): |
154 return 'Time: %s, Meter: %s, Import: %d Wh, Export: %d Wh' % (self.readdate.strftime('%Y/%m/%d %H:%M'), | 176 return 'Time: %s, Meter: %s, Import: %d Wh, Export: %d Wh' % (self.readdate.strftime('%Y/%m/%d %H:%M'), |
155 self.meterid, self.importWh, self.exportWh) | 177 self.meterid, self.importWh, self.exportWh) |
156 def main(): | 178 def main(): |