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 = {
'battery' : {
'id': 1,
'voltage' : 400.11,
'current' : 20.264,
'power' : 53.465,
'capacity' : 80.34,
'Voltage' : 400.11,
'Current' : 20.264,
'Power' : 53.465,
'SoC' : 80.34,
'isCharging' : False,
'isMaster' : True,
'dyn' : None },
'ac_grid' : {
'id': 2,
'voltage' : 400.23,
'current' : 10.423,
'power' : 23.35,
'Voltage' : 400.23,
'Current' : 10.423,
'Power' : 23.35,
'isOn' : True,
'isFeed' : True,
'isMaster' : False,

View file

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

View file

@ -22,7 +22,8 @@ class Config():
'flask_port': '5000',
'flask_debug': False,
'config_debug' : False,
'config_read' : False
'config_read' : False,
'config_dictionary' : 'config'
}
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 ####
mySQL_server = localhost
mySQL_server = iisb-mysql.iisb.fraunhofer.de
mySQL_port = 3306 # default 3306
mySQL_user = smoke
mySQL_pass = KiWujcafAlor
mySQL_database = smoke_test
mySQL_user = stud_EMS
mySQL_pass = sql13
mySQL_database = photodb
mySQL_table = battery, grid, pv, charger
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
can_baudrate = 250k
can_messages = dc_battery, dc_grid, dc_pv, dc_charger
'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
symfile = ems-test.sym

View file

@ -6,6 +6,11 @@ Created on 26.11.2013
from MySQLdb import connect
def setup(conf):
'''
establishes a connection to the database and returns a cursor
@return: cursor to the database
'''
connection = connect(host = conf['mySQL_server'],
user = conf['mySQL_user'],
passwd = conf['mySQL_pass'],
@ -15,7 +20,11 @@ def setup(conf):
return cursor
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'
path = item.split('/')

View file

@ -31,6 +31,12 @@ class Switch(Thread):
self.source = source
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:
self.cursor = database.setup(conf)
elif self.source == MODBUS:
@ -49,6 +55,10 @@ class Switch(Thread):
return self.queue, self.query
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:
# Queue implementaion
# Queue contains the tablename for query