-
Notifications
You must be signed in to change notification settings - Fork 1
/
DatabaseRetriever_ver03.py
151 lines (127 loc) · 5.57 KB
/
DatabaseRetriever_ver03.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
##Hamtar all data sedan senaste hamtningen
##behover python 2.7 och MySQL-python installerat
import MySQLdb as mdb
from Tkinter import *
import pickle
from datetime import datetime
from time import strptime
import os
idDict=dict()
idDict["Plugwise.000D6F00003FE5A5"]="Livningroom"
idDict["Plugwise.000D6F000046957C"]="Dishwasher"
idDict["Plugwise.000D6F0000469D83"]="Office"
idDict["Plugwise.000D6F000055138F"]="Bedroom"
idDict["Plugwise.000D6F0000599D9B"]="Kettle"
idDict["Plugwise.000D6F000072A183"]="WashingMachine"
idDict["Plugwise.000D6F000397AA8A"]="MicrowaveOven"
idDict["Plugwise.000D6F00039745B7"]="KitchenFan"
idDict["Plugwise.000D6F000397AAFA"]="Dehumidifier"
idDict["Plugwise.000D6F00039745EF"]="Refridgerator"
idDict["Plugwise.000D6F00039747EC"]="Toaster"
idDict["Plugwise.000D6F0001A40370"]="Mains1"
idDict["Plugwise.000D6F0001A40664"]="Mains2"
idDict["Plugwise.000D6F0000D31BBE"]="Mains3"
idDict["Plugwise.Total"]="TotalEnergy"
invIdDict = {v:k for k, v in idDict.items()}
class DatabaseRetriever:
def __init__(self):
self.root=Tk()
self.frame=Frame(self.root)
self.frame.master.title("Database Retreiver")
ws = self.root.winfo_screenwidth()
hs = self.root.winfo_screenheight()
self.frame.master.geometry('%dx%d+%d+%d' % (220, 210, (ws/2) - (220/2), (hs/2) - (210/2)))
self.create_widgets()
self.root.mainloop()
def create_widgets(self):
self.frame.pack(padx=5,pady=5)
hostLabel = Label(self.frame)
hostLabel.grid(row=1,column=0,pady=3,sticky=W)
hostLabel["text"] = "Host:"
hostEntry = Entry(self.frame)
hostEntry.grid(row=1,column=1,pady=3)
hostEntry["width"] = 20
userLabel = Label(self.frame)
userLabel.grid(row=2,column=0,sticky=W,pady=3)
userLabel["text"] = "User:"
userEntry = Entry(self.frame)
userEntry.grid(row=2,column=1,pady=3)
userEntry["width"] = 20
passLabel = Label(self.frame)
passLabel["text"] = "Password:"
passLabel.grid(row=3,column=0,sticky=W,pady=3)
passEntry = Entry(self.frame)
passEntry["width"] = 20
passEntry.grid(row=3,column=1)
dbLabel = Label(self.frame)
dbLabel["text"] = "Database:"
dbLabel.grid(row=4,column=0,sticky=W,pady=3)
dbEntry = Entry(self.frame)
dbEntry["width"] = 20
dbEntry.grid(row=4,column=1)
tableLabel = Label(self.frame)
tableLabel["text"] = "Table:"
tableLabel.grid(row=5,column=0,sticky=W,pady=3)
tableEntry = Entry(self.frame)
tableEntry["width"] = 20
tableEntry.grid(row=5,column=1)
deviceLabel = Label(self.frame)
deviceLabel["text"] = "Device:"
deviceLabel.grid(row=6,column=0,sticky=W,pady=3)
var = StringVar()
var.set(invIdDict.keys()[0])
#OptionMenu(self.frame, var, tuple(invIdDict.keys())).grid(row=6,column=1)
apply(OptionMenu, (self.frame, var) + tuple(invIdDict.keys())).grid(row=6,column=1)
Button(self.frame,text='Get database',command=(lambda: self.connect_to(hostEntry.get(),userEntry.get(),passEntry.get(),dbEntry.get(),tableEntry.get(),var.get()))).grid(row=7,column=0,padx=1,columnspan=2)
self.enter=lambda event: self.connect_to(hostEntry.get(),userEntry.get(),passEntry.get(),dbEntry.get(),tableEntry.get(),var.get())
tableEntry.bind('<Return>',self.enter)
def connect_to(self,host,user,password,db,table,device):
self.root.destroy()
lastTimeTag="0000-00-00 00:00:00"
dataCollection=list()
file_not_found=0
try:
old=open(str(device)+".csv","rb")
print "Previous data found, will merge with new"
lines=old.readlines()
old.close()
newfile=open(str(device)+".csv","wb")
for line in lines:
newfile.write(line)
#ast.litheral.eval()
tempList=eval(lines[-1])
print tempList
lastTimeTag=tempList[0].strftime("%Y-%m-%d %H:%M:%S")
print lastTimeTag
except IOError:
file_not_found=1
print "No previous data, complete database will be retrieved.."
if file_not_found:
newfile=open(str(device)+".csv","wb")
print "Connecting to database with Host:"+str(host)+" User:"+str(user)+" Password:"+str(password)+" DB:"+str(db)+" Table:"+str(table)+" Device:"+str(device)
try:
conn= mdb.connect(host=str(host),user=str(user),passwd=str(password),db=str(db))
#conn = mdb.connect(host='mysql315.loopia.se', user='kthstud@a68445', passwd='2013IIstud!#', db='aktivahuset_com')
except Exception as e:
print e
return
print "Connected.."
with conn:
cur=conn.cursor()
sql="SELECT * FROM {0} WHERE id='{1}' OR id='{2}'".format(table,str(invIdDict[device]),str(idDict[invIdDict[device]]))
#sql="SELECT * FROM {0} WHERE id='{1}' OR id='{2}' AND time>'{3}'".format(table,str(invIdDict[device]),str(idDict[invIdDict[device]]),lastTimeTag)
print "Using: "+sql
cur.execute(sql)
rows=cur.fetchall()
for row in rows:
#print row[0]
newfile.write(str(row[0])+";"+str(row[3])+"\n")
#print row
newfile.close()
return
newfile.close()
os.remove(str(device)+".csv")
return
if __name__=='__main__':
DatabaseRetriever()
print "Done"