ems/scr/CANFilter.py

103 lines
4.1 KiB
Python
Raw Normal View History

'''
Created on 13.11.2013
@author: Philipp Rauch
@version: 0.1
'''
from CanMessage import CanMessage
from CanSignal import CanSignal
from PCan import PcanAdapter
from datetime import datetime
from time import sleep
from threading import Thread
from Queue import Queue
#from MySQLdb import connect
debug = False
class CANFilter(Thread):
battery_current, battery_voltage, battery_capacity, battery_timestamp, test = [], [], [], [], [] #create tmp lists
def __init__(self):
Thread.__init__(self)
self.queue = Queue()
self.pcan = PcanAdapter(PcanAdapter.Baudrate['250k'])
self.pcan.initialize()
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')
self.pcan.addMessage(dc_battery)
self.pcan.addMessage(dc_grid)
self.pcan.addMessage(dc_pv)
self.pcan.addMessage(dc_charging)
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.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 )
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 )
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
def run(self):
while True:
receiveMessageId = self.pcan.receiveMessage()
if receiveMessageId == self.pcan.Messages['dc_battery'].Id:
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.now())
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(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)'
self.queue.put(daten[0])
# for i in daten:
# print sql % i
# cursor.executemany(sql, daten)
del self.battery_current[:], self.battery_voltage[:], self.battery_capacity[:], self.battery_timestamp[:] #clear tmp lists
sleep(0.01)