funktionsfähige Testimplementierung

*test.py startet den CANFilter, der die Daten vom CAN-BUS in die Datenbank
schreibt. (CAN-msgid = 0x3A4, 2&3 Byte, Multiplikator 0.01)
*API.py startet die API
*Diese kann man unter localhost:5000 erreichen
*der CAN Wert läuft auf "localhost:5000/dc_labor/device/battery/voltage"
auf
This commit is contained in:
Philipp Rauch 2013-11-22 14:25:12 +01:00
parent 59340b5fee
commit cda58b0f2e
7 changed files with 132 additions and 80 deletions

View file

@ -10,10 +10,11 @@ from datetime import datetime
from flask import Flask, jsonify, abort, make_response, request
from socket import gethostname
from threading import Thread
from config import readConf
from config import Config
import ems
conf = readConf()
c = Config()
conf = c.readConf()
api_host = gethostname() if conf['flask_server'] == '0.0.0.0' else conf['flask_server']
api_url = 'http://%s:%s' % (api_host, conf['flask_port'])
@ -163,7 +164,7 @@ class API(object):
self.app.add_url_rule('/<path:path>', 'get_catch_all', get_catch_all, methods = ['GET'])
# def run(self):
self.app.run(host = conf['flask_server'], port = int(conf['flask_port']), debug = True)
# self.app.run(host = conf['flask_server'], port = int(conf['flask_port']), debug = True)
buf = Buffer()
@ -202,7 +203,8 @@ def start():
api = API()
api.app.run(host = conf['flask_server'], port = int(conf['flask_port']), debug = conf['flask_debug'])
emsthread = ems.ems(buf)
print 'EMS-Thread:\t', emsthread
EMS = ems.ems(buf)
emsthread = EMS.start()
print 'EMS-Thread:\t', EMS
print '\tAPI:\t', buf
start()

View file

@ -13,7 +13,7 @@ from time import sleep
from threading import Thread
from Queue import Queue
from MySQLdb import connect
from config import readConf
from config import Config
debug = False
@ -21,12 +21,14 @@ class CANFilter(Thread):
battery_current, battery_voltage, battery_capacity, battery_timestamp, test = [], [], [], [], [] #create tmp lists
conf = readConf()
c = Config()
conf = c.readConf()
connection = connect(host = conf['mySQL_server'],
user = conf['mySQL_user'],
passwd = conf['mySQL_pass'],
db = conf['mySQL_database'])
db = conf['mySQL_database'],
port = int(conf['mySQL_port']))
cursor = connection.cursor()
def __init__(self):
@ -34,7 +36,7 @@ class CANFilter(Thread):
self.queue = Queue()
self.pcan = PcanAdapter(PcanAdapter.Baudrate['250k'])
self.pcan = PcanAdapter(PcanAdapter.Baudrate['250k'], debug = self.conf['config_debug'])
self.pcan.initialize()
dc_battery = CanMessage(0x3A4, 8, 50, 'dc_battery')

View file

@ -3,8 +3,10 @@ Created on 21.11.2013
@author: rauchp
'''
class Config():
_instance = None
confDic = {
_confDic = {
'mySQL_server': 'localhost',
'mySQL_port': '3306',
'mySQL_user': 'smoke',
@ -14,9 +16,21 @@ confDic = {
'flask_server': '0.0.0.0',
'flask_port': '5000',
'flask_debug': False,
'config_debug' : False,
'config_read' : False
}
def readConf():
def __new__(cls, *args, **kwargs):
# http://stackoverflow.com/questions/42558/python-and-the-singleton-pattern
if not cls._instance:
cls._instance = super(Config, cls).__new__(
cls, *args, **kwargs)
return cls._instance
def readConf(self):
if self._confDic['config_read']:
return self._confDic
try:
confFile = open("config\ems.conf", "r")
error = False
@ -24,8 +38,8 @@ def readConf():
error = True
if error:
confDic.update({'error' : 'config/ems.conf not found'})
return confDic
self._confDic.update({'error' : 'config/ems.conf not found'})
return self._confDic
for line in confFile:
line = line.strip()
@ -37,9 +51,9 @@ def readConf():
val[0] = val[0].strip()
val[0] = True if val[0] == 'True' else val[0]
val[0] = False if val[0] == 'False' else val[0]
confDic[ident[0]] = val[0]
self._confDic[ident[0]] = val[0]
confFile.close()
return confDic
print 'config:\t', readConf()
self._confDic['config_read'] = True
if self._confDic['config_debug']:
print ('config:\t', self._confDic)
return self._confDic

View file

@ -11,4 +11,8 @@ mySQL_table = battery
flask_server = 0.0.0.0 # 0.0.0.0 for public access
flask_port = 5000
flask_debug = False # currently not used
flask_debug = True
#### CONFIG ####
config_debug = False

View file

@ -39,7 +39,8 @@ class ems(Thread):
alt = None
while True:
neu = self.queue.get()
if not alt == neu:
self.buffer.device.get['battery'].update({'Voltage' : neu[2]})
print neu
if not alt == neu or True:
# self.buffer.update({'Voltage' : neu[2]})
self.buffer.device.get('battery').update({'voltage' : neu[2]})
# print self.buffer.device.get('battery').get('voltage'), neu[2]
alt = neu

View file

@ -9,15 +9,15 @@ from threading import Thread
from Queue import Queue
#from CANFilter import CANFilter
from time import sleep
from config import readConf
from config import Config
from MySQLdb import connect
MYSQL = 0
CSV = 1
XML = 2
JSON = 3
conf = readConf()
c = Config()
conf = c.readConf()
class Swich(Thread):
@ -29,7 +29,8 @@ class Swich(Thread):
def __del__(self):
if self.source == MYSQL:
self.CAN.join()
# self.CAN.join()
pass
def initialisiere(self):
if self.source == MYSQL:
@ -44,7 +45,8 @@ class Swich(Thread):
self.connection = connect(host = conf['mySQL_server'],
user = conf['mySQL_user'],
passwd = conf['mySQL_pass'],
db = conf['mySQL_database'])
db = conf['mySQL_database'],
port = int(conf['mySQL_port']))
self.cursor = self.connection.cursor()
### init Queue ###

27
scr/test.py Normal file
View file

@ -0,0 +1,27 @@
'''
Created on 21.11.2013
@author: rauchp
'''
import CANFilter
# import ems
# import time
# from config import Config
#
# c = Config()
# conf = c.readConf()
# buffer={}
# if conf['config_debug']:
# print 'starte CAN'
can = CANFilter.CANFilter()
can.start()
# if conf['config_debug']:
# print 'starte EMS'
# th = ems.ems(buffer)
# th.start()
# if conf['config_debug']:
# print 'alles gestartet'
# while True:
# print buffer
# time.sleep(1)