switch kann nun auch mit warteschalngen initialisiert werden, pCharger überarbeitet
This commit is contained in:
parent
cee95b4b63
commit
4512f2b3dc
2 changed files with 139 additions and 117 deletions
|
|
@ -12,6 +12,7 @@ from Config.parser import config
|
|||
#import Module
|
||||
import database
|
||||
import modbus
|
||||
from test import query
|
||||
|
||||
MYSQL = 0
|
||||
MODBUS = 1
|
||||
|
|
@ -30,10 +31,10 @@ class Switch(Thread):
|
|||
Thread.__init__(self)
|
||||
self.source = source
|
||||
|
||||
def initialisiere(self):
|
||||
def initialisiere(self, query = None, queue = None):
|
||||
'''
|
||||
initialize the swich with the given source and creates a queue and a query
|
||||
it calls the setup method from the source module
|
||||
it calls the setup method from the submodule of the source
|
||||
|
||||
@return: queue and query
|
||||
'''
|
||||
|
|
@ -41,14 +42,16 @@ class Switch(Thread):
|
|||
self.cursor = database.setup(conf)
|
||||
elif self.source == MODBUS:
|
||||
self.cursor = modbus.setup(conf)
|
||||
else:
|
||||
raise NotImplementedError
|
||||
|
||||
### init Query ###
|
||||
self.query = Queue()
|
||||
self.query = Queue() if query is None else query
|
||||
if conf['config_debug']:
|
||||
print '\tSWITCH-QUERY:\t', self.query
|
||||
|
||||
### init Queue ###
|
||||
self.queue = Queue()
|
||||
self.queue = Queue() if queue is None else queue
|
||||
if conf['config_debug']:
|
||||
print '\tSWITCH-QUEUE:\t', self.queue
|
||||
|
||||
|
|
@ -56,7 +59,7 @@ class Switch(Thread):
|
|||
|
||||
def run(self):
|
||||
'''
|
||||
The loop method of the source module is called with a parameter from the query.
|
||||
The loop method from the submodule is called with a parameter from the query.
|
||||
Its output is written to the queue.
|
||||
'''
|
||||
while True:
|
||||
|
|
|
|||
243
src/pCharger.py
243
src/pCharger.py
|
|
@ -1,112 +1,131 @@
|
|||
'''
|
||||
Created on 16.01.2014
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
import csv
|
||||
from datetime import datetime
|
||||
from pandas import DataFrame
|
||||
from profile.database import Database
|
||||
from Config.parser import config
|
||||
import os
|
||||
import numpy
|
||||
|
||||
number = 1
|
||||
|
||||
sqlerr = "INSERT INTO %s (DateTime,Type,error) VALUES (\'%s\',%s,%s);"
|
||||
sqlnor = "INSERT INTO %s (DateTime,Type,cardnumber,customernumber,plug,meterreading) VALUES (\'%s\',%s,%s,%s,%s,%s);"
|
||||
folder = 'I:\Rauch\pCharger0%s'
|
||||
|
||||
def clean(li):
|
||||
'''
|
||||
Cleans every strings in a list with the strip() Method
|
||||
|
||||
@param li: all elements musst strings
|
||||
@return li: a list with clean strings
|
||||
'''
|
||||
for i in range(0, len(li)):
|
||||
li[i] = li[i].strip()
|
||||
#li[i] = li[i].strip('\xef\xbb\xbf')
|
||||
return li
|
||||
|
||||
def makeDatetime(time):
|
||||
'''
|
||||
create a datetime object from a string
|
||||
|
||||
@param: time: string with timeformat(dd.mm.yyy HH:MM:SS)
|
||||
@return: datetime object
|
||||
'''
|
||||
time = time.split(' ')
|
||||
time[0] = time[0].split('.')
|
||||
time[1] = time[1].split(':')
|
||||
res = datetime(int(time[0][2]),
|
||||
int(time[0][1]),
|
||||
int(time[0][0]),
|
||||
int(time[1][0]),
|
||||
int(time[1][1]),
|
||||
int(time[1][2]))
|
||||
|
||||
return res
|
||||
|
||||
def genData(folder):
|
||||
'''
|
||||
generate SQL commands of all pCarger csv documents in a folder
|
||||
|
||||
@param folder: path to the folder containing pCarger csv files
|
||||
'''
|
||||
ordner = os.listdir(folder)
|
||||
columns = ('DateTime', 'Type','cardnumber', 'customernumber', 'plug', 'meterreading', 'error')
|
||||
res_data = DataFrame(columns = ['DateTime', 'Type', 'cardnumber', 'customernumber', 'plug', 'meterreading', 'error'])
|
||||
for datei in ordner:
|
||||
path = "%s/%s" % (folder, datei)
|
||||
csv_data = csv.reader(file(path), delimiter=';')
|
||||
try:
|
||||
for row in csv_data:
|
||||
res = {}
|
||||
row = clean(row)
|
||||
row[0] = "P-Charger_0%s" % number #row[0]
|
||||
row[1] = makeDatetime(row[1])
|
||||
row[3] = numpy.int(row[3], 16)
|
||||
if row[2] not in 'SE':
|
||||
row[4] = numpy.nan if row[4] is '' else numpy.int(row[4], 16)
|
||||
row[5] = numpy.int(row[5], 16)
|
||||
row[6] = numpy.int(row[6], 16)
|
||||
for i in range(len(row)-1):
|
||||
res.update({ columns[i] : row[i+1] })
|
||||
else:
|
||||
for i in range(len(row)-1):
|
||||
if i is 2:
|
||||
res.update({ columns[6] : row[i+1] })
|
||||
else:
|
||||
res.update({ columns[i] : row[i+1] })
|
||||
pass
|
||||
res_data = res_data.append(res, ignore_index = True)
|
||||
except:
|
||||
continue
|
||||
#print res_data
|
||||
|
||||
#res_data = res_data.set_index('DateTime', verify_integrity = True)
|
||||
res_data = res_data.replace('', numpy.nan)
|
||||
res_data = res_data.where(DataFrame.notnull(res_data), 0)
|
||||
res_data[['cardnumber', 'customernumber', 'plug', 'meterreading', 'error']] = res_data[['cardnumber', 'customernumber', 'plug', 'meterreading', 'error']].astype(numpy.int64)
|
||||
return res_data
|
||||
|
||||
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
||||
db = Database()
|
||||
db.loadDatabase(strHost = conf['mySQL_server'],
|
||||
intPort = int(conf['mySQL_port']),
|
||||
strUser = conf['mySQL_user'],
|
||||
strPasswd = conf['mySQL_pass'],
|
||||
strDatabase = 'ems_testdb', #conf['mySQL_database'],
|
||||
strTable = None)
|
||||
|
||||
for i in range(1,8):
|
||||
number = i
|
||||
#print genData('I:\Rauch\pCharger%s' % '0{0}'.format(number))
|
||||
if i == 5:
|
||||
continue
|
||||
db.writeDatabase('pCharger%s' % '0{0}'.format(number), genData('I:\Rauch\pCharger%s' % '0{0}'.format(number)), bClear = False)
|
||||
print 'wrote pCharger%s' % '0{0}'.format(number)
|
||||
'''
|
||||
Created on 16.01.2014
|
||||
|
||||
@author: Philipp Rauch
|
||||
'''
|
||||
import csv
|
||||
from datetime import datetime
|
||||
from pandas import DataFrame
|
||||
from profile.database import Database
|
||||
from Config.parser import config
|
||||
import os
|
||||
import numpy
|
||||
|
||||
number = 1
|
||||
|
||||
#sqlerr = "INSERT INTO %s (DateTime,Type,error) VALUES (\'%s\',%s,%s);"
|
||||
#sqlnor = "INSERT INTO %s (DateTime,Type,cardnumber,customernumber,plug,meterreading) VALUES (\'%s\',%s,%s,%s,%s,%s);"
|
||||
folder = 'I:\Rauch\pCharger0%s'
|
||||
|
||||
columns = ('DateTime', 'Type', 'cardnumber', 'customernumber', 'plug', 'meterreading', 'error')
|
||||
|
||||
def clean(li):
|
||||
'''
|
||||
Cleans every strings in a list with the strip() Method
|
||||
|
||||
@param li: all elements musst strings
|
||||
@return li: a list with clean strings
|
||||
'''
|
||||
for i in range(0, len(li)):
|
||||
li[i] = li[i].strip()
|
||||
#li[i] = li[i].strip('\xef\xbb\xbf')
|
||||
return li
|
||||
|
||||
def makeDatetime(time):
|
||||
'''
|
||||
create a datetime object from a string
|
||||
|
||||
@param: time: string with timeformat(dd.mm.yyy HH:MM:SS)
|
||||
@return: datetime object
|
||||
'''
|
||||
time = time.split(' ')
|
||||
time[0] = time[0].split('.')
|
||||
time[1] = time[1].split(':')
|
||||
res = datetime(int(time[0][2]),
|
||||
int(time[0][1]),
|
||||
int(time[0][0]),
|
||||
int(time[1][0]),
|
||||
int(time[1][1]),
|
||||
int(time[1][2]))
|
||||
|
||||
return res
|
||||
|
||||
def genData(folder):
|
||||
'''
|
||||
generate SQL commands of all pCarger csv documents in a folder
|
||||
|
||||
@param folder: path to the folder containing pCarger csv files
|
||||
'''
|
||||
ordner = os.listdir(folder)
|
||||
res_data = DataFrame(columns = ['DateTime', 'Type', 'cardnumber', 'customernumber', 'plug', 'meterreading', 'error'])
|
||||
for datei in ordner:
|
||||
path = "%s/%s" % (folder, datei)
|
||||
csv_data = csv.reader(file(path), delimiter=';')
|
||||
try:
|
||||
for row in csv_data:
|
||||
res = {}
|
||||
row = clean(row)
|
||||
row[0] = "P-Charger_0%s" % number #row[0]
|
||||
row[1] = makeDatetime(row[1])
|
||||
row[3] = numpy.int(row[3], 16)
|
||||
if row[2] not in 'SE':
|
||||
row[4] = numpy.nan if row[4] is '' else numpy.int(row[4], 16)
|
||||
row[5] = numpy.int(row[5], 16)
|
||||
row[6] = numpy.int(row[6], 16)
|
||||
for i in range(len(row)-1):
|
||||
res.update({ columns[i] : row[i+1] })
|
||||
else:
|
||||
for i in range(len(row)-1):
|
||||
if i is 2:
|
||||
res.update({ columns[6] : row[i+1] })
|
||||
else:
|
||||
res.update({ columns[i] : row[i+1] })
|
||||
pass
|
||||
res_data = res_data.append(res, ignore_index = True)
|
||||
except:
|
||||
continue
|
||||
#print res_data
|
||||
|
||||
#res_data = res_data.set_index('DateTime', verify_integrity = True)
|
||||
res_data = res_data.replace('', numpy.nan)
|
||||
res_data = res_data.where(DataFrame.notnull(res_data), 0)
|
||||
res_data[['cardnumber', 'customernumber', 'plug', 'meterreading', 'error']] = res_data[[ 'cardnumber', 'customernumber', 'plug', 'meterreading', 'error']].astype(numpy.int64)
|
||||
return res_data
|
||||
|
||||
|
||||
c = config()
|
||||
conf = c.readConf()
|
||||
|
||||
db = Database()
|
||||
db.loadDatabase(strHost = conf['mySQL_server'],
|
||||
intPort = int(conf['mySQL_port']),
|
||||
strUser = conf['mySQL_user'],
|
||||
strPasswd = conf['mySQL_pass'],
|
||||
strDatabase = 'ems_testdb', #conf['mySQL_database'],
|
||||
strTable = None)
|
||||
big_data = DataFrame(columns = ['DateTime', 'pillarID', 'Type', 'cardnumber', 'customernumber', 'plug', 'meterreading', 'error'])
|
||||
for i in range(1,8):
|
||||
number = i
|
||||
#print genData('I:\Rauch\pCharger%s' % '0{0}'.format(number))
|
||||
if i == 5:
|
||||
continue
|
||||
tmp = genData('I:\Rauch\pCharger%s' % '0{0}'.format(number))
|
||||
tmp['pillarID'] = i
|
||||
big_data = big_data.append(tmp, ignore_index = True)
|
||||
#db.writeDatabase('pCharger%s' % '0{0}'.format(number), big_data, bClear = False)
|
||||
print 'wrote pCharger%s' % '0{0}'.format(number)
|
||||
|
||||
tmp = big_data.sort('DateTime')
|
||||
tmp = tmp.reset_index(drop = True)
|
||||
gr = tmp.groupby(['pillarID', 'plug'])
|
||||
cur = gr.agg('min')['meterreading']
|
||||
tmp['global'] = sum(list(cur))
|
||||
gl = cur
|
||||
|
||||
for i in range(tmp.index[-1]+1):
|
||||
x = tmp.ix[i]
|
||||
if cur[x['pillarID']][x['plug']] < x['meterreading']:
|
||||
cur[x['pillarID']][x['plug']] = x['meterreading']
|
||||
tmp.loc[i, 'global'] = sum(list(cur))
|
||||
|
||||
db.writeDatabase('pCharger', tmp, bClear = False)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue