summaryrefslogtreecommitdiff
path: root/heap/ebus-web-python/testfiles/bsdtest.py
blob: b6587d0514f9043de10c40b10807ee922adea247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import bsddb
import psycopg2
from subprocess import Popen,PIPE
import time
import sys

db = bsddb.btopen("test.btdbm")

conn = psycopg2.connect("dbname=ebus user=ebus")
cur = conn.cursor()
sql = """SELECT * from sensor"""
cur.execute(sql)
sensors = cur.fetchall()
sensors = map(lambda a: (a[0], a[1]), sensors)
sensors = dict(sensors)
cur.close()
conn.close()

pg_dump = Popen(["psql","-A","-t"], stdin=PIPE, stdout=PIPE)
pg_dump.stdin.write("COPY value TO stdout WITH DELIMITER '|' NULL AS '';\n")
pg_dump.stdin.close()

c=0
try:
        while not pg_dump.stdout.closed:
             line = pg_dump.stdout.readline()
             c += 1
             if line == "":
                 continue
             (oid, s_timestamp, sensor_id, type, value_float, value_int, value_string) = line.split("|")
             name = sensors[int(sensor_id)]
             timestamp = time.strptime(s_timestamp.split(".")[0], "%Y-%m-%d %H:%M:%S")
             timestamp = time.mktime(timestamp)
             if type == "int":
                   value= value_int
             elif type == "float":
                   value = value_float
             else:
                   continue

             db["%s:%d"%(name,timestamp)] = value
             if c%10000==0:
                     print "%d: %s:%s=%s"%(c,name,s_timestamp,value)
except KeyboardInterrupt:
        db.close()

print "done"