-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbstore.py
69 lines (62 loc) · 2.11 KB
/
dbstore.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
# Database stuff
import sqlite3
from sqlite3 import Error
import config as cfg
import globs
from globs import dbq, uiq
from ui import debug
def dbopen():
'''open the database
'''
global dbx
dbx = None
debug('Opening database', 2)
# if cfg.ui_debugmod['db']: uiq.put(('Opening database', 'DEBUG', 2))
try:
dbx = sqlite3.connect(cfg.db_file)
# dbx.isolation_level = 'EXCLUSIVE' # FIXME - this seems not to lock against other processes
# dbx.execute('BEGIN EXCLUSIVE')
sql = "CREATE TABLE IF NOT EXISTS beats ({});".format(globs.sqltable)
cur = dbx.cursor()
try:
cur.execute(sql)
dbx.commit()
except Error as dberr:
uiq.put(('Database table create error: {}'.format(dberr), 'ERR'))
except Error as dberr:
uiq.put(('Database open error: {}'.format(dberr), 'ERR'))
return dbx
def dbclose():
'''close the database
'''
global dbx
dbx.close()
def dbstorebeat(beattype, delta, hz, skew, err):
'''database storage routine:
beattype = 1/arrive, 0/depart
delta = delta since last beat, in uS
hz = calculated Hz
skew = skew from desired beat frequency
'''
global dbx, temperature, humidity
sql = "INSERT INTO beats(beattype, delta, hz, skew, temperature, humidity, error) VALUES ({}, {}, {}, {}, {}, {}, {})".format(beattype, delta, hz, skew, globs.temperature, globs.humidity, err)
try: dbx
except NameError: dbopen
else:
cur = dbx.cursor()
try:
debug('Executing SQL command {}'.format(sql), 3)
# if cfg.ui_debugmod['db']: uiq.put(('Executing SQL command {}'.format(sql), 'DEBUG', 3))
cur.execute(sql)
dbx.commit()
except Error as dberr:
uiq.put(('Database write error: {}'.format(dberr), 'ERR'))
def dbD():
'''database access thread
'''
debug('Database thread initialising')
# if cfg.ui_debugmod['db']: uiq.put(('Database thread initialising', 'DEBUG'))
dbopen()
while True:
item = dbq.get()
dbstorebeat(*item)