annotate iec1107.py @ 9:139e267c9965

Use DERIVE instead of counter. Add graph example
author Daniel O'Connor <darius@dons.net.au>
date Thu, 21 Nov 2013 00:01:28 +1030
parents 08b192a6e189
children c1892bd1460a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
2 #
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
3 # Copyright (c) 2013
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
5 #
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
6 # Redistribution and use in source and binary forms, with or without
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
7 # modification, are permitted provided that the following conditions
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
8 # are met:
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
9 # 1. Redistributions of source code must retain the above copyright
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
10 # notice, this list of conditions and the following disclaimer.
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
11 # 2. Redistributions in binary form must reproduce the above copyright
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
12 # notice, this list of conditions and the following disclaimer in the
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
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
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
25 # SUCH DAMAGE.
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
26 #
10a16898903d Add license.
Daniel O'Connor <darius@dons.net.au>
parents: 3
diff changeset
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
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
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
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
33 import optparse
3
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
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
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
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
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
109
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
110 cksum ^= reduce(lambda x, y: x ^ y, map(ord, line))
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
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
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
129 self.parse()
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
130 self.readdate = datetime.datetime.now()
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
131
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
132 def parse(self):
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
133 for l in self.rawreading:
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
134 m = parsere.match(l)
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
135 if m == None:
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
136 raise Error('Unable to parse result \"%s\"' % (l))
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
137
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
138 (code, xxx, value) = m.groups()
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
139 if code == 'C.1':
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
140 self.meterid, date = value.split('(')
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
141 # XXX: The meter I have is an hour slow
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
142 self.meterdate = datetime.datetime.strptime(date, '%H:%M %d-%m-%y')
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
143 elif code == '1.8.0':
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
144 self.importWh = int(value[0:-3])
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
145 elif code[0:4] == '1.8.':
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
146 # Differing tarrifs which I don't care about
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
147 pass
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
148 elif code == '2.8.0':
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
149 self.exportWh = int(value[0:-3])
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
150 else:
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
151 print 'Unknown code', code
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
152
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
153 def __str__(self):
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
154 return 'Time: %s, Meter: %s, Import: %d Wh, Export: %d Wh' % (self.readdate.strftime('%Y/%m/%d %H:%M'),
535076e31660 Parse the result.
Daniel O'Connor <darius@dons.net.au>
parents: 2
diff changeset
155 self.meterid, self.importWh, self.exportWh)
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
156 def main():
8
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
157 parser = optparse.OptionParser(usage = 'usage: %prog [options] port',
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
158 epilog = 'Read out from an IEC1107 meter')
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
159 parser.add_option('-r', '--rrd', dest = 'rrd', action = 'store_true', default = False, help = 'Output in a format suitable for rrdtool')
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
160 (opt, args) = parser.parse_args()
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
161 if len(args) != 1:
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
162 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
163
8
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
164 res = IEC1107Reading(args[0])
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
165 if opt.rrd:
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
166 print '%s:%d:%d' % (res.readdate.strftime('%s'), res.importWh, res.exportWh)
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
167 else:
08b192a6e189 Add RRD output mode.
Daniel O'Connor <darius@dons.net.au>
parents: 5
diff changeset
168 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
169
0
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
170 if __name__ == '__main__':
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
171 main()
fd8520d969c4 Initial commit, only readmeter function works
Daniel O'Connor <darius@dons.net.au>
parents:
diff changeset
172