123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- import datetime
- import pandas as pd
- import numpy as np
- import time
- from apscheduler.schedulers.blocking import BlockingScheduler
- from USER.SPF.alibaba.Common import log
- import CBMSBatSoc
- #...................................SOC计算函数......................................................................................................................
- def soc_cal():
- global df_ram
- global SNnums
- global df_bms
- global now_time
- end_time=now_time
- start_time=now_time-datetime.timedelta(seconds=11)
- now_time=now_time+datetime.timedelta(seconds=10)
- # start_time=start_time.strftime('%Y-%m-%d %H:%M:%S')
- # end_time=end_time.strftime('%Y-%m-%d %H:%M:%S')
- for sn in SNnums:
- # if 'PK500' in sn:
- # celltype=1 #6040三元电芯
- # elif 'PK502' in sn:
- # celltype=2 #4840三元电芯
- # elif 'K504B' in sn:
- # celltype=99 #60ah林磷酸铁锂电芯
- # elif 'MGMLXN750' in sn:
- # celltype=3 #力信50ah三元电芯
- # elif 'MGMCLN750' or 'UD' in sn:
- # celltype=4 #CATL 50ah三元电芯
- # else:
- # print('SN:{},未找到对应电池类型!!!'.format(sn))
- # continue
- # sys.exit()
- celltype=99
- #读取原始数据库数据........................................................................................................................................................
- df_bms1= df_bms[(df_bms['time']>=start_time) & (df_bms['time']<end_time)]
- df_bms1=df_bms1.reset_index(drop=True)
- if not df_bms1.empty:
- cellvolt_name=['CellVolt'+str(x) for x in range(1,73)]
- celltemp_name=['CellTemp'+str(x) for x in range(1,33)]
- df_volt=pd.DataFrame([x[0].split(",") for x in np.array(df_bms1[['cell_volt']])])
- df_volt.columns = cellvolt_name
- df_volt=df_volt.astype('float')
- df_temp=pd.DataFrame([x[0].split(",") for x in np.array(df_bms1[['cell_temp']])])
- df_temp.columns = celltemp_name
- df_temp=df_temp.astype('float')
- df_bms1=pd.concat([df_bms1,df_volt,df_temp],axis=1)
- # df_bms1.dropna(axis=0, inplace=True)
- df_bms1.reset_index(drop=True, inplace=True)
- #读取结果数据库数据........................................................................................................................................................
- # host='rm-bp10j10qy42bzy0q77o.mysql.rds.aliyuncs.com'
- # port=3306
- # db='qx_cas'
- # user='qx_read'
- # password='Qx@123456'
- # mode=1
- # tablename='cellstateestimation_soh'
- # DBRead=DBDownload.DBDownload(host, port, db, user, password, mode)
- # with DBRead as DBRead:
- # df_soh=DBRead.getdata(param='time_st,sn,soh', tablename=tablename, sn=sn, timename='time_sp', st=start_time, sp=end_time)
- df_soh=pd.DataFrame()
- # if celltype>50:
- # tablename='cellstateestimation_uniform_socvoltdiff'
- # with DBRead as DBRead:
- # df_socdiff=DBRead.getdata(param='time,sn,cellsoc_diff', tablename=tablename, sn=sn, timename='time', st=start_time, sp=end_time)
- # else:
- # df_socdiff=pd.DataFrame()
- df_socdiff=pd.DataFrame()
- # print(df_bms)
- # print(df_soh)
-
- #socram...............................................................................................................................................................
- if not df_bms1.empty:
- df_ram_sn=df_ram[df_ram['sn']==sn]
- if df_ram_sn.empty:
- with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\04BatSoc\\'+'soc_'+sn+'.txt','a') as file:
- file.write(str(tuple(df_ram_sn.columns))+'\n')
-
- BatSoc=CBMSBatSoc.BatSoc(sn,celltype,df_bms1,df_soh,df_ram_sn,df_socdiff)
- df_res, df_ram_sn=BatSoc.batsoc()
- if not df_ram_sn.empty:
- sn_index=df_ram.loc[df_ram['sn']==sn].index
- df_ram=df_ram.drop(index=sn_index)
- df_ram=df_ram.append(df_ram_sn)
- df_ram.reset_index(inplace=True,drop=True) #重置索引
-
- #结果存入数据库...........................................................
- if not df_ram_sn.empty:
- with open(r'D:\01WorkSpace\python\data_analyze_platform\USER\SPF\alibaba\04BatSoc\\'+'soc_'+sn+'.txt','a') as file:
- file.write(str(tuple(df_ram_sn.iloc[-1][['time','sn','bms_soc', 'soc']]))+'\n')
- #...............................................主函数.......................................................................................................................
- if __name__ == "__main__":
- #所有SN号
- SNnums= ['LS002']
- #读取原始数据库数据........................................................................................................................................................
- df_bms= pd.read_excel(r'D:\01WorkSpace\项目\Alibaba\data(1)\原始数据_2022-04-20_2022-05-20\力神_LS001.xlsx')
- now_time=datetime.datetime.strptime('2022-5-2 09:59:00','%Y-%m-%d %H:%M:%S')
- #log信息配置
- mylog=log.Mylog('log_soc.txt','error')
- mylog.logcfg()
- #参数初始化.........................................................................................................................................................................
- column_name=['time', 'sn', 'bms_soc', 'soc','cellsoc','standingtime','rampackcrnt','ramcellvolt','kocellvoltmin','kocellvoltmax','ocvweight','as_accum','socstep']
- df_ram=pd.DataFrame(columns=column_name)
- scheduler = BlockingScheduler()
- scheduler.add_job(soc_cal, 'interval', seconds=0.05, id='soc_job')
-
- try:
- scheduler.start()
- except Exception as e:
- scheduler.shutdown()
- print(repr(e))
- mylog.logopt(e)
|