IoT - Kubus
/
Kubus
JNP3 16/17
server/databaseconnector.py@16:1569130916f1, 2017-01-07 (annotated)
- Committer:
- Jan Kopa?ski
- Date:
- Sat Jan 07 02:12:25 2017 +0100
- Revision:
- 16:1569130916f1
- Child:
- 30:27c08888a838
server init
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 | 16:1569130916f1 | 2 | |
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 | 16:1569130916f1 | 40 | |
Jan Kopa?ski | 16:1569130916f1 | 41 | def write(self, table, data): |
Jan Kopa?ski | 16:1569130916f1 | 42 | if not data: |
Jan Kopa?ski | 16:1569130916f1 | 43 | return 'Request message is empty' |
Jan Kopa?ski | 16:1569130916f1 | 44 | vals = data.split(self.separator) |
Jan Kopa?ski | 16:1569130916f1 | 45 | stmt = ("INSERT INTO %s VALUES (NULL" + (", ?" * len(vals)) + ")") % table |
Jan Kopa?ski | 16:1569130916f1 | 46 | try: |
Jan Kopa?ski | 16:1569130916f1 | 47 | self.curr.execute(stmt, vals) |
Jan Kopa?ski | 16:1569130916f1 | 48 | except Error as e: |
Jan Kopa?ski | 16:1569130916f1 | 49 | return e.message |
Jan Kopa?ski | 16:1569130916f1 | 50 | except: |
Jan Kopa?ski | 16:1569130916f1 | 51 | return 'Undefined exception' |
Jan Kopa?ski | 16:1569130916f1 | 52 | self.conn.commit() |
Jan Kopa?ski | 16:1569130916f1 | 53 | return None |
Jan Kopa?ski | 16:1569130916f1 | 54 | |
Jan Kopa?ski | 16:1569130916f1 | 55 | def read(self): |
Jan Kopa?ski | 16:1569130916f1 | 56 | stmt = 'SELECT value FROM %s ORDER BY id DESC LIMIT 1' |
Jan Kopa?ski | 16:1569130916f1 | 57 | json_dict = json_template |
Jan Kopa?ski | 16:1569130916f1 | 58 | for i, t in enumerate(self.tables): |
Jan Kopa?ski | 16:1569130916f1 | 59 | try: |
Jan Kopa?ski | 16:1569130916f1 | 60 | self.curr.execute(stmt % t) |
Jan Kopa?ski | 16:1569130916f1 | 61 | value = self.curr.fetchone()[0] |
Jan Kopa?ski | 16:1569130916f1 | 62 | json_dict['sensors'][i]['value'] = value |
Jan Kopa?ski | 16:1569130916f1 | 63 | except Error: |
Jan Kopa?ski | 16:1569130916f1 | 64 | print 'Database error on read: ' + t |
Jan Kopa?ski | 16:1569130916f1 | 65 | return json_dict |