-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateJSON.py
77 lines (63 loc) · 2.46 KB
/
createJSON.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
import mysql.connector as mdb
from config import *
def connectToDB():
return mdb.connect(**configRaspi)
def queryDB_station(station):
DBconn = connectToDB()
# This enables column access by name: row['column_name']
#DBconn.row_factory = sqlite3.Row
queryCurs = DBconn.cursor(dictionary=True)
queryCurs.execute(
'SELECT a.timestamp , originAddr, a.unit ,id, value, unit_name,sensor '
'FROM messwerte as a '
'INNER JOIN '
'(select max(timestamp) as timestamp, unit from messwerte where originAddr = %s group by unit ) as b '
'ON a.timestamp = b.timestamp and a.unit = b.unit '
'INNER JOIN einheiten ON a.unit = einheiten.unit_id '
' ; ',(station,))
row = queryCurs.fetchall()
row_json = [ dict(rec) for rec in row ]
DBconn.close()
return row_json
def queryDB_station_interval(station, unit, begin, end):
DBconn = connectToDB()
# This enables column access by name: row['column_name']
#DBconn.row_factory = sqlite3.Row
queryCurs = DBconn.cursor(dictionary=True)
queryCurs.execute(
'SELECT timestamp, originAddr, unit,unit_name,sensor ,ANY_VALUE(id) AS id, ANY_VALUE(value) AS value '
'FROM messwerte '
'INNER JOIN einheiten ON messwerte.unit = einheiten.unit_id '
'WHERE originAddr=%s AND unit=%s AND timestamp BETWEEN %s AND %s '
'ORDER BY timestamp DESC', (station, unit, begin, end,))
row = queryCurs.fetchall()
row_json = [ dict(rec) for rec in row ]
DBconn.close()
return row_json
def queryDB_id(id):
DBconn = connectToDB()
# This enables column access by name: row['column_name']
#DBconn.row_factory = sqlite3.Row
queryCurs = DBconn.cursor(dictionary=True)
anwser = queryCurs.execute(
'SELECT * '
'FROM messwerte '
'WHERE id=%s', (id,))
row = queryCurs.fetchall()
row_json = [ dict(rec) for rec in row ]
DBconn.close()
return row_json
def queryDBallStation():
DBconn = connectToDB()
# This enables column access by name: row['column_name']
#DBconn.row_factory = sqlite3.Row
queryCurs = DBconn.cursor(dictionary=True)
queryCurs.execute(
'SELECT originAddr, name , location, powerSaving '
'FROM messwerte '
'INNER JOIN stationen ON stationen.station_id = messwerte.originAddr '
'GROUP BY originAddr')
row = queryCurs.fetchall()
row_json = [ dict(rec) for rec in row ]
DBconn.close()
return row_json