ems/Connector/modules/database.py

68 lines
1.8 KiB
Python
Raw Normal View History

'''
Created on 26.11.2013
@author: Philipp Rauch
'''
from profile.database import Database
2014-03-17 17:25:53 +01:00
### Dict of Tables to sort after ###
2014-02-21 13:42:50 +01:00
_sort_lookup = { 'EA_Last' : 'DateTime',
2014-03-05 14:42:09 +01:00
'GRIDManager_I_IST' : 'DateTime',
'GRIDManager_I_PH' : 'DateTime',
'GRIDManager_Status' : 'DateTime',
'GRIDManager_Temperatur': 'DateTime',
'GRIDManager_U_IST': 'DateTime',
2014-02-21 13:42:50 +01:00
'PAC' : 'date_/_time',
'pCharger': 'DateTime'}
def setup(conf):
'''
establishes a connection to the database and returns a Database object
@return: Database object
'''
db = Database()
db.loadDatabase(strHost = conf['mySQL_server'],
intPort = int(conf['mySQL_port']),
strUser = conf['mySQL_user'],
strPasswd = conf['mySQL_pass'],
strDatabase = conf['mySQL_database'],
strTable = None)
return db
def loop(db, item):
'''
2014-03-17 17:25:53 +01:00
get the last line of the table 'item' in database
and return it
'''
path = item.split('/')
if path[0] == '':
path.remove('')
table = path[len(path) -1]
if table == 'device':
tmp = { table : {} }
for i in db.getInformation()['Table'].keys():
tmp[table][i] = {}
return tmp
try:
sort = db.getInformation()['Table_Prime'][table][0]
except:
try:
sort = _sort_lookup[table]
except:
return { 'error/db_error' : { table : 'no Prime-Key / Sort defined' } }
res = db.readDatabase(intRowOffset=0, intRowNumber=1, strTable=table, strSort=sort, bSortInvert=True)
result = res.to_dict()
for k in result:
result[k] = result[k][0]
return {item : result}
def close(cursor):
pass