umstellung auf sym files und datenbank im fraunhofer

This commit is contained in:
Philipp Rauch 2014-01-15 18:02:59 +01:00
parent 0c9a1d2f2c
commit 580a93d620
7 changed files with 89 additions and 52 deletions

View file

@ -23,18 +23,18 @@ class Buffer(object):
device = { device = {
'battery' : { 'battery' : {
'id': 1, 'id': 1,
'voltage' : 400.11, 'Voltage' : 400.11,
'current' : 20.264, 'Current' : 20.264,
'power' : 53.465, 'Power' : 53.465,
'capacity' : 80.34, 'SoC' : 80.34,
'isCharging' : False, 'isCharging' : False,
'isMaster' : True, 'isMaster' : True,
'dyn' : None }, 'dyn' : None },
'ac_grid' : { 'ac_grid' : {
'id': 2, 'id': 2,
'voltage' : 400.23, 'Voltage' : 400.23,
'current' : 10.423, 'Current' : 10.423,
'power' : 23.35, 'Power' : 23.35,
'isOn' : True, 'isOn' : True,
'isFeed' : True, 'isFeed' : True,
'isMaster' : False, 'isMaster' : False,

View file

@ -7,6 +7,7 @@ Created on 13.11.2013
from CanMessage import CanMessage from CanMessage import CanMessage
from CanSignal import CanSignal from CanSignal import CanSignal
from PCan import PcanAdapter from PCan import PcanAdapter
from Sym2Lib import Add2Adapter, Sym2Code
from datetime import datetime from datetime import datetime
from time import sleep from time import sleep
from threading import Thread from threading import Thread
@ -14,7 +15,7 @@ from Queue import Queue
from config import Config from config import Config
import database import database
debug = False debug = True
class CANFilter(Thread): class CANFilter(Thread):
@ -33,24 +34,18 @@ class CANFilter(Thread):
self.queue = Queue() self.queue = Queue()
self.pcan = PcanAdapter(PcanAdapter.Baudrate[self.conf['can_baudrate']], self.pcan = PcanAdapter(PcanAdapter.Baudrate[self.conf['can_baudrate']],
debug = self.conf['config_debug']) debug = self.conf['config_debug'])
self.pcan.initialize() self.pcan.initialize()
for message in self.conf['can_messages']: if isinstance(self.conf["symfile"], str):
self.pcan.addMessage(CanMessage(int(self.conf[message]['id'], 0), #hex-string to int sym = "%s/%s" % (self.conf["config_dictionary"], self.conf["symfile"])
int(self.conf[message]['length']), #Sym2Code(sym)
int(self.conf[message]['time']), Add2Adapter(self.pcan, sym)
message)) elif isinstance(self.conf["symfile"], list):
print "Message: %s" % message for element in self.conf["symfile"]:
for signal in self.conf["sig_%s" % message]: sym = "%s/%s" % (self.conf["config_dictionary"], element)
self.pcan.Messages[message].addSignal(CanSignal(int(self.conf[signal]['begin']), #Sym2Code(sym)
int(self.conf[signal]['length']), Add2Adapter(self.pcan, sym)
int(self.conf[signal]['offset']),
float(self.conf[signal]['scaling']),
int(self.conf[signal]['data']),
signal))
print " - %s" % signal
def mean(self, l): def mean(self, l):
return float(sum(l))/len(l) if len(l) > 0 else 0 return float(sum(l))/len(l) if len(l) > 0 else 0
@ -58,6 +53,7 @@ class CANFilter(Thread):
def run(self): def run(self):
while True: while True:
receiveMessageId = self.pcan.receiveMessage() receiveMessageId = self.pcan.receiveMessage()
if receiveMessageId == self.pcan.Messages['dc_battery'].Id: if receiveMessageId == self.pcan.Messages['dc_battery'].Id:
self.battery_current.append( self.battery_current.append(
@ -73,18 +69,18 @@ class CANFilter(Thread):
print 'current: ', self.mean(self.battery_current) print 'current: ', self.mean(self.battery_current)
print 'voltage: ', self.mean(self.battery_voltage) print 'voltage: ', self.mean(self.battery_voltage)
print 'SoC: ', self.mean(self.battery_soc) print 'SoC: ', self.mean(self.battery_soc)
print 'time: ', self.mean(self.battery_timestamp) print 'time: ', self.battery_timestamp[50]
tabelle = 'battery' tabelle = 'battery'
list_daten = [(tabelle, str(self.battery_timestamp[i]), list_daten = [(tabelle, str(self.battery_timestamp[i]),
str(self.battery_current[i]),
str(self.battery_voltage[i]), str(self.battery_voltage[i]),
str(self.battery_current[i]),
str(self.battery_soc[i])) for i in range(100)] str(self.battery_soc[i])) for i in range(100)]
mean_daten = (tabelle, str(self.battery_timestamp[50]), mean_daten = (tabelle, str(self.battery_timestamp[50]),
str(self.mean(self.battery_current)),
str(self.mean(self.battery_voltage)), str(self.mean(self.battery_voltage)),
str(self.mean(self.battery_current)),
str(self.mean(self.battery_soc))) str(self.mean(self.battery_soc)))
sql = "INSERT INTO %s VALUES (\'%s\',%s,%s,%s)" sql = "INSERT INTO %s VALUES (\'%s\',%s,%s,%s)"

View file

@ -22,7 +22,8 @@ class Config():
'flask_port': '5000', 'flask_port': '5000',
'flask_debug': False, 'flask_debug': False,
'config_debug' : False, 'config_debug' : False,
'config_read' : False 'config_read' : False,
'config_dictionary' : 'config'
} }
def __new__(cls, *args, **kwargs): def __new__(cls, *args, **kwargs):

39
src/config/ems-test.sym Normal file
View file

@ -0,0 +1,39 @@
FormatVersion=5.0 // Do not edit!
Title="EMS Test"
{SENDRECEIVE}
[dc_battery]
ID=3A4h
DLC=8
CycleTime=50
Var=current unsigned 0,16 /f:0.001
Var=voltage unsigned 16,16 /f:0.01
Var=soc unsigned 32,11 /f:0.05
Var=isMaster unsigned 56,1
Var=isCharging unsigned 57,1
[dc_grid]
ID=3A5h
DLC=8
CycleTime=50
Var=current unsigned 0,16 /f:0.001
Var=voltage unsigned 16,16 /f:0.01
Var=isMaster unsigned 56,1
Var=isFeed unsigned 57,1
Var=isOn unsigned 58,1
[dc_pv]
ID=3A6h
DLC=8
CycleTime=50
Var=current unsigned 0,16 /f:0.001
Var=voltage unsigned 16,16 /f:0.01
[dc_charger]
ID=3A7h
DLC=8
CycleTime=50
Var=current unsigned 0,16 /f:0.001
Var=voltage unsigned 16,16 /f:0.01

View file

@ -1,9 +1,9 @@
#### DATENBANK #### #### DATENBANK ####
mySQL_server = localhost mySQL_server = iisb-mysql.iisb.fraunhofer.de
mySQL_port = 3306 # default 3306 mySQL_port = 3306 # default 3306
mySQL_user = smoke mySQL_user = stud_EMS
mySQL_pass = KiWujcafAlor mySQL_pass = sql13
mySQL_database = smoke_test mySQL_database = photodb
mySQL_table = battery, grid, pv, charger mySQL_table = battery, grid, pv, charger
mySQL_speed = 0.1 # time between two query's in sec mySQL_speed = 0.1 # time between two query's in sec
@ -19,22 +19,4 @@ config_debug = True
#possible Baud Rates: 100k, 125k, 250k, 500k, 1000k #possible Baud Rates: 100k, 125k, 250k, 500k, 1000k
can_baudrate = 250k can_baudrate = 250k
can_messages = dc_battery, dc_grid, dc_pv, dc_charger symfile = ems-test.sym
'dc_battery = id:0x3A4, length:8, time:50
'dc_grid = id:0x3A5, length:8, time:50
'dc_pv = id:0x3A6, length:8, time:50
'dc_charger = id:0x3A7, length:8, time:50
sig_dc_battery = current, voltage, soc, isMaster, isCharging
sig_dc_grid = current, voltage, isMaster, isFeed, isOn
sig_dc_pv = current, voltage
sig_dc_charger = current, voltage
#can_signales = current, voltage, soc, isMaster, isFeed, isCharging, isOn
'current = begin:0, length:16, offset:0, scaling:0.001, data:0
'voltage = begin:16, length:16, offset:0, scaling:0.01, data:0
'soc = begin:32, length:11, offset:0, scaling:0.05, data:0
'isMaster = begin:56, length:1, offset:0, scaling:1, data:0
'isFeed = begin:57, length:1, offset:0, scaling:1, data:0
'isCharging = begin:57, length:1, offset:0, scaling:1, data:0
'isOn = begin:58, length:1, offset:0, scaling:1, data:0

View file

@ -6,6 +6,11 @@ Created on 26.11.2013
from MySQLdb import connect from MySQLdb import connect
def setup(conf): def setup(conf):
'''
establishes a connection to the database and returns a cursor
@return: cursor to the database
'''
connection = connect(host = conf['mySQL_server'], connection = connect(host = conf['mySQL_server'],
user = conf['mySQL_user'], user = conf['mySQL_user'],
passwd = conf['mySQL_pass'], passwd = conf['mySQL_pass'],
@ -15,7 +20,11 @@ def setup(conf):
return cursor return cursor
def loop(cursor, item): def loop(cursor, item):
sql_values = 'SELECT * FROM %s ORDER BY timestamp DESC LIMIT 1' '''
'''
sql_values = 'SELECT * FROM %s ORDER BY DateTime DESC LIMIT 1'
sql_collums = 'SHOW COLUMNS FROM %s' sql_collums = 'SHOW COLUMNS FROM %s'
path = item.split('/') path = item.split('/')

View file

@ -31,6 +31,12 @@ class Switch(Thread):
self.source = source self.source = source
def initialisiere(self): def initialisiere(self):
'''
initialize the swich with the given source and creates a queue and a query
it calls the setup method from the source module
@return: queue and query
'''
if self.source == MYSQL: if self.source == MYSQL:
self.cursor = database.setup(conf) self.cursor = database.setup(conf)
elif self.source == MODBUS: elif self.source == MODBUS:
@ -49,6 +55,10 @@ class Switch(Thread):
return self.queue, self.query return self.queue, self.query
def run(self): def run(self):
'''
The loop method of the source module is called with a parameter from the query.
Its output is written to the queue.
'''
while True: while True:
# Queue implementaion # Queue implementaion
# Queue contains the tablename for query # Queue contains the tablename for query