code aufgeräumt
This commit is contained in:
parent
bd216d5222
commit
25e406ed8c
6 changed files with 62 additions and 78 deletions
10
src/API.py
10
src/API.py
|
|
@ -4,7 +4,7 @@ Created on 24.10.2013
|
|||
@author: Philipp Rauch
|
||||
@version: 0.6
|
||||
'''
|
||||
# from sys import stderr
|
||||
# from sys import stderr
|
||||
from datetime import datetime
|
||||
from flask import Flask, jsonify, make_response, request
|
||||
from socket import gethostname
|
||||
|
|
@ -29,12 +29,18 @@ class Request(object):
|
|||
self.id = req_id
|
||||
|
||||
|
||||
def API_start():
|
||||
api = API()
|
||||
print 'API-Thread:\t%s\n' % api
|
||||
api.app.run(host = conf['flask_server'],
|
||||
port = int(conf['flask_port']))
|
||||
#debug = conf['flask_debug']
|
||||
|
||||
class API(object):
|
||||
|
||||
def __init__(self):
|
||||
self.app = Flask(__name__)
|
||||
|
||||
|
||||
### Start Buffer ###
|
||||
self.buf = Buffer()
|
||||
|
||||
|
|
|
|||
|
|
@ -12,25 +12,26 @@ from datetime import datetime
|
|||
from time import sleep
|
||||
from threading import Thread
|
||||
from Queue import Queue
|
||||
from config import Config
|
||||
from pandas import DataFrame
|
||||
from profile.database import Database
|
||||
|
||||
debug = True
|
||||
|
||||
class CANFilter(Thread):
|
||||
def CAN_start(conf):
|
||||
print 'starte CAN mit Baud von', conf['can_baudrate']
|
||||
can = CANFilter(conf)
|
||||
can.start()
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
conf = c.readConf()
|
||||
class CANFilter(Thread):
|
||||
|
||||
### Lookup f<>r CAN --> DB ###
|
||||
lookup = {
|
||||
'dc_battery' : 'battery'
|
||||
}
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, conf):
|
||||
Thread.__init__(self)
|
||||
self.conf = conf
|
||||
self.queue = Queue()
|
||||
|
||||
### init DB ###
|
||||
|
|
@ -48,14 +49,14 @@ class CANFilter(Thread):
|
|||
self.pcan = PcanAdapter(PcanAdapter.Baudrate[self.conf['can_baudrate']],
|
||||
debug = self.conf['config_debug'])
|
||||
self.pcan.initialize()
|
||||
if isinstance(self.conf["symfile"], str):
|
||||
sym = "%s/%s" % (self.conf["config_dictionary"],
|
||||
self.conf["symfile"])
|
||||
if isinstance(self.conf['can_symfile'], str):
|
||||
sym = '%s/%s' % (self.conf['config_dictionary'],
|
||||
self.conf['can_symfile'])
|
||||
self.symList.append(get_DataFrameDict(sym))
|
||||
Add2Adapter(self.pcan, sym)
|
||||
elif isinstance(self.conf["symfile"], list):
|
||||
for element in self.conf["symfile"]:
|
||||
sym = "%s/%s" % (self.conf["config_dictionary"], element)
|
||||
elif isinstance(self.conf['can_symfile'], list):
|
||||
for element in self.conf['can_symfile']:
|
||||
sym = '%s/%s' % (self.conf['config_dictionary'], element)
|
||||
self.symList.append(get_DataFrameDict(sym))
|
||||
Add2Adapter(self.pcan, sym)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ Created on 21.11.2013
|
|||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
from sys import stderr
|
||||
from sys import stderr, exit
|
||||
|
||||
config = "config\ems.conf"
|
||||
|
||||
|
|
@ -11,19 +11,19 @@ class Config():
|
|||
_instance = None
|
||||
|
||||
_confDic = {
|
||||
'mySQL_server': 'localhost',
|
||||
'mySQL_server': '',
|
||||
'mySQL_port': '3306',
|
||||
'mySQL_user': 'smoke',
|
||||
'mySQL_pass': 'KiWujcafAlor',
|
||||
'mySQL_database': 'smoke_test',
|
||||
#'mySQL_table': 'battery',
|
||||
'mySQL_speed' : '0.1',
|
||||
'mySQL_user': '',
|
||||
'mySQL_pass': '',
|
||||
'mySQL_database': '',
|
||||
'mySQL_speed' : '0.02',
|
||||
'flask_server': '0.0.0.0',
|
||||
'flask_port': '5000',
|
||||
'flask_debug': False,
|
||||
'config_debug' : False,
|
||||
'config_read' : False,
|
||||
'config_dictionary' : 'config'
|
||||
'config_dictionary' : 'config',
|
||||
'can_baudrate' : '500k',
|
||||
'can_symfile' : ''
|
||||
}
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
|
|
@ -41,7 +41,7 @@ class Config():
|
|||
mes = 'config:\t{'
|
||||
for x in self._confDic:
|
||||
mes = '%s\r %s \t: %s' % (mes, x, self._confDic[x])
|
||||
mes = '%s\r }' % mes
|
||||
mes = '%s\r }\n' % mes
|
||||
print mes
|
||||
|
||||
def makeList(self, arg):
|
||||
|
|
@ -58,17 +58,17 @@ class Config():
|
|||
val = str(val[0].strip()) #overwrite val with string
|
||||
|
||||
if key == '':
|
||||
stderr.write('config_error there is an empty key\r')
|
||||
stderr.write('config_error: there is an empty key\r')
|
||||
return None, None
|
||||
if val == '':
|
||||
stderr.write('config_error %s has no value\r' % key)
|
||||
stderr.write('config_error: %s has no value\r' % key)
|
||||
return None, None
|
||||
|
||||
arg = val.split(',')
|
||||
while arg.count('') > 0:
|
||||
arg.remove('')
|
||||
if len(arg) == 0:
|
||||
stderr.write('config_error %s has an empty argument\r' % key)
|
||||
stderr.write('config_error: %s has an empty argument\r' % key)
|
||||
return None, None
|
||||
return key, arg
|
||||
|
||||
|
|
@ -83,46 +83,36 @@ class Config():
|
|||
stderr.write('config_error %s not found\r' % config)
|
||||
return self._confDic
|
||||
|
||||
# master = None
|
||||
_linenumber = 0
|
||||
|
||||
for line in confFile:
|
||||
_linenumber += 1
|
||||
line = line.strip()
|
||||
|
||||
if len(line) == 0 or line[0] == "#": #remove empty lines and comments
|
||||
# master = None
|
||||
continue
|
||||
|
||||
if line[0] == '\'':
|
||||
line = line.lstrip('\'')
|
||||
# if master is not None:
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
res = []
|
||||
for a in arg:
|
||||
tmp = a.split(':')
|
||||
for t in tmp:
|
||||
res.append(t.strip())
|
||||
arg = dict(res[i:i+2] for i in range(0, len(res), 2))
|
||||
### prepared for more dimensional dictionaries ###
|
||||
# elif line.find(':') != -1:
|
||||
# master = line.split(':')[0]
|
||||
# key = master
|
||||
# arg = None
|
||||
# pass
|
||||
elif line.find("=") != -1:
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
self.makeList(arg)
|
||||
arg = arg if len(arg) > 1 else arg[0]
|
||||
if line.find("=") == -1:
|
||||
exit('config_error: an error has occurred in line %s\r' % _linenumber)
|
||||
|
||||
key, arg = self.getElement(line)
|
||||
if key == None:
|
||||
continue #skip errors
|
||||
self.makeList(arg)
|
||||
arg = arg if len(arg) > 1 else arg[0]
|
||||
|
||||
self._confDic[key] = arg
|
||||
|
||||
confFile.close()
|
||||
self._confDic['config_read'] = True
|
||||
|
||||
self.printConf()
|
||||
for key in self._confDic:
|
||||
if self._confDic[key] == '':
|
||||
exit('please select a value for {0}'.format(key))
|
||||
|
||||
if self._confDic['config_debug']:
|
||||
self.printConf()
|
||||
|
||||
return self._confDic
|
||||
|
||||
c = Config()
|
||||
|
|
|
|||
|
|
@ -4,17 +4,17 @@ mySQL_port = 3306 # default 3306
|
|||
mySQL_user = stud_EMS
|
||||
mySQL_pass = sql13
|
||||
mySQL_database = photodb
|
||||
mySQL_speed = 0.05 # time between two query's in sec
|
||||
mySQL_speed = 0.02 # time between two query's in sec # default 0.02
|
||||
#sdgsgssth
|
||||
|
||||
#### FLASK ####
|
||||
flask_server = 0.0.0.0 # 0.0.0.0 for public access
|
||||
flask_port = 5000
|
||||
flask_debug = True
|
||||
flask_server = 0.0.0.0 # 0.0.0.0 for public access # default 0.0.0.0
|
||||
flask_port = 5000 # default 5000
|
||||
|
||||
#### CONFIG ####
|
||||
config_debug = True
|
||||
config_debug = True # default False
|
||||
|
||||
#### CAN ####
|
||||
## possible Baud Rates: 100k, 125k, 250k, 500k, 1000k ##
|
||||
can_baudrate = 250k
|
||||
symfile = ems-test.sym
|
||||
can_baudrate = 250k # default 500k
|
||||
can_symfile = ems-test.sym
|
||||
|
|
|
|||
16
src/run.py
16
src/run.py
|
|
@ -4,22 +4,12 @@ Created on 29.01.2014
|
|||
@author: Philipp Rauch
|
||||
'''
|
||||
from config import Config
|
||||
from API import API
|
||||
from CANFilter import CANFilter
|
||||
from API import API_start
|
||||
from CANFilter import CAN_start
|
||||
|
||||
### LOAD CONFIG ###
|
||||
c = Config()
|
||||
conf = c.readConf()
|
||||
|
||||
### start CAN to DB ###
|
||||
print 'starte CAN mit Baud von', conf['can_baudrate']
|
||||
can = CANFilter()
|
||||
can.start()
|
||||
|
||||
### API-Service ###
|
||||
def API_start():
|
||||
api = API()
|
||||
api.app.run(host = conf['flask_server'],
|
||||
port = int(conf['flask_port']))
|
||||
#debug = conf['flask_debug']
|
||||
CAN_start(conf)
|
||||
API_start()
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ class Switch(Thread):
|
|||
# item = query.get() #block = True
|
||||
# query should be included in the result
|
||||
|
||||
item = self.getItem(block = True)
|
||||
item = self.query.get(block = True)
|
||||
|
||||
if self.source == MYSQL:
|
||||
result = database.loop(self.cursor, item)
|
||||
|
|
@ -80,6 +80,3 @@ class Switch(Thread):
|
|||
sleep(float(conf['mySQL_speed']))
|
||||
elif self.source == MODBUS:
|
||||
sleep(0.1)
|
||||
|
||||
def getItem(self, block = False):
|
||||
return self.query.get(block)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue