From 580a93d62072c216f49c6ee616d9cb5cf733888c Mon Sep 17 00:00:00 2001 From: Philipp Rauch Date: Wed, 15 Jan 2014 18:02:59 +0100 Subject: [PATCH] umstellung auf sym files und datenbank im fraunhofer --- src/API.py | 14 +++++++------- src/CANFilter.py | 36 ++++++++++++++++-------------------- src/config.py | 3 ++- src/config/ems-test.sym | 39 +++++++++++++++++++++++++++++++++++++++ src/config/ems.conf | 28 +++++----------------------- src/modules/database.py | 11 ++++++++++- src/switch.py | 10 ++++++++++ 7 files changed, 89 insertions(+), 52 deletions(-) create mode 100644 src/config/ems-test.sym diff --git a/src/API.py b/src/API.py index a253363..b25ec23 100644 --- a/src/API.py +++ b/src/API.py @@ -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, diff --git a/src/CANFilter.py b/src/CANFilter.py index eae9a5f..9bb7d0d 100644 --- a/src/CANFilter.py +++ b/src/CANFilter.py @@ -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)" diff --git a/src/config.py b/src/config.py index 5ab52dd..a1a780e 100644 --- a/src/config.py +++ b/src/config.py @@ -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): diff --git a/src/config/ems-test.sym b/src/config/ems-test.sym new file mode 100644 index 0000000..29a2a0d --- /dev/null +++ b/src/config/ems-test.sym @@ -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 + diff --git a/src/config/ems.conf b/src/config/ems.conf index 30acf37..52248dc 100644 --- a/src/config/ems.conf +++ b/src/config/ems.conf @@ -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 diff --git a/src/modules/database.py b/src/modules/database.py index 364d1cc..758e548 100644 --- a/src/modules/database.py +++ b/src/modules/database.py @@ -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('/') diff --git a/src/switch.py b/src/switch.py index 1a821dd..e8e3c07 100644 --- a/src/switch.py +++ b/src/switch.py @@ -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