diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8403b13 --- /dev/null +++ b/.gitignore @@ -0,0 +1,43 @@ +# Ignore-Datei selbst ausschließen +.gitignore +.gitmodules +Thumbs.db + +# Bestimmte Dateien ausschließen +cache.dat + +# Es können Wildcards (*,?) verwendet werden: +*.exe +*.dll +*.dsbackup +*.avrsln +*.avrsuo +*.avrgccproj +*.aps +*.atsln +*.atsuo +*.cproj +*.aws +*.xml +*.xslt +*.aux +*.dvi +*.lof +*.log +*.lot +*.out +*.synctex.gz +*.toc +.project +.pydevproject +tmp?.dat + +# Auch Verzeichnisse kann man ausschießen: +*default/ +*Debug/ +*bin/ +*ADC/ +*CAN/ +*UART/ +.*/ +CANLibrary/ diff --git a/scr/CANFilter.py b/scr/CANFilter.py index cb3cd8c..4b7a09a 100644 --- a/scr/CANFilter.py +++ b/scr/CANFilter.py @@ -10,80 +10,88 @@ from CanSignal import CanSignal from PCan import PcanAdapter import datetime import time +import threading #import MySQLdb debug = False -pcan = PcanAdapter(PcanAdapter.Baudrate['250k']) -pcan.initialize() +class CANFilter(threading.Thread): -dc_battery = CanMessage(0x3A4, 8, 50, 'dc_battery') -dc_grid = CanMessage(0x3A5, 8, 50, 'dc_grid') -dc_pv = CanMessage(0x3A6, 8, 50, 'dc_pv') -dc_charging = CanMessage(0x3A7, 8, 50, 'dc_charging') + battery_current, battery_voltage, battery_capacity, battery_timestamp, test = [], [], [], [], [] -pcan.addMessage(dc_battery) -pcan.addMessage(dc_grid) -pcan.addMessage(dc_pv) -pcan.addMessage(dc_charging) + def __init__(self): + print "INIT" + threading.Thread.__init__(self) -current = CanSignal(0, 16, 0, 0.001, 0, 'current') -voltage = CanSignal(16, 16, 0, 0.01, 0, 'voltage') -capacity = CanSignal(32, 11, 0, 0.05, 0, 'capacity') -isMaster = CanSignal(56, 1, 0, 1, 0, 'isMaster') -isFeed = CanSignal(57, 1, 0, 1, 0, 'isFeed') -isCharging = CanSignal(57, 1, 0, 1, 0, 'isCharging') -isOn = CanSignal(58, 1, 0, 1, 0, 'isOn') + self.pcan = PcanAdapter(PcanAdapter.Baudrate['250k']) + self.pcan.initialize() -pcan.Messages['dc_battery'].addSignal( current ) -pcan.Messages['dc_battery'].addSignal( voltage ) -pcan.Messages['dc_battery'].addSignal( capacity ) -pcan.Messages['dc_battery'].addSignal( isMaster ) -pcan.Messages['dc_battery'].addSignal( isCharging ) + dc_battery = CanMessage(0x3A4, 8, 50, 'dc_battery') + dc_grid = CanMessage(0x3A5, 8, 50, 'dc_grid') + dc_pv = CanMessage(0x3A6, 8, 50, 'dc_pv') + dc_charging = CanMessage(0x3A7, 8, 50, 'dc_charging') -pcan.Messages['dc_grid'].addSignal( current ) -pcan.Messages['dc_grid'].addSignal( voltage ) -pcan.Messages['dc_grid'].addSignal( isMaster ) -pcan.Messages['dc_grid'].addSignal( isFeed ) -pcan.Messages['dc_grid'].addSignal( isOn ) + self.pcan.addMessage(dc_battery) + self.pcan.addMessage(dc_grid) + self.pcan.addMessage(dc_pv) + self.pcan.addMessage(dc_charging) -pcan.Messages['dc_pv'].addSignal( current ) -pcan.Messages['dc_pv'].addSignal( voltage ) + current = CanSignal(0, 16, 0, 0.001, 0, 'current') + voltage = CanSignal(16, 16, 0, 0.01, 0, 'voltage') + capacity = CanSignal(32, 11, 0, 0.05, 0, 'capacity') + isMaster = CanSignal(56, 1, 0, 1, 0, 'isMaster') + isFeed = CanSignal(57, 1, 0, 1, 0, 'isFeed') + isCharging = CanSignal(57, 1, 0, 1, 0, 'isCharging') + isOn = CanSignal(58, 1, 0, 1, 0, 'isOn') -pcan.Messages['dc_charging'].addSignal( current ) -pcan.Messages['dc_charging'].addSignal( voltage ) + self.pcan.Messages['dc_battery'].addSignal( current ) + self.pcan.Messages['dc_battery'].addSignal( voltage ) + self.pcan.Messages['dc_battery'].addSignal( capacity ) + self.pcan.Messages['dc_battery'].addSignal( isMaster ) + self.pcan.Messages['dc_battery'].addSignal( isCharging ) -battery_current, battery_voltage, battery_capacity, battery_timestamp = [], [], [], [] + self.pcan.Messages['dc_grid'].addSignal( current ) + self.pcan.Messages['dc_grid'].addSignal( voltage ) + self.pcan.Messages['dc_grid'].addSignal( isMaster ) + self.pcan.Messages['dc_grid'].addSignal( isFeed ) + self.pcan.Messages['dc_grid'].addSignal( isOn ) + + self.pcan.Messages['dc_pv'].addSignal( current ) + self.pcan.Messages['dc_pv'].addSignal( voltage ) + + self.pcan.Messages['dc_charging'].addSignal( current ) + self.pcan.Messages['dc_charging'].addSignal( voltage ) #connection = MySQLdb.connect("url", "user", "passwort", "datenbankname") #cursor = connection.cursor() -def mean(l): - return float(sum(l))/len(l) if len(l) > 0 else 0 + def mean(self, l): + return float(sum(l))/len(l) if len(l) > 0 else 0 -while True: - receiveMessageId = pcan.receiveMessage() - if receiveMessageId == pcan.Messages['dc_battery'].Id: + def run(self): + while True: + receiveMessageId = self.pcan.receiveMessage() + if receiveMessageId == self.pcan.Messages['dc_battery'].Id: - battery_current.append(pcan.Messages['dc_battery'].Signals['current'].GetData()) - battery_voltage.append(pcan.Messages['dc_battery'].Signals['voltage'].GetData()) - battery_capacity.append(pcan.Messages['dc_battery'].Signals['capacity'].GetData()) - battery_timestamp.append(str(datetime.datetime.now())) + self.battery_current.append(self.pcan.Messages['dc_battery'].Signals['current'].GetData()) + self.battery_voltage.append(self.pcan.Messages['dc_battery'].Signals['voltage'].GetData()) + self.battery_capacity.append(self.pcan.Messages['dc_battery'].Signals['capacity'].GetData()) + self.battery_timestamp.append(datetime.datetime.now()) - if len(battery_timestamp) == 1000: - if debug: - print 'current: ', mean(battery_current) - print 'voltage: ', mean(battery_voltage) - print 'SoC: ', mean(battery_capacity) - print 'time: ', mean(battery_timestamp) + if len(self.battery_timestamp) == 100: + if debug: + print 'current: ', self.mean(self.battery_current) + print 'voltage: ', self.mean(self.battery_voltage) + print 'SoC: ', self.mean(self.battery_capacity) + print 'time: ', self.mean(self.battery_timestamp) - tabelle = 'EMS-TEST' - daten = [(tabelle, str(battery_timestamp[i]), str(battery_current[i]), str(battery_voltage[i]), str(battery_capacity[i])) for i in range(1000)] - sql = 'INSERT INTO %s VALUES(%s, %s, %s, %s)' + 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)' - for i in daten: - print sql % i -# cursor.executemany(sql, daten) + for i in daten: + print sql % i +# cursor.executemany(sql, daten) - del battery_current[:], battery_voltage[:], battery_capacity[:], battery_timestamp[:] - time.sleep(0.01) \ No newline at end of file + del self.battery_current[:], self.battery_voltage[:], self.battery_capacity[:], self.battery_timestamp[:] + time.sleep(0.01) \ No newline at end of file diff --git a/scr/swich.py b/scr/swich.py index 10d06a5..d646707 100644 --- a/scr/swich.py +++ b/scr/swich.py @@ -8,38 +8,51 @@ Created on 15.11.2013 import MySQLdb import threading import Queue +from CANFilter import CANFilter MYSQL = 0 CSV = 1 XML = 2 JSON = 3 -class Swich(object): - def __init__(self, source): +class Swich(threading.Thread): + + def __init__(self, source = MYSQL): + threading.Thread.__init__(self) self.source = source - - def start(self): + + def __del__(self): if self.source == MYSQL: -# @TODO CANFilter starten - self.connection = MySQLdb.connect("url", "user", "passwort", "datenbankname") + self.CAN.join() + + def initialisiere(self): + if self.source == MYSQL: + self.CAN = CANFilter() + self.CAN.start() + + self.connection = MySQLdb.connect("url", "user", "password", "database_name") self.cursor = self.connection.cursor() - self.queue = self.__createQueue__() - return self.queue - elif self.source ==CSV: + self.queue = Queue.Queue() + return self.queue, self.CAN + + elif self.source == CSV: pass + elif self.source == XML: pass + elif self.source == JSON: pass + else: return - def __createQueue__(self): - return - - def setQueue(self): - pass - - def getQueue(self): - pass - - \ No newline at end of file + + def run(self): + while True: + sql = 'SELECT * FROM %s ORDER BY timestamp DESC LIMIT 1' + item = Swich.queue.get(block = True) #item is a string which names the table that will be returned (newest values) + + print self.cursor.execute(sql % item) + + def setQueue(self, item): + self.queue.put(item) \ No newline at end of file