-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_last_update.py
61 lines (53 loc) · 1.89 KB
/
test_last_update.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
#!/usr/bin/python
#test_last_update.py
#this script tests the last update to the energy db
#if it is too long, there's a problem, populate error table
#written on 3/26/14
import MySQLdb
import ConfigParser
import time
###############################################################
config = ConfigParser.RawConfigParser()
config.read('/home/pi/gfiske.cfg')
db_user = config.get('section1', 'db_user')
db_passwd = config.get('section1', 'db_passwd')
db_user = db_user.decode('base64','strict')
db_passwd = db_passwd.decode('base64','strict')
###############################################################
#connect to db
db = MySQLdb.connect("127.0.0.1", db_user, db_passwd, "energy")
#do query on trend table
myq = "select timestampdiff(minute, now(), ts1) from trend order by ts1 desc limit 1;"
cursor = db.cursor()
cursor.execute(myq)
myq = cursor.fetchone()
diff = myq[0]
db.commit()
# if the time delay is greater than 2 minutes, populate error table
if int(diff) > -11 and int(diff) < -2:
#it is an error
myq2 = "insert into error values (DEFAULT, NOW(), 1);"
cursor.execute(myq2)
db.commit()
# if the time delay is greater than 30 minutes, restart logging script
elif int(diff) < -30:
os.system("python /home/pi/pi_energy/db_update_currentcost6.py &")
myq2 = "insert into error values (DEFAULT, NOW(), 1);"
cursor.execute(myq2)
db.commit()
filename = "/home/pi/db_error_log.txt"
f = open(filename,"r+")
f.readlines()
now = time.localtime(time.time())
curtime = time.asctime(now)
f.write("\n")
f.write(curtime + "\n")
f.write("Error greater than 30 minutes, restarted db_update_currentcost6" + "\n")
f.write("\n")
f.close()
# if no time delay, do nothing, note no error to db
else:
myq2 = "insert into error values (DEFAULT, NOW(), 0);"
cursor.execute(myq2)
db.commit()
db.close()