request klasse erstellt, buffer umgebaut, db-abfrage erweitert

This commit is contained in:
Philipp Rauch 2014-01-22 18:02:24 +01:00
parent 0db4c49f7b
commit b025d35fd8
6 changed files with 157 additions and 61 deletions

View file

@ -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()