-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_utils.py
135 lines (120 loc) · 4.53 KB
/
db_utils.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
import time
import datetime
import MySQLdb
import ConfigParser
DEFAULT_TIMESTAMP = '0000-00-00 00:00:00'
TIMESTAMP_FORMAT = '%Y-%m-%d %H:%M:%S'
config = ConfigParser.RawConfigParser()
config.read('/home/pi/motion_detector/config.properties')
DB_HOST = config.get('DatabaseSection', 'database.host');
DB_USER = config.get('DatabaseSection', 'database.user');
DB_PASSWORD = config.get('DatabaseSection', 'database.password');
DB_NAME = 'pingpong'
TABLE_NAME = 'sessions'
def print_log(msg):
st = datetime.datetime.fromtimestamp(time.time()).strftime(TIMESTAMP_FORMAT)
print st, msg
def connect_to_db():
print_log('connecting to db')
conn = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASSWORD, db=DB_NAME)
print_log('connected')
return conn
def is_open_session_exists():
print_log('checking if an open session exists')
open_session_found = False
conn = connect_to_db()
x = conn.cursor()
x.execute('SELECT id FROM ' + TABLE_NAME + ' WHERE end = %s', (DEFAULT_TIMESTAMP))
data = x.fetchall()
if (len(data) > 0):
print_log('found an open session')
open_session_found = True
else:
print_log('no open session found')
x.close()
conn.close()
return open_session_found
def handle_motion_detected():
print_log('motion detected!')
conn = connect_to_db()
x = conn.cursor()
print_log('checking open sessions')
x.execute('SELECT id FROM ' + TABLE_NAME + ' WHERE end = %s', (DEFAULT_TIMESTAMP))
data = x.fetchall()
if (len(data) > 0):
session_id = data[0][0]
print_log('updating session ' + str(session_id) + ' in progress')
update_session(conn, x, session_id)
else:
print_log('no open sessions found, adding new session')
add_new_session(conn, x)
x.close()
conn.close()
def handle_no_motion(DETECTION_FRAME_LENGTH_IN_SECONDS, MAX_SESSION_TIME_IN_SECONDS):
print_log('no motion detected for ' + str(DETECTION_FRAME_LENGTH_IN_SECONDS) + ' seconds')
conn = connect_to_db()
x = conn.cursor()
print_log('checking open sessions')
x.execute('SELECT id, unix_timestamp(last_updated) FROM ' + TABLE_NAME + ' WHERE end = %s', (DEFAULT_TIMESTAMP))
data = x.fetchall()
if (len(data) > 0):
session_id = data[0][0]
last_updated = data[0][1]
print_log('session ' + str(session_id) + ' in progress, checking if it needs to end')
diff = time.time() - last_updated
if (diff > MAX_SESSION_TIME_IN_SECONDS):
close_session(conn, x, session_id)
else:
print_log('session ' + str(session_id) + ' did not time out yet')
else:
print_log('no open sessions')
x.close()
conn.close()
def clean_open_sessions(conn, x):
print_log('cleaning open sessions')
x.execute('DELETE FROM ' + TABLE_NAME + ' WHERE end = %s', (DEFAULT_TIMESTAMP))
conn.commit()
print_log('cleaned open sessions')
def close_session(conn, x, session_id):
print_log('closing session ' + str(session_id))
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime(TIMESTAMP_FORMAT)
x.execute('UPDATE ' + TABLE_NAME + ' SET end = %s WHERE id = %s', (timestamp, session_id))
conn.commit()
print_log('closed session ' + str(session_id))
def update_session(conn, x, session_id):
print_log('updating session ' + str(session_id))
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime(TIMESTAMP_FORMAT)
x.execute('UPDATE ' + TABLE_NAME + ' SET last_updated = %s WHERE id = %s', (timestamp, session_id))
conn.commit()
print_log('updated session ' + str(session_id))
def add_new_session(conn, x):
print_log('adding new session')
ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime(TIMESTAMP_FORMAT)
x.execute('INSERT INTO ' + TABLE_NAME + ' (last_updated) VALUES (%s)', (timestamp))
conn.commit()
print_log('added new session')
def setup_database():
conn = connect_to_db()
x = conn.cursor()
try:
x.execute('CREATE DATABASE ' + DB_NAME)
conn.commit()
except:
pass
try:
cmd = '''CREATE TABLE IF NOT EXISTS ''' + DB_NAME + '''.''' + TABLE_NAME + ''' (
id smallint unsigned not null auto_increment,
start timestamp default current_timestamp,
end timestamp default 0,
last_updated timestamp,
primary key (id))'''
x.execute(cmd)
conn.commit()
except:
pass
clean_open_sessions(conn, x)
x.close()
conn.close()