Mercurial > ~darius > hgwebdir.cgi > iec1107
annotate iec1107.py @ 12:b4de1b6f8f78
Add note about nice looking circuit.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 25 Nov 2013 23:43:33 +1030 |
parents | c1892bd1460a |
children | 156313694bbb |
rev | line source |
---|---|
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
4 | 2 # |
3 # Copyright (c) 2013 | |
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved. | |
5 # | |
6 # Redistribution and use in source and binary forms, with or without | |
7 # modification, are permitted provided that the following conditions | |
8 # are met: | |
9 # 1. Redistributions of source code must retain the above copyright | |
10 # notice, this list of conditions and the following disclaimer. | |
11 # 2. Redistributions in binary form must reproduce the above copyright | |
12 # notice, this list of conditions and the following disclaimer in the | |
13 # documentation and/or other materials provided with the distribution. | |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 # |
4 | 15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE | |
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
25 # SUCH DAMAGE. | |
26 # | |
27 # Most of this is derived from the extremely helpful post at | |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 # http://www.domoticaforum.eu/viewtopic.php?f=71&t=7489 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
29 # |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
30 |
3 | 31 import datetime |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 import exceptions |
8 | 33 import optparse |
3 | 34 import re |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
35 import serial |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
36 import sys |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 import time |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 baudtable = {'0' : 300, '1' : 600, '2' : 1200, '3' : 2400, '4' : 4800, '5' : 9600, '6' : 19200} |
3 | 40 parsere = re.compile('([0-9A-Z](\.[0-9A-Z]){1,})\((.*)\)') |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
42 class Error(exceptions.BaseException): |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
43 pass |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
44 |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
45 class IEC1107Reading(object): |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
46 def __init__(self, port, force300bps = True): |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
47 # Open port |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
48 s = serial.Serial(port, baudrate = 300, bytesize = 7, parity = 'E', stopbits = 1) |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
49 s.timeout = 2.5 |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
50 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
51 # Send ident message |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
52 s.write('/?!\r\n') |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
53 rtn = s.readline() |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
54 if len(rtn) == 0: |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
55 raise Error('No reply to probe') |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
56 if len(rtn) < 6 or rtn[0] != '/' or rtn[-1] != '\n' or rtn[-2] != '\r': |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
57 raise Error('Invalid line "%s"' % (rtn)) |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
58 |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
59 rtn = rtn.strip() |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
60 self.mfg = rtn[1:4] |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
61 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
62 if self.mfg[2].isupper(): |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
63 self.restime = 0.2 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
64 else: |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
65 self.restime = 0.02 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
66 |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
67 if force300bps: |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
68 self.baudid = '0' |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
69 else: |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
70 self.baudid = rtn[4] |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
71 if self.baudid not in baudtable: |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
72 raise Error('Invalid baud rate %c from "%s"' % (selfbaudid, rtn)) |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
73 else: |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
74 self.baud = baudtable[self.baudid] |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
75 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
76 if rtn[5] == '/': |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
77 self.mode = rtn[6] |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
78 self.mfg = rtn[7:] |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
79 else: |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
80 self.mode = None |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
81 self.mfg = rtn[5:] |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
82 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
83 # Send ACK/option message |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
84 # Byte Meaning |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
85 # 0 ACK (0x06) |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
86 # 1 Protocol character ('0' = normal, '1' = secondary, '2' = HDLC protocol) |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
87 # 2 Baud rate ID ('0', '1', etc) |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
88 # 3 Mode control('0' = read data, '1' = device prog) |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
89 s.write('\x060%c0\r\n' % (self.baudid)) |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
90 |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
91 time.sleep(self.restime) |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
92 s.setBaudrate(self.baud) |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
93 |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
94 lines = [] |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
95 cksum = 0 |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
96 |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
97 # Read STX |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
98 head = s.read(1) |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
99 if len(head) == 0: |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
100 raise Error('No reply to query') |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
101 if head != '\x02': |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
102 raise Error('Invalid reply header 0x%02x' % (ord(head))) |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
103 |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
104 # Read result lines |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
105 while True: |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
106 line = s.readline() |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
107 if len(line) == 0: |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
108 raise Error('Timeout during message') |
3 | 109 |
110 cksum ^= reduce(lambda x, y: x ^ y, map(ord, line)) | |
111 if line.strip() == '!': | |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
112 break |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
113 lines.append(line) |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
114 |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
115 # Read trailer |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
116 fin = s.read(2) |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
117 if len(fin) != 2: |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
118 raise Error('Timeout reading trailer') |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
119 if fin[0] != '\x03': |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
120 raise Error('Trailer malformed, expected 0x03, got 0x%02x' % (ord(fin[0]))) |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
121 |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
122 # Validate checksum |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
123 cksum ^= ord(fin[0]) |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
124 if cksum != ord(fin[1]): |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
125 raise Error('Checksum mismatch, expected 0x%02x, got 0x%02x' % (cksum, ord(fin[1]))) |
2
8f1a773a3cd5
Don't strip lines to make printing easier.
Daniel O'Connor <darius@dons.net.au>
parents:
1
diff
changeset
|
126 self.rawreading = lines |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
127 del s |
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
128 |
3 | 129 self.parse() |
130 self.readdate = datetime.datetime.now() | |
131 | |
132 def parse(self): | |
133 for l in self.rawreading: | |
134 m = parsere.match(l) | |
135 if m == None: | |
136 raise Error('Unable to parse result \"%s\"' % (l)) | |
137 | |
138 (code, xxx, value) = m.groups() | |
139 if code == 'C.1': | |
140 self.meterid, date = value.split('(') | |
10
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
141 # XXX: The meter doesn't handle DST, assume the PC is correct |
3 | 142 self.meterdate = datetime.datetime.strptime(date, '%H:%M %d-%m-%y') |
143 elif code == '1.8.0': | |
10
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
144 self.importWh = self.parsevalue(value) |
3 | 145 elif code[0:4] == '1.8.': |
146 # Differing tarrifs which I don't care about | |
147 pass | |
148 elif code == '2.8.0': | |
10
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
149 self.exportWh = self.parsevalue(value) |
3 | 150 else: |
151 print 'Unknown code', code | |
152 | |
10
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
153 @staticmethod |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
154 def parsevalue(value): |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
155 count, units = value.split('*') |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
156 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
157 if units[0] == 'm': |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
158 exp = -3 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
159 elif units[0] == 'u': |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
160 exp = -6 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
161 elif units[0] == 'n': |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
162 exp = -9 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
163 elif units[0] == 'k': |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
164 exp = 3 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
165 elif units[0] == 'M': |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
166 exp = 6 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
167 elif units[0] == 'G': |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
168 exp = 9 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
169 elif units[1] == 'T': |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
170 exp = 12 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
171 else: |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
172 exp = 1 |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
173 return float(count) * 10 ** exp |
c1892bd1460a
Handle SI unit prefixes.
Daniel O'Connor <darius@dons.net.au>
parents:
8
diff
changeset
|
174 |
3 | 175 def __str__(self): |
176 return 'Time: %s, Meter: %s, Import: %d Wh, Export: %d Wh' % (self.readdate.strftime('%Y/%m/%d %H:%M'), | |
177 self.meterid, self.importWh, self.exportWh) | |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
178 def main(): |
8 | 179 parser = optparse.OptionParser(usage = 'usage: %prog [options] port', |
180 epilog = 'Read out from an IEC1107 meter') | |
181 parser.add_option('-r', '--rrd', dest = 'rrd', action = 'store_true', default = False, help = 'Output in a format suitable for rrdtool') | |
182 (opt, args) = parser.parse_args() | |
183 if len(args) != 1: | |
184 parser.error('Need to specify port') | |
1
99f25c8ab92f
Make class version actually work, remove procedure.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
185 |
8 | 186 res = IEC1107Reading(args[0]) |
187 if opt.rrd: | |
188 print '%s:%d:%d' % (res.readdate.strftime('%s'), res.importWh, res.exportWh) | |
189 else: | |
190 print res | |
5
b686ad203c1e
Don't print the raw value AND the parsed ones.
Daniel O'Connor <darius@dons.net.au>
parents:
4
diff
changeset
|
191 |
0
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
192 if __name__ == '__main__': |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
193 main() |
fd8520d969c4
Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
194 |