Module laufen als Thread
*Thread für swich implementiert *Thread für API implementiert (noch nicht lauffähig) *Swich startet CANFilter
This commit is contained in:
parent
704e02385f
commit
f0686090ec
5 changed files with 120 additions and 107 deletions
105
scr/API.py
105
scr/API.py
|
|
@ -2,17 +2,17 @@
|
|||
Created on 24.10.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
@version: 0.5
|
||||
@version: 0.6
|
||||
'''
|
||||
|
||||
from flask import Flask, jsonify, abort, make_response, request
|
||||
from time import time
|
||||
import socket
|
||||
from time import time
|
||||
from flask import Flask, jsonify, abort, make_response, request
|
||||
from socket import gethostname
|
||||
from threading import Thread
|
||||
|
||||
app = Flask(__name__)
|
||||
app_host = '0.0.0.0' #api_host='0.0.0.0' for public access
|
||||
app_port = '5000'
|
||||
api_host = socket.gethostname() if app_host == '0.0.0.0' else app_host
|
||||
api_host = gethostname() if app_host == '0.0.0.0' else app_host
|
||||
api_url = 'http://%s:%s' % (api_host, app_port)
|
||||
|
||||
#### BUFFER ####
|
||||
|
|
@ -58,22 +58,19 @@ class Buffer(object):
|
|||
}
|
||||
|
||||
system = {
|
||||
'dc_labor' : dc_labor,
|
||||
'dc_grid' : dc_grid,
|
||||
'dc_labor' : dc_labor,
|
||||
'dc_grid' : dc_grid,
|
||||
'ac_grid' : ac_grid
|
||||
}
|
||||
|
||||
isInitialised = False
|
||||
|
||||
def __init__(self):
|
||||
self.isInitialised = True
|
||||
|
||||
def get_instance(self):
|
||||
if self.isInitialised:
|
||||
return self
|
||||
else:
|
||||
return Buffer().__init__()
|
||||
_instance = None
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
# http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern
|
||||
if not cls._instance:
|
||||
cls._instance = super(Buffer, cls).__new__(
|
||||
cls, *args, **kwargs)
|
||||
return cls._instance
|
||||
|
||||
def gen_start_url(self, l):
|
||||
'''
|
||||
|
|
@ -90,7 +87,7 @@ class Buffer(object):
|
|||
'''
|
||||
iterating over a dictionary on a given path
|
||||
@param l: items witch tell the path to the value
|
||||
@return: dictionary with the key and value of the last item in the list
|
||||
@return: dictionary with the key and value of the last item in the list
|
||||
'''
|
||||
level = l.pop(0)
|
||||
for i in l:
|
||||
|
|
@ -148,28 +145,40 @@ class Buffer(object):
|
|||
#ToDo
|
||||
None
|
||||
|
||||
buf = Buffer().get_instance()
|
||||
class API(Thread):
|
||||
|
||||
def __init__(self):
|
||||
Thread.__init__(self)
|
||||
self.app = Flask(__name__)
|
||||
|
||||
### ADD URL RULES ###
|
||||
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.add_url_rule('/', 'get_root', get_root, methods = ['GET'])
|
||||
self.app.add_url_rule('/<path:path>', 'get_catch_all', get_catch_all, methods = ['GET'])
|
||||
|
||||
def run(self):
|
||||
self.app.run(host = app_host, port = int(app_port), debug = True)
|
||||
|
||||
buf = Buffer()
|
||||
print 'API:\t', buf
|
||||
|
||||
########## ERROR Handler ##########
|
||||
@app.errorhandler(405)
|
||||
def not_allowed(error):
|
||||
return make_response(jsonify( { 'error': '405 Not Allowed' } ), 405)
|
||||
|
||||
@app.errorhandler(404)
|
||||
def not_found(error):
|
||||
return make_response(jsonify( { 'error': '404 Not Found' } ), 404)
|
||||
|
||||
@app.errorhandler(400)
|
||||
def bad_reqest(error):
|
||||
return make_response(jsonify( { 'error': '400 Bad Reqest' } ), 400)
|
||||
|
||||
########## GET Handler ##########
|
||||
@app.route('/', methods = ['GET'])
|
||||
def get_root():
|
||||
buf.set_href(Buffer.system, api_url)
|
||||
return jsonify( { 'system' : Buffer.system } )
|
||||
|
||||
@app.route('/<path:path>', methods = ['GET'])
|
||||
def get_catch_all(path):
|
||||
l = path.split('/')
|
||||
l.insert(0, Buffer.system)
|
||||
|
|
@ -185,49 +194,3 @@ def get_catch_all(path):
|
|||
else:
|
||||
out = buf.get_level(l)
|
||||
return jsonify( out )
|
||||
|
||||
######### POST Handler ##########
|
||||
#@app.route('/db', methods = ['POST'])
|
||||
#def create_item():
|
||||
# if not request.json or not 'title' in request.json:
|
||||
# abort(400)
|
||||
# item = {
|
||||
# 'id': db[-1]['id'] + 1,
|
||||
# 'title': request.json['title'],
|
||||
# 'description': request.json.get('description', ""),
|
||||
# 'done': False
|
||||
# }
|
||||
# db.append(item)
|
||||
# return jsonify( { 'item': item } ), 201
|
||||
|
||||
########## PUT Handler ##########
|
||||
#@app.route('/db/<int:item_id>', methods = ['PUT'])
|
||||
#def update_item(item_id):
|
||||
# item = filter(lambda t: t['id'] == item_id, db)
|
||||
# if len(item) == 0:
|
||||
# abort(404)
|
||||
# if not request.json:
|
||||
# abort(400)
|
||||
# if 'title' in request.json and type(request.json['title']) != unicode:
|
||||
# abort(400)
|
||||
# if 'description' in request.json and type(request.json['description']) is not unicode:
|
||||
# abort(400)
|
||||
# if 'done' in request.json and type(request.json['done']) is not bool:
|
||||
# abort(400)
|
||||
# item[0]['title'] = request.json.get('title', item[0]['title'])
|
||||
# item[0]['description'] = request.json.get('description', item[0]['description'])
|
||||
# item[0]['done'] = request.json.get('done', item[0]['done'])
|
||||
# return jsonify( { 'item': item[0] } ), 201
|
||||
|
||||
########## DELET Handler ##########
|
||||
#@app.route('/db/<int:item_id>', methods = ['DELETE'])
|
||||
#def delete_item(item_id):
|
||||
# item = filter(lambda t: t['id'] == item_id, db)
|
||||
# if len(item) == 0:
|
||||
# abort(404)
|
||||
# db.remove(item[0])
|
||||
# return jsonify( { 'result': True } )
|
||||
|
||||
########## RUN ##########
|
||||
if __name__ == '__main__':
|
||||
app.run(host = app_host, port = int(app_port), debug = True)
|
||||
Loading…
Add table
Add a link
Reference in a new issue