-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
271 lines (222 loc) · 8.09 KB
/
database.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""
Module for interfacing with the sqlite3 database.
"""
import os
import sys
import time
import uuid
import Queue
import logging
import sqlite3
import threading
import traceback
from ConfigParser import NoSectionError
from datetime import datetime
try:
import cStringIO as StringIO
except ImportError:
import StringIO
__version__ = "0.2"
__all__ = ["Archive", "__version__", "__all__"]
# Logger instance
dbLogger = logging.getLogger('__main__')
class DatabaseProcessor(threading.Thread):
"""
Class responsible for providing access to the database from a single thread.
"""
def __init__(self, dbName):
self._dbName = dbName
self.running = False
self.input = Queue.Queue()
self.output = Queue.Queue()
self.thread = None
self.alive = threading.Event()
def start(self):
if self.thread is not None:
self.cancel()
self.thread = threading.Thread(target=self.run, name='dbAccess')
self.thread.setDaemon(1)
self.alive.set()
self.thread.start()
dbLogger.info('Started the DatabaseProcessor background thread')
def cancel(self):
if self.thread is not None:
self.alive.clear() # clear alive event for thread
self.thread.join()
dbLogger.info('Stopped the DatabaseProcessor background thread')
def appendRequest(self, cmd):
rid = str(uuid.uuid4())
self.input.put( (rid,cmd) )
return rid
def getResponse(self, rid):
qid, qresp = self.output.get()
while qid != rid:
self.output.put( (qid,qresp) )
qid, qresp = self.output.get()
return qresp
def dict_factory(self, cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
def run(self):
self._dbConn = sqlite3.connect(self._dbName)
self._dbConn.row_factory = self.dict_factory
self._cursor = self._dbConn.cursor()
while self.alive.isSet() or not self.input.empty():
try:
rid, cmd = self.input.get()
self._cursor.execute(cmd)
output = []
for row in self._cursor.fetchall():
output.append( row )
if cmd[:6] != 'SELECT':
self._dbConn.commit()
self.output.put( (rid,output) )
except Exception, e:
exc_type, exc_value, exc_traceback = sys.exc_info()
dbLogger.error("DatabaseProcessor: %s at line %i", e, traceback.tb_lineno(exc_traceback))
## Grab the full traceback and save it to a string via StringIO
fileObject = StringIO.StringIO()
traceback.print_tb(exc_traceback, file=fileObject)
tbString = fileObject.getvalue()
fileObject.close()
## Print the traceback to the logger as a series of DEBUG messages
for line in tbString.split('\n'):
dbLogger.debug("%s", line)
self._dbConn.close()
class Archive(object):
_dbConn = None
_cursor = None
_dbMapper = {'temperature': 'outTemp',
'humidity': 'outHumidity',
'dewpoint': 'outDewpoint',
'windchill': 'windchill',
'indoorTemperature': 'inTemp',
'indoorHumidity': 'inHumidity',
'indoorDewpoint': 'inDewpoint',
'pressure': 'barometer',
'average': 'windSpeed',
'gust': 'windGust',
'direction': 'windDir',
'rainrate': 'rainRate',
'rainfall': 'rain',
'uvIndex': 'uv'}
def __init__(self):
self._dbName = os.path.join(os.path.dirname(__file__), 'archive', 'wx-data.db')
if not os.path.exists(self._dbName):
raise RuntimeError("Archive database not found")
self._backend = None
def start(self):
"""
Open the database.
"""
if self._backend is None:
self._backend = DatabaseProcessor(self._dbName)
self._backend.start()
def cancel(self):
"""
Close the database.
"""
if self._backend is not None:
self._backend.cancel()
def getData(self, age=0):
"""
Return a collection of data a certain number of seconds into the past.
"""
# Fetch the entries that match
if age <= 0:
sqlCmd = 'SELECT * FROM wx ORDER BY dateTime DESC LIMIT 1'
rid = self._backend.appendRequest(sqlCmd)
else:
# Figure out how far to look back into the database
tNow = time.time()
tLookback = tNow - age
sqlCmd = 'SELECT * FROM wx WHERE dateTime >= %i ORDER BY dateTime LIMIT 1' % tLookback
rid = self._backend.appendRequest(sqlCmd)
# Fetch the output
output = self._backend.getResponse(rid)
try:
row = output[0]
except IndexError:
return 0, {}
# Check for an empty database
if row is None:
return 0, {}
# Convert it to the "standard" dictionary format
timestamp = row['dateTime']
output = {'temperature': row['outTemp'], 'humidity': row['outHumidity'],
'dewpoint': row['outDewpoint'], 'windchill': row['windchill'],
'indoorTemperature': row['inTemp'], 'indoorHumidity': row['inHumidity'],
'indoorDewpoint': row['inDewpoint'], 'pressure': row['barometer'],
'rainrate': row['rainRate'], 'rainfall': row['rain'],
'average': row['windSpeed'], 'gust': row['windGust'], 'direction': row['windDir'],
'altTemperature': [], 'altHumidity': [], 'altDewpoint': [],
'uvIndex': row['uv']}
for i in xrange(1, 5):
output['altTemperature'].append( row['outTemp%i' % i] if row['outTemp%i' % i] != -99 else None )
output['altHumidity'].append( row['outHumidity%i' % i] if row['outHumidity%i' % i] != -99 else None )
output['altDewpoint'].append( row['outDewpoint%i' % i] if row['outDewpoint%i' % i] != -99 else None )
# Get the rainfall relative to the start of the year
return timestamp, output
def getDataYearStart(self):
tNow = datetime.now()
tYear = tNow.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
tYear = int( tYear.strftime("%s") )
sqlCmd = 'SELECT * FROM wx WHERE dateTime >= %i ORDER BY dateTime LIMIT 1' % tYear
rid = self._backend.appendRequest(sqlCmd)
# Fetch the output
output = self._backend.getResponse(rid)
try:
row = output[0]
except IndexError:
## Looks like we don't have a full year yet, get the oldest entry avaliable
sqlCmd = 'SELECT * FROM wx ORDER BY dateTime LIMIT 1' % tYear
rid = self._backend.appendRequest(sqlCmd)
## Fetch the output
output = self._backend.getResponse(rid)
# Check for an empty database
if row is None:
return 0, {}
# Convert it to the "standard" dictionary format
timestamp = row['dateTime']
output = {'temperature': row['outTemp'], 'humidity': row['outHumidity'],
'dewpoint': row['outDewpoint'], 'windchill': row['windchill'],
'indoorTemperature': row['inTemp'], 'indoorHumidity': row['inHumidity'],
'indoorDewpoint': row['inDewpoint'], 'pressure': row['barometer'],
'rainrate': row['rainRate'], 'rainfall': row['rain'],
'average': row['windSpeed'], 'gust': row['windGust'], 'direction': row['windDir'],
'altTemperature': [], 'altHumidity': [], 'altDewpoint': [],
'uvIndex': row['uv']}
for i in xrange(1, 5):
output['altTemperature'].append( row['outTemp%i' % i] if row['outTemp%i' % i] != -99 else None )
output['altHumidity'].append( row['outHumidity%i' % i] if row['outHumidity%i' % i] != -99 else None )
output['altDewpoint'].append( row['outDewpoint%i' % i] if row['outDewpoint%i' % i] != -99 else None )
return timestamp, output
def writeData(self, timestamp, data):
"""
Write a collection of data to the database.
"""
# Build up the values to insert
cNames = ['dateTime', 'usUnits']
dValues = [int(timestamp), 0]
for key in data.keys():
try:
cNames.append( self._dbMapper[key] )
dValues.append( data[key] )
except KeyError:
if key[:3] == 'alt':
if key[3:6] == 'Tem':
nameBase = 'outTemp'
elif key[3:6] == 'Hum':
nameBase = 'outHumidity'
else:
nameBase = 'outDewpoint'
for i in xrange(len(data[key])):
if data[key][i] is not None:
cNames.append( "%s%i" % (nameBase, i+1) )
dValues.append( data[key][i] )
# Add the entry to the database
rid = self._backend.appendRequest('INSERT INTO wx (%s) VALUES (%s)' % (','.join(cNames), ','.join([str(v) for v in dValues])))
output = self._backend.getResponse(rid)
return True