IoT - Kubus
/
Kubus
JNP3 16/17
server/databaseconnector.py@30:27c08888a838, 2017-01-11 (annotated)
- Committer:
- Jan Kopa?ski
- Date:
- Wed Jan 11 00:11:56 2017 +0100
- Revision:
- 30:27c08888a838
- Parent:
- 16:1569130916f1
- Child:
- 35:0f01042765d6
server configuration; add lock
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Kopa?ski | 16:1569130916f1 | 1 | from sqlite3 import connect, Error |
Jan Kopa?ski | 30:27c08888a838 | 2 | from threading import Lock |
Jan Kopa?ski | 16:1569130916f1 | 3 | |
Jan Kopa?ski | 16:1569130916f1 | 4 | json_template = { |
Jan Kopa?ski | 16:1569130916f1 | 5 | 'sensors': [ |
Jan Kopa?ski | 16:1569130916f1 | 6 | { |
Jan Kopa?ski | 16:1569130916f1 | 7 | 'name': 'Motion at the stairway', |
Jan Kopa?ski | 16:1569130916f1 | 8 | 'type': 'bool', |
Jan Kopa?ski | 16:1569130916f1 | 9 | 'value': None |
Jan Kopa?ski | 16:1569130916f1 | 10 | }, |
Jan Kopa?ski | 16:1569130916f1 | 11 | { |
Jan Kopa?ski | 16:1569130916f1 | 12 | 'name': 'Motion at the entrance', |
Jan Kopa?ski | 16:1569130916f1 | 13 | 'type': 'bool', |
Jan Kopa?ski | 16:1569130916f1 | 14 | 'value': None |
Jan Kopa?ski | 16:1569130916f1 | 15 | }, |
Jan Kopa?ski | 16:1569130916f1 | 16 | { |
Jan Kopa?ski | 16:1569130916f1 | 17 | 'name': 'Distance at the entrance', |
Jan Kopa?ski | 16:1569130916f1 | 18 | 'type': 'number', |
Jan Kopa?ski | 16:1569130916f1 | 19 | 'range': [0, 90], |
Jan Kopa?ski | 16:1569130916f1 | 20 | 'unit': 'cm', |
Jan Kopa?ski | 16:1569130916f1 | 21 | 'value': None |
Jan Kopa?ski | 16:1569130916f1 | 22 | }, |
Jan Kopa?ski | 16:1569130916f1 | 23 | { |
Jan Kopa?ski | 16:1569130916f1 | 24 | 'name': 'Volume inside', |
Jan Kopa?ski | 16:1569130916f1 | 25 | 'type': 'number', |
Jan Kopa?ski | 16:1569130916f1 | 26 | 'range': [-30, 30], |
Jan Kopa?ski | 16:1569130916f1 | 27 | 'unit': 'dB', |
Jan Kopa?ski | 16:1569130916f1 | 28 | 'value': None |
Jan Kopa?ski | 16:1569130916f1 | 29 | } |
Jan Kopa?ski | 16:1569130916f1 | 30 | ] |
Jan Kopa?ski | 16:1569130916f1 | 31 | } |
Jan Kopa?ski | 16:1569130916f1 | 32 | |
Jan Kopa?ski | 16:1569130916f1 | 33 | |
Jan Kopa?ski | 16:1569130916f1 | 34 | class DatabaseConnector: |
Jan Kopa?ski | 16:1569130916f1 | 35 | def __init__(self, database, tables, separator=':'): |
Jan Kopa?ski | 16:1569130916f1 | 36 | self.tables = tables |
Jan Kopa?ski | 16:1569130916f1 | 37 | self.separator = separator |
Jan Kopa?ski | 16:1569130916f1 | 38 | self.conn = connect(database=database, check_same_thread=False) |
Jan Kopa?ski | 16:1569130916f1 | 39 | self.curr = self.conn.cursor() |
Jan Kopa?ski | 30:27c08888a838 | 40 | self.mutex = Lock() |
Jan Kopa?ski | 16:1569130916f1 | 41 | |
Jan Kopa?ski | 16:1569130916f1 | 42 | def write(self, table, data): |
Jan Kopa?ski | 16:1569130916f1 | 43 | if not data: |
Jan Kopa?ski | 16:1569130916f1 | 44 | return 'Request message is empty' |
Jan Kopa?ski | 16:1569130916f1 | 45 | vals = data.split(self.separator) |
Jan Kopa?ski | 16:1569130916f1 | 46 | stmt = ("INSERT INTO %s VALUES (NULL" + (", ?" * len(vals)) + ")") % table |
Jan Kopa?ski | 30:27c08888a838 | 47 | self.mutex.acquire() |
Jan Kopa?ski | 16:1569130916f1 | 48 | try: |
Jan Kopa?ski | 16:1569130916f1 | 49 | self.curr.execute(stmt, vals) |
Jan Kopa?ski | 16:1569130916f1 | 50 | except Error as e: |
Jan Kopa?ski | 16:1569130916f1 | 51 | return e.message |
Jan Kopa?ski | 16:1569130916f1 | 52 | except: |
Jan Kopa?ski | 16:1569130916f1 | 53 | return 'Undefined exception' |
Jan Kopa?ski | 16:1569130916f1 | 54 | self.conn.commit() |
Jan Kopa?ski | 30:27c08888a838 | 55 | self.mutex.release() |
Jan Kopa?ski | 16:1569130916f1 | 56 | return None |
Jan Kopa?ski | 16:1569130916f1 | 57 | |
Jan Kopa?ski | 16:1569130916f1 | 58 | def read(self): |
Jan Kopa?ski | 16:1569130916f1 | 59 | stmt = 'SELECT value FROM %s ORDER BY id DESC LIMIT 1' |
Jan Kopa?ski | 16:1569130916f1 | 60 | json_dict = json_template |
Jan Kopa?ski | 30:27c08888a838 | 61 | self.mutex.acquire() |
Jan Kopa?ski | 16:1569130916f1 | 62 | for i, t in enumerate(self.tables): |
Jan Kopa?ski | 16:1569130916f1 | 63 | try: |
Jan Kopa?ski | 16:1569130916f1 | 64 | self.curr.execute(stmt % t) |
Jan Kopa?ski | 30:27c08888a838 | 65 | entry = self.curr.fetchone() |
Jan Kopa?ski | 30:27c08888a838 | 66 | if entry: |
Jan Kopa?ski | 30:27c08888a838 | 67 | json_dict['sensors'][i]['value'] = entry[0] |
Jan Kopa?ski | 16:1569130916f1 | 68 | except Error: |
Jan Kopa?ski | 16:1569130916f1 | 69 | print 'Database error on read: ' + t |
Jan Kopa?ski | 30:27c08888a838 | 70 | self.mutex.release() |
Jan Kopa?ski | 30:27c08888a838 | 71 | return json_dict |