request klasse erstellt, buffer umgebaut, db-abfrage erweitert
This commit is contained in:
parent
0db4c49f7b
commit
b025d35fd8
6 changed files with 157 additions and 61 deletions
93
src/API.py
93
src/API.py
|
|
@ -5,7 +5,7 @@ Created on 24.10.2013
|
|||
@version: 0.6
|
||||
'''
|
||||
from sys import stderr
|
||||
from datetime import datetime
|
||||
import datetime
|
||||
from flask import Flask, jsonify, abort, make_response, request
|
||||
from socket import gethostname
|
||||
from config import Config
|
||||
|
|
@ -20,55 +20,23 @@ api_url = 'http://%s:%s' % (api_host, conf['flask_port'])
|
|||
|
||||
#### BUFFER ####
|
||||
class Buffer(object):
|
||||
device = {
|
||||
'battery' : {
|
||||
'id': 1,
|
||||
'Voltage' : 400.11,
|
||||
'Current' : 20.264,
|
||||
'Power' : 53.465,
|
||||
'SoC' : 80.34,
|
||||
'isCharging' : False,
|
||||
'isMaster' : True,
|
||||
'dyn' : None },
|
||||
'ac_grid' : {
|
||||
'id': 2,
|
||||
'Voltage' : 400.23,
|
||||
'Current' : 10.423,
|
||||
'Power' : 23.35,
|
||||
'isOn' : True,
|
||||
'isFeed' : True,
|
||||
'isMaster' : False,
|
||||
'dyn' : None }
|
||||
}
|
||||
|
||||
error = {
|
||||
'db_error' : {
|
||||
'id' : 1,
|
||||
'content' : None }
|
||||
}
|
||||
dc_labor = {
|
||||
'device' : device,
|
||||
'error' : error
|
||||
}
|
||||
|
||||
dc_grid = {
|
||||
'device' : 'device',
|
||||
'error' : 'error'
|
||||
}
|
||||
|
||||
ac_grid = {
|
||||
'device' : 'device',
|
||||
'error' : 'error'
|
||||
}
|
||||
|
||||
system = {
|
||||
'00_config' : conf,
|
||||
'dc_labor' : dc_labor,
|
||||
'dc_grid' : dc_grid,
|
||||
'ac_grid' : ac_grid,
|
||||
'request' : {}
|
||||
'device' : {},
|
||||
'error' : error,
|
||||
'request' : [],
|
||||
'done' : []
|
||||
}
|
||||
|
||||
_instance = None
|
||||
_id = 0
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
# http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern
|
||||
|
|
@ -128,7 +96,7 @@ class Buffer(object):
|
|||
for i in dic.keys():
|
||||
self.generate_links(dic.get(i), url, i)
|
||||
dic.update({'000_href': url})
|
||||
dic.update({'000_timestamp' : str(datetime.now())})
|
||||
dic.update({'000_timestamp' : str(datetime.datetime.now())})
|
||||
|
||||
def foo(self, l, args):
|
||||
'''
|
||||
|
|
@ -181,9 +149,18 @@ class Buffer(object):
|
|||
sys.update(value)
|
||||
|
||||
def init_buffer(self):
|
||||
#ToDo
|
||||
self.system['reqest'].append('init')
|
||||
pass
|
||||
|
||||
class Request(object):
|
||||
|
||||
def __init__(self, path, time, id):
|
||||
self.content = path
|
||||
self.time = time
|
||||
self.id = id
|
||||
|
||||
|
||||
|
||||
class API(object):
|
||||
|
||||
def __init__(self):
|
||||
|
|
@ -201,6 +178,7 @@ class API(object):
|
|||
self.app.error_handler_spec[None][400] = bad_reqest
|
||||
self.app.error_handler_spec[None][404] = not_found
|
||||
self.app.error_handler_spec[None][405] = not_allowed
|
||||
self.app.error_handler_spec[None][500] = server_error
|
||||
self.app.add_url_rule('/', 'get_root', get_root, methods = ['GET'])
|
||||
self.app.add_url_rule('/<path:path>', 'get_catch_all',
|
||||
get_catch_all, methods = ['GET'])
|
||||
|
|
@ -217,6 +195,9 @@ def not_found(error):
|
|||
def bad_reqest(error):
|
||||
return make_response(jsonify( { 'error': '400 Bad Reqest' } ), 400)
|
||||
|
||||
def server_error(error):
|
||||
return make_response(jsonify( { 'error': '500 Internal Server Error' } ), 500)
|
||||
|
||||
########## GET Handler ##########
|
||||
def get_root():
|
||||
buf.set_href(Buffer.system, api_url)
|
||||
|
|
@ -235,13 +216,37 @@ def get_catch_all(path):
|
|||
# to_dict(flat=True) returns the key and the first item of the value list.
|
||||
out = buf.foo(l, args)
|
||||
else:
|
||||
#req = add_reqest(path)
|
||||
|
||||
#while req not in Buffer.system['done']:
|
||||
# pass
|
||||
|
||||
#Buffer.system['done'].remove(req)
|
||||
out = buf.get_level(l)
|
||||
|
||||
return jsonify( out )
|
||||
|
||||
def add_reqest(path):
|
||||
notinlist = False
|
||||
time = datetime.datetime.now()
|
||||
id = Buffer._id
|
||||
tmp = Request(path, time, id)
|
||||
try:
|
||||
Buffer.system['request'].index(tmp)
|
||||
except ValueError:
|
||||
notinlist = True
|
||||
|
||||
if notinlist:
|
||||
Buffer.system['request'].insert(0, tmp)
|
||||
return id
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
|
||||
def API_start():
|
||||
api = API()
|
||||
api.app.run(host = conf['flask_server'],
|
||||
port = int(conf['flask_port']),
|
||||
debug = conf['flask_debug'])
|
||||
|
||||
port = int(conf['flask_port']))
|
||||
#debug = conf['flask_debug']
|
||||
API_start()
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ mySQL_port = 3306 # default 3306
|
|||
mySQL_user = stud_EMS
|
||||
mySQL_pass = sql13
|
||||
mySQL_database = photodb
|
||||
mySQL_table = battery, grid, pv, charger
|
||||
mySQL_speed = 0.1 # time between two query's in sec
|
||||
|
||||
#### FLASK ####
|
||||
|
|
|
|||
47
src/ems.py
47
src/ems.py
|
|
@ -4,9 +4,11 @@ Created on 15.11.2013
|
|||
@author: Philipp Rauch
|
||||
@version: 0.02
|
||||
'''
|
||||
from threading import Thread
|
||||
from threading import Thread, Timer
|
||||
from switch import Switch, MYSQL
|
||||
from config import Config
|
||||
from time import sleep
|
||||
#from API import Request
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
|
|
@ -24,6 +26,21 @@ def startSwitch():
|
|||
switch.start()
|
||||
return switch, queue, query
|
||||
|
||||
class Emulator(object):
|
||||
def __init__(self, func, time):
|
||||
self._func = func
|
||||
self._time = time
|
||||
|
||||
def run(self):
|
||||
self.threadTimer = Timer(self._time, self.runProfile)
|
||||
self.threadTimer.start()
|
||||
self._func()
|
||||
|
||||
def stop(self):
|
||||
self.threadTimer.cancel()
|
||||
sleep(1)
|
||||
print(self.threadTimer.isAlive())
|
||||
|
||||
class ems(Thread):
|
||||
def __init__(self, buf):
|
||||
Thread.__init__(self)
|
||||
|
|
@ -31,19 +48,36 @@ class ems(Thread):
|
|||
self.switch, self.queue, self.query = startSwitch()
|
||||
if conf['config_debug']:
|
||||
print '\tEMS-BUFFER:\t', buf
|
||||
self.update = Emulator(self.update_device())
|
||||
self.update.run(conf['mySQL_speed'])
|
||||
|
||||
def __del__(self):
|
||||
self.update.stop()
|
||||
self.switch.stop = True
|
||||
|
||||
def run(self):
|
||||
#while True
|
||||
# if self.getRequest():
|
||||
|
||||
old = None
|
||||
while True:
|
||||
new = self.getNewMsg(old)
|
||||
if conf['config_debug']:
|
||||
print 'GET:\t', new
|
||||
self.buffer.update_buffer(new)
|
||||
self.queue.task_done()
|
||||
old = new
|
||||
|
||||
def update_device(self):
|
||||
self.query.put('/device')
|
||||
self.buffer.update_buffer(self.queue.get())
|
||||
keys = self.buffer.system['device'].keys()
|
||||
for key in keys:
|
||||
if key.startswith("000"):
|
||||
continue
|
||||
self.query.put('/device/%s' % key)
|
||||
self.buffer.update_buffer(self.queue.get())
|
||||
|
||||
def getNewMsg(self, old):
|
||||
'''
|
||||
A blocking Method to get a different Message from Queue
|
||||
|
|
@ -51,10 +85,15 @@ class ems(Thread):
|
|||
'''
|
||||
tmp = self.queue.get()
|
||||
while tmp == old:
|
||||
self.queue.task_done()
|
||||
tmp = self.queue.get()
|
||||
return tmp
|
||||
|
||||
def getRequest(self):
|
||||
#TODO: get Request from buffer
|
||||
#TODO: define a request in buffer
|
||||
pass
|
||||
try:
|
||||
req = self.buffer.system['request'].pop()
|
||||
self.query.put(req.content)
|
||||
self.query.join()
|
||||
return self.queue.get()
|
||||
except IndexError:
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -4,6 +4,37 @@ Created on 26.11.2013
|
|||
@author: Philipp Rauch
|
||||
'''
|
||||
from MySQLdb import connect
|
||||
from datetime import date, timedelta
|
||||
from decimal import Decimal
|
||||
|
||||
tables = {}
|
||||
|
||||
def get_tables(cur, db):
|
||||
sql_tables = 'SHOW TABLES FROM %s'
|
||||
tables = []
|
||||
cur.execute(sql_tables % db)
|
||||
|
||||
#TODO try - except
|
||||
for i in cur:
|
||||
tables.append(i[0])
|
||||
return tables
|
||||
|
||||
|
||||
def get_prime(cur, tabele):
|
||||
sql_get_prim = "SHOW COLUMNS FROM %s WHERE `Key` = 'PRI'"
|
||||
sql_get_no_prim = "SHOW COLUMNS FROM %s WHERE `Key` != 'PRI'"
|
||||
collum = []
|
||||
cur.execute(sql_get_prim % tabele)
|
||||
|
||||
#TODO try - except
|
||||
for i in cur:
|
||||
collum.append(i[0])
|
||||
|
||||
cur.execute(sql_get_no_prim % tabele)
|
||||
for i in cur:
|
||||
collum.append(i[0])
|
||||
|
||||
return collum
|
||||
|
||||
def setup(conf):
|
||||
'''
|
||||
|
|
@ -17,6 +48,13 @@ def setup(conf):
|
|||
db = conf['mySQL_database'],
|
||||
port = int(conf['mySQL_port']))
|
||||
cursor = connection.cursor()
|
||||
|
||||
tab = get_tables(cursor, conf['mySQL_database'])
|
||||
for i in tab:
|
||||
primes = get_prime(cursor, i)
|
||||
tables[i] = primes
|
||||
print tables
|
||||
|
||||
return cursor
|
||||
|
||||
def loop(cursor, item):
|
||||
|
|
@ -24,25 +62,37 @@ def loop(cursor, item):
|
|||
|
||||
|
||||
'''
|
||||
sql_values = 'SELECT * FROM %s ORDER BY DateTime DESC LIMIT 1'
|
||||
sql_collums = 'SHOW COLUMNS FROM %s'
|
||||
sql_values = 'SELECT * FROM %s ORDER BY %s DESC LIMIT 1'
|
||||
|
||||
path = item.split('/')
|
||||
if path[0] == '':
|
||||
path.remove('')
|
||||
|
||||
collums = []
|
||||
cursor.execute(sql_collums % path[len(path) -1])
|
||||
for cul in cursor:
|
||||
collums.append(cul[0])
|
||||
table = path[len(path) -1]
|
||||
if table == 'device':
|
||||
tmp = { table : {} }
|
||||
for i in tables.keys():
|
||||
tmp[table][i] = {}
|
||||
return tmp
|
||||
|
||||
cursor.execute(sql_values % path[len(path) -1])
|
||||
|
||||
cursor.execute(sql_values % (table, tables[table][0]))
|
||||
values = []
|
||||
for row in cursor:
|
||||
values = row
|
||||
|
||||
result = {}
|
||||
for i in range(len(collums)):
|
||||
result[collums[i]] = values[i]
|
||||
|
||||
for i in range(0, len(tables[table])):
|
||||
if values == []:
|
||||
result[tables[table][i]] = None
|
||||
else:
|
||||
if isinstance(values[i], date) or isinstance(values[i], timedelta):
|
||||
result[tables[table][i]] = str(values[i])
|
||||
elif isinstance(values[i], Decimal):
|
||||
result[tables[table][i]] = float(values[i])
|
||||
else:
|
||||
result[tables[table][i]] = values[i]
|
||||
|
||||
# for p in range(len(path)):
|
||||
# result = { path.pop() : result }
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import csv
|
|||
from datetime import datetime
|
||||
import os
|
||||
|
||||
sqlerr = "INSERT INTO %s (DateTime,Type,Error) VALUES (\'%s\',%s,%s)"
|
||||
sqlerr = "INSERT INTO %s (DateTime,Type,error) VALUES (\'%s\',%s,%s)"
|
||||
sqlnor = "INSERT INTO %s (DateTime,Type,cardnumber,customernumber,plug,meterreading) VALUES (\'%s\',%s,%s,%s,%s,%s)"
|
||||
folder = 'pCharger'
|
||||
|
||||
|
|
|
|||
|
|
@ -65,7 +65,9 @@ class Switch(Thread):
|
|||
# item = query.get() #block = True
|
||||
# query should be included in the result
|
||||
|
||||
item = 'dc_labor/device/battery'
|
||||
item = self.getItem(block = True)
|
||||
|
||||
#item = 'device'
|
||||
if self.source == MYSQL:
|
||||
result = database.loop(self.cursor, item)
|
||||
elif self.source == MODBUS:
|
||||
|
|
@ -74,10 +76,11 @@ class Switch(Thread):
|
|||
#print 'PUT:\t%s' % result
|
||||
|
||||
self.queue.put(result)
|
||||
self.query.task_done()
|
||||
if self.source == MYSQL:
|
||||
sleep(float(conf['mySQL_speed']))
|
||||
elif self.source == MODBUS:
|
||||
sleep(0.1)
|
||||
|
||||
def getItem(self, block = False):
|
||||
return self.queue.get(block)
|
||||
return self.query.get(block)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue