Config auf Module angewand
*die readConf wurde verbessert *Standartwerte wurden hinterlegt *In den Modulen wird das Configfile verwendet
This commit is contained in:
parent
b205714658
commit
e70ef4de38
4 changed files with 67 additions and 30 deletions
21
scr/API.py
21
scr/API.py
|
|
@ -6,14 +6,17 @@ Created on 24.10.2013
|
|||
'''
|
||||
|
||||
from time import time
|
||||
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
|
||||
import ems
|
||||
|
||||
app_host = '0.0.0.0' #api_host='0.0.0.0' for public access
|
||||
app_port = '5000'
|
||||
api_host = gethostname() if app_host == '0.0.0.0' else app_host
|
||||
api_url = 'http://%s:%s' % (api_host, app_port)
|
||||
conf = 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'])
|
||||
|
||||
#### BUFFER ####
|
||||
class Buffer(object):
|
||||
|
|
@ -58,6 +61,7 @@ class Buffer(object):
|
|||
}
|
||||
|
||||
system = {
|
||||
'00_config' : conf,
|
||||
'dc_labor' : dc_labor,
|
||||
'dc_grid' : dc_grid,
|
||||
'ac_grid' : ac_grid
|
||||
|
|
@ -120,8 +124,8 @@ class Buffer(object):
|
|||
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()})
|
||||
dic.update({'000_href': url})
|
||||
dic.update({'000_timestamp' : str(datetime.now())})
|
||||
|
||||
def foo(self, l, args):
|
||||
'''
|
||||
|
|
@ -158,11 +162,10 @@ class API(Thread):
|
|||
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)
|
||||
# def run(self):
|
||||
self.app.run(host = conf['flask_server'], port = int(conf['flask_port']), debug = True)
|
||||
|
||||
buf = Buffer()
|
||||
print 'API:\t', buf
|
||||
|
||||
########## ERROR Handler ##########
|
||||
def not_allowed(error):
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ from datetime import datetime
|
|||
from time import sleep
|
||||
from threading import Thread
|
||||
from Queue import Queue
|
||||
#from MySQLdb import connect
|
||||
from MySQLdb import connect
|
||||
from config import readConf
|
||||
|
||||
debug = False
|
||||
|
||||
|
|
@ -20,6 +21,14 @@ class CANFilter(Thread):
|
|||
|
||||
battery_current, battery_voltage, battery_capacity, battery_timestamp, test = [], [], [], [], [] #create tmp lists
|
||||
|
||||
conf = readConf()
|
||||
|
||||
connection = connect(host = conf['mySQL_server'],
|
||||
user = conf['mySQL_user'],
|
||||
passwd = conf['mySQL_pass'],
|
||||
db = conf['mySQL_database'])
|
||||
cursor = connection.cursor()
|
||||
|
||||
def __init__(self):
|
||||
Thread.__init__(self)
|
||||
|
||||
|
|
@ -64,11 +73,6 @@ class CANFilter(Thread):
|
|||
self.pcan.Messages['dc_charging'].addSignal( current )
|
||||
self.pcan.Messages['dc_charging'].addSignal( voltage )
|
||||
|
||||
return self.queue
|
||||
|
||||
#connection = MySQLdb.connect("url", "user", "passwort", "datenbankname")
|
||||
#cursor = connection.cursor()
|
||||
|
||||
def mean(self, l):
|
||||
return float(sum(l))/len(l) if len(l) > 0 else 0
|
||||
|
||||
|
|
@ -89,15 +93,17 @@ class CANFilter(Thread):
|
|||
print 'SoC: ', self.mean(self.battery_capacity)
|
||||
print 'time: ', self.mean(self.battery_timestamp)
|
||||
|
||||
tabelle = 'EMS-TEST'
|
||||
daten = [(tabelle, str(self.battery_timestamp[i]), str(self.battery_current[i]), str(self.battery_voltage[i]), str(self.battery_capacity[i])) for i in range(100)]
|
||||
sql = 'INSERT INTO %s VALUES(%s, %s, %s, %s)'
|
||||
tabelle = 'battery'
|
||||
# daten = [(tabelle, str(self.battery_timestamp[i]), str(self.battery_current[i]), str(self.battery_voltage[i]), str(self.battery_capacity[i])) for i in range(100)]
|
||||
daten = (tabelle, str(self.battery_timestamp[50]), str(self.mean(self.battery_current)), str(self.mean(self.battery_voltage)), str(self.mean(self.battery_capacity)))
|
||||
sql = "INSERT INTO %s VALUES (%s,%s,%s,%s)"
|
||||
|
||||
self.queue.put(daten[0])
|
||||
|
||||
# for i in daten:
|
||||
# print sql % i
|
||||
# cursor.executemany(sql, daten)
|
||||
# print sql % daten
|
||||
self.cursor.execute("INSERT INTO %s VALUES (\'%s\',%s,%s,%s)" % daten)
|
||||
#self.cursor.executemany(sql, daten)
|
||||
|
||||
del self.battery_current[:], self.battery_voltage[:], self.battery_capacity[:], self.battery_timestamp[:] #clear tmp lists
|
||||
sleep(0.01)
|
||||
|
|
@ -4,9 +4,29 @@ Created on 21.11.2013
|
|||
@author: rauchp
|
||||
'''
|
||||
|
||||
confDic = {
|
||||
'mySQL_server': 'localhost',
|
||||
'mySQL_port': '3306',
|
||||
'mySQL_user': 'smoke',
|
||||
'mySQL_pass': 'KiWujcafAlor',
|
||||
'mySQL_database': 'smoke_test',
|
||||
'mySQL_table': 'battery',
|
||||
'flask_server': '0.0.0.0',
|
||||
'flask_port': '5000',
|
||||
'flask_debug': False,
|
||||
}
|
||||
|
||||
def readConf():
|
||||
try:
|
||||
confFile = open("config\ems.conf", "r")
|
||||
confDic = {}
|
||||
error = False
|
||||
except IOError:
|
||||
error = True
|
||||
|
||||
if error:
|
||||
confDic.update({'error' : 'config/ems.conf not found'})
|
||||
return confDic
|
||||
|
||||
for line in confFile:
|
||||
line = line.strip()
|
||||
if len(line) > 0 and not line[0] == "#":
|
||||
|
|
@ -19,6 +39,7 @@ def readConf():
|
|||
val[0] = False if val[0] == 'False' else val[0]
|
||||
confDic[ident[0]] = val[0]
|
||||
confFile.close()
|
||||
|
||||
return confDic
|
||||
|
||||
print readConf()
|
||||
print 'config:\t', readConf()
|
||||
21
scr/swich.py
21
scr/swich.py
|
|
@ -7,15 +7,18 @@ Created on 15.11.2013
|
|||
|
||||
from threading import Thread
|
||||
from Queue import Queue
|
||||
from CANFilter import CANFilter
|
||||
#from CANFilter import CANFilter
|
||||
from time import sleep
|
||||
#from MySQLdb import connect
|
||||
from config import readConf
|
||||
from MySQLdb import connect
|
||||
|
||||
MYSQL = 0
|
||||
CSV = 1
|
||||
XML = 2
|
||||
JSON = 3
|
||||
|
||||
conf = readConf()
|
||||
|
||||
class Swich(Thread):
|
||||
|
||||
_isInit = False
|
||||
|
|
@ -33,16 +36,20 @@ class Swich(Thread):
|
|||
_isInit = True
|
||||
|
||||
### start CAN ###
|
||||
self.CAN = CANFilter()
|
||||
self.CAN.start()
|
||||
# self.CAN = CANFilter()
|
||||
# self.CAN.start()
|
||||
self.CAN = None
|
||||
|
||||
### connect to DB ###
|
||||
# self.connection = MySQLdb.connect("url", "user", "password", "database_name")
|
||||
# self.cursor = self.connection.cursor()
|
||||
self.connection = connect(host = conf['mySQL_server'],
|
||||
user = conf['mySQL_user'],
|
||||
passwd = conf['mySQL_pass'],
|
||||
db = conf['mySQL_database'])
|
||||
self.cursor = self.connection.cursor()
|
||||
|
||||
### init Queue ###
|
||||
self.queue = Queue()
|
||||
print 'SWICH:\t', self.queue
|
||||
print '\tSWICH:\t', self.queue
|
||||
return self.queue, self.CAN
|
||||
|
||||
elif self.source == CSV:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue