Datenbankunterstützung und Buffer
* CANFilter kann jetzt empfangene Daten in eine MySQL Datenbank schreiben. * rest-api.py wurde in API.py umbenannt. * Buffer wurde zu einer Klasse umstrukturiert.
This commit is contained in:
parent
47ecc5fbe2
commit
5df7ae564a
3 changed files with 258 additions and 248 deletions
234
scr/API.py
Normal file
234
scr/API.py
Normal file
|
|
@ -0,0 +1,234 @@
|
|||
'''
|
||||
Created on 24.10.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
@version: 0.5
|
||||
'''
|
||||
|
||||
from flask import Flask, jsonify, abort, make_response, request
|
||||
from time import time
|
||||
import socket
|
||||
|
||||
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_url = 'http://%s:%s' % (api_host, app_port)
|
||||
|
||||
#### BUFFER ####
|
||||
class Buffer(object):
|
||||
device = {
|
||||
'battery' : {
|
||||
'id': 1,
|
||||
'voltage' : 400.11,
|
||||
'current' : 20.264,
|
||||
'power' : 53.465,
|
||||
'capacity' : 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 = {
|
||||
'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__()
|
||||
|
||||
|
||||
def gen_start_url(self, l):
|
||||
'''
|
||||
generate a URL form a list
|
||||
@param l: list of aspacts of the URL
|
||||
@return: URL
|
||||
'''
|
||||
url = api_url
|
||||
for i in l:
|
||||
url = '%s/%s' % (url, i)
|
||||
return url
|
||||
|
||||
def get_level(self, l):
|
||||
'''
|
||||
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
|
||||
'''
|
||||
level = l.pop(0)
|
||||
for i in l:
|
||||
if isinstance(level, dict) and i in level:
|
||||
level = level.get(i)
|
||||
else:
|
||||
abort(404)
|
||||
self.set_href(level, self.gen_start_url(l)) #set all links above
|
||||
return {l[-1] : level}
|
||||
|
||||
def generate_links(self, dic, url, postfix):
|
||||
'''
|
||||
generats a link to dic if dic is a dictionary
|
||||
@param dic: variable that is being tested on a dictionary
|
||||
@param url: previous url
|
||||
@param postfix: appendix to the given url
|
||||
'''
|
||||
if isinstance(dic, dict):
|
||||
url = '%s/%s' % (url, postfix)
|
||||
self.set_href(dic, url)
|
||||
else:
|
||||
return
|
||||
|
||||
def set_href(self, dic, url):
|
||||
'''
|
||||
set the ref link if dic is a dictionary
|
||||
@param dic: variable that is being tested on a dictionary
|
||||
@param url: url to the first dictionary
|
||||
'''
|
||||
if isinstance(dic, dict):
|
||||
for i in dic.keys():
|
||||
self.generate_links(dic.get(i), url, i)
|
||||
dic.update({'_href': url})
|
||||
dic.update({'_timestamp' : time()})
|
||||
|
||||
def foo(self, l, args):
|
||||
'''
|
||||
@param l: list
|
||||
@param args: arguments from the reqest
|
||||
@return: Dictionary
|
||||
'''
|
||||
del l[-1]
|
||||
dic = self.get_level(l).get(l[-1])
|
||||
if isinstance(dic, dict) and 'dyn' in dic:
|
||||
message = 'generating view for %s' % (l[-1])
|
||||
else:
|
||||
abort(404)
|
||||
return { l[-1] : message, 'args' : args.to_dict(flat=False)}
|
||||
|
||||
def update_buffer(self):
|
||||
#ToDo
|
||||
None
|
||||
|
||||
def init_buffer(self):
|
||||
#ToDo
|
||||
None
|
||||
|
||||
buf = Buffer().get_instance()
|
||||
print 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)
|
||||
if '' == l[-1]: # if last element of list is empty
|
||||
del l[-1] # remove it
|
||||
out = buf.get_level(l)
|
||||
elif 'dyn' == l[-1]:
|
||||
args = request.args # returns a dictionary with a list of values for each key
|
||||
# each value and each key is represented as a string
|
||||
# to convert it to a dictionary use to_dict(flat=False)
|
||||
# to_dict(flat=True) returns the key and the first item of the value list.
|
||||
out = buf.foo(l, args)
|
||||
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)
|
||||
|
|
@ -2,15 +2,15 @@
|
|||
Created on 13.11.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
@version: 0.01
|
||||
@version: 0.02
|
||||
'''
|
||||
|
||||
from CanMessage import CanMessage
|
||||
from CanSignal import CanSignal
|
||||
from PCan import PcanAdapter
|
||||
import datetime
|
||||
import time
|
||||
import sys
|
||||
import MySQLdb
|
||||
#import MySQLdb
|
||||
|
||||
debug = False
|
||||
|
||||
|
|
@ -53,10 +53,10 @@ pcan.Messages['dc_pv'].addSignal( voltage )
|
|||
pcan.Messages['dc_charging'].addSignal( current )
|
||||
pcan.Messages['dc_charging'].addSignal( voltage )
|
||||
|
||||
battery_current = []
|
||||
battery_voltage = []
|
||||
battery_capacity = []
|
||||
battery_timestamp = []
|
||||
battery_current, battery_voltage, battery_capacity, battery_timestamp = [], [], [], []
|
||||
|
||||
#connection = MySQLdb.connect("url", "user", "passwort", "datenbankname")
|
||||
#cursor = connection.cursor()
|
||||
|
||||
def mean(l):
|
||||
return float(sum(l))/len(l) if len(l) > 0 else 0
|
||||
|
|
@ -68,21 +68,22 @@ while True:
|
|||
battery_current.append(pcan.Messages['dc_battery'].Signals['current'].GetData())
|
||||
battery_voltage.append(pcan.Messages['dc_battery'].Signals['voltage'].GetData())
|
||||
battery_capacity.append(pcan.Messages['dc_battery'].Signals['capacity'].GetData())
|
||||
battery_timestamp.append(pcan.Messages['dc_battery'].timestamp)
|
||||
battery_timestamp.append(str(datetime.datetime.now()))
|
||||
|
||||
if len(battery_timestamp) == 10:
|
||||
if len(battery_timestamp) == 1000:
|
||||
if debug:
|
||||
print 'current: ', battery_current
|
||||
print 'voltage: ', battery_voltage
|
||||
print 'SoC: ', battery_capacity
|
||||
print 'time: ', battery_timestamp
|
||||
print 'current: ', mean(battery_current)
|
||||
print 'voltage: ', mean(battery_voltage)
|
||||
print 'SoC: ', mean(battery_capacity)
|
||||
print 'time: ', mean(battery_timestamp)
|
||||
del battery_current[:]
|
||||
del battery_voltage[:]
|
||||
del battery_capacity[:]
|
||||
del battery_timestamp[:]
|
||||
time.sleep(0.01)
|
||||
|
||||
print 'current: ', mean(battery_current)
|
||||
print 'voltage: ', mean(battery_voltage)
|
||||
print 'SoC: ', mean(battery_capacity)
|
||||
print 'time: ', mean(battery_timestamp)
|
||||
|
||||
tabelle = 'EMS-TEST'
|
||||
daten = [(tabelle, str(battery_timestamp[i]), str(battery_current[i]), str(battery_voltage[i]), str(battery_capacity[i])) for i in range(1000)]
|
||||
sql = 'INSERT INTO %s VALUES(%s, %s, %s, %s)'
|
||||
|
||||
for i in daten:
|
||||
print sql % i
|
||||
# cursor.executemany(sql, daten)
|
||||
|
||||
del battery_current[:], battery_voltage[:], battery_capacity[:], battery_timestamp[:]
|
||||
time.sleep(0.01)
|
||||
225
scr/rest-api.py
225
scr/rest-api.py
|
|
@ -1,225 +0,0 @@
|
|||
#!flask/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
'''
|
||||
Created on 24.10.2013
|
||||
|
||||
@author: Philipp Rauch
|
||||
@version: 0.5
|
||||
'''
|
||||
from flask import Flask, jsonify, abort, make_response, request
|
||||
from time import time
|
||||
import socket
|
||||
|
||||
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_url = 'http://%s:%s' % (api_host, app_port)
|
||||
|
||||
#### BUFFER ####
|
||||
device = {
|
||||
'battery' : {
|
||||
'id': 1,
|
||||
'voltage' : 400.11,
|
||||
'current' : 20.264,
|
||||
'power' : 53.465,
|
||||
'capacity' : 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 = {
|
||||
'dc_labor' : dc_labor,
|
||||
'dc_grid' : dc_grid,
|
||||
'ac_grid' : ac_grid
|
||||
}
|
||||
|
||||
#### END BUFFER ####
|
||||
|
||||
def gen_start_url(list):
|
||||
'''
|
||||
generate a URL form a list
|
||||
@param list: list of aspacts of the URL
|
||||
@return: URL
|
||||
'''
|
||||
url = api_url
|
||||
for i in list:
|
||||
url = '%s/%s' % (url, i)
|
||||
return url
|
||||
|
||||
def get_level(list):
|
||||
'''
|
||||
iterating over a dictionary on a given path
|
||||
@param list: items witch tell the path to the value
|
||||
@return: dictionary with the key and value of the last item in the list
|
||||
'''
|
||||
level = list.pop(0)
|
||||
for i in list:
|
||||
if isinstance(level, dict) and i in level:
|
||||
level = level.get(i)
|
||||
else:
|
||||
abort(404)
|
||||
set_href(level, gen_start_url(list)) #set all links above
|
||||
return {list[-1] : level}
|
||||
|
||||
def generate_links(dic, url, postfix):
|
||||
'''
|
||||
generats a link to dic if dic is a dictionary
|
||||
@param dic: variable that is being tested on a dictionary
|
||||
@param url: previous url
|
||||
@param postfix: appendix to the given url
|
||||
'''
|
||||
if isinstance(dic, dict):
|
||||
url = '%s/%s' % (url, postfix)
|
||||
set_href(dic, url)
|
||||
else:
|
||||
return
|
||||
|
||||
def set_href(dic, url):
|
||||
'''
|
||||
set the ref link if dic is a dictionary
|
||||
@param dic: variable that is being tested on a dictionary
|
||||
@param url: url to the first dictionary
|
||||
'''
|
||||
if isinstance(dic, dict):
|
||||
for i in dic.keys():
|
||||
generate_links(dic.get(i), url, i)
|
||||
dic.update({'_href': url})
|
||||
dic.update({'_timestamp' : time()})
|
||||
|
||||
def foo(list, args):
|
||||
'''
|
||||
@param lsit: list
|
||||
@param args: arguments from the reqest
|
||||
@return: Dictionary
|
||||
'''
|
||||
del list[-1]
|
||||
dic = get_level(list).get(list[-1])
|
||||
if isinstance(dic, dict) and 'dyn' in dic:
|
||||
str = 'generating view for %s' % (list[-1])
|
||||
else:
|
||||
abort(404)
|
||||
return { list[-1] : str, 'args' : args.to_dict(flat=False)}
|
||||
|
||||
def update_buffer():
|
||||
#ToDo
|
||||
None
|
||||
|
||||
def init_buffer():
|
||||
#ToDo
|
||||
None
|
||||
|
||||
########## ERROR Handler ##########
|
||||
@app.errorhandler(405)
|
||||
def not_found(error):
|
||||
return make_response(jsonify( { 'error': '405 Method 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():
|
||||
set_href(system, api_url)
|
||||
return jsonify( { 'system' : system } )
|
||||
|
||||
@app.route('/<path:path>', methods = ['GET'])
|
||||
def get_catch_all(path):
|
||||
list = path.split('/')
|
||||
list.insert(0, system)
|
||||
if '' == list[-1]: # if last element of list is empty
|
||||
del list[-1] # remove it
|
||||
out = get_level(list)
|
||||
elif 'dyn' == list[-1]:
|
||||
args = request.args # returns a dictionary with a list of values for each key
|
||||
# each value and each key is represented as a string
|
||||
# to convert it to a dictionary use to_dict(flat=False)
|
||||
# to_dict(flat=True) returns the key and the first item of the value list.
|
||||
out = foo(list, args)
|
||||
else:
|
||||
out = get_level(list)
|
||||
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