2014-01-29 18:29:53 +01:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
# -*- coding: utf8 -*-
|
2013-11-13 18:14:34 +01:00
|
|
|
|
'''
|
|
|
|
|
|
Created on 13.11.2013
|
|
|
|
|
|
|
|
|
|
|
|
@author: Philipp Rauch
|
2013-11-20 17:13:15 +01:00
|
|
|
|
@version: 0.1
|
2013-11-13 18:14:34 +01:00
|
|
|
|
'''
|
2014-01-29 18:29:53 +01:00
|
|
|
|
from PCan import PcanAdapter
|
|
|
|
|
|
from Sym2Lib import Add2Adapter, get_DataFrameDict
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
from time import sleep
|
|
|
|
|
|
from threading import Thread
|
|
|
|
|
|
from Queue import Queue
|
|
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
|
from profile.database import Database
|
2013-11-13 18:14:34 +01:00
|
|
|
|
|
2014-01-15 18:02:59 +01:00
|
|
|
|
debug = True
|
2013-11-13 18:14:34 +01:00
|
|
|
|
|
2014-01-30 16:06:14 +01:00
|
|
|
|
def CAN_start(conf):
|
|
|
|
|
|
print 'starte CAN mit Baud von', conf['can_baudrate']
|
|
|
|
|
|
can = CANFilter(conf)
|
|
|
|
|
|
can.start()
|
2013-11-13 18:14:34 +01:00
|
|
|
|
|
2014-01-30 16:06:14 +01:00
|
|
|
|
class CANFilter(Thread):
|
2013-11-21 18:11:38 +01:00
|
|
|
|
|
2014-01-29 18:29:53 +01:00
|
|
|
|
### Lookup f<>r CAN --> DB ###
|
|
|
|
|
|
lookup = {
|
|
|
|
|
|
'dc_battery' : 'battery'
|
|
|
|
|
|
}
|
2013-11-21 18:11:38 +01:00
|
|
|
|
|
2014-01-30 16:06:14 +01:00
|
|
|
|
def __init__(self, conf):
|
2013-11-20 17:13:15 +01:00
|
|
|
|
Thread.__init__(self)
|
2014-01-30 16:06:14 +01:00
|
|
|
|
self.conf = conf
|
2013-11-20 17:13:15 +01:00
|
|
|
|
self.queue = Queue()
|
2013-11-13 18:14:34 +01:00
|
|
|
|
|
2014-01-29 18:29:53 +01:00
|
|
|
|
### init DB ###
|
|
|
|
|
|
self.db = Database()
|
|
|
|
|
|
self.db.loadDatabase(strHost = self.conf['mySQL_server'],
|
|
|
|
|
|
intPort = int(self.conf['mySQL_port']),
|
|
|
|
|
|
strUser = self.conf['mySQL_user'],
|
|
|
|
|
|
strPasswd = self.conf['mySQL_pass'],
|
|
|
|
|
|
strDatabase = self.conf['mySQL_database'],
|
|
|
|
|
|
strTable = None)
|
|
|
|
|
|
|
|
|
|
|
|
self.symList = []
|
|
|
|
|
|
|
|
|
|
|
|
### init PCAN ###
|
2013-11-29 15:04:02 +01:00
|
|
|
|
self.pcan = PcanAdapter(PcanAdapter.Baudrate[self.conf['can_baudrate']],
|
2014-01-15 18:02:59 +01:00
|
|
|
|
debug = self.conf['config_debug'])
|
2013-11-19 17:11:25 +01:00
|
|
|
|
self.pcan.initialize()
|
2014-01-30 16:06:14 +01:00
|
|
|
|
if isinstance(self.conf['can_symfile'], str):
|
|
|
|
|
|
sym = '%s/%s' % (self.conf['config_dictionary'],
|
|
|
|
|
|
self.conf['can_symfile'])
|
2014-01-29 18:29:53 +01:00
|
|
|
|
self.symList.append(get_DataFrameDict(sym))
|
2014-01-15 18:02:59 +01:00
|
|
|
|
Add2Adapter(self.pcan, sym)
|
2014-01-30 16:06:14 +01:00
|
|
|
|
elif isinstance(self.conf['can_symfile'], list):
|
|
|
|
|
|
for element in self.conf['can_symfile']:
|
|
|
|
|
|
sym = '%s/%s' % (self.conf['config_dictionary'], element)
|
2014-01-29 18:29:53 +01:00
|
|
|
|
self.symList.append(get_DataFrameDict(sym))
|
2014-01-15 18:02:59 +01:00
|
|
|
|
Add2Adapter(self.pcan, sym)
|
2013-11-14 17:08:19 +01:00
|
|
|
|
|
2013-11-19 17:11:25 +01:00
|
|
|
|
def mean(self, l):
|
2013-11-22 14:25:12 +01:00
|
|
|
|
return float(sum(l))/len(l) if len(l) > 0 else 0
|
2013-11-13 18:14:34 +01:00
|
|
|
|
|
2013-11-19 17:11:25 +01:00
|
|
|
|
def run(self):
|
2013-11-22 14:25:12 +01:00
|
|
|
|
while True:
|
2014-01-29 18:29:53 +01:00
|
|
|
|
receiveMessageName = self.pcan.receiveMessage()
|
2013-11-20 17:13:15 +01:00
|
|
|
|
|
2014-01-29 18:29:53 +01:00
|
|
|
|
for sym in self.symList:
|
|
|
|
|
|
if receiveMessageName in sym.keys():
|
|
|
|
|
|
tmp = {}
|
|
|
|
|
|
for sig in list(sym[receiveMessageName].columns):
|
|
|
|
|
|
tmp[sig] = self.pcan.Messages[receiveMessageName].Signals[sig].GetData()
|
|
|
|
|
|
sym[receiveMessageName] = sym[receiveMessageName].append(tmp, ignore_index=True)
|
2013-11-28 12:41:01 +01:00
|
|
|
|
|
2014-01-29 18:29:53 +01:00
|
|
|
|
if len(sym[receiveMessageName].index) == 100:
|
|
|
|
|
|
res = DataFrame(sym[receiveMessageName].mean(axis=0)).T
|
2013-11-28 12:41:01 +01:00
|
|
|
|
|
2014-01-29 18:29:53 +01:00
|
|
|
|
res.drop('isMaster', axis = 1, inplace = True)
|
|
|
|
|
|
res.drop('isCharging', axis = 1, inplace = True)
|
|
|
|
|
|
res['DateTime'] = datetime.now()
|
2013-11-28 12:41:01 +01:00
|
|
|
|
|
2014-01-29 18:29:53 +01:00
|
|
|
|
self.db.writeDatabase(self.lookup[receiveMessageName], res)
|
|
|
|
|
|
sym[receiveMessageName] = sym[receiveMessageName].drop(sym[receiveMessageName].index[:])
|
|
|
|
|
|
#print res
|
2013-11-14 17:08:19 +01:00
|
|
|
|
|
2013-11-22 16:01:43 +01:00
|
|
|
|
sleep(0.01)
|